Added batch insert util to replace bulk insert. Fixed circular dependencies.
This commit is contained in:
64
src/tools/batch-test.js
Normal file
64
src/tools/batch-test.js
Normal 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();
|
||||
Reference in New Issue
Block a user