import fs from 'fs'; import path from 'path'; import { SqljsConnectionOptions } from 'typeorm/driver/sqljs/SqljsConnectionOptions'; import { Mutable } from '../types'; import { TestDbInitializer } from './test-db-initializer'; export class SqljsInitializer implements TestDbInitializer { private dbFilePath: string; private connectionOptions: SqljsConnectionOptions; /** * @param dataDir * @param postPopulateTimeoutMs Allows you to specify a timeout to wait after the population * step and before the server is shut down. Can resolve occasional race condition issues with * the job queue. */ constructor(private dataDir: string, private postPopulateTimeoutMs: number = 0) {} async init( testFileName: string, connectionOptions: SqljsConnectionOptions, ): Promise { this.dbFilePath = this.getDbFilePath(testFileName); this.connectionOptions = connectionOptions; (connectionOptions as Mutable).location = this.dbFilePath; return connectionOptions; } async populate(populateFn: () => Promise): Promise { if (!fs.existsSync(this.dbFilePath)) { const dirName = path.dirname(this.dbFilePath); if (!fs.existsSync(dirName)) { fs.mkdirSync(dirName); } (this.connectionOptions as Mutable).autoSave = true; (this.connectionOptions as Mutable).synchronize = true; await populateFn(); await new Promise(resolve => setTimeout(resolve, this.postPopulateTimeoutMs)); (this.connectionOptions as Mutable).autoSave = false; (this.connectionOptions as Mutable).synchronize = false; } } destroy(): void | Promise { return undefined; } private getDbFilePath(testFileName: string) { // tslint:disable-next-line:no-non-null-assertion const dbFileName = path.basename(testFileName) + '.sqlite'; const dbFilePath = path.join(this.dataDir, dbFileName); return dbFilePath; } }