@naturalcycles/db-lib 9.3.2 → 9.4.1
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.
- package/dist/adapter/cachedb/cache.db.d.ts +10 -10
- package/dist/adapter/cachedb/cache.db.model.d.ts +2 -2
- package/dist/adapter/file/file.db.d.ts +12 -12
- package/dist/adapter/file/file.db.model.d.ts +3 -3
- package/dist/adapter/file/inMemory.persistence.plugin.d.ts +1 -1
- package/dist/adapter/file/localFile.persistence.plugin.d.ts +4 -4
- package/dist/adapter/file/noop.persistence.plugin.d.ts +3 -3
- package/dist/adapter/inmemory/inMemory.db.d.ts +12 -12
- package/dist/adapter/inmemory/queryInMemory.d.ts +2 -2
- package/dist/base.common.db.d.ts +10 -10
- package/dist/common.db.d.ts +10 -10
- package/dist/commondao/common.dao.d.ts +30 -22
- package/dist/commondao/common.dao.js +45 -21
- package/dist/commondao/common.dao.model.d.ts +17 -10
- package/dist/db.model.d.ts +5 -5
- package/dist/pipeline/dbPipelineBackup.d.ts +1 -1
- package/dist/pipeline/dbPipelineCopy.d.ts +1 -1
- package/dist/pipeline/dbPipelineRestore.d.ts +1 -1
- package/dist/query/dbQuery.d.ts +7 -7
- package/dist/transaction/dbTransaction.util.d.ts +3 -3
- package/dist/validation/index.d.ts +2 -2
- package/package.json +1 -1
- package/src/adapter/cachedb/cache.db.model.ts +2 -2
- package/src/adapter/cachedb/cache.db.ts +14 -15
- package/src/adapter/file/file.db.model.ts +3 -3
- package/src/adapter/file/file.db.ts +15 -18
- package/src/adapter/file/inMemory.persistence.plugin.ts +1 -1
- package/src/adapter/file/localFile.persistence.plugin.ts +5 -5
- package/src/adapter/file/noop.persistence.plugin.ts +3 -3
- package/src/adapter/inmemory/inMemory.db.ts +17 -19
- package/src/adapter/inmemory/queryInMemory.ts +2 -5
- package/src/base.common.db.ts +10 -20
- package/src/common.db.ts +13 -20
- package/src/commondao/common.dao.model.ts +20 -18
- package/src/commondao/common.dao.ts +119 -72
- package/src/db.model.ts +5 -6
- package/src/pipeline/dbPipelineBackup.ts +1 -1
- package/src/pipeline/dbPipelineCopy.ts +11 -4
- package/src/pipeline/dbPipelineRestore.ts +5 -4
- package/src/query/dbQuery.ts +9 -11
- package/src/timeseries/commonTimeSeriesDao.ts +1 -1
- package/src/transaction/dbTransaction.util.ts +3 -3
- package/src/validation/index.ts +3 -3
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JsonSchemaObject, JsonSchemaRootObject,
|
|
1
|
+
import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { BaseCommonDB } from '../../base.common.db';
|
|
4
4
|
import { CommonDB, CommonDBSupport } from '../../common.db';
|
|
@@ -20,13 +20,13 @@ export declare class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
20
20
|
* Resets InMemory DB data
|
|
21
21
|
*/
|
|
22
22
|
getTables(): Promise<string[]>;
|
|
23
|
-
getTableSchema<ROW extends
|
|
24
|
-
createTable<ROW extends
|
|
25
|
-
getByIds<ROW extends
|
|
26
|
-
saveBatch<ROW extends
|
|
27
|
-
runQuery<ROW extends
|
|
28
|
-
runQueryCount<ROW extends
|
|
29
|
-
streamQuery<ROW extends
|
|
30
|
-
deleteByQuery<ROW extends
|
|
31
|
-
updateByQuery<ROW extends
|
|
23
|
+
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
|
|
24
|
+
createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CacheDBCreateOptions): Promise<void>;
|
|
25
|
+
getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CacheDBSaveOptions<ROW>): Promise<ROW[]>;
|
|
26
|
+
saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CacheDBSaveOptions<ROW>): Promise<void>;
|
|
27
|
+
runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBSaveOptions<ROW>): Promise<RunQueryResult<ROW>>;
|
|
28
|
+
runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>;
|
|
29
|
+
streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBStreamOptions): ReadableTyped<ROW>;
|
|
30
|
+
deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>;
|
|
31
|
+
updateByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CacheDBOptions): Promise<number>;
|
|
32
32
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CommonLogger,
|
|
1
|
+
import { CommonLogger, ObjectWithId } 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
|
|
50
|
+
export interface CacheDBSaveOptions<ROW extends ObjectWithId> extends CacheDBOptions, CommonDBSaveOptions<ROW> {
|
|
51
51
|
}
|
|
52
52
|
export interface CacheDBStreamOptions extends CacheDBOptions, CommonDBStreamOptions {
|
|
53
53
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JsonSchemaRootObject,
|
|
1
|
+
import { JsonSchemaRootObject, ObjectWithId } 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
|
|
25
|
-
saveBatch<ROW extends
|
|
26
|
-
runQuery<ROW extends
|
|
27
|
-
runQueryCount<ROW extends
|
|
28
|
-
streamQuery<ROW extends
|
|
29
|
-
deleteByQuery<ROW extends
|
|
24
|
+
getByIds<ROW extends ObjectWithId>(table: string, ids: string[], _opt?: CommonDBOptions): Promise<ROW[]>;
|
|
25
|
+
saveBatch<ROW extends 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>;
|
|
30
30
|
deleteByIds(table: string, ids: string[], _opt?: CommonDBOptions): Promise<number>;
|
|
31
|
-
getTableSchema<ROW extends
|
|
32
|
-
loadFile<ROW extends
|
|
33
|
-
saveFile<ROW extends
|
|
34
|
-
saveFiles<ROW extends
|
|
35
|
-
sortRows<ROW extends
|
|
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[];
|
|
36
36
|
private logStarted;
|
|
37
37
|
private logFinished;
|
|
38
38
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { CommonLogger,
|
|
1
|
+
import { CommonLogger, ObjectWithId } 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
|
|
7
|
+
loadFile: <ROW extends ObjectWithId>(table: string) => Promise<ROW[]>;
|
|
8
8
|
saveFiles: (ops: DBSaveBatchOperation<any>[]) => Promise<void>;
|
|
9
9
|
}
|
|
10
10
|
export interface FileDBCfg {
|
|
@@ -12,7 +12,7 @@ export interface FileDBCfg {
|
|
|
12
12
|
/**
|
|
13
13
|
* @default undefined, which means "insertion order"
|
|
14
14
|
*/
|
|
15
|
-
sortOnSave?: DBQueryOrder
|
|
15
|
+
sortOnSave?: DBQueryOrder<any>;
|
|
16
16
|
/**
|
|
17
17
|
* @default true
|
|
18
18
|
* If true - will run `sortObjectDeep()` on each object to achieve deterministic sort
|
|
@@ -9,5 +9,5 @@ export declare class InMemoryPersistencePlugin implements FileDBPersistencePlugi
|
|
|
9
9
|
ping(): Promise<void>;
|
|
10
10
|
getTables(): Promise<string[]>;
|
|
11
11
|
loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]>;
|
|
12
|
-
saveFiles(ops: DBSaveBatchOperation[]): Promise<void>;
|
|
12
|
+
saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void>;
|
|
13
13
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ObjectWithId } 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
|
|
23
|
-
saveFiles(ops: DBSaveBatchOperation[]): Promise<void>;
|
|
24
|
-
saveFile<ROW extends
|
|
22
|
+
loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]>;
|
|
23
|
+
saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void>;
|
|
24
|
+
saveFile<ROW extends ObjectWithId>(table: string, rows: ROW[]): Promise<void>;
|
|
25
25
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ObjectWithId } 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
|
|
8
|
-
saveFiles(_ops: DBSaveBatchOperation[]): Promise<void>;
|
|
7
|
+
loadFile<ROW extends ObjectWithId>(_table: string): Promise<ROW[]>;
|
|
8
|
+
saveFiles(_ops: DBSaveBatchOperation<any>[]): Promise<void>;
|
|
9
9
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JsonSchemaObject, StringMap, JsonSchemaRootObject, ObjectWithId, CommonLogger
|
|
1
|
+
import { JsonSchemaObject, StringMap, JsonSchemaRootObject, ObjectWithId, CommonLogger } 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
|
|
82
|
-
createTable<ROW extends
|
|
83
|
-
getByIds<ROW extends
|
|
84
|
-
saveBatch<ROW extends
|
|
85
|
-
deleteByQuery<ROW extends
|
|
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: string[], _opt?: CommonDBOptions): Promise<ROW[]>;
|
|
84
|
+
saveBatch<ROW extends ObjectWithId>(_table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
|
|
85
|
+
deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
|
|
86
86
|
deleteByIds(_table: string, ids: string[], _opt?: CommonDBOptions): Promise<number>;
|
|
87
|
-
updateByQuery<ROW extends
|
|
88
|
-
runQuery<ROW extends
|
|
89
|
-
runQueryCount<ROW extends
|
|
90
|
-
streamQuery<ROW extends
|
|
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>;
|
|
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
|
|
108
|
-
saveBatch<ROW extends
|
|
107
|
+
getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>;
|
|
108
|
+
saveBatch<ROW extends ObjectWithId>(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 {
|
|
1
|
+
import { ObjectWithId } from '@naturalcycles/js-lib';
|
|
2
2
|
import { DBQuery } from '../../query/dbQuery';
|
|
3
|
-
export declare function queryInMemory<ROW extends
|
|
3
|
+
export declare function queryInMemory<ROW extends ObjectWithId>(q: DBQuery<ROW>, rows?: ROW[]): ROW[];
|
package/dist/base.common.db.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JsonSchemaObject, JsonSchemaRootObject,
|
|
1
|
+
import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } 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
|
|
16
|
-
createTable<ROW extends
|
|
17
|
-
getByIds<ROW extends
|
|
18
|
-
deleteByQuery<ROW extends
|
|
19
|
-
updateByQuery<ROW extends
|
|
20
|
-
runQuery<ROW extends
|
|
21
|
-
runQueryCount<ROW extends
|
|
22
|
-
saveBatch<ROW extends
|
|
23
|
-
streamQuery<ROW extends
|
|
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 ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
|
|
23
|
+
streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): ReadableTyped<ROW>;
|
|
24
24
|
deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number>;
|
|
25
25
|
runInTransaction(fn: DBTransactionFn, opt?: CommonDBTransactionOptions): Promise<void>;
|
|
26
26
|
}
|
package/dist/common.db.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JsonSchemaObject, JsonSchemaRootObject,
|
|
1
|
+
import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } 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
|
|
50
|
+
getTableSchema: <ROW extends ObjectWithId>(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
|
|
55
|
+
createTable: <ROW extends ObjectWithId>(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
|
|
60
|
+
getByIds: <ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions) => Promise<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
|
|
65
|
-
runQueryCount: <ROW extends
|
|
66
|
-
streamQuery: <ROW extends
|
|
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>;
|
|
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
|
|
70
|
+
saveBatch: <ROW extends ObjectWithId>(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
|
|
80
|
+
deleteByQuery: <ROW extends ObjectWithId>(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
|
|
99
|
+
updateByQuery: <ROW extends ObjectWithId>(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,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Transform } from 'node:stream';
|
|
3
|
-
import { AnyObject, AsyncMapper, BaseDBEntity, CommonLogger, JsonSchemaObject, JsonSchemaRootObject,
|
|
3
|
+
import { AnyObject, AsyncMapper, BaseDBEntity, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, Saved, UnixTimestampMillisNumber, UnsavedId, ZodSchema } from '@naturalcycles/js-lib';
|
|
4
4
|
import { AjvSchema, ObjectSchema, ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
5
5
|
import { CommonDBTransactionOptions, DBModelType, DBPatch, DBTransaction, RunQueryResult } from '../db.model';
|
|
6
6
|
import { DBQuery, RunnableDBQuery } from '../query/dbQuery';
|
|
@@ -12,7 +12,7 @@ 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
|
|
15
|
+
export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = 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>;
|
|
@@ -82,11 +82,11 @@ export declare class CommonDao<BM extends PartialObjectWithId, DBM extends Parti
|
|
|
82
82
|
* Mutates!
|
|
83
83
|
* "Returns", just to have a type of "Saved"
|
|
84
84
|
*/
|
|
85
|
-
assignIdCreatedUpdated<T extends BaseDBEntity>(obj: T
|
|
85
|
+
assignIdCreatedUpdated<T extends BaseDBEntity>(obj: Partial<T>, opt?: CommonDaoOptions): T;
|
|
86
86
|
/**
|
|
87
87
|
* Mutates with id, created, updated
|
|
88
88
|
*/
|
|
89
|
-
save(bm: BM
|
|
89
|
+
save(bm: UnsavedId<BM>, opt?: CommonDaoSaveOptions<BM, DBM>): Promise<Saved<BM>>;
|
|
90
90
|
/**
|
|
91
91
|
* 1. Applies the patch
|
|
92
92
|
* 2. If object is the same after patching - skips saving it
|
|
@@ -95,7 +95,7 @@ export declare class CommonDao<BM extends PartialObjectWithId, DBM extends Parti
|
|
|
95
95
|
* Similar to `save` with skipIfEquals.
|
|
96
96
|
* Similar to `patch`, but doesn't load the object from the Database.
|
|
97
97
|
*/
|
|
98
|
-
savePatch(bm:
|
|
98
|
+
savePatch(bm: BM, patch: Partial<BM>, opt: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>;
|
|
99
99
|
/**
|
|
100
100
|
* Convenience method to replace 3 operations (loading+patching+saving) with one:
|
|
101
101
|
*
|
|
@@ -106,16 +106,24 @@ export declare class CommonDao<BM extends PartialObjectWithId, DBM extends Parti
|
|
|
106
106
|
* 3. Saves (as fast as possible since the read) with the Patch applied, but only if the data has changed.
|
|
107
107
|
*/
|
|
108
108
|
patchById(id: string, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>;
|
|
109
|
+
/**
|
|
110
|
+
* Like patchById, but runs all operations within a Transaction.
|
|
111
|
+
*/
|
|
112
|
+
patchByIdInTransaction(id: string, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>;
|
|
109
113
|
/**
|
|
110
114
|
* Same as patchById, but takes the whole object as input.
|
|
111
115
|
* This "whole object" is mutated with the patch and returned.
|
|
112
116
|
* Otherwise, similar behavior as patchById.
|
|
113
117
|
* It still loads the row from the DB.
|
|
114
118
|
*/
|
|
115
|
-
patch(bm:
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
+
patch(bm: BM, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>;
|
|
120
|
+
/**
|
|
121
|
+
* Like patch, but runs all operations within a Transaction.
|
|
122
|
+
*/
|
|
123
|
+
patchInTransaction(bm: BM, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>;
|
|
124
|
+
saveAsDBM(dbm: UnsavedId<DBM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<DBM>>;
|
|
125
|
+
saveBatch(bms: UnsavedId<BM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>[]>;
|
|
126
|
+
saveBatchAsDBM(dbms: UnsavedId<DBM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<DBM>[]>;
|
|
119
127
|
/**
|
|
120
128
|
* "Streaming" is implemented by buffering incoming rows into **batches**
|
|
121
129
|
* (of size opt.batchSize, which defaults to 500),
|
|
@@ -139,8 +147,8 @@ export declare class CommonDao<BM extends PartialObjectWithId, DBM extends Parti
|
|
|
139
147
|
updateByIds(ids: string[], patch: DBPatch<DBM>, opt?: CommonDaoOptions): Promise<number>;
|
|
140
148
|
updateByQuery(q: DBQuery<DBM>, patch: DBPatch<DBM>, opt?: CommonDaoOptions): Promise<number>;
|
|
141
149
|
dbmToBM(_dbm: undefined, opt?: CommonDaoOptions): Promise<undefined>;
|
|
142
|
-
dbmToBM(_dbm?:
|
|
143
|
-
dbmsToBM(dbms:
|
|
150
|
+
dbmToBM(_dbm?: DBM, opt?: CommonDaoOptions): Promise<Saved<BM>>;
|
|
151
|
+
dbmsToBM(dbms: DBM[], opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
|
|
144
152
|
/**
|
|
145
153
|
* Mutates object with properties: id, created, updated.
|
|
146
154
|
* Returns DBM (new reference).
|
|
@@ -150,24 +158,24 @@ export declare class CommonDao<BM extends PartialObjectWithId, DBM extends Parti
|
|
|
150
158
|
bmsToDBM(bms: BM[], opt?: CommonDaoOptions): Promise<Saved<DBM>[]>;
|
|
151
159
|
anyToDBM(dbm: undefined, opt?: CommonDaoOptions): undefined;
|
|
152
160
|
anyToDBM(dbm?: any, opt?: CommonDaoOptions): Saved<DBM>;
|
|
153
|
-
anyToDBMs(entities:
|
|
161
|
+
anyToDBMs(entities: DBM[], opt?: CommonDaoOptions): Saved<DBM>[];
|
|
154
162
|
bmToTM(bm: undefined, opt?: CommonDaoOptions): TM | undefined;
|
|
155
|
-
bmToTM(bm?:
|
|
156
|
-
bmsToTM(bms:
|
|
163
|
+
bmToTM(bm?: BM, opt?: CommonDaoOptions): TM;
|
|
164
|
+
bmsToTM(bms: BM[], opt?: CommonDaoOptions): TM[];
|
|
157
165
|
/**
|
|
158
166
|
* Returns *converted value*.
|
|
159
167
|
* Validates (unless `skipValidation=true` passed).
|
|
160
168
|
*
|
|
161
169
|
* Does NOT mutate the object.
|
|
162
170
|
*/
|
|
163
|
-
validateAndConvert<
|
|
171
|
+
validateAndConvert<T>(obj: Partial<T>, schema: ObjectSchema<T> | AjvSchema<T> | ZodSchema<T> | undefined, modelType: DBModelType, opt?: CommonDaoOptions): any;
|
|
164
172
|
getTableSchema(): Promise<JsonSchemaRootObject<DBM>>;
|
|
165
173
|
createTable(schema: JsonSchemaObject<DBM>, opt?: CommonDaoCreateOptions): Promise<void>;
|
|
166
174
|
/**
|
|
167
175
|
* Proxy to this.cfg.db.ping
|
|
168
176
|
*/
|
|
169
177
|
ping(): Promise<void>;
|
|
170
|
-
runInTransaction(fn: CommonDaoTransactionFn
|
|
178
|
+
runInTransaction<T = void>(fn: CommonDaoTransactionFn<T>, opt?: CommonDBTransactionOptions): Promise<T>;
|
|
171
179
|
protected logResult(started: number, op: string, res: any, table: string): void;
|
|
172
180
|
protected logSaveResult(started: number, op: string, table: string): void;
|
|
173
181
|
protected logStarted(op: string, table: string, force?: boolean): UnixTimestampMillisNumber;
|
|
@@ -178,23 +186,23 @@ export declare class CommonDao<BM extends PartialObjectWithId, DBM extends Parti
|
|
|
178
186
|
*
|
|
179
187
|
* Transaction is rolled back when the function returns rejected Promise (aka "throws").
|
|
180
188
|
*/
|
|
181
|
-
export type CommonDaoTransactionFn = (tx: CommonDaoTransaction) => Promise<
|
|
189
|
+
export type CommonDaoTransactionFn<T = void> = (tx: CommonDaoTransaction) => Promise<T>;
|
|
182
190
|
/**
|
|
183
191
|
* Transaction context.
|
|
184
192
|
* Has similar API than CommonDao, but all operations are performed in the context of the transaction.
|
|
185
193
|
*/
|
|
186
194
|
export declare class CommonDaoTransaction {
|
|
187
|
-
|
|
195
|
+
tx: DBTransaction;
|
|
188
196
|
private logger;
|
|
189
197
|
constructor(tx: DBTransaction, logger: CommonLogger);
|
|
190
198
|
/**
|
|
191
199
|
* Perform a graceful rollback without throwing/re-throwing any error.
|
|
192
200
|
*/
|
|
193
201
|
rollback(): Promise<void>;
|
|
194
|
-
getById<BM extends
|
|
195
|
-
getByIds<BM extends
|
|
196
|
-
save<BM extends
|
|
197
|
-
saveBatch<BM extends
|
|
202
|
+
getById<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM, any>, id?: string | null, opt?: CommonDaoOptions): Promise<Saved<BM> | null>;
|
|
203
|
+
getByIds<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM, any>, ids: string[], opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
|
|
204
|
+
save<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM, any>, bm: UnsavedId<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>;
|
|
205
|
+
saveBatch<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM, any>, bms: UnsavedId<BM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>[]>;
|
|
198
206
|
deleteById(dao: CommonDao<any>, id?: string | null, opt?: CommonDaoOptions): Promise<number>;
|
|
199
207
|
deleteByIds(dao: CommonDao<any>, ids: string[], opt?: CommonDaoOptions): Promise<number>;
|
|
200
208
|
}
|