I'm trying to insert around 20.000 users at the same time in my MSSQL Server from a csv file. I created an API for my Angular2 application and I parsed the csv file to JSON format. But now I'm kind of stuck, I found an answer for this from more that 2 years old so that's not working anymore. Any help?
This is the code I'm already using:
//Insert gebruikers from CSV file
router.post('/gebruikers/csv', type, (req, res) => {
fs.readFile(req.file.path, 'utf8', function (err, csvData) {
if (err) {
res.send("error in file reader: " + err);
return console.log(err);
}
csvParser(csvData, {
delimiter: ',',
columns: true
}, function (err, json) {
if (err) {
res.send("error in csvParser: " + err);
console.log(err);
} else {
console.log(json);
//I think the query should be done here...
}
})
})
});
Thanks in advance!!
EDIT
Using this code right now but it returns this error:
TypeError: parameter.value.getTime is not a function
Code:
router.post('/gebruikers/csv', type, (req, res) => {
fs.readFile(req.file.path, 'utf8', function (err, csvData) {
if (err) {
res.send("error in file reader: " + err);
return console.log(err);
}
csvParser(csvData, {
columns: true,
ltrim: true,
rtrim: true
}, function (err, json) {
if (err) {
res.send("error in csvParser: " + err);
console.log(err);
} else {
console.log(json);
const table = new sql.Table('[Alg].[User]');
table.create = true;
table.columns.add('Firstname', sql.NVarChar, { nullable: false });
table.columns.add('LastName', sql.NVarChar, { nullable: false });
table.columns.add('PrivateMail', sql.NVarChar, { nullable: false });
table.columns.add('UserName', sql.NVarChar, { nullable: false });
table.columns.add('Password', sql.NVarChar, { nullable: false });
table.columns.add('Auth', sql.NVarChar, { nullable: false });
table.columns.add('Enabled', sql.Bit, { nullable: false });
table.columns.add('Created', sql.SmallDateTime, { nullable: false });
table.columns.add('Manual', sql.Bit, { nullable: false });
table.columns.add('LastChanged', sql.SmallDateTime, { nullable: false });
table.columns.add('Staff', sql.Bit, { nullable: false });
var count = Object.keys(json).length;
console.log(count);
for (i = 0; i < count; i++) {
table.rows.add(json[i].Firstname, json[i].LastName, json[i].PrivateMail, json[i].UserName, json[i].Password, 'manual', 1, "GetDate()", 1, "GetDate()", 1);
}
console.log("Async function started!");
const request = new sql.Request();
request.bulk(table, (err, result) => {
// error checks?
if (err) {
console.log("ERROR post bulk gebruikers: " + err);
}
else {
res.send("SUCCES! " + result);
}
})
}
})
})
});
FIXED
As user Grigoriy Chudnov pointed out, I need an instance of new Date()
. I was thinking this wouldn't work because it would be in the wrong format, but node-mssql handles this for me.
currentDateTime = new Date();
table.rows.add(json[i].Firstname, json[i].LastName, json[i].PrivateMail, json[i].UserName, json[i].Password, 'manual', 1, currentDateTime, 1, currentDateTime, 1);
See Question&Answers more detail:
os