UNPKG

@naturalcycles/db-lib

Version:

Lowest Common Denominator API to supported Databases

76 lines (75 loc) 4.01 kB
import { type JsonSchemaObject, type JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema'; import type { CommonLogger } from '@naturalcycles/js-lib/log'; import { type AnyObjectWithId, type ObjectWithId, type StringMap } from '@naturalcycles/js-lib/types'; import type { ReadableTyped } from '@naturalcycles/nodejs-lib/stream/stream.model.js'; import type { CommonDB, CommonDBSupport } from '../commondb/common.db.js'; import { CommonDBType } from '../commondb/common.db.js'; import type { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBTransactionOptions, DBOperation, DBTransaction, DBTransactionFn, RunQueryResult } from '../db.model.js'; import type { DBQuery } from '../query/dbQuery.js'; 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; /** * 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>; createTransaction(opt?: CommonDBTransactionOptions): Promise<DBTransaction>; incrementBatch(table: string, prop: string, incrementMap: StringMap<number>, _opt?: CommonDBOptions): Promise<StringMap<number>>; } 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>; }