I have a function bulkInsert()
which performs an insert operation using pg-promise and when I call it using the await
keyword, it executes without any result (success/error). I am not sure if I am missing anything. Any help is appreciated.
const pgp = require('pg-promise');
const db = pgp({
host: 'localhost',
port: 5443,
database: 'data_base',
user: 'user_name',
password: 'pass_word',
});
.......
.......
.......
const bulkInsert = async (data) => { //function when called, executes without any result(success/failure)
const cs = new pgp.helpers.ColumnSet(['col_A', 'col_B','col_C'], { table: 'table_name' });
db.tx('massive-insert', t => {
const processData = data => {
if (data) {
const insert = pgp.helpers.insert(data, cs);
return t.none(insert);
}
};
return t.sequence(index => getNextData(t, index, data).then(processData));
})
.then(() => {
console.log('Success');
})
.catch(error => {
console.log(error);
});
function getNextData(t, pageIndex, data) {
return new Promise((resolve, reject) => {
const batchSize = 1000;
const nextDataBatch = [];
try{
for (let i = pageIndex; i < data.length; i += batchSize) {
const batch = data.slice(i, i + batchSize);
nextDataBatch.push(batch);
}
if (nextDataBatch.length === 0) {
resolve(null);
} else {
resolve(nextDataBatch);
}
}
catch(err){
reject(err);
}
});
}
};
PS: I am using pg-promise v11.5.4 and in node v18.17.1