All files / src sqlite.connector.ts

100% Statements 37/37
100% Branches 19/19
100% Functions 6/6
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84    2x   2x   2x 2x 2x 2x 2x                       2x   7x   6x   6x           6x 6x 6x 6x   6x 5x 5x     6x 10x 5x     6x   6x 12x 12x   12x 12x     6x 6x       3x       3x       10x 10x 6x 6x                
'use strict';
 
import {MikroORM} from '@mikro-orm/sqlite';
 
import {BackendRegistry} from '@owservable/core';
 
import SqliteBackend from './sqlite.backend';
import SqliteJournalPoller from './sqlite.journal.poller';
import SqliteTablesEntitiesMap from './tables.entities.map';
import installSqliteTriggers from './functions/install.triggers';
import {SqliteLiveUpdatesRegistry} from './decorators/live.updates';
 
export type SqliteConnectorOptionsType = {
	entities: any[];
	dbName: string;
	pollIntervalMs?: number;
	updateSchema?: boolean;
	safe?: boolean;
	triggers?: boolean;
	ormOptions?: any;
};
 
export default class SqliteConnector {
	public static async init(options: SqliteConnectorOptionsType): Promise<any> {
		if (SqliteConnector._orm) return SqliteConnector._orm;
 
		const {entities, dbName, pollIntervalMs = 250, updateSchema = true, safe = true, triggers = true, ormOptions = {}} = options;
 
		const orm: any = await MikroORM.init({
			entities,
			dbName,
			...ormOptions
		});
 
		const connection: any = orm.em.getConnection();
		await connection.execute('PRAGMA journal_mode = WAL');
		await connection.execute('PRAGMA busy_timeout = 5000');
		console.log(`[@owservable/sqlite] -> SQLite connected to ${dbName} (WAL)`);
 
		if (updateSchema) {
			await orm.schema.update({safe});
			console.log('[@owservable/sqlite] -> SQLite schema synchronized', safe ? '(safe mode)' : '');
		}
 
		if (triggers) {
			const liveEntities: any[] = entities.filter((entity: any): boolean => SqliteLiveUpdatesRegistry.has(entity));
			await installSqliteTriggers(orm, liveEntities);
		}
 
		const poller: SqliteJournalPoller = SqliteJournalPoller.init(orm, pollIntervalMs);
 
		for (const entity of entities) {
			const meta: any = orm.getMetadata().get(entity.name);
			const tableName: string = meta.tableName;
 
			SqliteTablesEntitiesMap.addTableToEntityMapping(tableName, entity);
			BackendRegistry.register(tableName, new SqliteBackend(orm, entity, poller));
		}
 
		SqliteConnector._orm = orm;
		return orm;
	}
 
	public static get orm(): any {
		return SqliteConnector._orm;
	}
 
	public static em(): any {
		return SqliteConnector._orm?.em.fork();
	}
 
	public static async close(): Promise<void> {
		SqliteJournalPoller.stop();
		if (SqliteConnector._orm) {
			await SqliteConnector._orm.close(true);
			SqliteConnector._orm = null;
		}
	}
 
	private static _orm: any;
 
	private constructor() {}
}