@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.
@@ -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 { CommonLogger } from '@naturalcycles/js-lib/log';
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, DBTransaction, RunQueryResult } from '../db.model.js';
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, DaoWithId, DaoWithIds, DaoWithRows } from './common.dao.model.js';
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
- getById(id?: ID | null, opt?: CommonDaoReadOptions): Promise<BM | null>;
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
- requireById(id: ID, opt?: CommonDaoReadOptions): Promise<BM>;
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): T;
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<undefined>;
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<undefined>;
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): undefined;
146
+ anyToDBM(dbm: undefined, opt?: CommonDaoOptions): null;
156
147
  anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM;
157
- anyToDBMs(entities: DBM[], opt?: CommonDaoOptions): DBM[];
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
- id(id: string): DaoWithId;
171
- ids(ids: string[]): DaoWithIds;
172
- rows(rows: BM[]): DaoWithRows;
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
- * Very @experimental.
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 multiGetByIds(inputs: DaoWithIds[], opt?: CommonDaoReadOptions): Promise<StringMap<unknown[]>>;
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 multiSaveBatch(inputs: DaoWithRows[], opt?: CommonDaoSaveBatchOptions<any>): Promise<void>;
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
- * Transaction context.
205
- * Has similar API than CommonDao, but all operations are performed in the context of the transaction.
206
- */
207
- export declare class CommonDaoTransaction {
208
- tx: DBTransaction;
209
- private logger;
210
- constructor(tx: DBTransaction, logger: CommonLogger);
211
- /**
212
- * Commits the underlying DBTransaction.
213
- * May throw.
214
- */
215
- commit(): Promise<void>;
216
- /**
217
- * Perform a graceful rollback without throwing/re-throwing any error.
218
- * Never throws.
219
- */
220
- rollback(): Promise<void>;
221
- getById<BM extends BaseDBEntity, DBM extends BaseDBEntity, ID = BM['id']>(dao: CommonDao<BM, DBM, ID>, id?: ID | null, opt?: CommonDaoReadOptions): Promise<BM | null>;
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 {};