mirror of
https://github.com/xfarrow/blink
synced 2025-06-27 09:03:02 +02:00
Change endpoint from persons to people
This commit is contained in:
51
backend/apis/nodejs/node_modules/knex/lib/execution/batch-insert.js
generated
vendored
Normal file
51
backend/apis/nodejs/node_modules/knex/lib/execution/batch-insert.js
generated
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
const chunk = require('lodash/chunk');
|
||||
const flatten = require('lodash/flatten');
|
||||
const delay = require('./internal/delay');
|
||||
const { isNumber } = require('../util/is');
|
||||
|
||||
function batchInsert(client, tableName, batch, chunkSize = 1000) {
|
||||
let returning = undefined;
|
||||
let transaction = null;
|
||||
if (!isNumber(chunkSize) || chunkSize < 1) {
|
||||
throw new TypeError(`Invalid chunkSize: ${chunkSize}`);
|
||||
}
|
||||
if (!Array.isArray(batch)) {
|
||||
throw new TypeError(`Invalid batch: Expected array, got ${typeof batch}`);
|
||||
}
|
||||
const chunks = chunk(batch, chunkSize);
|
||||
|
||||
const runInTransaction = (cb) => {
|
||||
if (transaction) {
|
||||
return cb(transaction);
|
||||
}
|
||||
return client.transaction(cb);
|
||||
};
|
||||
|
||||
return Object.assign(
|
||||
Promise.resolve().then(async () => {
|
||||
//Next tick to ensure wrapper functions are called if needed
|
||||
await delay(1);
|
||||
return runInTransaction(async (tr) => {
|
||||
const chunksResults = [];
|
||||
for (const items of chunks) {
|
||||
chunksResults.push(await tr(tableName).insert(items, returning));
|
||||
}
|
||||
return flatten(chunksResults);
|
||||
});
|
||||
}),
|
||||
{
|
||||
returning(columns) {
|
||||
returning = columns;
|
||||
|
||||
return this;
|
||||
},
|
||||
transacting(tr) {
|
||||
transaction = tr;
|
||||
|
||||
return this;
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = batchInsert;
|
Reference in New Issue
Block a user