@naturalcycles/db-lib 10.18.0 → 10.20.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/commondao/common.dao.d.ts +64 -71
- package/dist/commondao/common.dao.js +207 -466
- package/dist/commondao/common.dao.model.d.ts +1 -27
- package/dist/commondao/commonDaoTransaction.d.ts +38 -0
- package/dist/commondao/commonDaoTransaction.js +78 -0
- package/dist/commondb/base.common.db.d.ts +3 -3
- package/dist/commondb/base.common.db.js +3 -3
- package/dist/commondb/common.db.d.ts +7 -11
- package/dist/inmemory/inMemory.db.d.ts +3 -3
- package/dist/inmemory/inMemory.db.js +3 -3
- package/dist/query/dbQuery.d.ts +1 -1
- package/dist/testing/commonDaoTest.js +0 -3
- package/package.json +1 -1
- package/src/commondao/common.dao.model.ts +5 -26
- package/src/commondao/common.dao.ts +302 -562
- package/src/commondao/commonDaoTransaction.ts +124 -0
- package/src/commondb/base.common.db.ts +3 -3
- package/src/commondb/common.db.ts +8 -13
- package/src/inmemory/inMemory.db.ts +3 -3
- package/src/query/dbQuery.ts +1 -1
- package/src/testing/commonDaoTest.ts +0 -3
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { Transform } from 'node:stream';
|
|
2
2
|
import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
|
|
3
|
-
import type
|
|
4
|
-
import type { AsyncIndexedMapper, BaseDBEntity, NonNegativeInteger, StringMap, UnixTimestampMillis, Unsaved } from '@naturalcycles/js-lib/types';
|
|
3
|
+
import { type AsyncIndexedMapper, type BaseDBEntity, type NonNegativeInteger, type StringMap, type Unsaved } from '@naturalcycles/js-lib/types';
|
|
5
4
|
import { type ReadableTyped } from '@naturalcycles/nodejs-lib/stream';
|
|
6
|
-
import type { CommonDBTransactionOptions,
|
|
5
|
+
import type { CommonDBTransactionOptions, RunQueryResult } from '../db.model.js';
|
|
7
6
|
import type { DBQuery } from '../query/dbQuery.js';
|
|
8
7
|
import { RunnableDBQuery } from '../query/dbQuery.js';
|
|
9
|
-
import type { CommonDaoCfg, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoPatchByIdOptions, CommonDaoPatchOptions, CommonDaoReadOptions, CommonDaoSaveBatchOptions, CommonDaoSaveOptions, CommonDaoStreamDeleteOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, CommonDaoStreamSaveOptions
|
|
8
|
+
import type { CommonDaoCfg, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoPatchByIdOptions, CommonDaoPatchOptions, CommonDaoReadOptions, CommonDaoSaveBatchOptions, CommonDaoSaveOptions, CommonDaoStreamDeleteOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, CommonDaoStreamSaveOptions } from './common.dao.model.js';
|
|
9
|
+
import { CommonDaoTransaction } from './commonDaoTransaction.js';
|
|
10
10
|
/**
|
|
11
11
|
* Lowest common denominator API between supported Databases.
|
|
12
12
|
*
|
|
@@ -14,27 +14,18 @@ import type { CommonDaoCfg, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoP
|
|
|
14
14
|
* BM = Backend model (optimized for API access)
|
|
15
15
|
* TM = Transport model (optimized to be sent over the wire)
|
|
16
16
|
*/
|
|
17
|
-
export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, ID = BM['id']> {
|
|
17
|
+
export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, ID extends string = BM['id']> {
|
|
18
18
|
cfg: CommonDaoCfg<BM, DBM, ID>;
|
|
19
19
|
constructor(cfg: CommonDaoCfg<BM, DBM, ID>);
|
|
20
20
|
create(part?: Partial<BM>, opt?: CommonDaoOptions): BM;
|
|
21
|
-
|
|
21
|
+
requireById(id: ID, opt?: CommonDaoReadOptions): Promise<BM>;
|
|
22
|
+
requireByIdAsDBM(id: ID, opt?: CommonDaoReadOptions): Promise<DBM>;
|
|
22
23
|
getByIdOrEmpty(id: ID, part?: Partial<BM>, opt?: CommonDaoReadOptions): Promise<BM>;
|
|
24
|
+
getById(id?: ID | null, opt?: CommonDaoReadOptions): Promise<BM | null>;
|
|
23
25
|
getByIdAsDBM(id?: ID | null, opt?: CommonDaoReadOptions): Promise<DBM | null>;
|
|
24
26
|
getByIds(ids: ID[], opt?: CommonDaoReadOptions): Promise<BM[]>;
|
|
25
27
|
getByIdsAsDBM(ids: ID[], opt?: CommonDaoReadOptions): Promise<DBM[]>;
|
|
26
|
-
|
|
27
|
-
requireByIdAsDBM(id: ID, opt?: CommonDaoReadOptions): Promise<DBM>;
|
|
28
|
-
private throwRequiredError;
|
|
29
|
-
/**
|
|
30
|
-
* Throws if readOnly is true
|
|
31
|
-
*/
|
|
32
|
-
private requireWriteAccess;
|
|
33
|
-
/**
|
|
34
|
-
* Throws if readOnly is true
|
|
35
|
-
*/
|
|
36
|
-
private requireObjectMutability;
|
|
37
|
-
private ensureUniqueId;
|
|
28
|
+
private loadByIds;
|
|
38
29
|
getBy(by: keyof DBM, value: any, limit?: number, opt?: CommonDaoReadOptions): Promise<BM[]>;
|
|
39
30
|
getOneBy(by: keyof DBM, value: any, opt?: CommonDaoReadOptions): Promise<BM | null>;
|
|
40
31
|
getAll(opt?: CommonDaoReadOptions): Promise<BM[]>;
|
|
@@ -75,9 +66,8 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
75
66
|
streamQueryIdsForEach(q: DBQuery<DBM>, mapper: AsyncIndexedMapper<ID, void>, opt?: CommonDaoStreamForEachOptions<ID>): Promise<void>;
|
|
76
67
|
/**
|
|
77
68
|
* Mutates!
|
|
78
|
-
* "Returns", just to have a type of "Saved"
|
|
79
69
|
*/
|
|
80
|
-
assignIdCreatedUpdated<T extends BaseDBEntity>(obj: Partial<T>, opt?: CommonDaoOptions):
|
|
70
|
+
assignIdCreatedUpdated<T extends BaseDBEntity>(obj: Partial<T>, opt?: CommonDaoOptions): void;
|
|
81
71
|
/**
|
|
82
72
|
* Convenience method to replace 3 operations (loading+patching+saving) with one:
|
|
83
73
|
*
|
|
@@ -110,6 +100,7 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
110
100
|
saveAsDBM(dbm: Unsaved<DBM>, opt?: CommonDaoSaveOptions<BM, DBM>): Promise<DBM>;
|
|
111
101
|
saveBatch(bms: Unsaved<BM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM[]>;
|
|
112
102
|
saveBatchAsDBM(dbms: Unsaved<DBM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<DBM[]>;
|
|
103
|
+
private prepareSaveOptions;
|
|
113
104
|
/**
|
|
114
105
|
* "Streaming" is implemented by buffering incoming rows into **batches**
|
|
115
106
|
* (of size opt.chunkSize, which defaults to 500),
|
|
@@ -142,19 +133,19 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
142
133
|
* @experimental
|
|
143
134
|
*/
|
|
144
135
|
incrementBatch(prop: keyof DBM, incrementMap: StringMap<number>, opt?: CommonDaoOptions): Promise<StringMap<number>>;
|
|
145
|
-
dbmToBM(_dbm: undefined, opt?: CommonDaoOptions): Promise<
|
|
136
|
+
dbmToBM(_dbm: undefined, opt?: CommonDaoOptions): Promise<null>;
|
|
146
137
|
dbmToBM(_dbm?: DBM, opt?: CommonDaoOptions): Promise<BM>;
|
|
147
138
|
dbmsToBM(dbms: DBM[], opt?: CommonDaoOptions): Promise<BM[]>;
|
|
148
139
|
/**
|
|
149
140
|
* Mutates object with properties: id, created, updated.
|
|
150
141
|
* Returns DBM (new reference).
|
|
151
142
|
*/
|
|
152
|
-
bmToDBM(bm: undefined, opt?: CommonDaoOptions): Promise<
|
|
143
|
+
bmToDBM(bm: undefined, opt?: CommonDaoOptions): Promise<null>;
|
|
153
144
|
bmToDBM(bm?: BM, opt?: CommonDaoOptions): Promise<DBM>;
|
|
154
145
|
bmsToDBM(bms: BM[], opt?: CommonDaoOptions): Promise<DBM[]>;
|
|
155
|
-
anyToDBM(dbm: undefined, opt?: CommonDaoOptions):
|
|
146
|
+
anyToDBM(dbm: undefined, opt?: CommonDaoOptions): null;
|
|
156
147
|
anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM;
|
|
157
|
-
anyToDBMs(
|
|
148
|
+
anyToDBMs(rows: DBM[], opt?: CommonDaoOptions): DBM[];
|
|
158
149
|
/**
|
|
159
150
|
* Returns *converted value* (NOT the same reference).
|
|
160
151
|
* Does NOT mutate the object.
|
|
@@ -167,32 +158,42 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
167
158
|
* Proxy to this.cfg.db.ping
|
|
168
159
|
*/
|
|
169
160
|
ping(): Promise<void>;
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
* Very @experimental.
|
|
175
|
-
*/
|
|
176
|
-
static multiGetById<T>(inputs: DaoWithId[], opt?: CommonDaoReadOptions): Promise<T>;
|
|
161
|
+
withId(id: ID): DaoWithId<typeof this>;
|
|
162
|
+
withIds(ids: ID[]): DaoWithIds<typeof this>;
|
|
163
|
+
withRows(rows: BM[]): DaoWithRows<typeof this>;
|
|
164
|
+
withRow(row: BM, opt?: DaoWithRowOptions<BM>): DaoWithRow<typeof this>;
|
|
177
165
|
/**
|
|
178
|
-
*
|
|
166
|
+
* Load rows (by their ids) from Multiple tables at once.
|
|
167
|
+
* An optimized way to load data, minimizing DB round-trips.
|
|
168
|
+
*
|
|
169
|
+
* @experimental.
|
|
179
170
|
*/
|
|
180
|
-
static
|
|
171
|
+
static multiGet<MAP extends Record<string, DaoWithIds<AnyDao> | DaoWithId<AnyDao>>>(inputMap: MAP, opt?: CommonDaoReadOptions): Promise<{
|
|
172
|
+
[K in keyof MAP]: MAP[K] extends DaoWithIds<any> ? InferBM<MAP[K]['dao']>[] : InferBM<MAP[K]['dao']> | null;
|
|
173
|
+
}>;
|
|
174
|
+
private static prepareMultiGetIds;
|
|
175
|
+
private static multiGetMapByTableById;
|
|
176
|
+
private static prepareMultiGetOutput;
|
|
181
177
|
/**
|
|
182
178
|
* Very @experimental.
|
|
183
179
|
*/
|
|
184
|
-
static multiDeleteByIds(inputs: DaoWithIds[], _opt?: CommonDaoOptions): Promise<NonNegativeInteger>;
|
|
185
|
-
static
|
|
180
|
+
static multiDeleteByIds(inputs: DaoWithIds<any>[], _opt?: CommonDaoOptions): Promise<NonNegativeInteger>;
|
|
181
|
+
static multiSave(inputs: (DaoWithRows<any> | DaoWithRow<any>)[], opt?: CommonDaoSaveBatchOptions<any>): Promise<void>;
|
|
186
182
|
createTransaction(opt?: CommonDBTransactionOptions): Promise<CommonDaoTransaction>;
|
|
187
183
|
runInTransaction<T = void>(fn: CommonDaoTransactionFn<T>, opt?: CommonDBTransactionOptions): Promise<T>;
|
|
184
|
+
private ensureRequired;
|
|
185
|
+
/**
|
|
186
|
+
* Throws if readOnly is true
|
|
187
|
+
*/
|
|
188
|
+
private requireWriteAccess;
|
|
189
|
+
/**
|
|
190
|
+
* Throws if readOnly is true
|
|
191
|
+
*/
|
|
192
|
+
private requireObjectMutability;
|
|
188
193
|
/**
|
|
189
194
|
* Throws if query uses a property that is in `excludeFromIndexes` list.
|
|
190
195
|
*/
|
|
191
196
|
private validateQueryIndexes;
|
|
192
|
-
protected logResult(started: UnixTimestampMillis, op: string, res: any, table: string): void;
|
|
193
|
-
protected logSaveResult(started: UnixTimestampMillis, op: string, table: string): void;
|
|
194
|
-
protected logStarted(op: string, table: string, force?: boolean): UnixTimestampMillis;
|
|
195
|
-
protected logSaveStarted(op: string, items: any, table: string): UnixTimestampMillis;
|
|
196
197
|
}
|
|
197
198
|
/**
|
|
198
199
|
* Transaction is committed when the function returns resolved Promise (aka "returns normally").
|
|
@@ -200,36 +201,28 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
200
201
|
* Transaction is rolled back when the function returns rejected Promise (aka "throws").
|
|
201
202
|
*/
|
|
202
203
|
export type CommonDaoTransactionFn<T = void> = (tx: CommonDaoTransaction) => Promise<T>;
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
export
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
getByIds<BM extends BaseDBEntity, DBM extends BaseDBEntity, ID = BM['id']>(dao: CommonDao<BM, DBM, ID>, ids: ID[], opt?: CommonDaoReadOptions): Promise<BM[]>;
|
|
223
|
-
save<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM>, bm: Unsaved<BM>, opt?: CommonDaoSaveOptions<BM, DBM>): Promise<BM>;
|
|
224
|
-
saveBatch<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM>, bms: Unsaved<BM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM[]>;
|
|
225
|
-
/**
|
|
226
|
-
* DaoTransaction.patch does not load from DB.
|
|
227
|
-
* It assumes the bm was previously loaded in the same Transaction, hence could not be
|
|
228
|
-
* concurrently modified. Hence it's safe to not sync with DB.
|
|
229
|
-
*
|
|
230
|
-
* So, this method is a rather simple convenience "Object.assign and then save".
|
|
231
|
-
*/
|
|
232
|
-
patch<BM extends BaseDBEntity, DBM extends BaseDBEntity, ID = BM['id']>(dao: CommonDao<BM, DBM, ID>, bm: BM, patch: Partial<BM>, opt?: CommonDaoSaveOptions<BM, DBM>): Promise<BM>;
|
|
233
|
-
deleteById<BM extends BaseDBEntity, DBM extends BaseDBEntity, ID = BM['id']>(dao: CommonDao<BM, DBM, ID>, id?: ID | null, opt?: CommonDaoOptions): Promise<number>;
|
|
234
|
-
deleteByIds<BM extends BaseDBEntity, DBM extends BaseDBEntity, ID = BM['id']>(dao: CommonDao<BM, DBM, ID>, ids: ID[], opt?: CommonDaoOptions): Promise<number>;
|
|
204
|
+
export interface DaoWithIds<DAO extends AnyDao> {
|
|
205
|
+
dao: DAO;
|
|
206
|
+
ids: string[];
|
|
207
|
+
}
|
|
208
|
+
export interface DaoWithId<DAO extends AnyDao> {
|
|
209
|
+
dao: DAO;
|
|
210
|
+
id: string;
|
|
211
|
+
}
|
|
212
|
+
export interface DaoWithRows<DAO extends AnyDao> {
|
|
213
|
+
dao: DAO;
|
|
214
|
+
rows: InferBM<DAO>[];
|
|
215
|
+
}
|
|
216
|
+
export interface DaoWithRow<DAO extends AnyDao> {
|
|
217
|
+
dao: DAO;
|
|
218
|
+
row: InferBM<DAO>;
|
|
219
|
+
opt?: DaoWithRowOptions<InferBM<DAO>>;
|
|
220
|
+
}
|
|
221
|
+
interface DaoWithRowOptions<BM extends BaseDBEntity> {
|
|
222
|
+
skipIfEquals?: BM;
|
|
235
223
|
}
|
|
224
|
+
export type InferBM<DAO> = DAO extends CommonDao<infer BM> ? BM : never;
|
|
225
|
+
export type InferDBM<DAO> = DAO extends CommonDao<any, infer DBM> ? DBM : never;
|
|
226
|
+
export type InferID<DAO> = DAO extends CommonDao<any, any, infer ID> ? ID : never;
|
|
227
|
+
export type AnyDao = CommonDao<any>;
|
|
228
|
+
export {};
|