import type { FilledMangoQuery, LokiDatabaseSettings, LokiSettings, LokiStorageInternals, RxDocumentData, RxJsonSchema, RxStorage, RxStorageInstanceCreationParams, RxStorageStatics } from 'nxdb-old/src/types'; import { ensureNotFalsy, flatClone } from 'nxdb-old/src/plugins/utils'; import { createLokiStorageInstance, RxStorageInstanceLoki } from 'nxdb-old/src/plugins/storage-lokijs/rx-storage-instance-loki'; import { RX_STORAGE_NAME_LOKIJS } from 'nxdb-old/src/plugins/storage-lokijs/lokijs-helper'; import type { LeaderElector } from 'broadcast-channel'; import { ensureRxStorageInstanceParamsAreCorrect } from 'nxdb-old/src/rx-storage-helper'; import { DEFAULT_CHECKPOINT_SCHEMA } from 'nxdb-old/src/rx-schema-helper'; export const RxStorageLokiStatics: RxStorageStatics = { prepareQuery( _schema: RxJsonSchema>, mutateableQuery: FilledMangoQuery ) { mutateableQuery = flatClone(mutateableQuery); if (Object.keys(ensureNotFalsy(mutateableQuery.selector)).length > 0) { mutateableQuery.selector = { $and: [ { _deleted: false }, mutateableQuery.selector ] } as any; } else { mutateableQuery.selector = { _deleted: false } as any; } return mutateableQuery; }, checkpointSchema: DEFAULT_CHECKPOINT_SCHEMA }; export class RxStorageLoki implements RxStorage { public name = RX_STORAGE_NAME_LOKIJS; public statics = RxStorageLokiStatics; /** * Create one leader elector by db name. * This is done inside of the storage, not globally * to make it easier to test multi-tab behavior. */ public leaderElectorByLokiDbName: Map = new Map(); constructor( public databaseSettings: LokiDatabaseSettings ) { } public createStorageInstance( params: RxStorageInstanceCreationParams ): Promise> { ensureRxStorageInstanceParamsAreCorrect(params); return createLokiStorageInstance(this, params, this.databaseSettings); } } export function getRxStorageLoki( databaseSettings: LokiDatabaseSettings = {} ): RxStorageLoki { const storage = new RxStorageLoki(databaseSettings); return storage; }