@naturalcycles/db-lib 9.2.0 → 9.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist/adapter/cachedb/cache.db.d.ts +11 -12
  2. package/dist/adapter/cachedb/cache.db.model.d.ts +2 -2
  3. package/dist/adapter/file/file.db.d.ts +12 -12
  4. package/dist/adapter/file/file.db.model.d.ts +2 -2
  5. package/dist/adapter/file/localFile.persistence.plugin.d.ts +3 -3
  6. package/dist/adapter/file/noop.persistence.plugin.d.ts +2 -2
  7. package/dist/adapter/inmemory/inMemory.db.d.ts +12 -12
  8. package/dist/adapter/inmemory/queryInMemory.d.ts +2 -2
  9. package/dist/base.common.db.d.ts +10 -10
  10. package/dist/common.db.d.ts +10 -10
  11. package/dist/commondao/common.dao.d.ts +26 -27
  12. package/dist/commondao/common.dao.js +12 -12
  13. package/dist/commondao/common.dao.model.d.ts +13 -13
  14. package/dist/db.model.d.ts +4 -4
  15. package/dist/query/dbQuery.d.ts +9 -9
  16. package/dist/testing/test.model.d.ts +3 -4
  17. package/dist/testing/test.model.js +1 -1
  18. package/dist/transaction/dbTransaction.util.d.ts +3 -3
  19. package/dist/validation/index.d.ts +2 -2
  20. package/package.json +1 -1
  21. package/src/adapter/cachedb/cache.db.model.ts +2 -2
  22. package/src/adapter/cachedb/cache.db.ts +18 -16
  23. package/src/adapter/file/file.db.model.ts +2 -2
  24. package/src/adapter/file/file.db.ts +17 -15
  25. package/src/adapter/file/localFile.persistence.plugin.ts +4 -4
  26. package/src/adapter/file/noop.persistence.plugin.ts +2 -2
  27. package/src/adapter/inmemory/inMemory.db.ts +20 -18
  28. package/src/adapter/inmemory/queryInMemory.ts +5 -2
  29. package/src/base.common.db.ts +20 -10
  30. package/src/common.db.ts +20 -13
  31. package/src/commondao/common.dao.model.ts +21 -15
  32. package/src/commondao/common.dao.ts +80 -69
  33. package/src/db.model.ts +4 -4
  34. package/src/query/dbQuery.ts +13 -11
  35. package/src/testing/test.model.ts +4 -6
  36. package/src/transaction/dbTransaction.util.ts +3 -3
@@ -1,6 +1,5 @@
1
- /// <reference types="node" />
2
- import { Readable } from 'node:stream';
3
- import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { JsonSchemaObject, JsonSchemaRootObject, PartialObjectWithId, Saved } from '@naturalcycles/js-lib';
2
+ import { ReadableTyped } from '@naturalcycles/nodejs-lib';
4
3
  import { BaseCommonDB } from '../../base.common.db';
5
4
  import { CommonDB, CommonDBSupport } from '../../common.db';
6
5
  import { DBPatch, RunQueryResult } from '../../db.model';
@@ -21,13 +20,13 @@ export declare class CacheDB extends BaseCommonDB implements CommonDB {
21
20
  * Resets InMemory DB data
22
21
  */
23
22
  getTables(): Promise<string[]>;
24
- getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
25
- createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CacheDBCreateOptions): Promise<void>;
26
- getByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], opt?: CacheDBSaveOptions<ROW>): Promise<ROW[]>;
27
- saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CacheDBSaveOptions<ROW>): Promise<void>;
28
- runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBSaveOptions<ROW>): Promise<RunQueryResult<ROW>>;
29
- runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>;
30
- streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBStreamOptions): Readable;
31
- deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>;
32
- updateByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CacheDBOptions): Promise<number>;
23
+ getTableSchema<ROW extends PartialObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
24
+ createTable<ROW extends PartialObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CacheDBCreateOptions): Promise<void>;
25
+ getByIds<ROW extends PartialObjectWithId>(table: string, ids: string[], opt?: CacheDBSaveOptions<ROW>): Promise<Saved<ROW>[]>;
26
+ saveBatch<ROW extends PartialObjectWithId>(table: string, rows: ROW[], opt?: CacheDBSaveOptions<ROW>): Promise<void>;
27
+ runQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBSaveOptions<ROW>): Promise<RunQueryResult<Saved<ROW>>>;
28
+ runQueryCount<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>;
29
+ streamQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBStreamOptions): ReadableTyped<Saved<ROW>>;
30
+ deleteByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>;
31
+ updateByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CacheDBOptions): Promise<number>;
33
32
  }
@@ -1,4 +1,4 @@
1
- import { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { CommonLogger, PartialObjectWithId } from '@naturalcycles/js-lib';
2
2
  import { CommonDB } from '../../common.db';
3
3
  import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions } from '../../db.model';
