import { SQLJsonCore } from 'debe-sql' ; import sql from 'better-sqlite3' ; import { ICollection , Debe , ICollectionInput } from 'debe' ; import { DebeBackend } from 'debe-adapter' ; export class BetterSqlite3Debe extends Debe { constructor ( dbPath , collections , options ? ) { super ( new DebeBackend ( new BetterSqlite3Adapter ( dbPath ) , collections , options ) ) ; } } export class BetterSqlite3Adapter extends SQLJsonCore { db : any ; chunks = 50 * 10000 ; constructor ( dbPath ) { super ( ) ; this . db = sql ( dbPath ) ; } close ( ) { return this . db . close ( ) ; } selectJSONField ( collection , field ) { return `json_extract(${ this . getCollectionBodyField ( collection ) }, '$.${ field }')` ; } createTableIndex ( collection , field , type ? ) { if ( collection . fields [ field ] ) { return super . createTableIndex ( collection , field ) ; } return `CREATE INDEX IF NOT EXISTS "${ collection . name }_${ field }" ON "${ collection . name }" (json_extract(${ this . getCollectionBodyField ( collection ) }, '$.${ field }'))` ; } exec < T > ( sql , args , type ? ) { return new Promise ( ( yay , nay ) => { try { this . db . transaction ( ( ) => { if ( type === 'count' ) { yay ( this . db . prepare ( sql ) . all ( ... args ) [ 0 ] . count ) ; } else if ( type === 'get' ) { yay ( this . db . prepare ( sql ) . get ( ... args ) ) ; } else if ( type === 'all' ) { yay ( this . db . prepare ( sql ) . all ( ... args ) ) ; } else if ( type === 'insert' ) { const prepare = this . db . prepare ( sql ) ; yay ( args . map ( arg => prepare . run ( arg ) ) as any ) ; } else { if ( sql ) { this . db . prepare ( sql ) . run ( ) ; } args . forEach ( arg => this . db . prepare ( arg ) . run ( ) ) ; yay ( ) ; } } ) . immediate ( ) ; } catch ( err ) { nay ( err ) ; } } ) ; } }