@naturalcycles/db-lib 10.15.1 → 10.17.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.
@@ -2,7 +2,7 @@ import type { Transform } from 'node:stream';
2
2
  import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
3
3
  import type { CommonLogger } from '@naturalcycles/js-lib/log';
4
4
  import type { AsyncIndexedMapper, BaseDBEntity, StringMap, UnixTimestampMillis, Unsaved } from '@naturalcycles/js-lib/types';
5
- import type { ReadableTyped } from '@naturalcycles/nodejs-lib/stream';
5
+ import { type ReadableTyped } from '@naturalcycles/nodejs-lib/stream';
6
6
  import type { CommonDBTransactionOptions, DBTransaction, RunQueryResult } from '../db.model.js';
7
7
  import type { DBQuery } from '../query/dbQuery.js';
8
8
  import { RunnableDBQuery } from '../query/dbQuery.js';
@@ -9,6 +9,7 @@ import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
9
9
  import { _truncate } from '@naturalcycles/js-lib/string/string.util.js';
10
10
  import { _passthroughPredicate, _typeCast, SKIP } from '@naturalcycles/js-lib/types';
11
11
  import { stringId } from '@naturalcycles/nodejs-lib';
12
+ import { transformFlatten } from '@naturalcycles/nodejs-lib/stream';
12
13
  import { _pipeline, transformChunk, transformLogProgress, transformMap, transformNoOp, writableVoid, } from '@naturalcycles/nodejs-lib/stream';
13
14
  import { DBLibError } from '../cnst.js';
14
15
  import { RunnableDBQuery } from '../query/dbQuery.js';
@@ -473,7 +474,7 @@ export class CommonDao {
473
474
  obj.created ||= obj.updated || now;
474
475
  }
475
476
  if (this.cfg.useUpdatedProperty) {
476
- obj.updated = opt.preserveUpdatedCreated && obj.updated ? obj.updated : now;
477
+ obj.updated = opt.preserveUpdated && obj.updated ? obj.updated : now;
477
478
  }
