@naturalcycles/db-lib 8.49.1 → 8.50.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.
- package/dist/adapter/cachedb/cache.db.d.ts +8 -8
- package/dist/adapter/cachedb/cache.db.js +2 -1
- package/dist/adapter/file/file.db.d.ts +8 -8
- package/dist/adapter/file/file.db.model.d.ts +1 -1
- package/dist/adapter/file/inMemory.persistence.plugin.d.ts +1 -1
- package/dist/adapter/file/localFile.persistence.plugin.d.ts +1 -1
- package/dist/adapter/file/noop.persistence.plugin.d.ts +1 -1
- package/dist/adapter/inmemory/inMemory.db.d.ts +8 -8
- package/dist/adapter/inmemory/queryInMemory.d.ts +1 -1
- package/dist/base.common.db.d.ts +8 -8
- package/dist/common.db.d.ts +8 -8
- package/dist/commondao/common.dao.d.ts +5 -16
- package/dist/commondao/common.dao.js +1 -81
- package/dist/commondao/common.dao.model.d.ts +11 -15
- package/dist/db.model.d.ts +1 -2
- package/dist/db.model.js +0 -1
- package/dist/query/dbQuery.d.ts +8 -10
- package/dist/query/dbQuery.js +0 -6
- package/dist/testing/daoTest.js +0 -1
- package/package.json +1 -1
- package/src/adapter/cachedb/cache.db.ts +13 -12
- package/src/adapter/file/file.db.model.ts +1 -1
- package/src/adapter/file/file.db.ts +12 -12
- package/src/adapter/file/inMemory.persistence.plugin.ts +1 -1
- package/src/adapter/file/localFile.persistence.plugin.ts +1 -1
- package/src/adapter/file/noop.persistence.plugin.ts +1 -1
- package/src/adapter/inmemory/inMemory.db.ts +11 -11
- package/src/adapter/inmemory/queryInMemory.ts +4 -1
- package/src/base.common.db.ts +11 -8
- package/src/common.db.ts +16 -8
- package/src/commondao/common.dao.model.ts +14 -20
- package/src/commondao/common.dao.ts +11 -117
- package/src/db.model.ts +0 -1
- package/src/query/dbQuery.ts +7 -17
- package/src/testing/daoTest.ts +0 -2
|
@@ -21,14 +21,14 @@ export declare class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
21
21
|
* Resets InMemory DB data
|
|
22
22
|
*/
|
|
23
23
|
getTables(): Promise<string[]>;
|
|
24
|
-
getTableSchema<ROW extends ObjectWithId
|
|
25
|
-
createTable<ROW extends ObjectWithId
|
|
26
|
-
getByIds<ROW extends ObjectWithId
|
|
24
|
+
getTableSchema<ROW extends Partial<ObjectWithId>>(table: string): Promise<JsonSchemaRootObject<ROW>>;
|
|
25
|
+
createTable<ROW extends Partial<ObjectWithId>>(table: string, schema: JsonSchemaObject<ROW>, opt?: CacheDBCreateOptions): Promise<void>;
|
|
26
|
+
getByIds<ROW extends Partial<ObjectWithId>>(table: string, ids: ROW['id'][], opt?: CacheDBSaveOptions<ROW>): Promise<ROW[]>;
|
|
27
27
|
saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CacheDBSaveOptions<ROW>): Promise<void>;
|
|
28
|
-
runQuery<ROW extends ObjectWithId
|
|
29
|
-
runQueryCount<ROW extends ObjectWithId
|
|
30
|
-
streamQuery<ROW extends ObjectWithId
|
|
31
|
-
deleteByQuery<ROW extends ObjectWithId
|
|
32
|
-
updateByQuery<ROW extends ObjectWithId
|
|
28
|
+
runQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, opt?: CacheDBSaveOptions<ROW>): Promise<RunQueryResult<ROW>>;
|
|
29
|
+
runQueryCount<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>;
|
|
30
|
+
streamQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, opt?: CacheDBStreamOptions): Readable;
|
|
31
|
+
deleteByQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>;
|
|
32
|
+
updateByQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CacheDBOptions): Promise<number>;
|
|
33
33
|
commitTransaction(tx: DBTransaction, opt?: CommonDBOptions): Promise<void>;
|
|
34
34
|
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CacheDB = void 0;
|
|
4
4
|
const node_stream_1 = require("node:stream");
|
|
5
|
+
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
6
|
const base_common_db_1 = require("../../base.common.db");
|
|
6
7
|
/**
|
|
7
8
|
* CommonDB implementation that proxies requests to downstream CommonDB
|
|
@@ -70,7 +71,7 @@ class CacheDB extends base_common_db_1.BaseCommonDB {
|
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
73
|
// return in right order
|
|
73
|
-
return ids.map(id => resultMap[id]).filter(
|
|
74
|
+
return ids.map(id => resultMap[id]).filter(js_lib_1._isTruthy);
|
|
74
75
|
}
|
|
75
76
|
async saveBatch(table, rows, opt = {}) {
|
|
76
77
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
@@ -21,19 +21,19 @@ 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
|
|
24
|
+
getByIds<ROW extends Partial<ObjectWithId>>(table: string, ids: ROW['id'][], _opt?: CommonDBOptions): Promise<ROW[]>;
|
|
25
25
|
saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], _opt?: CommonDBSaveOptions<ROW>): Promise<void>;
|
|
26
26
|
/**
|
|
27
27
|
* Implementation is optimized for loading/saving _whole files_.
|
|
28
28
|
*/
|
|
29
29
|
commitTransaction(tx: DBTransaction, _opt?: CommonDBOptions): Promise<void>;
|
|
30
|
-
runQuery<ROW extends ObjectWithId
|
|
31
|
-
runQueryCount<ROW extends ObjectWithId
|
|
32
|
-
streamQuery<ROW extends ObjectWithId
|
|
33
|
-
deleteByQuery<ROW extends ObjectWithId
|
|
34
|
-
getTableSchema<ROW extends ObjectWithId
|
|
35
|
-
loadFile<ROW extends ObjectWithId
|
|
36
|
-
saveFile<ROW extends ObjectWithId
|
|
30
|
+
runQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<RunQueryResult<ROW>>;
|
|
31
|
+
runQueryCount<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
|
|
32
|
+
streamQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, opt?: CommonDBStreamOptions): ReadableTyped<ROW>;
|
|
33
|
+
deleteByQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
|
|
34
|
+
getTableSchema<ROW extends Partial<ObjectWithId>>(table: string): Promise<JsonSchemaRootObject<ROW>>;
|
|
35
|
+
loadFile<ROW extends Partial<ObjectWithId>>(table: string): Promise<ROW[]>;
|
|
36
|
+
saveFile<ROW extends Partial<ObjectWithId>>(table: string, _rows: ROW[]): Promise<void>;
|
|
37
37
|
saveFiles<ROW extends ObjectWithId>(ops: DBSaveBatchOperation<ROW>[]): Promise<void>;
|
|
38
38
|
private sortRows;
|
|
39
39
|
private logStarted;
|
|
@@ -4,7 +4,7 @@ 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
|
|
7
|
+
loadFile<ROW extends Partial<ObjectWithId>>(table: string): Promise<ROW[]>;
|
|
8
8
|
saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void>;
|
|
9
9
|
}
|
|
10
10
|
export interface FileDBCfg {
|
|
@@ -8,6 +8,6 @@ export declare class InMemoryPersistencePlugin implements FileDBPersistencePlugi
|
|
|
8
8
|
data: StringMap<StringMap<ObjectWithId>>;
|
|
9
9
|
ping(): Promise<void>;
|
|
10
10
|
getTables(): Promise<string[]>;
|
|
11
|
-
loadFile<ROW extends ObjectWithId
|
|
11
|
+
loadFile<ROW extends Partial<ObjectWithId>>(table: string): Promise<ROW[]>;
|
|
12
12
|
saveFiles(ops: DBSaveBatchOperation[]): Promise<void>;
|
|
13
13
|
}
|
|
@@ -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
|
|
22
|
+
loadFile<ROW extends Partial<ObjectWithId>>(table: string): Promise<ROW[]>;
|
|
23
23
|
saveFiles(ops: DBSaveBatchOperation[]): Promise<void>;
|
|
24
24
|
saveFile<ROW extends ObjectWithId>(table: string, rows: ROW[]): Promise<void>;
|
|
25
25
|
}
|
|
@@ -4,6 +4,6 @@ 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
|
|
7
|
+
loadFile<ROW extends Partial<ObjectWithId>>(_table: string): Promise<ROW[]>;
|
|
8
8
|
saveFiles(_ops: DBSaveBatchOperation[]): Promise<void>;
|
|
9
9
|
}
|
|
@@ -50,15 +50,15 @@ export declare class InMemoryDB implements CommonDB {
|
|
|
50
50
|
*/
|
|
51
51
|
resetCache(_table?: string): Promise<void>;
|
|
52
52
|
getTables(): Promise<string[]>;
|
|
53
|
-
getTableSchema<ROW extends ObjectWithId
|
|
54
|
-
createTable<ROW extends ObjectWithId
|
|
55
|
-
getByIds<ROW extends ObjectWithId
|
|
53
|
+
getTableSchema<ROW extends Partial<ObjectWithId>>(_table: string): Promise<JsonSchemaRootObject<ROW>>;
|
|
54
|
+
createTable<ROW extends Partial<ObjectWithId>>(_table: string, _schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
|
|
55
|
+
getByIds<ROW extends Partial<ObjectWithId>>(_table: string, ids: ROW['id'][], _opt?: CommonDBOptions): Promise<ROW[]>;
|
|
56
56
|
saveBatch<ROW extends Partial<ObjectWithId>>(_table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
|
|
57
|
-
deleteByQuery<ROW extends ObjectWithId
|
|
58
|
-
updateByQuery<ROW extends ObjectWithId
|
|
59
|
-
runQuery<ROW extends ObjectWithId
|
|
60
|
-
runQueryCount<ROW extends ObjectWithId
|
|
61
|
-
streamQuery<ROW extends ObjectWithId
|
|
57
|
+
deleteByQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
|
|
58
|
+
updateByQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, patch: DBPatch<ROW>): Promise<number>;
|
|
59
|
+
runQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<RunQueryResult<ROW>>;
|
|
60
|
+
runQueryCount<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
|
|
61
|
+
streamQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, _opt?: CommonDBOptions): ReadableTyped<ROW>;
|
|
62
62
|
commitTransaction(tx: DBTransaction, opt?: CommonDBOptions): Promise<void>;
|
|
63
63
|
/**
|
|
64
64
|
* Flushes all tables (all namespaces) at once.
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { ObjectWithId } from '@naturalcycles/js-lib';
|
|
2
2
|
import { DBQuery } from '../../query/dbQuery';
|
|
3
|
-
export declare function queryInMemory<ROW extends ObjectWithId
|
|
3
|
+
export declare function queryInMemory<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, rows?: ROW[]): ROW[];
|
package/dist/base.common.db.d.ts
CHANGED
|
@@ -11,15 +11,15 @@ import { DBTransaction } from './transaction/dbTransaction';
|
|
|
11
11
|
export declare class BaseCommonDB implements CommonDB {
|
|
12
12
|
ping(): Promise<void>;
|
|
13
13
|
getTables(): Promise<string[]>;
|
|
14
|
-
getTableSchema<ROW extends ObjectWithId
|
|
15
|
-
createTable<ROW extends ObjectWithId
|
|
16
|
-
getByIds<ROW extends ObjectWithId
|
|
17
|
-
deleteByQuery<ROW extends ObjectWithId
|
|
18
|
-
updateByQuery<ROW extends ObjectWithId
|
|
19
|
-
runQuery<ROW extends ObjectWithId
|
|
20
|
-
runQueryCount<ROW extends ObjectWithId
|
|
14
|
+
getTableSchema<ROW extends Partial<ObjectWithId>>(table: string): Promise<JsonSchemaRootObject<ROW>>;
|
|
15
|
+
createTable<ROW extends Partial<ObjectWithId>>(table: string, schema: JsonSchemaObject<ROW>): Promise<void>;
|
|
16
|
+
getByIds<ROW extends Partial<ObjectWithId>>(table: string, ids: ROW['id'][]): Promise<ROW[]>;
|
|
17
|
+
deleteByQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>): Promise<number>;
|
|
18
|
+
updateByQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CommonDBOptions): Promise<number>;
|
|
19
|
+
runQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>): Promise<RunQueryResult<ROW>>;
|
|
20
|
+
runQueryCount<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>): Promise<number>;
|
|
21
21
|
saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
|
|
22
|
-
streamQuery<ROW extends ObjectWithId
|
|
22
|
+
streamQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>): ReadableTyped<ROW>;
|
|
23
23
|
/**
|
|
24
24
|
* Naive implementation.
|
|
25
25
|
* Doesn't support rollback on error, hence doesn't pass dbTest.
|
package/dist/common.db.d.ts
CHANGED
|
@@ -21,23 +21,23 @@ export interface CommonDB {
|
|
|
21
21
|
*
|
|
22
22
|
* This is important for the code to rely on it, and it's verified by dbTest
|
|
23
23
|
*/
|
|
24
|
-
getTableSchema<ROW extends ObjectWithId
|
|
24
|
+
getTableSchema<ROW extends Partial<ObjectWithId>>(table: string): Promise<JsonSchemaRootObject<ROW>>;
|
|
25
25
|
/**
|
|
26
26
|
* Will do like `create table ...` for mysql.
|
|
27
27
|
* Caution! dropIfExists defaults to false. If set to true - will actually DROP the table!
|
|
28
28
|
*/
|
|
29
|
-
createTable<ROW extends ObjectWithId
|
|
29
|
+
createTable<ROW extends Partial<ObjectWithId>>(table: string, schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
|
|
30
30
|
/**
|
|
31
31
|
* Order of items returned is not guaranteed to match order of ids.
|
|
32
32
|
* (Such limitation exists because Datastore doesn't support it).
|
|
33
33
|
*/
|
|
34
|
-
getByIds<ROW extends ObjectWithId
|
|
34
|
+
getByIds<ROW extends Partial<ObjectWithId>>(table: string, ids: ROW['id'][], opt?: CommonDBOptions): Promise<ROW[]>;
|
|
35
35
|
/**
|
|
36
36
|
* Order by 'id' is not supported by all implementations (for example, Datastore doesn't support it).
|
|
37
37
|
*/
|
|
38
|
-
runQuery<ROW extends ObjectWithId
|
|
39
|
-
runQueryCount<ROW extends ObjectWithId
|
|
40
|
-
streamQuery<ROW extends ObjectWithId
|
|
38
|
+
runQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, opt?: CommonDBOptions): Promise<RunQueryResult<ROW>>;
|
|
39
|
+
runQueryCount<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, opt?: CommonDBOptions): Promise<number>;
|
|
40
|
+
streamQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, opt?: CommonDBStreamOptions): ReadableTyped<ROW>;
|
|
41
41
|
/**
|
|
42
42
|
* rows can have missing ids only if DB supports auto-generating them (like mysql auto_increment).
|
|
43
43
|
*/
|
|
@@ -46,7 +46,7 @@ export interface CommonDB {
|
|
|
46
46
|
* Returns number of deleted items.
|
|
47
47
|
* Not supported by all implementations (e.g Datastore will always return same number as number of ids).
|
|
48
48
|
*/
|
|
49
|
-
deleteByQuery<ROW extends ObjectWithId
|
|
49
|
+
deleteByQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, opt?: CommonDBOptions): Promise<number>;
|
|
50
50
|
/**
|
|
51
51
|
* Applies patch to the rows returned by the query.
|
|
52
52
|
*
|
|
@@ -65,7 +65,7 @@ export interface CommonDB {
|
|
|
65
65
|
*
|
|
66
66
|
* Returns number of rows affected.
|
|
67
67
|
*/
|
|
68
|
-
updateByQuery<ROW extends ObjectWithId
|
|
68
|
+
updateByQuery<ROW extends Partial<ObjectWithId>>(q: DBQuery<ROW>, patch: DBPatch<ROW>, opt?: CommonDBOptions): Promise<number>;
|
|
69
69
|
/**
|
|
70
70
|
* Should be implemented as a Transaction (best effort), which means that
|
|
71
71
|
* either ALL or NONE of the operations should be applied.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AsyncMapper, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, Saved, Unsaved } from '@naturalcycles/js-lib';
|
|
2
2
|
import { AjvSchema, ObjectSchemaTyped, ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { DBDeleteByIdsOperation, DBModelType, DBOperation, DBPatch, DBSaveBatchOperation, RunQueryResult } from '../db.model';
|
|
4
4
|
import { DBQuery, RunnableDBQuery } from '../query/dbQuery';
|
|
@@ -8,11 +8,10 @@ import { CommonDaoCfg, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoSaveOp
|
|
|
8
8
|
*
|
|
9
9
|
* DBM = Database model (how it's stored in DB)
|
|
10
10
|
* BM = Backend model (optimized for API access)
|
|
11
|
-
* TM = Transport model (optimized to be sent over the wire)
|
|
12
11
|
*/
|
|
13
|
-
export declare class CommonDao<BM extends Partial<ObjectWithId<ID>>, DBM extends ObjectWithId<ID
|
|
14
|
-
cfg: CommonDaoCfg<BM, DBM,
|
|
15
|
-
constructor(cfg: CommonDaoCfg<BM, DBM,
|
|
12
|
+
export declare class CommonDao<BM extends Partial<ObjectWithId<ID>>, DBM extends Partial<ObjectWithId<ID>> = BM, ID extends string | number = NonNullable<BM['id']>> {
|
|
13
|
+
cfg: CommonDaoCfg<BM, DBM, ID>;
|
|
14
|
+
constructor(cfg: CommonDaoCfg<BM, DBM, ID>);
|
|
16
15
|
create(part?: Partial<BM>, opt?: CommonDaoOptions): Saved<BM>;
|
|
17
16
|
getById(id: undefined | null, opt?: CommonDaoOptions): Promise<null>;
|
|
18
17
|
getById(id?: ID | null, opt?: CommonDaoOptions): Promise<Saved<BM> | null>;
|
|
@@ -20,8 +19,6 @@ export declare class CommonDao<BM extends Partial<ObjectWithId<ID>>, DBM extends
|
|
|
20
19
|
getByIdAsDBMOrEmpty(id: ID, part?: Partial<BM>, opt?: CommonDaoOptions): Promise<DBM>;
|
|
21
20
|
getByIdAsDBM(id: undefined | null, opt?: CommonDaoOptions): Promise<null>;
|
|
22
21
|
getByIdAsDBM(id?: ID | null, opt?: CommonDaoOptions): Promise<DBM | null>;
|
|
23
|
-
getByIdAsTM(id: undefined | null, opt?: CommonDaoOptions): Promise<null>;
|
|
24
|
-
getByIdAsTM(id?: ID | null, opt?: CommonDaoOptions): Promise<TM | null>;
|
|
25
22
|
getByIds(ids: ID[], opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
|
|
26
23
|
getByIdsAsDBM(ids: ID[], opt?: CommonDaoOptions): Promise<DBM[]>;
|
|
27
24
|
requireById(id: ID, opt?: CommonDaoOptions): Promise<Saved<BM>>;
|
|
@@ -42,7 +39,7 @@ export declare class CommonDao<BM extends Partial<ObjectWithId<ID>>, DBM extends
|
|
|
42
39
|
/**
|
|
43
40
|
* Pass `table` to override table
|
|
44
41
|
*/
|
|
45
|
-
query(table?: string): RunnableDBQuery<BM, DBM,
|
|
42
|
+
query(table?: string): RunnableDBQuery<BM, DBM, ID>;
|
|
46
43
|
runQuery(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
|
|
47
44
|
runQuerySingleColumn<T = any>(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<T[]>;
|
|
48
45
|
/**
|
|
@@ -54,8 +51,6 @@ export declare class CommonDao<BM extends Partial<ObjectWithId<ID>>, DBM extends
|
|
|
54
51
|
runQueryExtended(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<Saved<BM>>>;
|
|
55
52
|
runQueryAsDBM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<DBM[]>;
|
|
56
53
|
runQueryExtendedAsDBM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<DBM>>;
|
|
57
|
-
runQueryAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<TM[]>;
|
|
58
|
-
runQueryExtendedAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<TM>>;
|
|
59
54
|
runQueryCount(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<number>;
|
|
60
55
|
streamQueryForEach(q: DBQuery<DBM>, mapper: AsyncMapper<Saved<BM>, void>, opt?: CommonDaoStreamForEachOptions<Saved<BM>>): Promise<void>;
|
|
61
56
|
streamQueryAsDBMForEach(q: DBQuery<DBM>, mapper: AsyncMapper<DBM, void>, opt?: CommonDaoStreamForEachOptions<DBM>): Promise<void>;
|
|
@@ -136,12 +131,6 @@ export declare class CommonDao<BM extends Partial<ObjectWithId<ID>>, DBM extends
|
|
|
136
131
|
anyToDBM(dbm: undefined, opt?: CommonDaoOptions): undefined;
|
|
137
132
|
anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM;
|
|
138
133
|
anyToDBMs(entities: DBM[], opt?: CommonDaoOptions): DBM[];
|
|
139
|
-
bmToTM(bm: undefined, opt?: CommonDaoOptions): TM | undefined;
|
|
140
|
-
bmToTM(bm?: Saved<BM>, opt?: CommonDaoOptions): TM;
|
|
141
|
-
bmsToTM(bms: Saved<BM>[], opt?: CommonDaoOptions): TM[];
|
|
142
|
-
tmToBM(tm: undefined, opt?: CommonDaoOptions): undefined;
|
|
143
|
-
tmToBM(tm?: TM, opt?: CommonDaoOptions): BM;
|
|
144
|
-
tmsToBM(tms: TM[], opt?: CommonDaoOptions): BM[];
|
|
145
134
|
/**
|
|
146
135
|
* Returns *converted value*.
|
|
147
136
|
* Validates (unless `skipValidation=true` passed).
|
|
@@ -15,7 +15,6 @@ const isCI = !!process.env['CI'];
|
|
|
15
15
|
*
|
|
16
16
|
* DBM = Database model (how it's stored in DB)
|
|
17
17
|
* BM = Backend model (optimized for API access)
|
|
18
|
-
* TM = Transport model (optimized to be sent over the wire)
|
|
19
18
|
*/
|
|
20
19
|
class CommonDao {
|
|
21
20
|
constructor(cfg) {
|
|
@@ -80,8 +79,6 @@ class CommonDao {
|
|
|
80
79
|
beforeDBMValidate: dbm => dbm,
|
|
81
80
|
beforeDBMToBM: dbm => dbm,
|
|
82
81
|
beforeBMToDBM: bm => bm,
|
|
83
|
-
beforeTMToBM: tm => tm,
|
|
84
|
-
beforeBMToTM: bm => bm,
|
|
85
82
|
anonymize: dbm => dbm,
|
|
86
83
|
onValidationError: err => err,
|
|
87
84
|
...cfg.hooks,
|
|
@@ -108,19 +105,7 @@ class CommonDao {
|
|
|
108
105
|
const op = `getById(${id})`;
|
|
109
106
|
const table = opt.table || this.cfg.table;
|
|
110
107
|
const started = this.logStarted(op, table);
|
|
111
|
-
|
|
112
|
-
if (opt.timeout) {
|
|
113
|
-
// todo: possibly remove it after debugging is done
|
|
114
|
-
dbm = await (0, js_lib_1.pTimeout)(async () => {
|
|
115
|
-
return (await this.cfg.db.getByIds(table, [id]))[0];
|
|
116
|
-
}, {
|
|
117
|
-
timeout: opt.timeout,
|
|
118
|
-
name: `getById(${table})`,
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
dbm = (await this.cfg.db.getByIds(table, [id]))[0];
|
|
123
|
-
}
|
|
108
|
+
const dbm = (await this.cfg.db.getByIds(table, [id]))[0];
|
|
124
109
|
const bm = opt.raw ? dbm : await this.dbmToBM(dbm, opt);
|
|
125
110
|
this.logResult(started, op, bm, table);
|
|
126
111
|
return bm || null;
|
|
@@ -151,22 +136,6 @@ class CommonDao {
|
|
|
151
136
|
this.logResult(started, op, dbm, table);
|
|
152
137
|
return dbm || null;
|
|
153
138
|
}
|
|
154
|
-
async getByIdAsTM(id, opt = {}) {
|
|
155
|
-
if (!id)
|
|
156
|
-
return null;
|
|
157
|
-
const op = `getByIdAsTM(${id})`;
|
|
158
|
-
const table = opt.table || this.cfg.table;
|
|
159
|
-
const started = this.logStarted(op, table);
|
|
160
|
-
const [dbm] = await this.cfg.db.getByIds(table, [id]);
|
|
161
|
-
if (opt.raw) {
|
|
162
|
-
this.logResult(started, op, dbm, table);
|
|
163
|
-
return dbm || null;
|
|
164
|
-
}
|
|
165
|
-
const bm = await this.dbmToBM(dbm, opt);
|
|
166
|
-
const tm = this.bmToTM(bm, opt);
|
|
167
|
-
this.logResult(started, op, tm, table);
|
|
168
|
-
return tm || null;
|
|
169
|
-
}
|
|
170
139
|
async getByIds(ids, opt = {}) {
|
|
171
140
|
const op = `getByIds ${ids.length} id(s) (${(0, js_lib_1._truncate)(ids.slice(0, 10).join(', '), 50)})`;
|
|
172
141
|
const table = opt.table || this.cfg.table;
|
|
@@ -302,23 +271,6 @@ class CommonDao {
|
|
|
302
271
|
this.logResult(started, op, dbms, q.table);
|
|
303
272
|
return { rows: dbms, ...queryResult };
|
|
304
273
|
}
|
|
305
|
-
async runQueryAsTM(q, opt) {
|
|
306
|
-
const { rows } = await this.runQueryExtendedAsTM(q, opt);
|
|
307
|
-
return rows;
|
|
308
|
-
}
|
|
309
|
-
async runQueryExtendedAsTM(q, opt = {}) {
|
|
310
|
-
q.table = opt.table || q.table;
|
|
311
|
-
const op = `runQueryAsTM(${q.pretty()})`;
|
|
312
|
-
const started = this.logStarted(op, q.table);
|
|
313
|
-
const { rows, ...queryResult } = await this.cfg.db.runQuery(q, opt);
|
|
314
|
-
const partialQuery = !!q._selectedFieldNames;
|
|
315
|
-
const tms = partialQuery || opt.raw ? rows : this.bmsToTM(await this.dbmsToBM(rows, opt), opt);
|
|
316
|
-
this.logResult(started, op, tms, q.table);
|
|
317
|
-
return {
|
|
318
|
-
rows: tms,
|
|
319
|
-
...queryResult,
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
274
|
async runQueryCount(q, opt = {}) {
|
|
323
275
|
q.table = opt.table || q.table;
|
|
324
276
|
const op = `runQueryCount(${q.pretty()})`;
|
|
@@ -780,38 +732,6 @@ class CommonDao {
|
|
|
780
732
|
anyToDBMs(entities, opt = {}) {
|
|
781
733
|
return entities.map(entity => this.anyToDBM(entity, opt));
|
|
782
734
|
}
|
|
783
|
-
bmToTM(bm, opt) {
|
|
784
|
-
if (bm === undefined)
|
|
785
|
-
return;
|
|
786
|
-
// optimization: 1 validation is enough
|
|
787
|
-
// Validate/convert BM
|
|
788
|
-
// bm gets assigned to the new reference
|
|
789
|
-
// bm = this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
|
|
790
|
-
// BM > TM
|
|
791
|
-
const tm = this.cfg.hooks.beforeBMToTM(bm);
|
|
792
|
-
// Validate/convert DBM
|
|
793
|
-
return this.validateAndConvert(tm, this.cfg.tmSchema, db_model_1.DBModelType.TM, opt);
|
|
794
|
-
}
|
|
795
|
-
bmsToTM(bms, opt = {}) {
|
|
796
|
-
// try/catch?
|
|
797
|
-
return bms.map(bm => this.bmToTM(bm, opt));
|
|
798
|
-
}
|
|
799
|
-
tmToBM(tm, opt = {}) {
|
|
800
|
-
if (!tm)
|
|
801
|
-
return;
|
|
802
|
-
// optimization: 1 validation is enough
|
|
803
|
-
// Validate/convert TM
|
|
804
|
-
// bm gets assigned to the new reference
|
|
805
|
-
// tm = this.validateAndConvert(tm, this.cfg.tmSchema, DBModelType.TM, opt)
|
|
806
|
-
// TM > BM
|
|
807
|
-
const bm = this.cfg.hooks.beforeTMToBM(tm);
|
|
808
|
-
// Validate/convert BM
|
|
809
|
-
return this.validateAndConvert(bm, this.cfg.bmSchema, db_model_1.DBModelType.BM, opt);
|
|
810
|
-
}
|
|
811
|
-
tmsToBM(tms, opt = {}) {
|
|
812
|
-
// try/catch?
|
|
813
|
-
return tms.map(tm => this.tmToBM(tm, opt));
|
|
814
|
-
}
|
|
815
735
|
/**
|
|
816
736
|
* Returns *converted value*.
|
|
817
737
|
* Validates (unless `skipValidation=true` passed).
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { CommonLogger, ErrorMode, ObjectWithId
|
|
1
|
+
import { CommonLogger, ErrorMode, ObjectWithId } from '@naturalcycles/js-lib';
|
|
2
2
|
import { AjvSchema, AjvValidationError, JoiValidationError, ObjectSchemaTyped, 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<ID>>, DBM extends ObjectWithId<ID
|
|
5
|
+
export interface CommonDaoHooks<BM extends Partial<ObjectWithId<ID>>, DBM extends Partial<ObjectWithId<ID>> = BM, ID extends string | number = NonNullable<BM['id']>> {
|
|
6
6
|
createRandomId(): ID;
|
|
7
7
|
/**
|
|
8
8
|
* createNaturalId hook is called (tried) first.
|
|
@@ -14,8 +14,9 @@ export interface CommonDaoHooks<BM extends Partial<ObjectWithId<ID>>, DBM extend
|
|
|
14
14
|
beforeDBMValidate(dbm: Partial<DBM>): Partial<DBM>;
|
|
15
15
|
beforeDBMToBM(dbm: DBM): Partial<BM> | Promise<Partial<BM>>;
|
|
16
16
|
beforeBMToDBM(bm: BM): Partial<DBM> | Promise<Partial<DBM>>;
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Applicable to **read** operations, never to **save** operations.
|
|
19
|
+
*/
|
|
19
20
|
anonymize(dbm: DBM): DBM;
|
|
20
21
|
/**
|
|
21
22
|
* If hook is defined - allows to prevent or modify the error thrown.
|
|
@@ -43,7 +44,7 @@ export declare enum CommonDaoLogLevel {
|
|
|
43
44
|
*/
|
|
44
45
|
DATA_FULL = 30
|
|
45
46
|
}
|
|
46
|
-
export interface CommonDaoCfg<BM extends Partial<ObjectWithId<ID>>, DBM extends ObjectWithId<ID
|
|
47
|
+
export interface CommonDaoCfg<BM extends Partial<ObjectWithId<ID>>, DBM extends Partial<ObjectWithId<ID>> = BM, ID extends string | number = NonNullable<BM['id']>> {
|
|
47
48
|
db: CommonDB;
|
|
48
49
|
table: string;
|
|
49
50
|
/**
|
|
@@ -51,7 +52,6 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId<ID>>, DBM extends
|
|
|
51
52
|
*/
|
|
52
53
|
dbmSchema?: ObjectSchemaTyped<DBM> | AjvSchema<DBM>;
|
|
53
54
|
bmSchema?: ObjectSchemaTyped<BM> | AjvSchema<BM>;
|
|
54
|
-
tmSchema?: ObjectSchemaTyped<TM> | AjvSchema<TM>;
|
|
55
55
|
excludeFromIndexes?: (keyof DBM)[];
|
|
56
56
|
/**
|
|
57
57
|
* Defaults to false.
|
|
@@ -61,6 +61,8 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId<ID>>, DBM extends
|
|
|
61
61
|
* `delete*` and `patch` will throw.
|
|
62
62
|
*
|
|
63
63
|
* You can still override saveMethod, or set opt.allowMutability to allow deletion.
|
|
64
|
+
*
|
|
65
|
+
* todo: consider merging it with readOnly, as it's almost the same
|
|
64
66
|
*/
|
|
65
67
|
immutable?: boolean;
|
|
66
68
|
/**
|
|
@@ -80,7 +82,7 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId<ID>>, DBM extends
|
|
|
80
82
|
* @default false
|
|
81
83
|
*/
|
|
82
84
|
logStarted?: boolean;
|
|
83
|
-
hooks?: Partial<CommonDaoHooks<BM, DBM,
|
|
85
|
+
hooks?: Partial<CommonDaoHooks<BM, DBM, ID>>;
|
|
84
86
|
/**
|
|
85
87
|
* Defaults to 'string'
|
|
86
88
|
*/
|
|
@@ -94,6 +96,7 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId<ID>>, DBM extends
|
|
|
94
96
|
/**
|
|
95
97
|
* See the same option in CommonDB.
|
|
96
98
|
* Defaults to false normally.
|
|
99
|
+
* "Generated" means "generated by the underlying DB" (e.g MySQL autoincrement).
|
|
97
100
|
*/
|
|
98
101
|
assignGeneratedIds?: boolean;
|
|
99
102
|
/**
|
|
@@ -165,13 +168,6 @@ export interface CommonDaoOptions extends CommonDBOptions {
|
|
|
165
168
|
* Useful e.g in AirtableDB where you can have one Dao to control multiple tables.
|
|
166
169
|
*/
|
|
167
170
|
table?: string;
|
|
168
|
-
/**
|
|
169
|
-
* If set - wraps the method in `pTimeout` with a timeout of given number of milliseconds.
|
|
170
|
-
* Currently, it is only used to debug an ongoing GCP infra issue.
|
|
171
|
-
*
|
|
172
|
-
* @experimental
|
|
173
|
-
*/
|
|
174
|
-
timeout?: number;
|
|
175
171
|
/**
|
|
176
172
|
* If passed - operation will not be performed immediately, but instead "added" to the transaction.
|
|
177
173
|
* In the end - transaction needs to be committed (by calling `commit`).
|
|
@@ -186,7 +182,7 @@ export interface CommonDaoOptions extends CommonDBOptions {
|
|
|
186
182
|
/**
|
|
187
183
|
* All properties default to undefined.
|
|
188
184
|
*/
|
|
189
|
-
export interface CommonDaoSaveOptions<DBM extends ObjectWithId
|
|
185
|
+
export interface CommonDaoSaveOptions<DBM extends Partial<ObjectWithId>> extends CommonDaoOptions, CommonDBSaveOptions<DBM> {
|
|
190
186
|
/**
|
|
191
187
|
* @default false
|
|
192
188
|
*
|
package/dist/db.model.d.ts
CHANGED
package/dist/db.model.js
CHANGED
package/dist/query/dbQuery.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AnyObjectWithId, ObjectWithId, AsyncMapper, Saved
|
|
1
|
+
import { AnyObjectWithId, ObjectWithId, AsyncMapper, Saved } from '@naturalcycles/js-lib';
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { CommonDaoOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, DBPatch } from '..';
|
|
4
4
|
import { CommonDao } from '../commondao/common.dao';
|
|
@@ -24,12 +24,12 @@ import { RunQueryResult } from '../db.model';
|
|
|
24
24
|
*/
|
|
25
25
|
export type DBQueryFilterOperator = '<' | '<=' | '==' | '!=' | '>=' | '>' | 'in' | 'not-in' | 'array-contains' | 'array-contains-any';
|
|
26
26
|
export declare const dbQueryFilterOperatorValues: DBQueryFilterOperator[];
|
|
27
|
-
export interface DBQueryFilter<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
27
|
+
export interface DBQueryFilter<ROW extends Partial<ObjectWithId> = AnyObjectWithId> {
|
|
28
28
|
name: keyof ROW;
|
|
29
29
|
op: DBQueryFilterOperator;
|
|
30
30
|
val: any;
|
|
31
31
|
}
|
|
32
|
-
export interface DBQueryOrder<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
32
|
+
export interface DBQueryOrder<ROW extends Partial<ObjectWithId> = AnyObjectWithId> {
|
|
33
33
|
name: keyof ROW;
|
|
34
34
|
descending?: boolean;
|
|
35
35
|
}
|
|
@@ -43,14 +43,14 @@ export interface DBQueryOrder<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
|
43
43
|
*
|
|
44
44
|
* <DBM> is the type of **queried** object (so e.g `key of DBM` can be used), not **returned** object.
|
|
45
45
|
*/
|
|
46
|
-
export declare class DBQuery<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
46
|
+
export declare class DBQuery<ROW extends Partial<ObjectWithId> = AnyObjectWithId> {
|
|
47
47
|
table: string;
|
|
48
48
|
constructor(table: string);
|
|
49
49
|
/**
|
|
50
50
|
* Convenience method.
|
|
51
51
|
*/
|
|
52
52
|
static create<ROW extends ObjectWithId = AnyObjectWithId>(table: string): DBQuery<ROW>;
|
|
53
|
-
static fromPlainObject<ROW extends ObjectWithId = AnyObjectWithId>(obj: Partial<DBQuery<ROW>> & {
|
|
53
|
+
static fromPlainObject<ROW extends Partial<ObjectWithId> = AnyObjectWithId>(obj: Partial<DBQuery<ROW>> & {
|
|
54
54
|
table: string;
|
|
55
55
|
}): DBQuery<ROW>;
|
|
56
56
|
_filters: DBQueryFilter<ROW>[];
|
|
@@ -84,19 +84,17 @@ export declare class DBQuery<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
|
84
84
|
/**
|
|
85
85
|
* DBQuery that has additional method to support Fluent API style.
|
|
86
86
|
*/
|
|
87
|
-
export declare class RunnableDBQuery<BM extends Partial<ObjectWithId<ID>>, DBM extends ObjectWithId<ID
|
|
88
|
-
dao: CommonDao<BM, DBM,
|
|
87
|
+
export declare class RunnableDBQuery<BM extends Partial<ObjectWithId<ID>>, DBM extends Partial<ObjectWithId<ID>> = BM, ID extends string | number = NonNullable<BM['id']>> extends DBQuery<DBM> {
|
|
88
|
+
dao: CommonDao<BM, DBM, ID>;
|
|
89
89
|
/**
|
|
90
90
|
* Pass `table` to override table.
|
|
91
91
|
*/
|
|
92
|
-
constructor(dao: CommonDao<BM, DBM,
|
|
92
|
+
constructor(dao: CommonDao<BM, DBM, ID>, table?: string);
|
|
93
93
|
runQuery(opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
|
|
94
94
|
runQuerySingleColumn<T = any>(opt?: CommonDaoOptions): Promise<T[]>;
|
|
95
95
|
runQueryAsDBM(opt?: CommonDaoOptions): Promise<DBM[]>;
|
|
96
|
-
runQueryAsTM(opt?: CommonDaoOptions): Promise<TM[]>;
|
|
97
96
|
runQueryExtended(opt?: CommonDaoOptions): Promise<RunQueryResult<Saved<BM>>>;
|
|
98
97
|
runQueryExtendedAsDBM(opt?: CommonDaoOptions): Promise<RunQueryResult<DBM>>;
|
|
99
|
-
runQueryExtendedAsTM(opt?: CommonDaoOptions): Promise<RunQueryResult<TM>>;
|
|
100
98
|
runQueryCount(opt?: CommonDaoOptions): Promise<number>;
|
|
101
99
|
updateByQuery(patch: DBPatch<DBM>, opt?: CommonDaoOptions): Promise<number>;
|
|
102
100
|
streamQueryForEach(mapper: AsyncMapper<Saved<BM>, void>, opt?: CommonDaoStreamForEachOptions<Saved<BM>>): Promise<void>;
|
package/dist/query/dbQuery.js
CHANGED
|
@@ -153,18 +153,12 @@ class RunnableDBQuery extends DBQuery {
|
|
|
153
153
|
async runQueryAsDBM(opt) {
|
|
154
154
|
return await this.dao.runQueryAsDBM(this, opt);
|
|
155
155
|
}
|
|
156
|
-
async runQueryAsTM(opt) {
|
|
157
|
-
return await this.dao.runQueryAsTM(this, opt);
|
|
158
|
-
}
|
|
159
156
|
async runQueryExtended(opt) {
|
|
160
157
|
return await this.dao.runQueryExtended(this, opt);
|
|
161
158
|
}
|
|
162
159
|
async runQueryExtendedAsDBM(opt) {
|
|
163
160
|
return await this.dao.runQueryExtendedAsDBM(this, opt);
|
|
164
161
|
}
|
|
165
|
-
async runQueryExtendedAsTM(opt) {
|
|
166
|
-
return await this.dao.runQueryExtendedAsTM(this, opt);
|
|
167
|
-
}
|
|
168
162
|
async runQueryCount(opt) {
|
|
169
163
|
return await this.dao.runQueryCount(this, opt);
|
|
170
164
|
}
|
package/dist/testing/daoTest.js
CHANGED
|
@@ -13,7 +13,6 @@ function runCommonDaoTest(db, features = {}, quirks = {}) {
|
|
|
13
13
|
db,
|
|
14
14
|
dbmSchema: test_model_1.testItemDBMSchema,
|
|
15
15
|
bmSchema: test_model_1.testItemBMSchema,
|
|
16
|
-
tmSchema: test_model_1.testItemTMSchema,
|
|
17
16
|
logStarted: true,
|
|
18
17
|
logLevel: __1.CommonDaoLogLevel.DATA_FULL,
|
|
19
18
|
});
|