4
4
  export interface CacheDBCfg {
@@ -47,7 +47,7 @@ export interface CacheDBOptions extends CommonDBOptions {
47
47
  */
48
48
  onlyCache?: boolean;
49
49
  }
50
- export interface CacheDBSaveOptions<ROW extends Partial<ObjectWithId>> extends CacheDBOptions, CommonDBSaveOptions<ROW> {
50
+ export interface CacheDBSaveOptions<ROW extends PartialObjectWithId> extends CacheDBOptions, CommonDBSaveOptions<ROW> {
51
51
  }
52
52
  export interface CacheDBStreamOptions extends CacheDBOptions, CommonDBStreamOptions {
53
53
  }
@@ -1,4 +1,4 @@
1
- import { JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { JsonSchemaRootObject, Saved, PartialObjectWithId } from '@naturalcycles/js-lib';
2
2
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
3
  import { BaseCommonDB, CommonDBSupport, DBSaveBatchOperation } from '../..';
4
4
  import { CommonDB } from '../../common.db';
@@ -21,18 +21,18 @@ export declare class FileDB extends BaseCommonDB implements CommonDB {
21
21
  cfg: FileDBCfg;
22
22
  ping(): Promise<void>;
23
23
  getTables(): Promise<string[]>;
24
- getByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], _opt?: CommonDBOptions): Promise<ROW[]>;
25
- saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], _opt?: CommonDBSaveOptions<ROW>): Promise<void>;
26
- runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<RunQueryResult<ROW>>;
27
- runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
28
- streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBStreamOptions): ReadableTyped<ROW>;
29
- deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
24
+ getByIds<ROW extends PartialObjectWithId>(table: string, ids: string[], _opt?: CommonDBOptions): Promise<Saved<ROW>[]>;
25
+ saveBatch<ROW extends PartialObjectWithId>(table: string, rows: ROW[], _opt?: CommonDBSaveOptions<ROW>): Promise<void>;
26
+ runQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<RunQueryResult<Saved<ROW>>>;
27
+ runQueryCount<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
28
+ streamQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBStreamOptions): ReadableTyped<ROW>;
29
+ deleteByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
30
30
  deleteByIds(table: string, ids: string[], _opt?: CommonDBOptions): Promise<number>;
31
- getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
32
- loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]>;
33
- saveFile<ROW extends ObjectWithId>(table: string, _rows: ROW[]): Promise<void>;
34
- saveFiles<ROW extends ObjectWithId>(ops: DBSaveBatchOperation<ROW>[]): Promise<void>;
35
- sortRows<ROW extends ObjectWithId>(rows: ROW[]): ROW[];
31
+ getTableSchema<ROW extends PartialObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
32
+ loadFile<ROW extends PartialObjectWithId>(table: string): Promise<Saved<ROW>[]>;
33
+ saveFile<ROW extends PartialObjectWithId>(table: string, _rows: ROW[]): Promise<void>;
34
+ saveFiles<ROW extends PartialObjectWithId>(ops: DBSaveBatchOperation<ROW>[]): Promise<void>;
35
+ sortRows<ROW extends PartialObjectWithId>(rows: ROW[]): ROW[];
36
36
  private logStarted;
37
37
  private logFinished;
38
38
  }
