UNPKG

@naturalcycles/datastore-lib

Version:

Opinionated library to work with Google Datastore, implements CommonDB

79 lines (78 loc) 4.3 kB
import type { Key, Transaction } from '@google-cloud/datastore'; import { Datastore } from '@google-cloud/datastore'; import type { CommonDB, CommonDBOptions, CommonDBReadOptions, CommonDBSaveOptions, CommonDBSupport, CommonDBTransactionOptions, DBQuery, DBTransaction, DBTransactionFn, RunQueryResult } from '@naturalcycles/db-lib'; import { BaseCommonDB } from '@naturalcycles/db-lib'; import type { CommonLogger } from '@naturalcycles/js-lib/log'; import type { ObjectWithId, StringMap } from '@naturalcycles/js-lib/types'; import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'; import { Pipeline } from '@naturalcycles/nodejs-lib/stream'; import type { DatastoreDBCfg, DatastoreDBOptions, DatastoreDBReadOptions, DatastoreDBSaveOptions, DatastoreDBStreamOptions, DatastorePropertyStats, DatastoreStats } from './datastore.model.js'; /** * Datastore API: * https://googlecloudplatform.github.io/google-cloud-node/#/docs/datastore/1.0.3/datastore * https://cloud.google.com/datastore/docs/datastore-api-tutorial */ export declare class DatastoreDB extends BaseCommonDB implements CommonDB { support: CommonDBSupport; constructor(cfg?: DatastoreDBCfg); cfg: DatastoreDBCfg & { logger: CommonLogger; }; private cachedDatastore?; /** * Datastore.KEY */ protected KEY: symbol; ds(): Datastore; ping(): Promise<void>; getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: DatastoreDBReadOptions): Promise<ROW[]>; multiGet<ROW extends ObjectWithId>(map: StringMap<string[]>, opt?: DatastoreDBReadOptions): Promise<StringMap<ROW[]>>; runQuery<ROW extends ObjectWithId>(dbQuery: DBQuery<ROW>, opt?: DatastoreDBReadOptions): Promise<RunQueryResult<ROW>>; runQueryCount<ROW extends ObjectWithId>(dbQuery: DBQuery<ROW>, opt?: DatastoreDBReadOptions): Promise<number>; private runDatastoreQuery; streamQuery<ROW extends ObjectWithId>(dbQuery: DBQuery<ROW>, _opt?: DatastoreDBStreamOptions): Pipeline<ROW>; /** * Returns saved entities with generated id/updated/created (non-mutating!) */ saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: DatastoreDBSaveOptions<ROW>): Promise<void>; deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: DatastoreDBReadOptions): Promise<number>; /** * Limitation: Datastore's delete returns void, so we always return all ids here as "deleted" * regardless if they were actually deleted or not. */ deleteByIds(table: string, ids: string[], opt?: DatastoreDBOptions): Promise<number>; multiDelete(map: StringMap<string[]>, opt?: DatastoreDBOptions): Promise<number>; createTransaction(opt?: CommonDBTransactionOptions): Promise<DatastoreDBTransaction>; runInTransaction(fn: DBTransactionFn, opt?: CommonDBTransactionOptions): Promise<void>; getAllStats(): Promise<DatastoreStats[]>; /** * Returns undefined e.g when Table is non-existing */ getStats(table: string): Promise<DatastoreStats | undefined>; getStatsCount(table: string): Promise<number | undefined>; getTableProperties(table: string): Promise<DatastorePropertyStats[]>; private mapId; private parseDatastoreEntity; private toDatastoreEntity; key(ds: Datastore, kind: string, id: string): Key; private getDsKey; private getIdFromKey; createTable<ROW extends ObjectWithId>(_table: string, _schema: JsonSchema<ROW>): Promise<void>; getTables(): Promise<string[]>; getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchema<ROW>>; private getPRetryOptions; private rollback; } /** * https://cloud.google.com/datastore/docs/concepts/transactions#datastore-datastore-transactional-update-nodejs */ export declare class DatastoreDBTransaction implements DBTransaction { db: DatastoreDB; tx: Transaction; constructor(db: DatastoreDB, tx: Transaction); commit(): Promise<void>; rollback(): Promise<void>; getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBReadOptions): Promise<ROW[]>; saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>; deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number>; }