csv insert with pg library in node - javascript

So i did that kind of code.
var csv = require('csv-stream');
var request = require('request');
var fs = require('fs');
var pg = require('pg');
var conString = "pg://admin:admin#localhost:5432/labels";
// All of these arguments are optional.
var options = {
delimiter : ';', // default is ,
endLine : '\n', // default is \n,
escapeChar : '"', // default is an empty string
enclosedChar : '"', // default is an empty string
}
try {
var csvStream = csv.createStream(options);
fs.createReadStream('personss.csv').pipe(csvStream)
.on('error', function (err) {
console.error(err);
})
.on('data', function (data) {
// outputs an object containing a set of key/value pair representing a line found in the csv file.
console.log(data);
pg.connect(conString, function (err, client, done) {
client.query(
'INSERT into test (firstname, lastname) from',
function (err, result) {
if (err) {
console.log(err);
} else {
console.log('data inserted');
}
});
});
});
} catch (e) {
console(e.message);
};
where i stream the csv file with csv-stream library, how i can insert now that values from variable 'data' to my database? Should i do it by for cycle?
Update
added for cycle to iterate data
pg.connect(conString, function (err, client, done) {
for (var i = 0; data.length; i++) {
client.query(
'INSERT into test (firstname, lastname)',
function (err, result) {
if (err) {
console.log(err);
} else {
console.log('data inserted' + result.data[0]);
}
});
};
});

Related

parsem array of string data into json

I have a folder of .TXT extensions file with plain text in it how can I insert that plain text into the MongoDB database using the mongoose and fs module in node js
const fs = require('fs');
var parsedata;
var parsedTwice = [];
let fileName;
fs.readdir("D:/scnner_data/public", (err, data) => {
if(err) throw err
console.log(data);
fileName = data;
console.log(fileName);
});
fs.readFile('D:/scnner_data/public/DC221020042103SC.TXT',"utf8", (err, data) =>{
if (err) {
console.log(err);
} else {
parsedata = data.split("\n");
for(let item of parsedata) {
parsedTwice.push(item.split(";"));
}
//console.log('successful' , parsedTwice);
// console.log('this line is being readed' + parsedata);
}
}
)

Replacing line in a file using nodejs

I need to find a line in a file and replace it with a new one, all using NodeJS.
Here's what I've done to achieve that:
var fs = require('fs');
fs.readFile('infra_setting.conf', 'utf-8', function(err, data){
if (err) throw err;
console.log(data)
});
var fs = require('fs')
fs.readFile('myfile.conf', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var result = data.replace(/example/g, 'example: 12345678');
fs.writeFile('myfile.conf', result, 'utf8', function (err) {
if (err) return console.log(err);
});
});
The problem I have is that the string of the line keeps changing.
1st time => example : 2222
2nd time => example : somthing else
Is there a way to localize the line and replace it by NodeJS ?
Here's the example where i changed line from .gitignore file
// simulate fs.readFileSync('./gitignore', { encoding: 'utf8' })
const fileContent = 'node_modules\r\npackage-lock.json\r\nyarn.lock\r\n*.code-workspace\r\n'
function changeLine(content, lineString, newLineString) {
const delimeter = '\r\n'
const parts = content.split(delimeter).filter(v => v.length)
const lineIndex = parts.findIndex(v => v.includes(lineString))
parts[lineIndex] = newLineString
return parts.join(delimeter)
}
console.log(fileContent)
const change1 = changeLine(fileContent, 'node_modules', 'not_node_modules')
console.log(change1)
const change2 = changeLine(change1, 'package-lock.json', '.vscode')
console.log(change2)
try and tell me if it works or not
var fs = require('fs')
function searchReplaceFile(regexpFind, replace, FileName) {
var file = fs.createReadStream(FileName, 'utf8');
var newDATA= '';
file.on('data', function (chunk) {
newDATA+= chunk.toString().replace(regexpFind, replace);
});
file.on('end', function () {
fs.writeFile(FileName, newDATA, function(err) {
if (err) {
return console.log(err);
} else {
console.log('Updated!');
}
});
});
searchReplaceFile(/example/g, 'example: 12345678', 'infra_setting.conf');

Unable to access data from outside the scope of function

I have a function that downloads the user input(currently named app.json) from browser(client) to the server
function downloadUpdate(callback) {
//Using formidable node package for downloading user input to server
var form = new formidable.IncomingForm();
form.on('fileBegin', function(name, file) {
file.path = "app.json";
});
form.parse(req, function(err, fields, files) {
res.writeHead(200, {
'content-type': 'text/plain'
});
res.write('received upload:\n\n');
res.end(util.inspect({
fields: fields,
files: files
}));
});
form.on('end', function() {
callback(null);
});
}
I have another function that takes the file downloaded above and converts it into required format(final.json) something like this.
function UpdateCode(callback) {
var obj = fs.readFileSync('app.json', 'utf8');
console.log(abc); //Im getting undefined here
var object = JSON.parse(obj);
var data2 = [];
for (var j = 0; j < object.length; j++) {
if (object[j].value == `${abc}`) {
data2.push(object[j]);
}
}
console.log(data2);
fs.appendFile('final.json', JSON.stringify(data2), function(err) {
if (err) throw err;
console.log('Saved!');
callback(null);
});
}
I used async series function to make them run in an order like this
async.series([
downloadUpload,
UpdateCode
], function(err, result) {
if (err) throw err;
else {
console.log(result);
}
});
All of this code is inside a post request. I'm getting abc from the server
app.post('/', function(req,res){
var abc = req.body.abc;
console.log(abc); //I'm getting abc here
function downloadfile(callback){
//here goes the downloadfile definition
}
function UpdateCode(){
//upload code function
}
//now i call async.series method
async.series([
downloadUpload,
UpdateCode
], function(err, result) {
if (err) throw err;
else {
console.log(result);
}
});
});
the thing is the value of abc is not going to function UploadCode and when I console log abc, I get undefined. Where am I going wrong?

Node Js callback return function

I want to return database value in node js and pass as a variable in ejs file.
Bellow is the code, Which I used. it did not return value.
function getExternalLocation(cb) {
mssql.connect(msSqlSettings, function (err ) {
if (err) {
cb(err);
}
var getQuery = "SELECT [Title] FROM [dbo].[StyleTemplates] " ;
//console.log(getQuery);
var request = new mssql.Request();
// query to the database and get the data
request.query(getQuery, function (err, rows) {
mssql.close();
cb(err, rows);
});
});
}
exports.eejsBlock_editbarMenuLeft = function (hook_name, args, cb) {
var userData = getExternalLocation(args, function(err, rows) {});
args.content = args.content + eejs.require(
'ep_resources/templates/editbarButtons.ejs', {
userData: userData
});
return cb();
})
userData did not return any value.
var userData = getExternalLocation(args, function(err, rows) {});
I don't think userData will get right data in async function, there is no await, so you can try to get data in callback.
getExternalLocation(args, function(err, rows) {
var userData = rows;
args.content = args.content + eejs.require(
'ep_resources/templates/editbarButtons.ejs', {
userData: userData
});
});

