import * as Sqlite from 'better-sqlite3' ; export class Inserter { private sqlStmt : Sqlite . Statement ; private batch : any [ ] ; public constructor ( private db , private stmt , private numberOfArgs , private batchSize ) { this . sqlStmt = this . makeStatement ( this . batchSize ) ; this . batch = [ ] ; } public do ( ... params ) { if ( params . length !== this . numberOfArgs ) { throw new Error ( `Wrong number of arguments. Expected ${ this . numberOfArgs } but got ${ params . length }` ) ; } this . batch . push ( ... params ) ; if ( this . batch . length === this . numberOfArgs * this . batchSize ) { this . sqlStmt . run ( ... this . batch ) ; this . batch = [ ] ; } } public finish ( ) { if ( this . batch . length === 0 ) { return ; } const finalStatement = this . makeStatement ( this . batch . length / this . numberOfArgs ) ; finalStatement . run ( ... this . batch ) ; } private makeStatement ( size ) { const args = `(${ new Array ( this . numberOfArgs ) . fill ( '?' ) . join ( ',' ) })` ; return this . db . prepare ( `${ this . stmt } Values ${ new Array ( size ) . fill ( args ) . join ( ',' ) }` ) ; } }