import { Inject , Injectable , Scope } from '@nestjs/common' ; import { from , Observable , of } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; import { Database } from 'sqlite3' ; import { DATABASE_FEATURE } from 'apps/api/src/app/database/database.constants' ; import { DatabaseFeatureOptions , DatabaseInterface , InsertParams , QueryParams , UpdateManyParams , UpdateParams , } from 'apps/api/src/app/database/interfaces/database.interface' ; @ Injectable ( { scope : Scope . TRANSIENT , } ) export class DatabaseTestService < T > implements DatabaseInterface < T > { private pool : Database ; tableName : string ; constructor ( @ Inject ( DATABASE_FEATURE ) readonly feature ) { this . tableName = feature . tableName ; this . pool = new Database ( ':memory:' ) ; } private sqLiteQuery ( query , params ) { return new Promise < T [ ] > ( ( resolve , reject ) => { this . pool . all ( query , params , ( err , rows ) => { if ( err ) { reject ( err ) ; } resolve ( rows ) ; } ) ; } ) ; } private runQuery ( query , params ) { return from ( this . sqLiteQuery ( query , params ) ) . pipe ( catchError ( ( ) => { return of ( [ ] ) ; } ) ) ; } query ( params ) { const query = 'SELECT ' + params . query + ' ' + this . tableName + ' WHERE ' + params . where ; return this . runQuery ( query , params . variables ) ; } insert ( params ) { const query = 'INSERT INTO ' + this . tableName + ' (' + params . query + ') VALUES (' + params . where + ') RETURNING id;' ; return this . runQuery ( query , params . variables ) ; } update ( params ) { const query = 'UPDATE ' + this . tableName + ' SET ' + params . query + ' WHERE ' + params . where + ' RETURNING id;' ; return this . runQuery ( query , params . variables ) ; } updateMany ( params ) { const query = 'UPDATE ' + this . tableName + ' AS ' + params . tableAlias + ' SET ' + params . query + ' FROM ' + params . tempTable + ' WHERE ' + params . where + ';' ; return this . runQuery ( query , params . variables ) ; } delete ( params ) { return of ( [ ] ) ; } }