howto Node module.exports

I want to separate the code for executing mysql query in Node, so I am trying to use the Revealing Module pattern here
/* pool -create connection pool mysql*/
var sqlQuery = function (sqlString) {
var _result = pool.getConnection(function (err, connection) {
/* error handling*/
connection.query(sqlString, function (err, rows) {
console.log(this.sql);
if (!err) {
return _result = rows; <============
}
connection.release();
});
return;
});
console.log(_result);
return { recordSet : _result }
};
module.exports = sqlQuery;
How can I return rows to my app.js. the code below for calling sqlQuery is not working
var SqlQuery = require(./path/to/sqlQueryFile);
var rows = SqlQuery('pass sql here').recordSet;
console.log(row);
res.json(rows);
Your code is asynchronous, but you're calling it synchronously.
If you wanted to do it like this, you'll also need to pass a callback to SqlQuery.
/* pool -create connection pool mysql*/
var sqlQuery = function (sqlString, callback) {
var _result = pool.getConnection(function (err, connection) {
/* error handling*/
connection.query(sqlString, function (err, rows) {
console.log(this.sql);
if (!err) {
callback(rows);
}
connection.release();
});
});
};
module.exports = sqlQuery;
And then call it with:
var SqlQuery = require(./path/to/sqlQueryFile);
var rows = SqlQuery('pass sql here', function(recordSet){
console.log(recordSet);
res.json(recordSet);
});
Edit: If you're using newer versions of JavaScript, you have a few more options.
If you have access to Promises, you can do this:
function sqlQuery (sqlString) {
return new Promise((resolve, reject) => {
pool.getConnection(function (err, connection) {
if (err) { return reject(err); } // error handling
connection.query(sqlString, function (err, rows) {
if (err) { return reject(err); }
resolve(rows);
connection.release();
});
});
});
}
module.exports = sqlQuery;
And then you'd use it like:
var SqlQuery = require(./path/to/sqlQueryFile);
SqlQuery('pass sql here')
.then(function(recordSet) {
console.log(recordSet);
res.json(recordSet);
})
.catch(function(err) {
// do your error handling
res.status(500).json({ err: 'Sorry there was an error' });
});
If you're using even newer JavaScript, you can use the async/await syntax (currently available via Babel, and I think in FireFox. Chrome in V55).
var SqlQuery = require(./path/to/sqlQueryFile);
async handleQuery(query) {
try {
var rows = await SqlQuery(query);
res.json(rows);
} catch (e) {
console.log('Error!', e);
}
}
To chain multiple queries together:
async handleQuery(query) {
try {
return await SqlQuery(query);
} catch (e) {
console.log('Error!', e);
}
}
var rows = await handleQuery('select * from tablename');
var rowsToReturn = await handleQuery('select id from another_table where name = "' + rows[0].name + '"');

Categories