@@ -1,10 +1,10 @@
1
- import { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { CommonLogger, PartialObjectWithId, Saved } from '@naturalcycles/js-lib';
2
2
  import { DBSaveBatchOperation } from '../../db.model';
3
3
  import type { DBQueryOrder } from '../../query/dbQuery';
4
4
  export interface FileDBPersistencePlugin {
5
5
  ping: () => Promise<void>;
6
6
  getTables: () => Promise<string[]>;
7
- loadFile: <ROW extends ObjectWithId>(table: string) => Promise<ROW[]>;
7
+ loadFile: <ROW extends PartialObjectWithId>(table: string) => Promise<Saved<ROW>[]>;
8
8
  saveFiles: (ops: DBSaveBatchOperation<any>[]) => Promise<void>;
9
9
  }
10
10
  export interface FileDBCfg {
@@ -1,4 +1,4 @@
1
- import { ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { PartialObjectWithId, Saved } from '@naturalcycles/js-lib';
2
2
  import { DBSaveBatchOperation } from '../../db.model';
3
3
  import { FileDBPersistencePlugin } from './file.db.model';
4
4
  export interface LocalFilePersistencePluginCfg {
@@ -19,7 +19,7 @@ export declare class LocalFilePersistencePlugin implements FileDBPersistencePlug
19
19
  cfg: LocalFilePersistencePluginCfg;
20
20
  ping(): Promise<void>;
21
21
  getTables(): Promise<string[]>;
22
- loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]>;
22
+ loadFile<ROW extends PartialObjectWithId>(table: string): Promise<Saved<ROW>[]>;
23
23
  saveFiles(ops: DBSaveBatchOperation[]): Promise<void>;
24
- saveFile<ROW extends ObjectWithId>(table: string, rows: ROW[]): Promise<void>;
24
+ saveFile<ROW extends PartialObjectWithId>(table: string, rows: ROW[]): Promise<void>;
25
25
  }
@@ -1,9 +1,9 @@
1
- import { ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { PartialObjectWithId, Saved } from '@naturalcycles/js-lib';
2
2
  import { DBSaveBatchOperation } from '../../db.model';
3
3
  import { FileDBPersistencePlugin } from './file.db.model';
4
4
  export declare class NoopPersistencePlugin implements FileDBPersistencePlugin {
5
5
  ping(): Promise<void>;
6
6
  getTables(): Promise<string[]>;
7
- loadFile<ROW extends ObjectWithId>(_table: string): Promise<ROW[]>;
7
+ loadFile<ROW extends PartialObjectWithId>(_table: string): Promise<Saved<ROW>[]>;
8
8
  saveFiles(_ops: DBSaveBatchOperation[]): Promise<void>;
9
9
  }
@@ -1,4 +1,4 @@
1
- import { JsonSchemaObject, StringMap, JsonSchemaRootObject, ObjectWithId, CommonLogger } from '@naturalcycles/js-lib';
1
+ import { JsonSchemaObject, StringMap, JsonSchemaRootObject, ObjectWithId, CommonLogger, PartialObjectWithId, Saved } from '@naturalcycles/js-lib';
2
2
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
3
  import { CommonDB, CommonDBTransactionOptions, CommonDBType, DBOperation, DBPatch, DBTransactionFn } from '../..';
4
4
  import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, DBTransaction, RunQueryResult } from '../../db.model';
@@ -78,16 +78,16 @@ export declare class InMemoryDB implements CommonDB {
78
78
  */
79
79
  resetCache(_table?: string): Promise<void>;
80
80
  getTables(): Promise<string[]>;
81
- getTableSchema<ROW extends ObjectWithId>(_table: string): Promise<JsonSchemaRootObject<ROW>>;
82
- createTable<ROW extends ObjectWithId>(_table: string, _schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
83
- getByIds<ROW extends ObjectWithId>(_table: string, ids: ROW['id'][], _opt?: CommonDBOptions): Promise<ROW[]>;
84
- saveBatch<ROW extends Partial<ObjectWithId>>(_table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
85
- deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
81
+ getTableSchema<ROW extends PartialObjectWithId>(_table: string): Promise<JsonSchemaRootObject<ROW>>;
82
+ createTable<ROW extends PartialObjectWithId>(_table: string, _schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
83
+ getByIds<ROW extends PartialObjectWithId>(_table: string, ids: string[], _opt?: CommonDBOptions): Promise<Saved<ROW>[]>;
84
+ saveBatch<ROW extends PartialObjectWithId>(_table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
85
+ deleteByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
86
86
  deleteByIds(_table: string, ids: string[], _opt?: CommonDBOptions): Promise<number>;
87
- updateByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>): Promise<number>;
88
- runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<RunQueryResult<ROW>>;
89
- runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
90
- streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): ReadableTyped<ROW>;
87
+ updateByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>): Promise<number>;
88
+ runQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<RunQueryResult<Saved<ROW>>>;
89
+ runQueryCount<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
90
+ streamQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): ReadableTyped<Saved<ROW>>;
91
91
  runInTransaction(fn: DBTransactionFn, opt?: CommonDBTransactionOptions): Promise<void>;
92
92
  /**
93
93
  * Flushes all tables (all namespaces) at once.
@@ -104,8 +104,8 @@ export declare class InMemoryDBTransaction implements DBTransaction {
104
104
  constructor(db: InMemoryDB, opt: Required<CommonDBTransactionOptions>);
105
105
  ops: DBOperation[];
106
106
  writeOperationHappened: boolean;
107
- getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>;
108
- saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
107
+ getByIds<ROW extends PartialObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>;
108
+ saveBatch<ROW extends PartialObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
109
109
  deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number>;
110
110
  commit(): Promise<void>;
111
111
  rollback(): Promise<void>;
@@ -1,3 +1,3 @@
1
- import { ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { PartialObjectWithId } from '@naturalcycles/js-lib';
2
2
  import { DBQuery } from '../../query/dbQuery';
3
- export declare function queryInMemory<ROW extends ObjectWithId>(q: DBQuery<ROW>, rows?: ROW[]): ROW[];
3
+ export declare function queryInMemory<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, rows?: ROW[]): ROW[];
@@ -1,4 +1,4 @@
1
- import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { JsonSchemaObject, JsonSchemaRootObject, PartialObjectWithId, Saved } from '@naturalcycles/js-lib';
2
2
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
3
  import { CommonDB, CommonDBSupport, CommonDBType } from './common.db';
4
4
  import { CommonDBOptions, CommonDBSaveOptions, CommonDBTransactionOptions, DBPatch, DBTransactionFn, RunQueryResult } from './db.model';
@@ -12,15 +12,15 @@ export declare class BaseCommonDB implements CommonDB {
12
12
  support: CommonDBSupport;
13
13
  ping(): Promise<void>;
14
14
  getTables(): Promise<string[]>;
15
- getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
16
- createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>): Promise<void>;
17
- getByIds<ROW extends ObjectWithId>(table: string, ids: string[]): Promise<ROW[]>;
18
- deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<number>;
19
- updateByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CommonDBOptions): Promise<number>;
20
- runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<RunQueryResult<ROW>>;
21
- runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<number>;
22
- saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
23
- streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): ReadableTyped<ROW>;
15
+ getTableSchema<ROW extends PartialObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
16
+ createTable<ROW extends PartialObjectWithId>(table: string, schema: JsonSchemaObject<ROW>): Promise<void>;
17
+ getByIds<ROW extends PartialObjectWithId>(table: string, ids: string[]): Promise<Saved<ROW>[]>;
18
+ deleteByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>): Promise<number>;
19
+ updateByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CommonDBOptions): Promise<number>;
20
+ runQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>): Promise<RunQueryResult<Saved<ROW>>>;
21
+ runQueryCount<ROW extends PartialObjectWithId>(q: DBQuery<ROW>): Promise<number>;
22
+ saveBatch<ROW extends PartialObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
23
+ streamQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>): ReadableTyped<Saved<ROW>>;
24
24
  deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number>;
25
25
  runInTransaction(fn: DBTransactionFn, opt?: CommonDBTransactionOptions): Promise<void>;
26
26
  }
@@ -1,4 +1,4 @@
1
- import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { JsonSchemaObject, JsonSchemaRootObject, PartialObjectWithId, Saved } from '@naturalcycles/js-lib';
2
2
  import type { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
3
  import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, CommonDBTransactionOptions, DBPatch, DBTransactionFn, RunQueryResult } from './db.model';
4
4
  import { DBQuery } from './query/dbQuery';
@@ -47,27 +47,27 @@ export interface CommonDB {
47
47
  *
48
48
  * This is important for the code to rely on it, and it's verified by dbTest
49
49
  */
50
- getTableSchema: <ROW extends ObjectWithId>(table: string) => Promise<JsonSchemaRootObject<ROW>>;
50
+ getTableSchema: <ROW extends PartialObjectWithId>(table: string) => Promise<JsonSchemaRootObject<ROW>>;
51
51
  /**
52
52
  * Will do like `create table ...` for mysql.
53
53
  * Caution! dropIfExists defaults to false. If set to true - will actually DROP the table!
54
54
  */
55
- createTable: <ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions) => Promise<void>;
55
+ createTable: <ROW extends PartialObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions) => Promise<void>;
56
56
  /**
57
57
  * Order of items returned is not guaranteed to match order of ids.
58
58
  * (Such limitation exists because Datastore doesn't support it).
59
59
  */
60
- getByIds: <ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions) => Promise<ROW[]>;
60
+ getByIds: <ROW extends PartialObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions) => Promise<Saved<ROW>[]>;
61
61
  /**
62
62
  * Order by 'id' is not supported by all implementations (for example, Datastore doesn't support it).
63
63
  */
