@naturalcycles/db-lib
Version:
Lowest Common Denominator API to supported Databases
98 lines (97 loc) • 4.41 kB
TypeScript
import { AnyObjectWithId, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, StringMap } from '@naturalcycles/js-lib';
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
import { CommonDB, CommonDBSupport, CommonDBTransactionOptions, CommonDBType, DBOperation, DBTransactionFn } from '../..';
import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, DBTransaction, RunQueryResult } from '../../db.model';
import { DBQuery } from '../../query/dbQuery';
export interface InMemoryDBCfg {
/**
* @default ''
*
* Allows to support "Namespacing".
* E.g, pass `ns1_` to it and all tables will be prefixed by it.
* Reset cache respects this prefix (won't touch other namespaces!)
*/
tablesPrefix: string;
/**
* Many DB implementations (e.g Datastore and Firestore) forbid doing
* read operations after a write/delete operation was done inside a Transaction.
*
* To help spot that type of bug - InMemoryDB by default has this setting to `true`,
* which will throw on such occasions.
*
* Defaults to true.
*/
forbidTransactionReadAfterWrite?: boolean;
/**
* @default false
*
* Set to true to enable disk persistence (!).
*/
persistenceEnabled: boolean;
/**
* @default ./tmp/inmemorydb.ndjson.gz
*
* Will store one ndjson file per table.
* Will only flush on demand (see .flushToDisk() and .restoreFromDisk() methods).
* Even if persistence is enabled - nothing is flushed or restored automatically.
*/
persistentStoragePath: string;
/**
* @default true
*/
persistZip: boolean;
/**
* Defaults to `console`.
*/
logger?: CommonLogger;
}
export declare class InMemoryDB implements CommonDB {
dbType: CommonDBType;
support: CommonDBSupport;
constructor(cfg?: Partial<InMemoryDBCfg>);
cfg: InMemoryDBCfg;
data: StringMap<StringMap<AnyObjectWithId>>;
/**
* Returns internal "Data snapshot".
* Deterministic - jsonSorted.
*/
getDataSnapshot(): StringMap<StringMap<ObjectWithId>>;
ping(): Promise<void>;
/**
* Resets InMemory DB data
*/
resetCache(_table?: string): Promise<void>;
getTables(): Promise<string[]>;
getTableSchema<ROW extends ObjectWithId>(_table: string): Promise<JsonSchemaRootObject<ROW>>;
createTable<ROW extends ObjectWithId>(_table: string, _schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
getByIds<ROW extends ObjectWithId>(_table: string, ids: string[], _opt?: CommonDBOptions): Promise<ROW[]>;
saveBatch<ROW extends ObjectWithId>(_table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
deleteByIds(_table: string, ids: string[], _opt?: CommonDBOptions): Promise<number>;
patchByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: Partial<ROW>): Promise<number>;
runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<RunQueryResult<ROW>>;
runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): ReadableTyped<ROW>;
runInTransaction(fn: DBTransactionFn, opt?: CommonDBTransactionOptions): Promise<void>;
incrementBatch(table: string, prop: string, incrementMap: StringMap<number>, _opt?: CommonDBOptions): Promise<StringMap<number>>;
/**
* Flushes all tables (all namespaces) at once.
*/
flushToDisk(): Promise<void>;
/**
* Restores all tables (all namespaces) at once.
*/
restoreFromDisk(): Promise<void>;
}
export declare class InMemoryDBTransaction implements DBTransaction {
private db;
private opt;
constructor(db: InMemoryDB, opt: Required<CommonDBTransactionOptions>);
ops: DBOperation[];
writeOperationHappened: boolean;
getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>;
saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number>;
commit(): Promise<void>;
rollback(): Promise<void>;
}