Added batch insert util to replace bulk insert. Fixed circular dependencies.

This commit is contained in:
DebaucheryLibrarian
2026-02-25 01:09:49 +01:00
parent 9f37f54634
commit e77ced44c7
6 changed files with 260 additions and 87 deletions

64
src/tools/batch-test.js Normal file
View File

@@ -0,0 +1,64 @@
'use strict';
const knex = require('../knex');
const batchInsert = require('../utils/batch-insert');
async function createTestTable() {
const tableExists = await knex.schema.hasTable('batch_test');
if (tableExists) {
// await knex('batch_test').delete();
return;
}
await knex.schema.createTable('batch_test', (table) => {
table.increments('id');
table.string('name')
.unique();
table.integer('age');
table.text('location');
table.datetime('created_at')
.notNullable()
.defaultTo(knex.fn.now());
});
}
async function init() {
await createTestTable();
const transaction = await knex.transaction();
const entries = await batchInsert('batch_test', [
{
name: 'John',
age: 18,
location: 'Home',
},
{
name: 'Jack',
age: 38,
location: 'Work',
},
{
name: 'James',
age: 35,
location: 'Club',
},
], {
conflict: 'name',
update: true,
transaction,
commit: false,
});
await transaction.commit();
console.log('ENTRIES', entries);
// await knex.schema.dropTable('batch_test');
await knex.destroy();
}
init();