Reputation: 434
I'm curious how knex
works because it seems so fluid whereas sql
is more structured. Ultimately knex
transpiles into sql
or whatever database engine.
Questions
Is it better to chain methods to continually resolve the promise? ex:
knex('table').select('wire').limit(1).then((row)=>{
if(row){
knex('table2').update()... (1)
}
throw new Error('Nothing')
}).then(r=>{
knex('tablw')..... (2)
})
....
catch(e=>{
....
})
or to create a transaction in knex?
knex()
statement?I cannot find any documentation on how it actually calls/queries the database.
From first glance, it looks like each knex()
statement would call the database (in my example it would be three separate times?) which would mean that creating a transaction would be 1000% better than all these small queries/calls
thanks a bunch!
Upvotes: 3
Views: 1073
Reputation: 65
The update can be done in batches, i.e 1000 rows in a batch
And as long as it does it in batches, the bluebird map could be used.
For more information on bluebird map: http://bluebirdjs.com/docs/api/promise.map.html
const limit = 1000;
const totalRows = 50000;
const seq = count => Array(Math.ceil(count / limit)).keys();
let noErrors = true;
map(seq(totalRows), page => updateTable(dbTable, page), { concurrency: 1 });
const updateTable = async (dbTable, page) => {
let offset = limit* page;
return knex(dbTable).pluck('id').limit(limit).offset(offset).then(ids => {
return knex(dbTable)
.whereIn('id', ids)
.update({ date: new Date() })
.then((rows) => {
console.log(`${page} - Updated rows of the table ${dbTable} from ${offset} to ${offset + batch}: `, rows);
})
.catch((err) => {
noErrors = false;
console.log({ err });
rollback();
throw error; // Both with and without it - failed
});
})
.then(() => {
if(noErrors)
console.log({result:'MyResultsHere'});
else
console.log({ err });
})
.catch((err) => {
console.log({ err });
});
};
Where pluck() is used to get ids in array form.
You can create a custom rollback() function if you want that if any error is occured during process you want to undo your changes.
Upvotes: 0
Reputation: 35493
Excellent questions, here are some answers:
Small tip, make use of async/await
syntax, it will make you code more readable.
then
method it builds the query and passes it to the DB.
You can test it, build a select
query and don't call the "then", it won't execute any query on the DB.Upvotes: 2