@naturalcycles/datastore-lib
Version:
Opinionated library to work with Google Datastore, implements CommonDB
82 lines (81 loc) • 4.36 kB
TypeScript
import type { Datastore, Key, Transaction } 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 { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
import type { CommonLogger } from '@naturalcycles/js-lib/log';
import type { ObjectWithId } from '@naturalcycles/js-lib/types';
import type { ReadableTyped } 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(): Promise<Datastore>;
private getPropertyFilter;
private getDatastoreLib;
ping(): Promise<void>;
getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: DatastoreDBReadOptions): Promise<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): ReadableTyped<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>;
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 toDatastoreEntity;
key(ds: Datastore, kind: string, id: string): Key;
private getDsKey;
getKey(key: Key): string | undefined;
createTable<ROW extends ObjectWithId>(_table: string, _schema: JsonSchemaObject<ROW>): Promise<void>;
getTables(): Promise<string[]>;
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
private getPRetryOptions;
/**
* Silently rollback the transaction.
* It may happen that transaction is already committed/rolled back, so we don't want to throw an error here.
*/
private rollback;
private getRunQueryOptions;
}
/**
* 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>;
}