@naturalcycles/db-lib
Advanced tools
Comparing version 9.4.0 to 9.4.1
@@ -1,2 +0,2 @@ | ||
import { JsonSchemaObject, JsonSchemaRootObject, PartialObjectWithId, Saved } from '@naturalcycles/js-lib'; | ||
import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib'; | ||
import { ReadableTyped } from '@naturalcycles/nodejs-lib'; | ||
@@ -23,11 +23,11 @@ import { BaseCommonDB } from '../../base.common.db'; | ||
getTables(): Promise<string[]>; | ||
getTableSchema<ROW extends PartialObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>; | ||
createTable<ROW extends PartialObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CacheDBCreateOptions): Promise<void>; | ||
getByIds<ROW extends PartialObjectWithId>(table: string, ids: string[], opt?: CacheDBSaveOptions<ROW>): Promise<Saved<ROW>[]>; | ||
saveBatch<ROW extends PartialObjectWithId>(table: string, rows: ROW[], opt?: CacheDBSaveOptions<ROW>): Promise<void>; | ||
runQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBSaveOptions<ROW>): Promise<RunQueryResult<Saved<ROW>>>; | ||
runQueryCount<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>; | ||
streamQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBStreamOptions): ReadableTyped<Saved<ROW>>; | ||
deleteByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>; | ||
updateByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CacheDBOptions): Promise<number>; | ||
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>; | ||
createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CacheDBCreateOptions): Promise<void>; | ||
getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CacheDBSaveOptions<ROW>): Promise<ROW[]>; | ||
saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CacheDBSaveOptions<ROW>): Promise<void>; | ||
runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBSaveOptions<ROW>): Promise<RunQueryResult<ROW>>; | ||
runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>; | ||
streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBStreamOptions): ReadableTyped<ROW>; | ||
deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>; | ||
updateByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CacheDBOptions): Promise<number>; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { CommonLogger, PartialObjectWithId } from '@naturalcycles/js-lib'; | ||
import { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib'; | ||
import { CommonDB } from '../../common.db'; | ||
@@ -50,3 +50,3 @@ import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions } from '../../db.model'; | ||
} | ||
export interface CacheDBSaveOptions<ROW extends PartialObjectWithId> extends CacheDBOptions, CommonDBSaveOptions<ROW> { | ||
export interface CacheDBSaveOptions<ROW extends ObjectWithId> extends CacheDBOptions, CommonDBSaveOptions<ROW> { | ||
} | ||
@@ -53,0 +53,0 @@ export interface CacheDBStreamOptions extends CacheDBOptions, CommonDBStreamOptions { |
@@ -1,2 +0,2 @@ | ||
import { JsonSchemaRootObject, Saved, PartialObjectWithId } from '@naturalcycles/js-lib'; | ||
import { JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib'; | ||
import { ReadableTyped } from '@naturalcycles/nodejs-lib'; | ||
@@ -24,16 +24,16 @@ import { BaseCommonDB, CommonDBSupport, DBSaveBatchOperation } from '../..'; | ||
getTables(): Promise<string[]>; | ||
getByIds<ROW extends PartialObjectWithId>(table: string, ids: string[], _opt?: CommonDBOptions): Promise<Saved<ROW>[]>; | ||
saveBatch<ROW extends PartialObjectWithId>(table: string, rows: ROW[], _opt?: CommonDBSaveOptions<ROW>): Promise<void>; | ||
runQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<RunQueryResult<Saved<ROW>>>; | ||
runQueryCount<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>; | ||
streamQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBStreamOptions): ReadableTyped<ROW>; | ||
deleteByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>; | ||
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>; | ||
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?: CommonDBStreamOptions): ReadableTyped<ROW>; | ||
deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>; | ||
deleteByIds(table: string, ids: string[], _opt?: CommonDBOptions): Promise<number>; | ||
getTableSchema<ROW extends PartialObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>; | ||
loadFile<ROW extends PartialObjectWithId>(table: string): Promise<Saved<ROW>[]>; | ||
saveFile<ROW extends PartialObjectWithId>(table: string, _rows: ROW[]): Promise<void>; | ||
saveFiles<ROW extends PartialObjectWithId>(ops: DBSaveBatchOperation<ROW>[]): Promise<void>; | ||
sortRows<ROW extends PartialObjectWithId>(rows: ROW[]): ROW[]; | ||
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>; | ||
loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]>; | ||
saveFile<ROW extends ObjectWithId>(table: string, _rows: ROW[]): Promise<void>; | ||
saveFiles<ROW extends ObjectWithId>(ops: DBSaveBatchOperation<ROW>[]): Promise<void>; | ||
sortRows<ROW extends ObjectWithId>(rows: ROW[]): ROW[]; | ||
private logStarted; | ||
private logFinished; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { CommonLogger, PartialObjectWithId, Saved } from '@naturalcycles/js-lib'; | ||
import { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib'; | ||
import { DBSaveBatchOperation } from '../../db.model'; | ||
@@ -7,3 +7,3 @@ import type { DBQueryOrder } from '../../query/dbQuery'; | ||
getTables: () => Promise<string[]>; | ||
loadFile: <ROW extends PartialObjectWithId>(table: string) => Promise<Saved<ROW>[]>; | ||
loadFile: <ROW extends ObjectWithId>(table: string) => Promise<ROW[]>; | ||
saveFiles: (ops: DBSaveBatchOperation<any>[]) => Promise<void>; | ||
@@ -16,3 +16,3 @@ } | ||
*/ | ||
sortOnSave?: DBQueryOrder; | ||
sortOnSave?: DBQueryOrder<any>; | ||
/** | ||
@@ -19,0 +19,0 @@ * @default true |
@@ -12,3 +12,3 @@ import { StringMap, ObjectWithId } from '@naturalcycles/js-lib'; | ||
loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]>; | ||
saveFiles(ops: DBSaveBatchOperation[]): Promise<void>; | ||
saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void>; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { PartialObjectWithId, Saved } from '@naturalcycles/js-lib'; | ||
import { ObjectWithId } from '@naturalcycles/js-lib'; | ||
import { DBSaveBatchOperation } from '../../db.model'; | ||
@@ -22,5 +22,5 @@ import { FileDBPersistencePlugin } from './file.db.model'; | ||
getTables(): Promise<string[]>; | ||
loadFile<ROW extends PartialObjectWithId>(table: string): Promise<Saved<ROW>[]>; | ||
saveFiles(ops: DBSaveBatchOperation[]): Promise<void>; | ||
saveFile<ROW extends PartialObjectWithId>(table: string, rows: ROW[]): Promise<void>; | ||
loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]>; | ||
saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void>; | ||
saveFile<ROW extends ObjectWithId>(table: string, rows: ROW[]): Promise<void>; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { PartialObjectWithId, Saved } from '@naturalcycles/js-lib'; | ||
import { ObjectWithId } from '@naturalcycles/js-lib'; | ||
import { DBSaveBatchOperation } from '../../db.model'; | ||
@@ -7,4 +7,4 @@ import { FileDBPersistencePlugin } from './file.db.model'; | ||
getTables(): Promise<string[]>; | ||
loadFile<ROW extends PartialObjectWithId>(_table: string): Promise<Saved<ROW>[]>; | ||
saveFiles(_ops: DBSaveBatchOperation[]): Promise<void>; | ||
loadFile<ROW extends ObjectWithId>(_table: string): Promise<ROW[]>; | ||
saveFiles(_ops: DBSaveBatchOperation<any>[]): Promise<void>; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { JsonSchemaObject, StringMap, JsonSchemaRootObject, ObjectWithId, CommonLogger, PartialObjectWithId, Saved } from '@naturalcycles/js-lib'; | ||
import { JsonSchemaObject, StringMap, JsonSchemaRootObject, ObjectWithId, CommonLogger } from '@naturalcycles/js-lib'; | ||
import { ReadableTyped } from '@naturalcycles/nodejs-lib'; | ||
@@ -81,12 +81,12 @@ import { CommonDB, CommonDBTransactionOptions, CommonDBType, DBOperation, DBPatch, DBTransactionFn } from '../..'; | ||
getTables(): Promise<string[]>; | ||
getTableSchema<ROW extends PartialObjectWithId>(_table: string): Promise<JsonSchemaRootObject<ROW>>; | ||
createTable<ROW extends PartialObjectWithId>(_table: string, _schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>; | ||
getByIds<ROW extends PartialObjectWithId>(_table: string, ids: string[], _opt?: CommonDBOptions): Promise<Saved<ROW>[]>; | ||
saveBatch<ROW extends PartialObjectWithId>(_table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>; | ||
deleteByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>; | ||
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>; | ||
updateByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>): Promise<number>; | ||
runQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<RunQueryResult<Saved<ROW>>>; | ||
runQueryCount<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>; | ||
streamQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): ReadableTyped<Saved<ROW>>; | ||
updateByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<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>; | ||
@@ -108,4 +108,4 @@ /** | ||
writeOperationHappened: boolean; | ||
getByIds<ROW extends PartialObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>; | ||
saveBatch<ROW extends PartialObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): 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>; | ||
deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number>; | ||
@@ -112,0 +112,0 @@ commit(): Promise<void>; |
@@ -1,3 +0,3 @@ | ||
import { PartialObjectWithId } from '@naturalcycles/js-lib'; | ||
import { ObjectWithId } from '@naturalcycles/js-lib'; | ||
import { DBQuery } from '../../query/dbQuery'; | ||
export declare function queryInMemory<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, rows?: ROW[]): ROW[]; | ||
export declare function queryInMemory<ROW extends ObjectWithId>(q: DBQuery<ROW>, rows?: ROW[]): ROW[]; |
@@ -1,2 +0,2 @@ | ||
import { JsonSchemaObject, JsonSchemaRootObject, PartialObjectWithId, Saved } from '@naturalcycles/js-lib'; | ||
import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib'; | ||
import { ReadableTyped } from '@naturalcycles/nodejs-lib'; | ||
@@ -15,13 +15,13 @@ import { CommonDB, CommonDBSupport, CommonDBType } from './common.db'; | ||
getTables(): Promise<string[]>; | ||
getTableSchema<ROW extends PartialObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>; | ||
createTable<ROW extends PartialObjectWithId>(table: string, schema: JsonSchemaObject<ROW>): Promise<void>; | ||
getByIds<ROW extends PartialObjectWithId>(table: string, ids: string[]): Promise<Saved<ROW>[]>; | ||
deleteByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>): Promise<number>; | ||
updateByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CommonDBOptions): Promise<number>; | ||
runQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>): Promise<RunQueryResult<Saved<ROW>>>; | ||
runQueryCount<ROW extends PartialObjectWithId>(q: DBQuery<ROW>): Promise<number>; | ||
saveBatch<ROW extends PartialObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>; | ||
streamQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>): ReadableTyped<Saved<ROW>>; | ||
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>; | ||
createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>): Promise<void>; | ||
getByIds<ROW extends ObjectWithId>(table: string, ids: string[]): Promise<ROW[]>; | ||
deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<number>; | ||
updateByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CommonDBOptions): Promise<number>; | ||
runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<RunQueryResult<ROW>>; | ||
runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<number>; | ||
saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>; | ||
streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): ReadableTyped<ROW>; | ||
deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number>; | ||
runInTransaction(fn: DBTransactionFn, opt?: CommonDBTransactionOptions): Promise<void>; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { JsonSchemaObject, JsonSchemaRootObject, PartialObjectWithId, Saved } from '@naturalcycles/js-lib'; | ||
import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib'; | ||
import type { ReadableTyped } from '@naturalcycles/nodejs-lib'; | ||
@@ -50,3 +50,3 @@ import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, CommonDBTransactionOptions, DBPatch, DBTransactionFn, RunQueryResult } from './db.model'; | ||
*/ | ||
getTableSchema: <ROW extends PartialObjectWithId>(table: string) => Promise<JsonSchemaRootObject<ROW>>; | ||
getTableSchema: <ROW extends ObjectWithId>(table: string) => Promise<JsonSchemaRootObject<ROW>>; | ||
/** | ||
@@ -56,3 +56,3 @@ * Will do like `create table ...` for mysql. | ||
*/ | ||
createTable: <ROW extends PartialObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions) => Promise<void>; | ||
createTable: <ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions) => Promise<void>; | ||
/** | ||
@@ -62,13 +62,13 @@ * Order of items returned is not guaranteed to match order of ids. | ||
*/ | ||
getByIds: <ROW extends PartialObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions) => Promise<Saved<ROW>[]>; | ||
getByIds: <ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions) => Promise<ROW[]>; | ||
/** | ||
* Order by 'id' is not supported by all implementations (for example, Datastore doesn't support it). | ||
*/ | ||
runQuery: <ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions) => Promise<RunQueryResult<Saved<ROW>>>; | ||
runQueryCount: <ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions) => Promise<number>; | ||
streamQuery: <ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBStreamOptions) => ReadableTyped<Saved<ROW>>; | ||
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?: CommonDBStreamOptions) => ReadableTyped<ROW>; | ||
/** | ||
* rows can have missing ids only if DB supports auto-generating them (like mysql auto_increment). | ||
*/ | ||
saveBatch: <ROW extends PartialObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>) => Promise<void>; | ||
saveBatch: <ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>) => Promise<void>; | ||
/** | ||
@@ -83,3 +83,3 @@ * Returns number of deleted items. | ||
*/ | ||
deleteByQuery: <ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions) => Promise<number>; | ||
deleteByQuery: <ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions) => Promise<number>; | ||
/** | ||
@@ -103,3 +103,3 @@ * Applies patch to the rows returned by the query. | ||
*/ | ||
updateByQuery: <ROW extends PartialObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CommonDBOptions) => Promise<number>; | ||
updateByQuery: <ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CommonDBOptions) => Promise<number>; | ||
/** | ||
@@ -106,0 +106,0 @@ * Should be implemented as a Transaction (best effort), which means that |
/// <reference types="node" /> | ||
import { Transform } from 'node:stream'; | ||
import { AnyObject, AsyncMapper, BaseDBEntity, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, PartialObjectWithId, Saved, UnixTimestampMillisNumber, ZodSchema } from '@naturalcycles/js-lib'; | ||
import { AnyObject, AsyncMapper, BaseDBEntity, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, Saved, UnixTimestampMillisNumber, UnsavedId, ZodSchema } from '@naturalcycles/js-lib'; | ||
import { AjvSchema, ObjectSchema, ReadableTyped } from '@naturalcycles/nodejs-lib'; | ||
@@ -15,3 +15,3 @@ import { CommonDBTransactionOptions, DBModelType, DBPatch, DBTransaction, RunQueryResult } from '../db.model'; | ||
*/ | ||
export declare class CommonDao<BM extends PartialObjectWithId, DBM extends PartialObjectWithId = BM, TM extends AnyObject = BM> { | ||
export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, TM extends AnyObject = BM> { | ||
cfg: CommonDaoCfg<BM, DBM, TM>; | ||
@@ -86,7 +86,7 @@ constructor(cfg: CommonDaoCfg<BM, DBM, TM>); | ||
*/ | ||
assignIdCreatedUpdated<T extends BaseDBEntity>(obj: T, opt?: CommonDaoOptions): Saved<T>; | ||
assignIdCreatedUpdated<T extends BaseDBEntity>(obj: Partial<T>, opt?: CommonDaoOptions): T; | ||
/** | ||
* Mutates with id, created, updated | ||
*/ | ||
save(bm: BM, opt?: CommonDaoSaveOptions<BM, DBM>): Promise<Saved<BM>>; | ||
save(bm: UnsavedId<BM>, opt?: CommonDaoSaveOptions<BM, DBM>): Promise<Saved<BM>>; | ||
/** | ||
@@ -100,3 +100,3 @@ * 1. Applies the patch | ||
*/ | ||
savePatch(bm: Saved<BM>, patch: Partial<BM>, opt: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>; | ||
savePatch(bm: BM, patch: Partial<BM>, opt: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>; | ||
/** | ||
@@ -122,10 +122,10 @@ * Convenience method to replace 3 operations (loading+patching+saving) with one: | ||
*/ | ||
patch(bm: Saved<BM>, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>; | ||
patch(bm: BM, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>; | ||
/** | ||
* Like patch, but runs all operations within a Transaction. | ||
*/ | ||
patchInTransaction(bm: Saved<BM>, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>; | ||
saveAsDBM(dbm: DBM, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<DBM>>; | ||
saveBatch(bms: BM[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>[]>; | ||
saveBatchAsDBM(dbms: DBM[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<DBM>[]>; | ||
patchInTransaction(bm: BM, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>; | ||
saveAsDBM(dbm: UnsavedId<DBM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<DBM>>; | ||
saveBatch(bms: UnsavedId<BM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>[]>; | ||
saveBatchAsDBM(dbms: UnsavedId<DBM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<DBM>[]>; | ||
/** | ||
@@ -154,4 +154,4 @@ * "Streaming" is implemented by buffering incoming rows into **batches** | ||
dbmToBM(_dbm: undefined, opt?: CommonDaoOptions): Promise<undefined>; | ||
dbmToBM(_dbm?: Saved<DBM>, opt?: CommonDaoOptions): Promise<Saved<BM>>; | ||
dbmsToBM(dbms: Saved<DBM>[], opt?: CommonDaoOptions): Promise<Saved<BM>[]>; | ||
dbmToBM(_dbm?: DBM, opt?: CommonDaoOptions): Promise<Saved<BM>>; | ||
dbmsToBM(dbms: DBM[], opt?: CommonDaoOptions): Promise<Saved<BM>[]>; | ||
/** | ||
@@ -166,6 +166,6 @@ * Mutates object with properties: id, created, updated. | ||
anyToDBM(dbm?: any, opt?: CommonDaoOptions): Saved<DBM>; | ||
anyToDBMs(entities: Saved<DBM>[], opt?: CommonDaoOptions): Saved<DBM>[]; | ||
anyToDBMs(entities: DBM[], opt?: CommonDaoOptions): Saved<DBM>[]; | ||
bmToTM(bm: undefined, opt?: CommonDaoOptions): TM | undefined; | ||
bmToTM(bm?: Saved<BM>, opt?: CommonDaoOptions): TM; | ||
bmsToTM(bms: Saved<BM>[], opt?: CommonDaoOptions): TM[]; | ||
bmToTM(bm?: BM, opt?: CommonDaoOptions): TM; | ||
bmsToTM(bms: BM[], opt?: CommonDaoOptions): TM[]; | ||
/** | ||
@@ -208,8 +208,8 @@ * Returns *converted value*. | ||
rollback(): Promise<void>; | ||
getById<BM extends PartialObjectWithId, DBM extends PartialObjectWithId>(dao: CommonDao<BM, DBM, any>, id?: string | null, opt?: CommonDaoOptions): Promise<Saved<BM> | null>; | ||
getByIds<BM extends PartialObjectWithId, DBM extends PartialObjectWithId>(dao: CommonDao<BM, DBM, any>, ids: string[], opt?: CommonDaoOptions): Promise<Saved<BM>[]>; | ||
save<BM extends PartialObjectWithId, DBM extends PartialObjectWithId>(dao: CommonDao<BM, DBM, any>, bm: BM, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>; | ||
saveBatch<BM extends PartialObjectWithId, DBM extends PartialObjectWithId>(dao: CommonDao<BM, DBM, any>, bms: BM[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>[]>; | ||
getById<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM, any>, id?: string | null, opt?: CommonDaoOptions): Promise<Saved<BM> | null>; | ||
getByIds<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM, any>, ids: string[], opt?: CommonDaoOptions): Promise<Saved<BM>[]>; | ||
save<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM, any>, bm: UnsavedId<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>; | ||
saveBatch<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM, any>, bms: UnsavedId<BM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>[]>; | ||
deleteById(dao: CommonDao<any>, id?: string | null, opt?: CommonDaoOptions): Promise<number>; | ||
deleteByIds(dao: CommonDao<any>, ids: string[], opt?: CommonDaoOptions): Promise<number>; | ||
} |
@@ -511,2 +511,3 @@ "use strict"; | ||
this.assignIdCreatedUpdated(bm, opt); // mutates | ||
(0, js_lib_1._typeCast)(bm); | ||
let dbm = await this.bmToDBM(bm, opt); | ||
@@ -513,0 +514,0 @@ if (this.cfg.hooks.beforeSave) { |
@@ -1,6 +0,6 @@ | ||
import { AnyObject, CommonLogger, ErrorMode, PartialObjectWithId, Promisable, Saved, ZodError, ZodSchema } from '@naturalcycles/js-lib'; | ||
import { AnyObject, BaseDBEntity, CommonLogger, ErrorMode, Promisable, Saved, ZodError, ZodSchema } from '@naturalcycles/js-lib'; | ||
import { AjvSchema, AjvValidationError, JoiValidationError, ObjectSchema, TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib'; | ||
import { CommonDB } from '../common.db'; | ||
import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model'; | ||
export interface CommonDaoHooks<BM extends PartialObjectWithId, DBM extends PartialObjectWithId, TM> { | ||
export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntity, TM> { | ||
/** | ||
@@ -41,3 +41,3 @@ * Allows to override the id generation function. | ||
beforeDBMValidate: (dbm: Partial<DBM>) => Partial<DBM>; | ||
beforeDBMToBM: (dbm: Saved<DBM>) => Partial<BM> | Promise<Partial<BM>>; | ||
beforeDBMToBM: (dbm: DBM) => Partial<BM> | Promise<Partial<BM>>; | ||
beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>; | ||
@@ -81,3 +81,3 @@ beforeBMToTM: (bm: BM) => Partial<TM>; | ||
*/ | ||
anonymize: (dbm: Saved<DBM>) => Saved<DBM>; | ||
anonymize: (dbm: DBM) => DBM; | ||
/** | ||
@@ -109,3 +109,3 @@ * If hook is defined - allows to prevent or modify the error thrown. | ||
} | ||
export interface CommonDaoCfg<BM extends PartialObjectWithId, DBM extends PartialObjectWithId = BM, TM extends AnyObject = BM> { | ||
export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, TM extends AnyObject = BM> { | ||
db: CommonDB; | ||
@@ -236,3 +236,3 @@ table: string; | ||
} | ||
export interface CommonDaoSaveOptions<BM extends PartialObjectWithId, DBM extends PartialObjectWithId> extends CommonDaoSaveBatchOptions<DBM> { | ||
export interface CommonDaoSaveOptions<BM extends BaseDBEntity, DBM extends BaseDBEntity> extends CommonDaoSaveBatchOptions<DBM> { | ||
/** | ||
@@ -251,3 +251,3 @@ * If provided - a check will be made. | ||
*/ | ||
export interface CommonDaoSaveBatchOptions<DBM extends PartialObjectWithId> extends CommonDaoOptions, CommonDBSaveOptions<DBM> { | ||
export interface CommonDaoSaveBatchOptions<DBM extends BaseDBEntity> extends CommonDaoOptions, CommonDBSaveOptions<DBM> { | ||
/** | ||
@@ -264,5 +264,5 @@ * @default false | ||
} | ||
export interface CommonDaoStreamDeleteOptions<DBM extends PartialObjectWithId> extends CommonDaoStreamOptions<DBM> { | ||
export interface CommonDaoStreamDeleteOptions<DBM extends BaseDBEntity> extends CommonDaoStreamOptions<DBM> { | ||
} | ||
export interface CommonDaoStreamSaveOptions<DBM extends PartialObjectWithId> extends CommonDaoSaveBatchOptions<DBM>, CommonDaoStreamOptions<DBM> { | ||
export interface CommonDaoStreamSaveOptions<DBM extends BaseDBEntity> extends CommonDaoSaveBatchOptions<DBM>, CommonDaoStreamOptions<DBM> { | ||
} | ||
@@ -269,0 +269,0 @@ export interface CommonDaoStreamForEachOptions<IN> extends CommonDaoStreamOptions<IN>, TransformMapOptions<IN, any> { |
@@ -1,2 +0,2 @@ | ||
import type { PartialObjectWithId } from '@naturalcycles/js-lib'; | ||
import { ObjectWithId } from '@naturalcycles/js-lib'; | ||
import { CommonDB } from './common.db'; | ||
@@ -51,3 +51,3 @@ /** | ||
*/ | ||
export interface CommonDBSaveOptions<ROW extends PartialObjectWithId = any> extends CommonDBOptions { | ||
export interface CommonDBSaveOptions<ROW extends ObjectWithId> extends CommonDBOptions { | ||
excludeFromIndexes?: (keyof ROW)[]; | ||
@@ -79,4 +79,4 @@ /** | ||
} | ||
export type DBOperation = DBSaveBatchOperation | DBDeleteByIdsOperation; | ||
export interface DBSaveBatchOperation<ROW extends PartialObjectWithId = any> { | ||
export type DBOperation = DBSaveBatchOperation<any> | DBDeleteByIdsOperation; | ||
export interface DBSaveBatchOperation<ROW extends ObjectWithId> { | ||
type: 'saveBatch'; | ||
@@ -114,2 +114,2 @@ table: string; | ||
} | ||
export type DBPatch<ROW extends PartialObjectWithId> = Partial<Record<keyof ROW, ROW[keyof ROW] | DBIncrement>>; | ||
export type DBPatch<ROW extends ObjectWithId> = Partial<Record<keyof ROW, ROW[keyof ROW] | DBIncrement>>; |
@@ -49,3 +49,3 @@ /// <reference types="node" /> | ||
*/ | ||
queryPerTable?: StringMap<DBQuery>; | ||
queryPerTable?: StringMap<DBQuery<any>>; | ||
/** | ||
@@ -52,0 +52,0 @@ * Directory path to store dumped files. Will create `${tableName}.ndjson` (or .ndjson.gz if gzip=true) files. |
@@ -58,3 +58,3 @@ import { AsyncMapper, ErrorMode } from '@naturalcycles/js-lib'; | ||
transformMapOptions?: TransformMapOptions; | ||
saveOptionsPerTable?: Record<string, CommonDBSaveOptions>; | ||
saveOptionsPerTable?: Record<string, CommonDBSaveOptions<any>>; | ||
} | ||
@@ -61,0 +61,0 @@ /** |
@@ -71,3 +71,3 @@ import { AsyncMapper, ErrorMode } from '@naturalcycles/js-lib'; | ||
transformMapOptions?: TransformMapOptions; | ||
saveOptionsPerTable?: Record<string, CommonDBSaveOptions>; | ||
saveOptionsPerTable?: Record<string, CommonDBSaveOptions<any>>; | ||
} | ||
@@ -74,0 +74,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
import { AsyncMapper, Saved, AnyObject, PartialObjectWithId, AnyPartialObjectWithId } from '@naturalcycles/js-lib'; | ||
import { AsyncMapper, Saved, AnyObject, BaseDBEntity, ObjectWithId } from '@naturalcycles/js-lib'; | ||
import { ReadableTyped } from '@naturalcycles/nodejs-lib'; | ||
@@ -27,3 +27,3 @@ import { CommonDaoOptions, CommonDaoStreamDeleteOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, DBPatch } from '..'; | ||
export declare const dbQueryFilterOperatorValues: DBQueryFilterOperator[]; | ||
export interface DBQueryFilter<ROW extends PartialObjectWithId = AnyPartialObjectWithId> { | ||
export interface DBQueryFilter<ROW extends ObjectWithId> { | ||
name: keyof ROW; | ||
@@ -33,3 +33,3 @@ op: DBQueryFilterOperator; | ||
} | ||
export interface DBQueryOrder<ROW extends PartialObjectWithId = AnyPartialObjectWithId> { | ||
export interface DBQueryOrder<ROW extends ObjectWithId> { | ||
name: keyof ROW; | ||
@@ -48,3 +48,3 @@ descending?: boolean; | ||
*/ | ||
export declare class DBQuery<ROW extends PartialObjectWithId = AnyPartialObjectWithId> { | ||
export declare class DBQuery<ROW extends ObjectWithId> { | ||
table: string; | ||
@@ -55,4 +55,4 @@ constructor(table: string); | ||
*/ | ||
static create<ROW extends PartialObjectWithId = AnyPartialObjectWithId>(table: string): DBQuery<ROW>; | ||
static fromPlainObject<ROW extends PartialObjectWithId = AnyPartialObjectWithId>(obj: Partial<DBQuery<ROW>> & { | ||
static create<ROW extends ObjectWithId>(table: string): DBQuery<ROW>; | ||
static fromPlainObject<ROW extends ObjectWithId>(obj: Partial<DBQuery<ROW>> & { | ||
table: string; | ||
@@ -91,3 +91,3 @@ }): DBQuery<ROW>; | ||
*/ | ||
export declare class RunnableDBQuery<BM extends PartialObjectWithId, DBM extends PartialObjectWithId = BM, TM extends AnyObject = BM> extends DBQuery<DBM> { | ||
export declare class RunnableDBQuery<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, TM extends AnyObject = BM> extends DBQuery<DBM> { | ||
dao: CommonDao<BM, DBM, TM>; | ||
@@ -94,0 +94,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
import { PartialObjectWithId } from '@naturalcycles/js-lib'; | ||
import { ObjectWithId } from '@naturalcycles/js-lib'; | ||
import type { CommonDB } from '../common.db'; | ||
@@ -25,5 +25,5 @@ import { CommonDBOptions, CommonDBSaveOptions, DBTransaction } from '../db.model'; | ||
rollback(): Promise<void>; | ||
getByIds<ROW extends PartialObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>; | ||
saveBatch<ROW extends PartialObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): 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>; | ||
deleteByIds(table: string, ids: string[], opt?: CommonDBOptions | undefined): Promise<number>; | ||
} |
@@ -6,4 +6,4 @@ import { CommonDBOptions, CommonDBSaveOptions } from '../db.model'; | ||
export declare const dbQueryFilterOperatorSchema: import("@naturalcycles/nodejs-lib/dist/validation/joi/string.extensions").StringSchema<DBQueryFilterOperator>; | ||
export declare const dbQueryFilterSchema: import("joi").ObjectSchema<DBQueryFilter<import("@naturalcycles/js-lib").AnyPartialObjectWithId>>; | ||
export declare const dbQueryOrderSchema: import("joi").ObjectSchema<DBQueryOrder<import("@naturalcycles/js-lib").AnyPartialObjectWithId>>; | ||
export declare const dbQueryFilterSchema: import("joi").ObjectSchema<DBQueryFilter<any>>; | ||
export declare const dbQueryOrderSchema: import("joi").ObjectSchema<DBQueryOrder<any>>; | ||
export declare const dbQuerySchema: import("joi").ObjectSchema<DBQuery<any>>; |
@@ -43,3 +43,3 @@ { | ||
}, | ||
"version": "9.4.0", | ||
"version": "9.4.1", | ||
"description": "Lowest Common Denominator API to supported Databases", | ||
@@ -46,0 +46,0 @@ "keywords": [ |
@@ -1,2 +0,2 @@ | ||
import { CommonLogger, PartialObjectWithId } from '@naturalcycles/js-lib' | ||
import { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib' | ||
import { CommonDB } from '../../common.db' | ||
@@ -65,3 +65,3 @@ import { | ||
export interface CacheDBSaveOptions<ROW extends PartialObjectWithId> | ||
export interface CacheDBSaveOptions<ROW extends ObjectWithId> | ||
extends CacheDBOptions, | ||
@@ -68,0 +68,0 @@ CommonDBSaveOptions<ROW> {} |
@@ -6,4 +6,3 @@ import { Readable } from 'node:stream' | ||
JsonSchemaRootObject, | ||
PartialObjectWithId, | ||
Saved, | ||
ObjectWithId, | ||
StringMap, | ||
@@ -63,3 +62,3 @@ } from '@naturalcycles/js-lib' | ||
override async getTableSchema<ROW extends PartialObjectWithId>( | ||
override async getTableSchema<ROW extends ObjectWithId>( | ||
table: string, | ||
@@ -70,3 +69,3 @@ ): Promise<JsonSchemaRootObject<ROW>> { | ||
override async createTable<ROW extends PartialObjectWithId>( | ||
override async createTable<ROW extends ObjectWithId>( | ||
table: string, | ||
@@ -85,8 +84,8 @@ schema: JsonSchemaObject<ROW>, | ||
override async getByIds<ROW extends PartialObjectWithId>( | ||
override async getByIds<ROW extends ObjectWithId>( | ||
table: string, | ||
ids: string[], | ||
opt: CacheDBSaveOptions<ROW> = {}, | ||
): Promise<Saved<ROW>[]> { | ||
const resultMap: StringMap<Saved<ROW>> = {} | ||
): Promise<ROW[]> { | ||
const resultMap: StringMap<ROW> = {} | ||
const missingIds: string[] = [] | ||
@@ -132,3 +131,3 @@ | ||
override async saveBatch<ROW extends PartialObjectWithId>( | ||
override async saveBatch<ROW extends ObjectWithId>( | ||
table: string, | ||
@@ -162,6 +161,6 @@ rows: ROW[], | ||
override async runQuery<ROW extends PartialObjectWithId>( | ||
override async runQuery<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
opt: CacheDBSaveOptions<ROW> = {}, | ||
): Promise<RunQueryResult<Saved<ROW>>> { | ||
): Promise<RunQueryResult<ROW>> { | ||
if (!opt.onlyCache && !this.cfg.onlyCache) { | ||
@@ -193,3 +192,3 @@ const { rows, ...queryResult } = await this.cfg.downstreamDB.runQuery(q, opt) | ||
override async runQueryCount<ROW extends PartialObjectWithId>( | ||
override async runQueryCount<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
@@ -211,6 +210,6 @@ opt: CacheDBOptions = {}, | ||
override streamQuery<ROW extends PartialObjectWithId>( | ||
override streamQuery<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
opt: CacheDBStreamOptions = {}, | ||
): ReadableTyped<Saved<ROW>> { | ||
): ReadableTyped<ROW> { | ||
if (!opt.onlyCache && !this.cfg.onlyCache) { | ||
@@ -251,3 +250,3 @@ const stream = this.cfg.downstreamDB.streamQuery<ROW>(q, opt) | ||
override async deleteByQuery<ROW extends PartialObjectWithId>( | ||
override async deleteByQuery<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
@@ -284,3 +283,3 @@ opt: CacheDBOptions = {}, | ||
override async updateByQuery<ROW extends PartialObjectWithId>( | ||
override async updateByQuery<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
@@ -287,0 +286,0 @@ patch: DBPatch<ROW>, |
@@ -1,2 +0,2 @@ | ||
import { CommonLogger, PartialObjectWithId, Saved } from '@naturalcycles/js-lib' | ||
import { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib' | ||
import { DBSaveBatchOperation } from '../../db.model' | ||
@@ -8,3 +8,3 @@ import type { DBQueryOrder } from '../../query/dbQuery' | ||
getTables: () => Promise<string[]> | ||
loadFile: <ROW extends PartialObjectWithId>(table: string) => Promise<Saved<ROW>[]> | ||
loadFile: <ROW extends ObjectWithId>(table: string) => Promise<ROW[]> | ||
saveFiles: (ops: DBSaveBatchOperation<any>[]) => Promise<void> | ||
@@ -19,3 +19,3 @@ } | ||
*/ | ||
sortOnSave?: DBQueryOrder | ||
sortOnSave?: DBQueryOrder<any> | ||
@@ -22,0 +22,0 @@ /** |
@@ -12,4 +12,3 @@ import { | ||
_assert, | ||
Saved, | ||
PartialObjectWithId, | ||
ObjectWithId, | ||
} from '@naturalcycles/js-lib' | ||
@@ -78,7 +77,7 @@ import { readableCreate, ReadableTyped, dimGrey } from '@naturalcycles/nodejs-lib' | ||
override async getByIds<ROW extends PartialObjectWithId>( | ||
override async getByIds<ROW extends ObjectWithId>( | ||
table: string, | ||
ids: string[], | ||
_opt?: CommonDBOptions, | ||
): Promise<Saved<ROW>[]> { | ||
): Promise<ROW[]> { | ||
const byId = _by(await this.loadFile<ROW>(table), r => r.id) | ||
@@ -88,3 +87,3 @@ return ids.map(id => byId[id]!).filter(Boolean) | ||
override async saveBatch<ROW extends PartialObjectWithId>( | ||
override async saveBatch<ROW extends ObjectWithId>( | ||
table: string, | ||
@@ -97,3 +96,3 @@ rows: ROW[], | ||
// 1. Load the whole file | ||
const byId = _by(await this.loadFile<Saved<ROW>>(table), r => r.id) | ||
const byId = _by(await this.loadFile<ROW>(table), r => r.id) | ||
@@ -118,6 +117,6 @@ // 2. Merge with new data (using ids) | ||
override async runQuery<ROW extends PartialObjectWithId>( | ||
override async runQuery<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
_opt?: CommonDBOptions, | ||
): Promise<RunQueryResult<Saved<ROW>>> { | ||
): Promise<RunQueryResult<ROW>> { | ||
return { | ||
@@ -128,3 +127,3 @@ rows: queryInMemory(q, await this.loadFile<ROW>(q.table)), | ||
override async runQueryCount<ROW extends PartialObjectWithId>( | ||
override async runQueryCount<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
@@ -136,3 +135,3 @@ _opt?: CommonDBOptions, | ||
override streamQuery<ROW extends PartialObjectWithId>( | ||
override streamQuery<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
@@ -151,3 +150,3 @@ opt?: CommonDBStreamOptions, | ||
override async deleteByQuery<ROW extends PartialObjectWithId>( | ||
override async deleteByQuery<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
@@ -192,3 +191,3 @@ _opt?: CommonDBOptions, | ||
override async getTableSchema<ROW extends PartialObjectWithId>( | ||
override async getTableSchema<ROW extends ObjectWithId>( | ||
table: string, | ||
@@ -204,3 +203,3 @@ ): Promise<JsonSchemaRootObject<ROW>> { | ||
// wrapper, to handle logging | ||
async loadFile<ROW extends PartialObjectWithId>(table: string): Promise<Saved<ROW>[]> { | ||
async loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]> { | ||
const started = this.logStarted(`loadFile(${table})`) | ||
@@ -213,3 +212,3 @@ const rows = await this.cfg.plugin.loadFile<ROW>(table) | ||
// wrapper, to handle logging, sorting rows before saving | ||
async saveFile<ROW extends PartialObjectWithId>(table: string, _rows: ROW[]): Promise<void> { | ||
async saveFile<ROW extends ObjectWithId>(table: string, _rows: ROW[]): Promise<void> { | ||
// if (!_rows.length) return // NO, it should be able to save file with 0 rows! | ||
@@ -226,5 +225,3 @@ | ||
async saveFiles<ROW extends PartialObjectWithId>( | ||
ops: DBSaveBatchOperation<ROW>[], | ||
): Promise<void> { | ||
async saveFiles<ROW extends ObjectWithId>(ops: DBSaveBatchOperation<ROW>[]): Promise<void> { | ||
if (!ops.length) return | ||
@@ -242,3 +239,3 @@ const op = | ||
sortRows<ROW extends PartialObjectWithId>(rows: ROW[]): ROW[] { | ||
sortRows<ROW extends ObjectWithId>(rows: ROW[]): ROW[] { | ||
rows = rows.map(r => _filterUndefinedValues(r)) | ||
@@ -245,0 +242,0 @@ |
@@ -21,3 +21,3 @@ import { StringMap, _by, ObjectWithId } from '@naturalcycles/js-lib' | ||
async saveFiles(ops: DBSaveBatchOperation[]): Promise<void> { | ||
async saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void> { | ||
ops.forEach(op => { | ||
@@ -24,0 +24,0 @@ this.data[op.table] = _by(op.rows, r => r.id) |
@@ -5,3 +5,3 @@ import fs from 'node:fs' | ||
import { createGzip, createUnzip } from 'node:zlib' | ||
import { pMap, PartialObjectWithId, Saved } from '@naturalcycles/js-lib' | ||
import { ObjectWithId, pMap } from '@naturalcycles/js-lib' | ||
import { | ||
@@ -52,3 +52,3 @@ transformJsonParse, | ||
async loadFile<ROW extends PartialObjectWithId>(table: string): Promise<Saved<ROW>[]> { | ||
async loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]> { | ||
await fs2.ensureDirAsync(this.cfg.storagePath) | ||
@@ -62,3 +62,3 @@ const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}` | ||
const rows: Saved<ROW>[] = [] | ||
const rows: ROW[] = [] | ||
@@ -76,7 +76,7 @@ await _pipeline([ | ||
async saveFiles(ops: DBSaveBatchOperation[]): Promise<void> { | ||
async saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void> { | ||
await pMap(ops, async op => await this.saveFile(op.table, op.rows), { concurrency: 16 }) | ||
} | ||
async saveFile<ROW extends PartialObjectWithId>(table: string, rows: ROW[]): Promise<void> { | ||
async saveFile<ROW extends ObjectWithId>(table: string, rows: ROW[]): Promise<void> { | ||
await fs2.ensureDirAsync(this.cfg.storagePath) | ||
@@ -83,0 +83,0 @@ const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}` |
@@ -1,2 +0,2 @@ | ||
import { PartialObjectWithId, Saved } from '@naturalcycles/js-lib' | ||
import { ObjectWithId } from '@naturalcycles/js-lib' | ||
import { DBSaveBatchOperation } from '../../db.model' | ||
@@ -12,7 +12,7 @@ import { FileDBPersistencePlugin } from './file.db.model' | ||
async loadFile<ROW extends PartialObjectWithId>(_table: string): Promise<Saved<ROW>[]> { | ||
async loadFile<ROW extends ObjectWithId>(_table: string): Promise<ROW[]> { | ||
return [] | ||
} | ||
async saveFiles(_ops: DBSaveBatchOperation[]): Promise<void> {} | ||
async saveFiles(_ops: DBSaveBatchOperation<any>[]): Promise<void> {} | ||
} |
@@ -19,4 +19,2 @@ import fs from 'node:fs' | ||
_assert, | ||
PartialObjectWithId, | ||
Saved, | ||
} from '@naturalcycles/js-lib' | ||
@@ -158,3 +156,3 @@ import { | ||
async getTableSchema<ROW extends PartialObjectWithId>( | ||
async getTableSchema<ROW extends ObjectWithId>( | ||
_table: string, | ||
@@ -169,3 +167,3 @@ ): Promise<JsonSchemaRootObject<ROW>> { | ||
async createTable<ROW extends PartialObjectWithId>( | ||
async createTable<ROW extends ObjectWithId>( | ||
_table: string, | ||
@@ -183,13 +181,13 @@ _schema: JsonSchemaObject<ROW>, | ||
async getByIds<ROW extends PartialObjectWithId>( | ||
async getByIds<ROW extends ObjectWithId>( | ||
_table: string, | ||
ids: string[], | ||
_opt?: CommonDBOptions, | ||
): Promise<Saved<ROW>[]> { | ||
): Promise<ROW[]> { | ||
const table = this.cfg.tablesPrefix + _table | ||
this.data[table] ||= {} | ||
return ids.map(id => this.data[table]![id] as Saved<ROW>).filter(Boolean) | ||
return ids.map(id => this.data[table]![id] as ROW).filter(Boolean) | ||
} | ||
async saveBatch<ROW extends PartialObjectWithId>( | ||
async saveBatch<ROW extends ObjectWithId>( | ||
_table: string, | ||
@@ -225,3 +223,3 @@ rows: ROW[], | ||
async deleteByQuery<ROW extends PartialObjectWithId>( | ||
async deleteByQuery<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
@@ -232,3 +230,3 @@ _opt?: CommonDBOptions, | ||
if (!this.data[table]) return 0 | ||
const ids = queryInMemory(q, Object.values(this.data[table]!) as Saved<ROW>[]).map(r => r.id) | ||
const ids = queryInMemory(q, Object.values(this.data[table]!) as ROW[]).map(r => r.id) | ||
return await this.deleteByIds(q.table, ids) | ||
@@ -251,3 +249,3 @@ } | ||
async updateByQuery<ROW extends PartialObjectWithId>( | ||
async updateByQuery<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
@@ -274,11 +272,11 @@ patch: DBPatch<ROW>, | ||
async runQuery<ROW extends PartialObjectWithId>( | ||
async runQuery<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
_opt?: CommonDBOptions, | ||
): Promise<RunQueryResult<Saved<ROW>>> { | ||
): Promise<RunQueryResult<ROW>> { | ||
const table = this.cfg.tablesPrefix + q.table | ||
return { rows: queryInMemory(q, Object.values(this.data[table] || {}) as Saved<ROW>[]) } | ||
return { rows: queryInMemory(q, Object.values(this.data[table] || {}) as ROW[]) } | ||
} | ||
async runQueryCount<ROW extends PartialObjectWithId>( | ||
async runQueryCount<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
@@ -291,6 +289,6 @@ _opt?: CommonDBOptions, | ||
streamQuery<ROW extends PartialObjectWithId>( | ||
streamQuery<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
_opt?: CommonDBOptions, | ||
): ReadableTyped<Saved<ROW>> { | ||
): ReadableTyped<ROW> { | ||
const table = this.cfg.tablesPrefix + q.table | ||
@@ -402,3 +400,3 @@ return Readable.from(queryInMemory(q, Object.values(this.data[table] || {}) as ROW[])) | ||
async getByIds<ROW extends PartialObjectWithId>( | ||
async getByIds<ROW extends ObjectWithId>( | ||
table: string, | ||
@@ -418,3 +416,3 @@ ids: string[], | ||
async saveBatch<ROW extends PartialObjectWithId>( | ||
async saveBatch<ROW extends ObjectWithId>( | ||
table: string, | ||
@@ -421,0 +419,0 @@ rows: ROW[], |
@@ -1,2 +0,2 @@ | ||
import { _pick, PartialObjectWithId } from '@naturalcycles/js-lib' | ||
import { _pick, ObjectWithId } from '@naturalcycles/js-lib' | ||
import { DBQuery, DBQueryFilterOperator } from '../../query/dbQuery' | ||
@@ -21,6 +21,3 @@ | ||
// But should be careful here.. | ||
export function queryInMemory<ROW extends PartialObjectWithId>( | ||
q: DBQuery<ROW>, | ||
rows: ROW[] = [], | ||
): ROW[] { | ||
export function queryInMemory<ROW extends ObjectWithId>(q: DBQuery<ROW>, rows: ROW[] = []): ROW[] { | ||
// .filter | ||
@@ -27,0 +24,0 @@ // eslint-disable-next-line unicorn/no-array-reduce |
@@ -1,7 +0,2 @@ | ||
import { | ||
JsonSchemaObject, | ||
JsonSchemaRootObject, | ||
PartialObjectWithId, | ||
Saved, | ||
} from '@naturalcycles/js-lib' | ||
import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib' | ||
import { ReadableTyped } from '@naturalcycles/nodejs-lib' | ||
@@ -39,3 +34,3 @@ import { CommonDB, CommonDBSupport, CommonDBType } from './common.db' | ||
async getTableSchema<ROW extends PartialObjectWithId>( | ||
async getTableSchema<ROW extends ObjectWithId>( | ||
table: string, | ||
@@ -46,3 +41,3 @@ ): Promise<JsonSchemaRootObject<ROW>> { | ||
async createTable<ROW extends PartialObjectWithId>( | ||
async createTable<ROW extends ObjectWithId>( | ||
table: string, | ||
@@ -54,14 +49,11 @@ schema: JsonSchemaObject<ROW>, | ||
async getByIds<ROW extends PartialObjectWithId>( | ||
table: string, | ||
ids: string[], | ||
): Promise<Saved<ROW>[]> { | ||
async getByIds<ROW extends ObjectWithId>(table: string, ids: string[]): Promise<ROW[]> { | ||
throw new Error('getByIds is not implemented') | ||
} | ||
async deleteByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>): Promise<number> { | ||
async deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<number> { | ||
throw new Error('deleteByQuery is not implemented') | ||
} | ||
async updateByQuery<ROW extends PartialObjectWithId>( | ||
async updateByQuery<ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
@@ -74,13 +66,11 @@ patch: DBPatch<ROW>, | ||
async runQuery<ROW extends PartialObjectWithId>( | ||
q: DBQuery<ROW>, | ||
): Promise<RunQueryResult<Saved<ROW>>> { | ||
async runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<RunQueryResult<ROW>> { | ||
throw new Error('runQuery is not implemented') | ||
} | ||
async runQueryCount<ROW extends PartialObjectWithId>(q: DBQuery<ROW>): Promise<number> { | ||
async runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<number> { | ||
throw new Error('runQueryCount is not implemented') | ||
} | ||
async saveBatch<ROW extends PartialObjectWithId>( | ||
async saveBatch<ROW extends ObjectWithId>( | ||
table: string, | ||
@@ -93,3 +83,3 @@ rows: ROW[], | ||
streamQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>): ReadableTyped<Saved<ROW>> { | ||
streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): ReadableTyped<ROW> { | ||
throw new Error('streamQuery is not implemented') | ||
@@ -96,0 +86,0 @@ } |
@@ -1,7 +0,2 @@ | ||
import { | ||
JsonSchemaObject, | ||
JsonSchemaRootObject, | ||
PartialObjectWithId, | ||
Saved, | ||
} from '@naturalcycles/js-lib' | ||
import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib' | ||
import type { ReadableTyped } from '@naturalcycles/nodejs-lib' | ||
@@ -72,5 +67,3 @@ import { | ||
*/ | ||
getTableSchema: <ROW extends PartialObjectWithId>( | ||
table: string, | ||
) => Promise<JsonSchemaRootObject<ROW>> | ||
getTableSchema: <ROW extends ObjectWithId>(table: string) => Promise<JsonSchemaRootObject<ROW>> | ||
@@ -81,3 +74,3 @@ /** | ||
*/ | ||
createTable: <ROW extends PartialObjectWithId>( | ||
createTable: <ROW extends ObjectWithId>( | ||
table: string, | ||
@@ -93,7 +86,7 @@ schema: JsonSchemaObject<ROW>, | ||
*/ | ||
getByIds: <ROW extends PartialObjectWithId>( | ||
getByIds: <ROW extends ObjectWithId>( | ||
table: string, | ||
ids: string[], | ||
opt?: CommonDBOptions, | ||
) => Promise<Saved<ROW>[]> | ||
) => Promise<ROW[]> | ||
@@ -104,8 +97,8 @@ // QUERY | ||
*/ | ||
runQuery: <ROW extends PartialObjectWithId>( | ||
runQuery: <ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
opt?: CommonDBOptions, | ||
) => Promise<RunQueryResult<Saved<ROW>>> | ||
) => Promise<RunQueryResult<ROW>> | ||
runQueryCount: <ROW extends PartialObjectWithId>( | ||
runQueryCount: <ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
@@ -115,6 +108,6 @@ opt?: CommonDBOptions, | ||
streamQuery: <ROW extends PartialObjectWithId>( | ||
streamQuery: <ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
opt?: CommonDBStreamOptions, | ||
) => ReadableTyped<Saved<ROW>> | ||
) => ReadableTyped<ROW> | ||
@@ -125,3 +118,3 @@ // SAVE | ||
*/ | ||
saveBatch: <ROW extends PartialObjectWithId>( | ||
saveBatch: <ROW extends ObjectWithId>( | ||
table: string, | ||
@@ -143,3 +136,3 @@ rows: ROW[], | ||
*/ | ||
deleteByQuery: <ROW extends PartialObjectWithId>( | ||
deleteByQuery: <ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
@@ -167,3 +160,3 @@ opt?: CommonDBOptions, | ||
*/ | ||
updateByQuery: <ROW extends PartialObjectWithId>( | ||
updateByQuery: <ROW extends ObjectWithId>( | ||
q: DBQuery<ROW>, | ||
@@ -170,0 +163,0 @@ patch: DBPatch<ROW>, |
import { | ||
AnyObject, | ||
BaseDBEntity, | ||
CommonLogger, | ||
ErrorMode, | ||
PartialObjectWithId, | ||
Promisable, | ||
@@ -22,7 +22,3 @@ Saved, | ||
export interface CommonDaoHooks< | ||
BM extends PartialObjectWithId, | ||
DBM extends PartialObjectWithId, | ||
TM, | ||
> { | ||
export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntity, TM> { | ||
/** | ||
@@ -68,3 +64,3 @@ * Allows to override the id generation function. | ||
beforeDBMToBM: (dbm: Saved<DBM>) => Partial<BM> | Promise<Partial<BM>> | ||
beforeDBMToBM: (dbm: DBM) => Partial<BM> | Promise<Partial<BM>> | ||
beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>> | ||
@@ -111,3 +107,3 @@ beforeBMToTM: (bm: BM) => Partial<TM> | ||
*/ | ||
anonymize: (dbm: Saved<DBM>) => Saved<DBM> | ||
anonymize: (dbm: DBM) => DBM | ||
@@ -143,4 +139,4 @@ /** | ||
export interface CommonDaoCfg< | ||
BM extends PartialObjectWithId, | ||
DBM extends PartialObjectWithId = BM, | ||
BM extends BaseDBEntity, | ||
DBM extends BaseDBEntity = BM, | ||
TM extends AnyObject = BM, | ||
@@ -296,6 +292,4 @@ > { | ||
export interface CommonDaoSaveOptions< | ||
BM extends PartialObjectWithId, | ||
DBM extends PartialObjectWithId, | ||
> extends CommonDaoSaveBatchOptions<DBM> { | ||
export interface CommonDaoSaveOptions<BM extends BaseDBEntity, DBM extends BaseDBEntity> | ||
extends CommonDaoSaveBatchOptions<DBM> { | ||
/** | ||
@@ -315,3 +309,3 @@ * If provided - a check will be made. | ||
*/ | ||
export interface CommonDaoSaveBatchOptions<DBM extends PartialObjectWithId> | ||
export interface CommonDaoSaveBatchOptions<DBM extends BaseDBEntity> | ||
extends CommonDaoOptions, | ||
@@ -331,6 +325,6 @@ CommonDBSaveOptions<DBM> { | ||
export interface CommonDaoStreamDeleteOptions<DBM extends PartialObjectWithId> | ||
export interface CommonDaoStreamDeleteOptions<DBM extends BaseDBEntity> | ||
extends CommonDaoStreamOptions<DBM> {} | ||
export interface CommonDaoStreamSaveOptions<DBM extends PartialObjectWithId> | ||
export interface CommonDaoStreamSaveOptions<DBM extends BaseDBEntity> | ||
extends CommonDaoSaveBatchOptions<DBM>, | ||
@@ -337,0 +331,0 @@ CommonDaoStreamOptions<DBM> {} |
@@ -12,2 +12,3 @@ import { Transform } from 'node:stream' | ||
_truncate, | ||
_typeCast, | ||
_uniqBy, | ||
@@ -23,3 +24,2 @@ AnyObject, | ||
ObjectWithId, | ||
PartialObjectWithId, | ||
pMap, | ||
@@ -29,2 +29,3 @@ Saved, | ||
UnixTimestampMillisNumber, | ||
UnsavedId, | ||
ZodSchema, | ||
@@ -83,4 +84,4 @@ ZodValidationError, | ||
export class CommonDao< | ||
BM extends PartialObjectWithId, | ||
DBM extends PartialObjectWithId = BM, | ||
BM extends BaseDBEntity, | ||
DBM extends BaseDBEntity = BM, | ||
TM extends AnyObject = BM, | ||
@@ -137,3 +138,3 @@ > { | ||
let dbm = (await (opt.tx || this.cfg.db).getByIds<DBM>(table, [id]))[0] | ||
let dbm = (await (opt.tx || this.cfg.db).getByIds<Saved<DBM>>(table, [id]))[0] | ||
if (dbm && !opt.raw && this.cfg.hooks!.afterLoad) { | ||
@@ -178,3 +179,3 @@ dbm = (await this.cfg.hooks!.afterLoad(dbm)) || undefined | ||
const started = this.logStarted(op, table) | ||
let [dbm] = await (opt.tx || this.cfg.db).getByIds<DBM>(table, [id]) | ||
let [dbm] = await (opt.tx || this.cfg.db).getByIds<Saved<DBM>>(table, [id]) | ||
if (dbm && !opt.raw && this.cfg.hooks!.afterLoad) { | ||
@@ -198,3 +199,3 @@ dbm = (await this.cfg.hooks!.afterLoad(dbm)) || undefined | ||
const started = this.logStarted(op, table) | ||
let [dbm] = await (opt.tx || this.cfg.db).getByIds<DBM>(table, [id]) | ||
let [dbm] = await (opt.tx || this.cfg.db).getByIds<Saved<DBM>>(table, [id]) | ||
if (dbm && !opt.raw && this.cfg.hooks!.afterLoad) { | ||
@@ -219,3 +220,3 @@ dbm = (await this.cfg.hooks!.afterLoad(dbm)) || undefined | ||
const started = this.logStarted(op, table) | ||
let dbms = await (opt.tx || this.cfg.db).getByIds<DBM>(table, ids) | ||
let dbms = await (opt.tx || this.cfg.db).getByIds<Saved<DBM>>(table, ids) | ||
if (!opt.raw && this.cfg.hooks!.afterLoad && dbms.length) { | ||
@@ -237,3 +238,3 @@ dbms = (await pMap(dbms, async dbm => await this.cfg.hooks!.afterLoad!(dbm))).filter( | ||
const started = this.logStarted(op, table) | ||
let dbms = await (opt.tx || this.cfg.db).getByIds<DBM>(table, ids) | ||
let dbms = await (opt.tx || this.cfg.db).getByIds<Saved<DBM>>(table, ids) | ||
if (!opt.raw && this.cfg.hooks!.afterLoad && dbms.length) { | ||
@@ -295,3 +296,3 @@ dbms = (await pMap(dbms, async dbm => await this.cfg.hooks!.afterLoad!(dbm))).filter( | ||
private async ensureUniqueId(table: string, dbm: Saved<DBM>): Promise<void> { | ||
private async ensureUniqueId(table: string, dbm: DBM): Promise<void> { | ||
// todo: retry N times | ||
@@ -364,3 +365,3 @@ const existing = await this.cfg.db.getByIds<DBM>(table, [dbm.id]) | ||
const started = this.logStarted(op, q.table) | ||
let { rows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt) | ||
let { rows, ...queryResult } = await this.cfg.db.runQuery<Saved<DBM>>(q, opt) | ||
const partialQuery = !!q._selectedFieldNames | ||
@@ -393,3 +394,3 @@ if (!opt.raw && this.cfg.hooks!.afterLoad && rows.length) { | ||
const started = this.logStarted(op, q.table) | ||
let { rows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt) | ||
let { rows, ...queryResult } = await this.cfg.db.runQuery<Saved<DBM>>(q, opt) | ||
if (!opt.raw && this.cfg.hooks!.afterLoad && rows.length) { | ||
@@ -419,3 +420,3 @@ rows = (await pMap(rows, async dbm => await this.cfg.hooks!.afterLoad!(dbm))).filter( | ||
const started = this.logStarted(op, q.table) | ||
let { rows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt) | ||
let { rows, ...queryResult } = await this.cfg.db.runQuery<Saved<DBM>>(q, opt) | ||
if (!opt.raw && this.cfg.hooks!.afterLoad && rows.length) { | ||
@@ -694,3 +695,3 @@ rows = (await pMap(rows, async dbm => await this.cfg.hooks!.afterLoad!(dbm))).filter( | ||
*/ | ||
assignIdCreatedUpdated<T extends BaseDBEntity>(obj: T, opt: CommonDaoOptions = {}): Saved<T> { | ||
assignIdCreatedUpdated<T extends BaseDBEntity>(obj: Partial<T>, opt: CommonDaoOptions = {}): T { | ||
const now = Math.floor(Date.now() / 1000) | ||
@@ -710,3 +711,3 @@ | ||
return obj as Saved<T> | ||
return obj as T | ||
} | ||
@@ -718,3 +719,3 @@ | ||
*/ | ||
async save(bm: BM, opt: CommonDaoSaveOptions<BM, DBM> = {}): Promise<Saved<BM>> { | ||
async save(bm: UnsavedId<BM>, opt: CommonDaoSaveOptions<BM, DBM> = {}): Promise<Saved<BM>> { | ||
this.requireWriteAccess() | ||
@@ -729,2 +730,3 @@ | ||
this.assignIdCreatedUpdated(bm, opt) // mutates | ||
_typeCast<Saved<BM>>(bm) | ||
let dbm = await this.bmToDBM(bm, opt) | ||
@@ -734,3 +736,3 @@ | ||
dbm = (await this.cfg.hooks!.beforeSave(dbm))! | ||
if (dbm === null) return bm as any | ||
if (dbm === null) return bm | ||
} | ||
@@ -759,3 +761,3 @@ | ||
this.logSaveResult(started, op, table) | ||
return bm as Saved<BM> | ||
return bm | ||
} | ||
@@ -772,7 +774,7 @@ | ||
async savePatch( | ||
bm: Saved<BM>, | ||
bm: BM, | ||
patch: Partial<BM>, | ||
opt: CommonDaoSaveBatchOptions<DBM>, | ||
): Promise<Saved<BM>> { | ||
const patched: Saved<BM> = { | ||
const patched: BM = { | ||
...bm, | ||
@@ -784,3 +786,3 @@ ...patch, | ||
// Skipping the save operation, as data is the same | ||
return bm | ||
return bm as Saved<BM> | ||
} | ||
@@ -852,3 +854,3 @@ | ||
async patch( | ||
bm: Saved<BM>, | ||
bm: BM, | ||
patch: Partial<BM>, | ||
@@ -871,3 +873,3 @@ opt: CommonDaoSaveBatchOptions<DBM> = {}, | ||
// Skipping the save operation, as data is the same | ||
return bm | ||
return bm as Saved<BM> | ||
} | ||
@@ -888,3 +890,3 @@ | ||
async patchInTransaction( | ||
bm: Saved<BM>, | ||
bm: BM, | ||
patch: Partial<BM>, | ||
@@ -898,3 +900,6 @@ opt?: CommonDaoSaveBatchOptions<DBM>, | ||
async saveAsDBM(dbm: DBM, opt: CommonDaoSaveBatchOptions<DBM> = {}): Promise<Saved<DBM>> { | ||
async saveAsDBM( | ||
dbm: UnsavedId<DBM>, | ||
opt: CommonDaoSaveBatchOptions<DBM> = {}, | ||
): Promise<Saved<DBM>> { | ||
this.requireWriteAccess() | ||
@@ -939,3 +944,6 @@ const table = opt.table || this.cfg.table | ||
async saveBatch(bms: BM[], opt: CommonDaoSaveBatchOptions<DBM> = {}): Promise<Saved<BM>[]> { | ||
async saveBatch( | ||
bms: UnsavedId<BM>[], | ||
opt: CommonDaoSaveBatchOptions<DBM> = {}, | ||
): Promise<Saved<BM>[]> { | ||
if (!bms.length) return [] | ||
@@ -945,3 +953,3 @@ this.requireWriteAccess() | ||
bms.forEach(bm => this.assignIdCreatedUpdated(bm, opt)) | ||
let dbms = await this.bmsToDBM(bms, opt) | ||
let dbms = await this.bmsToDBM(bms as BM[], opt) | ||
@@ -986,3 +994,3 @@ if (this.cfg.hooks!.beforeSave && dbms.length) { | ||
async saveBatchAsDBM( | ||
dbms: DBM[], | ||
dbms: UnsavedId<DBM>[], | ||
opt: CommonDaoSaveBatchOptions<DBM> = {}, | ||
@@ -1214,4 +1222,4 @@ ): Promise<Saved<DBM>[]> { | ||
async dbmToBM(_dbm: undefined, opt?: CommonDaoOptions): Promise<undefined> | ||
async dbmToBM(_dbm?: Saved<DBM>, opt?: CommonDaoOptions): Promise<Saved<BM>> | ||
async dbmToBM(_dbm?: Saved<DBM>, opt: CommonDaoOptions = {}): Promise<Saved<BM> | undefined> { | ||
async dbmToBM(_dbm?: DBM, opt?: CommonDaoOptions): Promise<Saved<BM>> | ||
async dbmToBM(_dbm?: DBM, opt: CommonDaoOptions = {}): Promise<Saved<BM> | undefined> { | ||
if (!_dbm) return | ||
@@ -1221,3 +1229,3 @@ | ||
// const dbm = this.anyToDBM(_dbm, opt) | ||
let dbm: Saved<DBM> = { ..._dbm, ...this.cfg.hooks!.parseNaturalId!(_dbm.id) } | ||
let dbm: DBM = { ..._dbm, ...this.cfg.hooks!.parseNaturalId!(_dbm.id) } | ||
@@ -1236,3 +1244,3 @@ if (opt.anonymize) { | ||
async dbmsToBM(dbms: Saved<DBM>[], opt: CommonDaoOptions = {}): Promise<Saved<BM>[]> { | ||
async dbmsToBM(dbms: DBM[], opt: CommonDaoOptions = {}): Promise<Saved<BM>[]> { | ||
return await pMap(dbms, async dbm => await this.dbmToBM(dbm, opt)) | ||
@@ -1273,3 +1281,3 @@ } | ||
anyToDBM(dbm?: any, opt?: CommonDaoOptions): Saved<DBM> | ||
anyToDBM(dbm?: Saved<DBM>, opt: CommonDaoOptions = {}): Saved<DBM> | undefined { | ||
anyToDBM(dbm?: DBM, opt: CommonDaoOptions = {}): Saved<DBM> | undefined { | ||
if (!dbm) return | ||
@@ -1290,3 +1298,3 @@ | ||
anyToDBMs(entities: Saved<DBM>[], opt: CommonDaoOptions = {}): Saved<DBM>[] { | ||
anyToDBMs(entities: DBM[], opt: CommonDaoOptions = {}): Saved<DBM>[] { | ||
return entities.map(entity => this.anyToDBM(entity, opt)) | ||
@@ -1296,4 +1304,4 @@ } | ||
bmToTM(bm: undefined, opt?: CommonDaoOptions): TM | undefined | ||
bmToTM(bm?: Saved<BM>, opt?: CommonDaoOptions): TM | ||
bmToTM(bm?: Saved<BM>, opt?: CommonDaoOptions): TM | undefined { | ||
bmToTM(bm?: BM, opt?: CommonDaoOptions): TM | ||
bmToTM(bm?: BM, opt?: CommonDaoOptions): TM | undefined { | ||
if (bm === undefined) return | ||
@@ -1313,3 +1321,3 @@ | ||
bmsToTM(bms: Saved<BM>[], opt: CommonDaoOptions = {}): TM[] { | ||
bmsToTM(bms: BM[], opt: CommonDaoOptions = {}): TM[] { | ||
// try/catch? | ||
@@ -1517,3 +1525,3 @@ return bms.map(bm => this.bmToTM(bm, opt)) | ||
async getById<BM extends PartialObjectWithId, DBM extends PartialObjectWithId>( | ||
async getById<BM extends BaseDBEntity, DBM extends BaseDBEntity>( | ||
dao: CommonDao<BM, DBM, any>, | ||
@@ -1526,3 +1534,3 @@ id?: string | null, | ||
async getByIds<BM extends PartialObjectWithId, DBM extends PartialObjectWithId>( | ||
async getByIds<BM extends BaseDBEntity, DBM extends BaseDBEntity>( | ||
dao: CommonDao<BM, DBM, any>, | ||
@@ -1549,5 +1557,5 @@ ids: string[], | ||
async save<BM extends PartialObjectWithId, DBM extends PartialObjectWithId>( | ||
async save<BM extends BaseDBEntity, DBM extends BaseDBEntity>( | ||
dao: CommonDao<BM, DBM, any>, | ||
bm: BM, | ||
bm: UnsavedId<BM>, | ||
opt?: CommonDaoSaveBatchOptions<DBM>, | ||
@@ -1558,5 +1566,5 @@ ): Promise<Saved<BM>> { | ||
async saveBatch<BM extends PartialObjectWithId, DBM extends PartialObjectWithId>( | ||
async saveBatch<BM extends BaseDBEntity, DBM extends BaseDBEntity>( | ||
dao: CommonDao<BM, DBM, any>, | ||
bms: BM[], | ||
bms: UnsavedId<BM>[], | ||
opt?: CommonDaoSaveBatchOptions<DBM>, | ||
@@ -1563,0 +1571,0 @@ ): Promise<Saved<BM>[]> { |
@@ -1,2 +0,2 @@ | ||
import type { PartialObjectWithId } from '@naturalcycles/js-lib' | ||
import { ObjectWithId } from '@naturalcycles/js-lib' | ||
import { CommonDB } from './common.db' | ||
@@ -58,4 +58,3 @@ | ||
*/ | ||
export interface CommonDBSaveOptions<ROW extends PartialObjectWithId = any> | ||
extends CommonDBOptions { | ||
export interface CommonDBSaveOptions<ROW extends ObjectWithId> extends CommonDBOptions { | ||
excludeFromIndexes?: (keyof ROW)[] | ||
@@ -93,5 +92,5 @@ | ||
export type DBOperation = DBSaveBatchOperation | DBDeleteByIdsOperation | ||
export type DBOperation = DBSaveBatchOperation<any> | DBDeleteByIdsOperation | ||
export interface DBSaveBatchOperation<ROW extends PartialObjectWithId = any> { | ||
export interface DBSaveBatchOperation<ROW extends ObjectWithId> { | ||
type: 'saveBatch' | ||
@@ -136,4 +135,4 @@ table: string | ||
export type DBPatch<ROW extends PartialObjectWithId> = Partial< | ||
export type DBPatch<ROW extends ObjectWithId> = Partial< | ||
Record<keyof ROW, ROW[keyof ROW] | DBIncrement> | ||
> |
@@ -81,3 +81,3 @@ import fs from 'node:fs' | ||
*/ | ||
queryPerTable?: StringMap<DBQuery> | ||
queryPerTable?: StringMap<DBQuery<any>> | ||
@@ -84,0 +84,0 @@ /** |
@@ -1,3 +0,10 @@ | ||
import { AsyncMapper, ErrorMode, pMap, _passthroughMapper, localTime } from '@naturalcycles/js-lib' | ||
import { | ||
AsyncMapper, | ||
ErrorMode, | ||
pMap, | ||
_passthroughMapper, | ||
localTime, | ||
BaseDBEntity, | ||
} from '@naturalcycles/js-lib' | ||
import { | ||
NDJsonStats, | ||
@@ -83,3 +90,3 @@ transformBuffer, | ||
saveOptionsPerTable?: Record<string, CommonDBSaveOptions> | ||
saveOptionsPerTable?: Record<string, CommonDBSaveOptions<any>> | ||
} | ||
@@ -122,3 +129,3 @@ | ||
async table => { | ||
let q = DBQuery.create(table).limit(limit) | ||
let q = DBQuery.create<BaseDBEntity>(table).limit(limit) | ||
@@ -129,3 +136,3 @@ if (sinceUpdated) { | ||
const saveOptions: CommonDBSaveOptions = saveOptionsPerTable[table] || {} | ||
const saveOptions: CommonDBSaveOptions<any> = saveOptionsPerTable[table] || {} | ||
const mapper = mapperPerTable[table] || _passthroughMapper | ||
@@ -132,0 +139,0 @@ |
@@ -10,5 +10,6 @@ import fs from 'node:fs' | ||
_passthroughMapper, | ||
SavedDBEntity, | ||
localTime, | ||
JsonSchemaObject, | ||
BaseDBEntity, | ||
Saved, | ||
} from '@naturalcycles/js-lib' | ||
@@ -115,3 +116,3 @@ import { | ||
saveOptionsPerTable?: Record<string, CommonDBSaveOptions> | ||
saveOptionsPerTable?: Record<string, CommonDBSaveOptions<any>> | ||
} | ||
@@ -199,3 +200,3 @@ | ||
const filePath = `${inputDirPath}/${table}.ndjson` + (gzip ? '.gz' : '') | ||
const saveOptions: CommonDBSaveOptions = saveOptionsPerTable[table] || {} | ||
const saveOptions: CommonDBSaveOptions<any> = saveOptionsPerTable[table] || {} | ||
@@ -222,3 +223,3 @@ const started = Date.now() | ||
...(sinceUpdated | ||
? [transformFilterSync<SavedDBEntity>(r => r.updated >= sinceUpdated)] | ||
? [transformFilterSync<Saved<BaseDBEntity>>(r => r.updated >= sinceUpdated)] | ||
: []), | ||
@@ -225,0 +226,0 @@ transformMap(mapperPerTable[table] || _passthroughMapper, { |
@@ -7,4 +7,4 @@ import { | ||
_objectAssign, | ||
PartialObjectWithId, | ||
AnyPartialObjectWithId, | ||
BaseDBEntity, | ||
ObjectWithId, | ||
} from '@naturalcycles/js-lib' | ||
@@ -66,3 +66,3 @@ import { ReadableTyped } from '@naturalcycles/nodejs-lib' | ||
export interface DBQueryFilter<ROW extends PartialObjectWithId = AnyPartialObjectWithId> { | ||
export interface DBQueryFilter<ROW extends ObjectWithId> { | ||
name: keyof ROW | ||
@@ -73,3 +73,3 @@ op: DBQueryFilterOperator | ||
export interface DBQueryOrder<ROW extends PartialObjectWithId = AnyPartialObjectWithId> { | ||
export interface DBQueryOrder<ROW extends ObjectWithId> { | ||
name: keyof ROW | ||
@@ -89,3 +89,3 @@ descending?: boolean | ||
*/ | ||
export class DBQuery<ROW extends PartialObjectWithId = AnyPartialObjectWithId> { | ||
export class DBQuery<ROW extends ObjectWithId> { | ||
constructor(public table: string) {} | ||
@@ -96,9 +96,7 @@ | ||
*/ | ||
static create<ROW extends PartialObjectWithId = AnyPartialObjectWithId>( | ||
table: string, | ||
): DBQuery<ROW> { | ||
static create<ROW extends ObjectWithId>(table: string): DBQuery<ROW> { | ||
return new DBQuery(table) | ||
} | ||
static fromPlainObject<ROW extends PartialObjectWithId = AnyPartialObjectWithId>( | ||
static fromPlainObject<ROW extends ObjectWithId>( | ||
obj: Partial<DBQuery<ROW>> & { table: string }, | ||
@@ -247,4 +245,4 @@ ): DBQuery<ROW> { | ||
export class RunnableDBQuery< | ||
BM extends PartialObjectWithId, | ||
DBM extends PartialObjectWithId = BM, | ||
BM extends BaseDBEntity, | ||
DBM extends BaseDBEntity = BM, | ||
TM extends AnyObject = BM, | ||
@@ -251,0 +249,0 @@ > extends DBQuery<DBM> { |
@@ -81,3 +81,3 @@ import { _isTruthy, ObjectWithId } from '@naturalcycles/js-lib' | ||
async query(q: TimeSeriesQuery): Promise<TimeSeriesDataPoint[]> { | ||
const dbq = DBQuery.create(`${q.series}${_TIMESERIES_RAW}`).order('ts') | ||
const dbq = DBQuery.create<any>(`${q.series}${_TIMESERIES_RAW}`).order('ts') | ||
if (q.fromIncl) dbq.filter('ts', '>=', q.fromIncl) | ||
@@ -84,0 +84,0 @@ if (q.toExcl) dbq.filter('ts', '<', q.toExcl) |
@@ -1,2 +0,2 @@ | ||
import { PartialObjectWithId } from '@naturalcycles/js-lib' | ||
import { ObjectWithId } from '@naturalcycles/js-lib' | ||
import type { CommonDB } from '../common.db' | ||
@@ -103,3 +103,3 @@ import { CommonDBOptions, CommonDBSaveOptions, DBTransaction } from '../db.model' | ||
async getByIds<ROW extends PartialObjectWithId>( | ||
async getByIds<ROW extends ObjectWithId>( | ||
table: string, | ||
@@ -117,3 +117,3 @@ ids: string[], | ||
// } | ||
async saveBatch<ROW extends PartialObjectWithId>( | ||
async saveBatch<ROW extends ObjectWithId>( | ||
table: string, | ||
@@ -120,0 +120,0 @@ rows: ROW[], |
@@ -24,3 +24,3 @@ import { | ||
export const commonDBSaveOptionsSchema = objectSchema<CommonDBSaveOptions>({ | ||
export const commonDBSaveOptionsSchema = objectSchema<CommonDBSaveOptions<any>>({ | ||
excludeFromIndexes: arraySchema(stringSchema).optional(), | ||
@@ -33,3 +33,3 @@ }).concat(commonDBOptionsSchema) | ||
export const dbQueryFilterSchema = objectSchema<DBQueryFilter>({ | ||
export const dbQueryFilterSchema = objectSchema<DBQueryFilter<any>>({ | ||
name: stringSchema, | ||
@@ -40,3 +40,3 @@ op: dbQueryFilterOperatorSchema, | ||
export const dbQueryOrderSchema = objectSchema<DBQueryOrder>({ | ||
export const dbQueryOrderSchema = objectSchema<DBQueryOrder<any>>({ | ||
name: stringSchema, | ||
@@ -43,0 +43,0 @@ descending: booleanSchema.optional(), |
303
1
2
1
36
431769
11328