@naturalcycles/db-lib 10.19.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 +36 -63
- package/dist/commondao/common.dao.js +138 -432
- package/dist/commondao/common.dao.model.d.ts +0 -13
- package/dist/commondao/commonDaoTransaction.d.ts +38 -0
- package/dist/commondao/commonDaoTransaction.js +78 -0
- package/dist/testing/commonDaoTest.js +0 -3
- package/package.json +1 -1
- package/src/commondao/common.dao.model.ts +4 -4
- package/src/commondao/common.dao.ts +188 -519
- package/src/commondao/commonDaoTransaction.ts +124 -0
- 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, type BaseDBEntity, type NonNegativeInteger, type StringMap, type UnixTimestampMillis, type 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
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
|
*
|
|
@@ -18,23 +18,14 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
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.
|
|
@@ -169,7 +160,8 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
169
160
|
ping(): Promise<void>;
|
|
170
161
|
withId(id: ID): DaoWithId<typeof this>;
|
|
171
162
|
withIds(ids: ID[]): DaoWithIds<typeof this>;
|
|
172
|
-
|
|
163
|
+
withRows(rows: BM[]): DaoWithRows<typeof this>;
|
|
164
|
+
withRow(row: BM, opt?: DaoWithRowOptions<BM>): DaoWithRow<typeof this>;
|
|
173
165
|
/**
|
|
174
166
|
* Load rows (by their ids) from Multiple tables at once.
|
|
175
167
|
* An optimized way to load data, minimizing DB round-trips.
|
|
@@ -186,17 +178,22 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
186
178
|
* Very @experimental.
|
|
187
179
|
*/
|
|
188
180
|
static multiDeleteByIds(inputs: DaoWithIds<any>[], _opt?: CommonDaoOptions): Promise<NonNegativeInteger>;
|
|
189
|
-
static multiSave(inputs: DaoWithRows<any>[], opt?: CommonDaoSaveBatchOptions<any>): Promise<void>;
|
|
181
|
+
static multiSave(inputs: (DaoWithRows<any> | DaoWithRow<any>)[], opt?: CommonDaoSaveBatchOptions<any>): Promise<void>;
|
|
190
182
|
createTransaction(opt?: CommonDBTransactionOptions): Promise<CommonDaoTransaction>;
|
|
191
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;
|
|
192
193
|
/**
|
|
193
194
|
* Throws if query uses a property that is in `excludeFromIndexes` list.
|
|
194
195
|
*/
|
|
195
196
|
private validateQueryIndexes;
|
|
196
|
-
protected logResult(started: UnixTimestampMillis, op: string, res: any, table: string): void;
|
|
197
|
-
protected logSaveResult(started: UnixTimestampMillis, op: string, table: string): void;
|
|
198
|
-
protected logStarted(op: string, table: string, force?: boolean): UnixTimestampMillis;
|
|
199
|
-
protected logSaveStarted(op: string, items: any, table: string): UnixTimestampMillis;
|
|
200
197
|
}
|
|
201
198
|
/**
|
|
202
199
|
* Transaction is committed when the function returns resolved Promise (aka "returns normally").
|
|
@@ -204,39 +201,6 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
204
201
|
* Transaction is rolled back when the function returns rejected Promise (aka "throws").
|
|
205
202
|
*/
|
|
206
203
|
export type CommonDaoTransactionFn<T = void> = (tx: CommonDaoTransaction) => Promise<T>;
|
|
207
|
-
/**
|
|
208
|
-
* Transaction context.
|
|
209
|
-
* Has similar API than CommonDao, but all operations are performed in the context of the transaction.
|
|
210
|
-
*/
|
|
211
|
-
export declare class CommonDaoTransaction {
|
|
212
|
-
tx: DBTransaction;
|
|
213
|
-
private logger;
|
|
214
|
-
constructor(tx: DBTransaction, logger: CommonLogger);
|
|
215
|
-
/**
|
|
216
|
-
* Commits the underlying DBTransaction.
|
|
217
|
-
* May throw.
|
|
218
|
-
*/
|
|
219
|
-
commit(): Promise<void>;
|
|
220
|
-
/**
|
|
221
|
-
* Perform a graceful rollback without throwing/re-throwing any error.
|
|
222
|
-
* Never throws.
|
|
223
|
-
*/
|
|
224
|
-
rollback(): Promise<void>;
|
|
225
|
-
getById<BM extends BaseDBEntity, DBM extends BaseDBEntity, ID extends string = BM['id']>(dao: CommonDao<BM, DBM, ID>, id?: ID | null, opt?: CommonDaoReadOptions): Promise<BM | null>;
|
|
226
|
-
getByIds<BM extends BaseDBEntity, DBM extends BaseDBEntity, ID extends string = BM['id']>(dao: CommonDao<BM, DBM, ID>, ids: ID[], opt?: CommonDaoReadOptions): Promise<BM[]>;
|
|
227
|
-
save<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM>, bm: Unsaved<BM>, opt?: CommonDaoSaveOptions<BM, DBM>): Promise<BM>;
|
|
228
|
-
saveBatch<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM>, bms: Unsaved<BM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM[]>;
|
|
229
|
-
/**
|
|
230
|
-
* DaoTransaction.patch does not load from DB.
|
|
231
|
-
* It assumes the bm was previously loaded in the same Transaction, hence could not be
|
|
232
|
-
* concurrently modified. Hence it's safe to not sync with DB.
|
|
233
|
-
*
|
|
234
|
-
* So, this method is a rather simple convenience "Object.assign and then save".
|
|
235
|
-
*/
|
|
236
|
-
patch<BM extends BaseDBEntity, DBM extends BaseDBEntity, ID extends string = BM['id']>(dao: CommonDao<BM, DBM, ID>, bm: BM, patch: Partial<BM>, opt?: CommonDaoSaveOptions<BM, DBM>): Promise<BM>;
|
|
237
|
-
deleteById<DAO extends AnyDao>(dao: DAO, id?: InferID<DAO> | null, opt?: CommonDaoOptions): Promise<number>;
|
|
238
|
-
deleteByIds<DAO extends AnyDao>(dao: DAO, ids: InferID<DAO>[], opt?: CommonDaoOptions): Promise<number>;
|
|
239
|
-
}
|
|
240
204
|
export interface DaoWithIds<DAO extends AnyDao> {
|
|
241
205
|
dao: DAO;
|
|
242
206
|
ids: string[];
|
|
@@ -249,7 +213,16 @@ export interface DaoWithRows<DAO extends AnyDao> {
|
|
|
249
213
|
dao: DAO;
|
|
250
214
|
rows: InferBM<DAO>[];
|
|
251
215
|
}
|
|
252
|
-
|
|
253
|
-
|
|
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;
|
|
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;
|
|
254
227
|
export type AnyDao = CommonDao<any>;
|
|
255
228
|
export {};
|