478
479
  if (this.cfg.generateId) {
479
480
  obj.id ||= (this.cfg.hooks.createNaturalId?.(obj) ||
@@ -766,8 +767,8 @@ export class CommonDao {
766
767
  }, {
767
768
  concurrency: chunkConcurrency,
768
769
  errorMode,
769
- flattenArrayOutput: true,
770
770
  }),
771
+ transformFlatten(),
771
772
  transformLogProgress({
772
773
  metric: 'saved',
773
774
  ...opt,
@@ -106,7 +106,15 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
106
106
  * and abstracts it away.
107
107
  */
108
108
  validateBM?: ValidationFunction<BM, any>;
109
+ /**
110
+ * Used by e.g Datastore.
111
+ */
109
112
  excludeFromIndexes?: (keyof DBM)[];
113
+ /**
114
+ * Used by e.g Firestore.
115
+ */
116
+ indexes?: CommonDaoIndex<DBM>[];
117
+ ttl?: CommonDaoTTL<DBM>;
110
118
  /**
111
119
  * Defaults to true.
112
120
  * If set to false - load (read) operations will skip validation (and conversion).
@@ -174,6 +182,28 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
174
182
  */
175
183
  patchInTransaction?: boolean;
176
184
  }
185
+ export interface CommonDaoIndex<DBM extends BaseDBEntity> {
186
+ /**
187
+ * Name of the property to index.
188
+ */
189
+ name: keyof DBM;
190
+ /**
191
+ * Defaults to ['asc']
192
+ */
193
+ order?: CommonDaoIndexOrder[];
194
+ }
195
+ export type CommonDaoIndexOrder = 'asc' | 'desc' | 'array-contains';
196
+ /**
197
+ * TTL definition - a map from Table name to an array of properties that should be used for TTL.
198
+ * Example:
199
+ *
200
+ * {
201
+ * myTable: ['deleteAt']
202
+ * }
203
+ */
204
+ export interface CommonDaoTTL<DBM extends BaseDBEntity> {
205
+ [table: string]: (keyof DBM)[];
206
+ }
177
207
  /**
178
208
  * All properties default to undefined.
179
209
  */
@@ -202,9 +232,12 @@ export interface CommonDaoOptions extends CommonDBOptions {
202
232
  */
203
233
  mutateInput?: boolean;
204
234
  /**
205
- * @default false
235
+ * Defaults to false.
236
+ *
237
+ * If false (default) - will set `updated` property to the current timestamp.
238
+ * If true - will NOT set it (preserve the original value).
206
239
  */
207
- preserveUpdatedCreated?: boolean;
240
+ preserveUpdated?: boolean;
208
241
  /**
209
242
  * @default false (for streams). Setting to true enables deletion of immutable objects
210
243
  */
@@ -4,7 +4,7 @@ import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
4
4
  import { _passthroughMapper } from '@naturalcycles/js-lib/types';
5
5
  import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors';
6
6
  import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
7
- import { createWriteStreamAsNDJSON, } from '@naturalcycles/nodejs-lib/stream';
7
+ import { createWriteStreamAsNDJSON, transformFlatten, } from '@naturalcycles/nodejs-lib/stream';
8
8
  import { _pipeline, NDJsonStats, transformLogProgress, transformMap, transformTap, } from '@naturalcycles/nodejs-lib/stream';
9
9
  import { DBQuery } from '../query/dbQuery.js';
10
10
  /**
@@ -65,10 +65,10 @@ export async function dbPipelineBackup(opt) {
65
65
  }),
66
66
  transformMap(mapperPerTable[table] || _passthroughMapper, {
67
67
  errorMode,
68
- flattenArrayOutput: true,
69
68
  ...transformMapOptions,
70
69
  metric: table,
71
70
  }),
71
+ transformFlatten(),
72
72
  transformTap(() => {
73
73
  rows++;
74
74
  }),
@@ -1,6 +1,6 @@
1
1
  import { ErrorMode } from '@naturalcycles/js-lib/error/errorMode.js';
2
2
  import type { AsyncMapper, UnixTimestamp } from '@naturalcycles/js-lib/types';
3
- import type { TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib/stream';
3
+ import { type TransformLogProgressOptions, type TransformMapOptions } from '@naturalcycles/nodejs-lib/stream';
4
4
  import { NDJsonStats } from '@naturalcycles/nodejs-lib/stream';
5
5
  import type { CommonDB } from '../commondb/common.db.js';
6
6
  import type { CommonDBSaveOptions } from '../db.model.js';
@@ -3,6 +3,7 @@ import { ErrorMode } from '@naturalcycles/js-lib/error/errorMode.js';
3
3
  import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
4
4
  import { _passthroughMapper } from '@naturalcycles/js-lib/types';
5
5
  import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors';
6
+ import { transformFlatten, } from '@naturalcycles/nodejs-lib/stream';
6
7
  import { _pipeline, NDJsonStats, transformChunk, transformLogProgress, transformMap, transformTap, writableForEach, } from '@naturalcycles/nodejs-lib/stream';
7
8
  import { DBQuery } from '../query/dbQuery.js';
8
9
  /**
@@ -38,10 +39,10 @@ export async function dbPipelineCopy(opt) {
38
39
  }),
39
40
  transformMap(mapper, {
40
41
  errorMode,
41
- flattenArrayOutput: true,
42
42
  ...transformMapOptions,
43
43
  metric: table,
44
44
  }),
45
+ transformFlatten(),
45
46
  transformTap(() => rows++),
46
47
  transformChunk({ chunkSize }),
47
48
  writableForEach(async (dbms) => {
@@ -6,7 +6,7 @@ import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
6
6
  import { _passthroughMapper } from '@naturalcycles/js-lib/types';
7
7
  import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors';
8
8
  import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
9
- import { createReadStreamAsNDJSON, } from '@naturalcycles/nodejs-lib/stream';
9
+ import { createReadStreamAsNDJSON, transformFlatten, } from '@naturalcycles/nodejs-lib/stream';
10
10
  import { _pipeline, NDJsonStats, transformChunk, transformFilterSync, transformLogProgress, transformMap, transformTap, writableForEach, } from '@naturalcycles/nodejs-lib/stream';
11
11
  /**
12
12
  * Pipeline from NDJSON files in a folder (optionally gzipped) to CommonDB.
@@ -80,10 +80,10 @@ export async function dbPipelineRestore(opt) {
80
80
  : []),
81
81
  transformMap(mapperPerTable[table] || _passthroughMapper, {
82
82
  errorMode,
83
- flattenArrayOutput: true,
84
83
  ...transformMapOptions,
85
84
  metric: table,
86
85
  }),
86
+ transformFlatten(),
87
87
  transformChunk({ chunkSize }),
88
88
  writableForEach(async (dbms) => {
89
89
  await db.saveBatch(table, dbms, saveOptions);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
3
  "type": "module",
4
- "version": "10.15.1",
4
+ "version": "10.17.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@naturalcycles/nodejs-lib": "^15"
@@ -131,8 +131,18 @@ export interface CommonDaoCfg<
131
131
  */
132
132
  validateBM?: ValidationFunction<BM, any>
133
133
 
134
+ /**
135
+ * Used by e.g Datastore.
136
+ */
134
137
  excludeFromIndexes?: (keyof DBM)[]
135
138
 
139
+ /**
140
+ * Used by e.g Firestore.
141
+ */
142
+ indexes?: CommonDaoIndex<DBM>[]
143
+
144
+ ttl?: CommonDaoTTL<DBM>
145
+
136
146
  /**
137
147
  * Defaults to true.
138
148
  * If set to false - load (read) operations will skip validation (and conversion).
@@ -214,6 +224,31 @@ export interface CommonDaoCfg<
214
224
  patchInTransaction?: boolean
215
225
  }
216
226
 
227
+ export interface CommonDaoIndex<DBM extends BaseDBEntity> {
228
+ /**
229
+ * Name of the property to index.
230
+ */
231
+ name: keyof DBM
232
+ /**
233
+ * Defaults to ['asc']
234
+ */
235
+ order?: CommonDaoIndexOrder[]
236
+ }
237
+
238
+ export type CommonDaoIndexOrder = 'asc' | 'desc' | 'array-contains'
239
+
240
+ /**
241
+ * TTL definition - a map from Table name to an array of properties that should be used for TTL.
242
+ * Example:
243
+ *
244
+ * {
245
+ * myTable: ['deleteAt']
246
+ * }
247
+ */
248
+ export interface CommonDaoTTL<DBM extends BaseDBEntity> {
249
+ [table: string]: (keyof DBM)[]
250
+ }
251
+
217
252
  /**
218
253
  * All properties default to undefined.
219
254
  */
@@ -244,9 +279,12 @@ export interface CommonDaoOptions extends CommonDBOptions {
244
279
  mutateInput?: boolean
245
280
 
246
281
  /**
247
- * @default false
282
+ * Defaults to false.
283
+ *
284
+ * If false (default) - will set `updated` property to the current timestamp.
285
+ * If true - will NOT set it (preserve the original value).
248
286
  */
249
- preserveUpdatedCreated?: boolean
287
+ preserveUpdated?: boolean
250
288
 
251
289
  /**
252
290
  * @default false (for streams). Setting to true enables deletion of immutable objects
@@ -24,7 +24,7 @@ import type {
24
24
  } from '@naturalcycles/js-lib/types'
25
25
  import { _passthroughPredicate, _typeCast, SKIP } from '@naturalcycles/js-lib/types'
26
26
  import { stringId } from '@naturalcycles/nodejs-lib'
27
- import type { ReadableTyped } from '@naturalcycles/nodejs-lib/stream'
27
+ import { type ReadableTyped, transformFlatten } from '@naturalcycles/nodejs-lib/stream'
28
28
  import {
29
29
  _pipeline,
30
30
  transformChunk,
@@ -617,7 +617,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
617
617
  }
618
618
 
619
619
  if (this.cfg.useUpdatedProperty) {
620
- obj.updated = opt.preserveUpdatedCreated && obj.updated ? obj.updated : now
620
+ obj.updated = opt.preserveUpdated && obj.updated ? obj.updated : now
621
621
  }
622
622
 
623
623
  if (this.cfg.generateId) {
@@ -983,9 +983,9 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
983
983
  {
984
984
  concurrency: chunkConcurrency,
985
985
  errorMode,
986
- flattenArrayOutput: true,
987
986
  },
988
987
  ),
988
+ transformFlatten(),
989
989
  transformLogProgress({
990
990
  metric: 'saved',
991
991
  ...opt,
@@ -7,6 +7,7 @@ import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/col
7
7
  import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
8
8
  import {
9
9
  createWriteStreamAsNDJSON,
10
+ transformFlatten,
10
11
  type TransformLogProgressOptions,
11
12
  type TransformMapOptions,
12
13
  } from '@naturalcycles/nodejs-lib/stream'
@@ -220,10 +221,10 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
220
221
  }),
221
222
  transformMap(mapperPerTable[table] || _passthroughMapper, {
222
223
  errorMode,
223
- flattenArrayOutput: true,
224
224
  ...transformMapOptions,
225
225
  metric: table,
226
226
  }),
227
+ transformFlatten(),
227
228
  transformTap(() => {
228
229
  rows++
229
230
  }),
@@ -4,9 +4,10 @@ import { pMap } from '@naturalcycles/js-lib/promise/pMap.js'
4
4
  import type { AsyncMapper, BaseDBEntity, UnixTimestamp } from '@naturalcycles/js-lib/types'
5
5
  import { _passthroughMapper } from '@naturalcycles/js-lib/types'
6
6
  import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors'
7
- import type {
8
- TransformLogProgressOptions,
9
- TransformMapOptions,
7
+ import {
8
+ transformFlatten,
9
+ type TransformLogProgressOptions,
10
+ type TransformMapOptions,
10
11
  } from '@naturalcycles/nodejs-lib/stream'
11
12
  import {
12
13
  _pipeline,
@@ -144,10 +145,10 @@ export async function dbPipelineCopy(opt: DBPipelineCopyOptions): Promise<NDJson
144
145
  }),
145
146
  transformMap(mapper, {
146
147
  errorMode,
147
- flattenArrayOutput: true,
148
148
  ...transformMapOptions,
149
149
  metric: table,
150
150
  }),
151
+ transformFlatten(),
151
152
  transformTap(() => rows++),
152
153
  transformChunk({ chunkSize }),
153
154
  writableForEach(async dbms => {
@@ -10,6 +10,7 @@ import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/col
10
10
  import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
11
11
  import {
12
12
  createReadStreamAsNDJSON,
13
+ transformFlatten,
13
14
  type TransformLogProgressOptions,
14
15
  type TransformMapOptions,
15
16
  } from '@naturalcycles/nodejs-lib/stream'
@@ -207,10 +208,10 @@ export async function dbPipelineRestore(opt: DBPipelineRestoreOptions): Promise<
207
208
  : []),
208
209
  transformMap(mapperPerTable[table] || _passthroughMapper, {
209
210
  errorMode,
210
- flattenArrayOutput: true,
211
211
  ...transformMapOptions,
212
212
  metric: table,
213
213
  }),
214
+ transformFlatten(),
214
215
  transformChunk({ chunkSize }),
215
216
  writableForEach(async dbms => {
216
217
  await db.saveBatch(table, dbms, saveOptions)