64
- runQuery: <ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions) => Promise<RunQueryResult<ROW>>;
65
- runQueryCount: <ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions) => Promise<number>;
66
- streamQuery: <ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBStreamOptions) => ReadableTyped<ROW>;
64
+ runQuery: <ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions) => Promise<RunQueryResult<Saved<ROW>>>;
65
+ runQueryCount: <ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions) => Promise<number>;
66
+ streamQuery: <ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBStreamOptions) => ReadableTyped<Saved<ROW>>;
67
67
  /**
68
68
  * rows can have missing ids only if DB supports auto-generating them (like mysql auto_increment).
69
69
  */
70
- saveBatch: <ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>) => Promise<void>;
70
+ saveBatch: <ROW extends PartialObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>) => Promise<void>;
71
71
  /**
72
72
  * Returns number of deleted items.
73
73
  * Not supported by all implementations (e.g Datastore will always return same number as number of ids).
@@ -77,7 +77,7 @@ export interface CommonDB {
77
77
  * Returns number of deleted items.
78
78
  * Not supported by all implementations (e.g Datastore will always return same number as number of ids).
79
79
  */
80
- deleteByQuery: <ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions) => Promise<number>;
80
+ deleteByQuery: <ROW extends PartialObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions) => Promise<number>;
81
81
  /**
82
82
  * Applies patch to the rows returned by the query.
83
83
  *
@@ -96,7 +96,7 @@ export interface CommonDB {
96
96
  *
97
97
  * Returns number of rows affected.
98
98
  */
99
- updateByQuery: <ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CommonDBOptions) => Promise<number>;
99
+ updateByQuery: <ROW extends PartialObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CommonDBOptions) => Promise<number>;
100
100
  /**
101
101
  * Should be implemented as a Transaction (best effort), which means that
102
102
  * either ALL or NONE of the operations should be applied.
@@ -1,8 +1,8 @@
1
1
  /// <reference types="node" />
2
2
  import { Transform } from 'node:stream';
3
- import { AnyObject, AsyncMapper, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, Saved, UnixTimestampMillisNumber, Unsaved, ZodSchema } from '@naturalcycles/js-lib';
3
+ import { AnyObject, AsyncMapper, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, PartialObjectWithId, Saved, UnixTimestampMillisNumber, ZodSchema } from '@naturalcycles/js-lib';
4
4
  import { AjvSchema, ObjectSchema, ReadableTyped } from '@naturalcycles/nodejs-lib';
5
- import { DBModelType, DBPatch, DBTransaction, RunQueryResult } from '../db.model';
5
+ import { CommonDBTransactionOptions, DBModelType, DBPatch, DBTransaction, RunQueryResult } from '../db.model';
6
6
  import { DBQuery, RunnableDBQuery } from '../query/dbQuery';
7
7
  import { CommonDaoCfg, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoSaveBatchOptions, CommonDaoSaveOptions, CommonDaoStreamDeleteOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, CommonDaoStreamSaveOptions } from './common.dao.model';
8
8
  /**
@@ -12,22 +12,22 @@ import { CommonDaoCfg, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoSaveBa
12
12
  * BM = Backend model (optimized for API access)
13
13
  * TM = Transport model (optimized to be sent over the wire)
14
14
  */
15
- export declare class CommonDao<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId = Saved<BM>, TM extends AnyObject = BM> {
15
+ export declare class CommonDao<BM extends PartialObjectWithId, DBM extends PartialObjectWithId = BM, TM extends AnyObject = BM> {
16
16
  cfg: CommonDaoCfg<BM, DBM, TM>;
17
17
  constructor(cfg: CommonDaoCfg<BM, DBM, TM>);
18
18
  create(part?: Partial<BM>, opt?: CommonDaoOptions): Saved<BM>;
19
19
  getById(id: undefined | null, opt?: CommonDaoOptions): Promise<null>;
20
20
  getById(id?: string | null, opt?: CommonDaoOptions): Promise<Saved<BM> | null>;
21
21
  getByIdOrEmpty(id: string, part?: Partial<BM>, opt?: CommonDaoOptions): Promise<Saved<BM>>;
22
- getByIdAsDBMOrEmpty(id: string, part?: Partial<BM>, opt?: CommonDaoOptions): Promise<DBM>;
22
+ getByIdAsDBMOrEmpty(id: string, part?: Partial<BM>, opt?: CommonDaoOptions): Promise<Saved<DBM>>;
23
23
  getByIdAsDBM(id: undefined | null, opt?: CommonDaoOptions): Promise<null>;
24
- getByIdAsDBM(id?: string | null, opt?: CommonDaoOptions): Promise<DBM | null>;
24
+ getByIdAsDBM(id?: string | null, opt?: CommonDaoOptions): Promise<Saved<DBM> | null>;
25
25
  getByIdAsTM(id: undefined | null, opt?: CommonDaoOptions): Promise<null>;
26
26
  getByIdAsTM(id?: string | null, opt?: CommonDaoOptions): Promise<TM | null>;
27
27
  getByIds(ids: string[], opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
28
- getByIdsAsDBM(ids: string[], opt?: CommonDaoOptions): Promise<DBM[]>;
28
+ getByIdsAsDBM(ids: string[], opt?: CommonDaoOptions): Promise<Saved<DBM>[]>;
29
29
  requireById(id: string, opt?: CommonDaoOptions): Promise<Saved<BM>>;
30
- requireByIdAsDBM(id: string, opt?: CommonDaoOptions): Promise<DBM>;
30
+ requireByIdAsDBM(id: string, opt?: CommonDaoOptions): Promise<Saved<DBM>>;
31
31
  private throwRequiredError;
32
32
  /**
33
33
  * Throws if readOnly is true
@@ -54,8 +54,8 @@ export declare class CommonDao<BM extends Partial<ObjectWithId>, DBM extends Obj
54
54
  */
55
55
  runUnionQueries(queries: DBQuery<DBM>[], opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
56
56
  runQueryExtended(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<Saved<BM>>>;
57
- runQueryAsDBM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<DBM[]>;
58
- runQueryExtendedAsDBM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<DBM>>;
57
+ runQueryAsDBM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<Saved<DBM>[]>;
58
+ runQueryExtendedAsDBM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<Saved<DBM>>>;
59
59
  runQueryAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<TM[]>;
60
60
  runQueryExtendedAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<TM>>;
61
61
  runQueryCount(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<number>;
@@ -82,13 +82,12 @@ export declare class CommonDao<BM extends Partial<ObjectWithId>, DBM extends Obj
82
82
  * Mutates!
83
83
  * "Returns", just to have a type of "Saved"
84
84
  */
85
- assignIdCreatedUpdated(obj: DBM, opt?: CommonDaoOptions): DBM;
86
- assignIdCreatedUpdated(obj: BM, opt?: CommonDaoOptions): Saved<BM>;
87
- assignIdCreatedUpdated(obj: Unsaved<BM>, opt?: CommonDaoOptions): Saved<BM>;
85
+ assignIdCreatedUpdated(obj: Partial<DBM>, opt?: CommonDaoOptions): Saved<Partial<DBM>>;
86
+ assignIdCreatedUpdated(obj: Partial<BM>, opt?: CommonDaoOptions): Saved<Partial<BM>>;
88
87
  /**
89
88
  * Mutates with id, created, updated
90
89
  */
91
- save(bm: Unsaved<BM>, opt?: CommonDaoSaveOptions<BM, DBM>): Promise<Saved<BM>>;
90
+ save(bm: BM, opt?: CommonDaoSaveOptions<BM, DBM>): Promise<Saved<BM>>;
92
91
  /**
93
92
  * 1. Applies the patch
94
93
  * 2. If object is the same after patching - skips saving it
@@ -115,9 +114,9 @@ export declare class CommonDao<BM extends Partial<ObjectWithId>, DBM extends Obj
115
114
  * It still loads the row from the DB.
116
115
  */
117
116
  patch(bm: Saved<BM>, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>;
118
- saveAsDBM(dbm: DBM, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<DBM>;
119
- saveBatch(bms: Unsaved<BM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>[]>;
120
- saveBatchAsDBM(dbms: DBM[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<DBM[]>;
117
+ saveAsDBM(dbm: DBM, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<DBM>>;
118
+ saveBatch(bms: BM[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>[]>;
119
+ saveBatchAsDBM(dbms: DBM[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<DBM>[]>;
121
120
  /**
122
121
  * "Streaming" is implemented by buffering incoming rows into **batches**
123
122
  * (of size opt.batchSize, which defaults to 500),
@@ -141,18 +140,18 @@ export declare class CommonDao<BM extends Partial<ObjectWithId>, DBM extends Obj
141
140
  updateByIds(ids: string[], patch: DBPatch<DBM>, opt?: CommonDaoOptions): Promise<number>;
142
141
  updateByQuery(q: DBQuery<DBM>, patch: DBPatch<DBM>, opt?: CommonDaoOptions): Promise<number>;
143
142
  dbmToBM(_dbm: undefined, opt?: CommonDaoOptions): Promise<undefined>;
144
- dbmToBM(_dbm?: DBM, opt?: CommonDaoOptions): Promise<Saved<BM>>;
145
- dbmsToBM(dbms: DBM[], opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
143
+ dbmToBM(_dbm?: Saved<DBM>, opt?: CommonDaoOptions): Promise<Saved<BM>>;
144
+ dbmsToBM(dbms: Saved<DBM>[], opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
146
145
  /**
147
146
  * Mutates object with properties: id, created, updated.
148
147
  * Returns DBM (new reference).
149
148
  */
150
149
  bmToDBM(bm: undefined, opt?: CommonDaoOptions): Promise<undefined>;
151
- bmToDBM(bm?: BM, opt?: CommonDaoOptions): Promise<DBM>;
152
- bmsToDBM(bms: BM[], opt?: CommonDaoOptions): Promise<DBM[]>;
150
+ bmToDBM(bm?: BM, opt?: CommonDaoOptions): Promise<Saved<DBM>>;
151
+ bmsToDBM(bms: BM[], opt?: CommonDaoOptions): Promise<Saved<DBM>[]>;
153
152
  anyToDBM(dbm: undefined, opt?: CommonDaoOptions): undefined;
154
- anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM;
155
- anyToDBMs(entities: DBM[], opt?: CommonDaoOptions): DBM[];
153
+ anyToDBM(dbm?: any, opt?: CommonDaoOptions): Saved<DBM>;
154
+ anyToDBMs(entities: Saved<DBM>[], opt?: CommonDaoOptions): Saved<DBM>[];
156
155
  bmToTM(bm: undefined, opt?: CommonDaoOptions): TM | undefined;
157
156
  bmToTM(bm?: Saved<BM>, opt?: CommonDaoOptions): TM;
158
157
  bmsToTM(bms: Saved<BM>[], opt?: CommonDaoOptions): TM[];
@@ -169,7 +168,7 @@ export declare class CommonDao<BM extends Partial<ObjectWithId>, DBM extends Obj
169
168
  * Proxy to this.cfg.db.ping
170
169
  */
171
170
  ping(): Promise<void>;
172
- runInTransaction(fn: CommonDaoTransactionFn): Promise<void>;
171
+ runInTransaction(fn: CommonDaoTransactionFn, opt?: CommonDBTransactionOptions): Promise<void>;
173
172
  protected logResult(started: number, op: string, res: any, table: string): void;
174
173
  protected logSaveResult(started: number, op: string, table: string): void;
175
174
  protected logStarted(op: string, table: string, force?: boolean): UnixTimestampMillisNumber;
@@ -193,10 +192,10 @@ export declare class CommonDaoTransaction {
193
192
  * Perform a graceful rollback without throwing/re-throwing any error.
194
193
  */
195
194
  rollback(): Promise<void>;
196
- getById<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId>(dao: CommonDao<BM, DBM, any>, id?: string | null, opt?: CommonDaoOptions): Promise<Saved<BM> | null>;
197
- getByIds<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId>(dao: CommonDao<BM, DBM, any>, ids: string[], opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
198
- save<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId>(dao: CommonDao<BM, DBM, any>, bm: Unsaved<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>;
199
- saveBatch<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId>(dao: CommonDao<BM, DBM, any>, bms: Unsaved<BM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>[]>;
195
+ getById<BM extends PartialObjectWithId, DBM extends PartialObjectWithId>(dao: CommonDao<BM, DBM, any>, id?: string | null, opt?: CommonDaoOptions): Promise<Saved<BM> | null>;
196
+ getByIds<BM extends PartialObjectWithId, DBM extends PartialObjectWithId>(dao: CommonDao<BM, DBM, any>, ids: string[], opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
197
+ save<BM extends PartialObjectWithId, DBM extends PartialObjectWithId>(dao: CommonDao<BM, DBM, any>, bm: BM, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>;
198
+ saveBatch<BM extends PartialObjectWithId, DBM extends PartialObjectWithId>(dao: CommonDao<BM, DBM, any>, bms: BM[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>[]>;
200
199
  deleteById(dao: CommonDao<any>, id?: string | null, opt?: CommonDaoOptions): Promise<number>;
201
200
  deleteByIds(dao: CommonDao<any>, ids: string[], opt?: CommonDaoOptions): Promise<number>;
202
201
  }
@@ -26,8 +26,8 @@ class CommonDao {
26
26
  logLevel: isGAE || isCI ? common_dao_model_1.CommonDaoLogLevel.NONE : common_dao_model_1.CommonDaoLogLevel.OPERATIONS,
27
27
  createId: true,
28
28
  assignGeneratedIds: false,
29
- created: true,
30
- updated: true,
29
+ useCreatedProperty: true,
30
+ useUpdatedProperty: true,
31
31
  logger: console,
32
32
  ...cfg,
33
33
  hooks: {
@@ -447,7 +447,7 @@ class CommonDao {
447
447
  const stream = this.cfg.db
448
448
  .streamQuery(q.select(['id']), opt)
449
449
  .on('error', err => stream.emit('error', err))
450
- .pipe((0, nodejs_lib_1.transformMapSimple)(objectWithId => objectWithId.id, {
450
+ .pipe((0, nodejs_lib_1.transformMapSimple)(r => r.id, {
451
451
  errorMode: js_lib_1.ErrorMode.SUPPRESS, // cause .pipe() cannot propagate errors
452
452
  }));
453
453
  return stream;
@@ -460,9 +460,9 @@ class CommonDao {
460
460
  let count = 0;
461
461
  await (0, nodejs_lib_1._pipeline)([
462
462
  this.cfg.db.streamQuery(q.select(['id']), opt),
463
- (0, nodejs_lib_1.transformMapSimple)(objectWithId => {
463
+ (0, nodejs_lib_1.transformMapSimple)(r => {
464
464
  count++;
465
- return objectWithId.id;
465
+ return r.id;
466
466
  }),
467
467
  (0, nodejs_lib_1.transformMap)(mapper, {
468
468
  ...opt,
@@ -481,11 +481,11 @@ class CommonDao {
481
481
  }
482
482
  assignIdCreatedUpdated(obj, opt = {}) {
483
483
  const now = Math.floor(Date.now() / 1000);
484
- if (this.cfg.created) {
484
+ if (this.cfg.useCreatedProperty) {
485
485
  ;
486
486
  obj['created'] ||= obj['updated'] || now;
487
487
  }
488
- if (this.cfg.updated) {
488
+ if (this.cfg.useUpdatedProperty) {
489
489
  ;
490
490
  obj['updated'] =
491
491
  opt.preserveUpdatedCreated && obj['updated'] ? obj['updated'] : now;
@@ -801,7 +801,7 @@ class CommonDao {
801
801
  const { batchSize, batchConcurrency = 16 } = opt;
802
802
  await (0, nodejs_lib_1._pipeline)([
803
803
  this.cfg.db.streamQuery(q.select(['id']), opt),
804
- (0, nodejs_lib_1.transformMapSimple)(objectWithId => objectWithId.id, {
804
+ (0, nodejs_lib_1.transformMapSimple)(r => r.id, {
805
805
  errorMode: js_lib_1.ErrorMode.SUPPRESS,
806
806
  }),
807
807
  (0, nodejs_lib_1.transformBuffer)({ batchSize }),
@@ -991,17 +991,17 @@ class CommonDao {
991
991
  async ping() {
992
992
  await this.cfg.db.ping();
993
993
  }
994
- async runInTransaction(fn) {
994
+ async runInTransaction(fn, opt) {
995
995
  await this.cfg.db.runInTransaction(async (tx) => {
996
996
  const daoTx = new CommonDaoTransaction(tx, this.cfg.logger);
997
997
  try {
998
998
  await fn(daoTx);
999
999
  }
1000
1000
  catch (err) {
1001
- await daoTx.rollback();
1001
+ await daoTx.rollback(); // graceful rollback that "never throws"
1002
1002
  throw err;
1003
1003
  }
1004
- });
1004
+ }, opt);
1005
1005
  }
1006
1006
  logResult(started, op, res, table) {
1007
1007
  if (!this.cfg.logLevel)
@@ -1088,7 +1088,7 @@ class CommonDaoTransaction {
1088
1088
  return await dao.getByIds(ids, { ...opt, tx: this.tx });
1089
1089
  }
1090
1090
  // todo: Queries inside Transaction are not supported yet
1091
- // async runQuery<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId>(
1091
+ // async runQuery<BM extends PartialObjectWithId, DBM extends ObjectWithId>(
1092
1092
  // dao: CommonDao<BM, DBM, any>,
1093
1093
  // q: DBQuery<DBM>,
1094
1094
  // opt?: CommonDaoOptions,
@@ -1,8 +1,8 @@
1
- import { CommonLogger, ErrorMode, ObjectWithId, Promisable, Saved, ZodError, ZodSchema } from '@naturalcycles/js-lib';
1
+ import { CommonLogger, ErrorMode, PartialObjectWithId, Promisable, Saved, ZodError, ZodSchema } from '@naturalcycles/js-lib';
2
2
  import { AjvSchema, AjvValidationError, JoiValidationError, ObjectSchema, TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib';
3
3
  import { CommonDB } from '../common.db';
4
4
  import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model';
5
- export interface CommonDaoHooks<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId, TM> {
5
+ export interface CommonDaoHooks<BM extends PartialObjectWithId, DBM extends PartialObjectWithId, TM> {
6
6
  /**
7
7
  * Allows to override the id generation function.
8
8
  * By default it uses `stringId` from nodejs-lib
@@ -38,7 +38,7 @@ export interface CommonDaoHooks<BM extends Partial<ObjectWithId>, DBM extends Ob
38
38
  * as it only validates "final state", not intermediate
39
39
  */
40
40
  beforeDBMValidate: (dbm: Partial<DBM>) => Partial<DBM>;
41
- beforeDBMToBM: (dbm: DBM) => Partial<BM> | Promise<Partial<BM>>;
41
+ beforeDBMToBM: (dbm: Saved<DBM>) => Partial<BM> | Promise<Partial<BM>>;
42
42
  beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>;
43
43
  beforeBMToTM: (bm: BM) => Partial<TM>;
44
44
  /**
@@ -53,7 +53,7 @@ export interface CommonDaoHooks<BM extends Partial<ObjectWithId>, DBM extends Ob
53
53
  *
54
54
  * You can do validations as needed here and throw errors, they will be propagated.
55
55
  */
56
- afterLoad?: (dbm: DBM) => Promisable<DBM | null>;
56
+ afterLoad?: (dbm: Saved<DBM>) => Promisable<Saved<DBM> | null>;
57
57
  /**
58
58
  * Allows to access the DBM just before it's supposed to be saved to the DB.
59
59
  *
@@ -67,7 +67,7 @@ export interface CommonDaoHooks<BM extends Partial<ObjectWithId>, DBM extends Ob
67
67
  *
68
68
  * You can do validations as needed here and throw errors, they will be propagated.
69
69
  */
70
- beforeSave?: (dbm: DBM) => Promisable<DBM | null>;
70
+ beforeSave?: (dbm: Saved<DBM>) => Promisable<Saved<DBM> | null>;
71
71
  /**
72
72
  * Called in:
73
73
  * - dbmToBM (applied before DBM becomes BM)
@@ -77,7 +77,7 @@ export interface CommonDaoHooks<BM extends Partial<ObjectWithId>, DBM extends Ob
77
77
  * It still applies to BM "transitively", during dbmToBM
78
78
  * (e.g after loaded from the Database).
79
79
  */
80
- anonymize: (dbm: DBM) => DBM;
80
+ anonymize: (dbm: Saved<DBM>) => Saved<DBM>;
81
81
  /**
82
82
  * If hook is defined - allows to prevent or modify the error thrown.
83
83
  * Return `false` to prevent throwing an error.
@@ -104,7 +104,7 @@ export declare enum CommonDaoLogLevel {
104
104
  */
105
105
  DATA_FULL = 30
106
106
  }
107
- export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId = Saved<BM>, TM = BM> {
107
+ export interface CommonDaoCfg<BM extends PartialObjectWithId, DBM extends PartialObjectWithId = BM, TM = BM> {
108
108
  db: CommonDB;
109
109
  table: string;
110
110
  /**
@@ -157,12 +157,12 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends Obje
157
157
  * Defaults to true
158
158
  * Set to false to disable `created` field management.
159
159
  */
160
- created?: boolean;
160
+ useCreatedProperty?: boolean;
161
161
  /**
162
162
  * Defaults to true
163
163
  * Set to false to disable `updated` field management.
164
164
  */
165
- updated?: boolean;
165
+ useUpdatedProperty?: boolean;
166
166
  /**
167
167
  * Default is false.
168
168
  * If true - will run `_filterNullishValues` inside `validateAndConvert` function
@@ -223,7 +223,7 @@ export interface CommonDaoOptions extends CommonDBOptions {
223
223
  */
224
224
  table?: string;
225
225
  }
226
- export interface CommonDaoSaveOptions<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId> extends CommonDaoSaveBatchOptions<DBM> {
226
+ export interface CommonDaoSaveOptions<BM extends PartialObjectWithId, DBM extends PartialObjectWithId> extends CommonDaoSaveBatchOptions<DBM> {
227
227
  /**
228
228
  * If provided - a check will be made.
229
229
  * If the object for saving equals to the object passed to `skipIfEquals` - save operation will be skipped.
@@ -237,7 +237,7 @@ export interface CommonDaoSaveOptions<BM extends Partial<ObjectWithId>, DBM exte
237
237
  /**
238
238
  * All properties default to undefined.
239
239
  */
240
- export interface CommonDaoSaveBatchOptions<DBM extends ObjectWithId> extends CommonDaoOptions, CommonDBSaveOptions<DBM> {
240
+ export interface CommonDaoSaveBatchOptions<DBM extends PartialObjectWithId> extends CommonDaoOptions, CommonDBSaveOptions<DBM> {
241
241
  /**
242
242
  * @default false
243
243
  *
@@ -249,9 +249,9 @@ export interface CommonDaoSaveBatchOptions<DBM extends ObjectWithId> extends Com
249
249
  */
250
250
  ensureUniqueId?: boolean;
251
251
  }
252
- export interface CommonDaoStreamDeleteOptions<DBM extends ObjectWithId> extends CommonDaoStreamOptions<DBM> {
252
+ export interface CommonDaoStreamDeleteOptions<DBM extends PartialObjectWithId> extends CommonDaoStreamOptions<DBM> {
253
253
  }
254
- export interface CommonDaoStreamSaveOptions<DBM extends ObjectWithId> extends CommonDaoSaveBatchOptions<DBM>, CommonDaoStreamOptions<DBM> {
254
+ export interface CommonDaoStreamSaveOptions<DBM extends PartialObjectWithId> extends CommonDaoSaveBatchOptions<DBM>, CommonDaoStreamOptions<DBM> {
255
255
  }
256
256
  export interface CommonDaoStreamForEachOptions<IN> extends CommonDaoStreamOptions<IN>, TransformMapOptions<IN, any> {
257
257
  }
@@ -1,4 +1,4 @@
1
- import type { ObjectWithId } from '@naturalcycles/js-lib';
1
+ import type { PartialObjectWithId } from '@naturalcycles/js-lib';
2
2
  import { CommonDB } from './common.db';
3
3
  /**
4
4
  * Similar to SQL INSERT, UPDATE.
@@ -48,7 +48,7 @@ export interface CommonDBOptions {
48
48
  /**
49
49
  * All properties default to undefined.
50
50
  */
51
- export interface CommonDBSaveOptions<ROW extends Partial<ObjectWithId> = any> extends CommonDBOptions {
51
+ export interface CommonDBSaveOptions<ROW extends PartialObjectWithId = any> extends CommonDBOptions {
52
52
  excludeFromIndexes?: (keyof ROW)[];
53
53
  /**
54
54
  * Default is `upsert`
@@ -76,7 +76,7 @@ export interface RunQueryResult<T> {
76
76
  endCursor?: string;
77
77
  }
78
78
  export type DBOperation = DBSaveBatchOperation | DBDeleteByIdsOperation;
79
- export interface DBSaveBatchOperation<ROW extends Partial<ObjectWithId> = any> {
79
+ export interface DBSaveBatchOperation<ROW extends PartialObjectWithId = any> {
80
80
  type: 'saveBatch';
81
81
  table: string;
82
82
  rows: ROW[];
@@ -109,4 +109,4 @@ export declare class DBIncrement {
109
109
  private constructor();
110
110
  static of(amount: number): DBIncrement;
111
111
  }
112
- export type DBPatch<ROW extends Partial<ObjectWithId>> = Partial<Record<keyof ROW, ROW[keyof ROW] | DBIncrement>>;
112
+ export type DBPatch<ROW extends PartialObjectWithId> = Partial<Record<keyof ROW, ROW[keyof ROW] | DBIncrement>>;