@naturalcycles/db-lib 9.2.1 → 9.3.1

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.
Files changed (36) hide show
  1. package/dist/adapter/cachedb/cache.db.d.ts +11 -12
  2. package/dist/adapter/cachedb/cache.db.model.d.ts +2 -2
  3. package/dist/adapter/file/file.db.d.ts +12 -12
  4. package/dist/adapter/file/file.db.model.d.ts +2 -2
  5. package/dist/adapter/file/localFile.persistence.plugin.d.ts +3 -3
  6. package/dist/adapter/file/noop.persistence.plugin.d.ts +2 -2
  7. package/dist/adapter/inmemory/inMemory.db.d.ts +12 -12
  8. package/dist/adapter/inmemory/queryInMemory.d.ts +2 -2
  9. package/dist/base.common.db.d.ts +10 -10
  10. package/dist/common.db.d.ts +10 -10
  11. package/dist/commondao/common.dao.d.ts +23 -25
  12. package/dist/commondao/common.dao.js +16 -15
  13. package/dist/commondao/common.dao.model.d.ts +13 -13
  14. package/dist/db.model.d.ts +4 -4
  15. package/dist/query/dbQuery.d.ts +9 -9
  16. package/dist/testing/test.model.d.ts +3 -4
  17. package/dist/testing/test.model.js +1 -1
  18. package/dist/transaction/dbTransaction.util.d.ts +3 -3
  19. package/dist/validation/index.d.ts +2 -2
  20. package/package.json +1 -1
  21. package/src/adapter/cachedb/cache.db.model.ts +2 -2
  22. package/src/adapter/cachedb/cache.db.ts +18 -16
  23. package/src/adapter/file/file.db.model.ts +2 -2
  24. package/src/adapter/file/file.db.ts +17 -15
  25. package/src/adapter/file/localFile.persistence.plugin.ts +4 -4
  26. package/src/adapter/file/noop.persistence.plugin.ts +2 -2
  27. package/src/adapter/inmemory/inMemory.db.ts +20 -18
  28. package/src/adapter/inmemory/queryInMemory.ts +5 -2
  29. package/src/base.common.db.ts +20 -10
  30. package/src/common.db.ts +20 -13
  31. package/src/commondao/common.dao.model.ts +23 -16
  32. package/src/commondao/common.dao.ts +68 -71
  33. package/src/db.model.ts +4 -4
  34. package/src/query/dbQuery.ts +13 -11
  35. package/src/testing/test.model.ts +4 -6
  36. package/src/transaction/dbTransaction.util.ts +3 -3
@@ -1,4 +1,4 @@
1
- import { AnyObjectWithId, ObjectWithId, AsyncMapper, Saved, AnyObject } from '@naturalcycles/js-lib';
1
+ import { AsyncMapper, Saved, AnyObject, PartialObjectWithId, AnyPartialObjectWithId } from '@naturalcycles/js-lib';
2
2
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
3
  import { CommonDaoOptions, CommonDaoStreamDeleteOptions, 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 PartialObjectWithId = AnyPartialObjectWithId> {
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 PartialObjectWithId = AnyPartialObjectWithId> {
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 PartialObjectWithId = AnyPartialObjectWithId> {
47
47
  table: string;
48
48
  constructor(table: string);
49
49
  /**
50
50
  * Convenience method.
51
51
  */
52
- static create<ROW extends ObjectWithId = AnyObjectWithId>(table: string): DBQuery<ROW>;
53
- static fromPlainObject<ROW extends ObjectWithId = AnyObjectWithId>(obj: Partial<DBQuery<ROW>> & {
52
+ static create<ROW extends PartialObjectWithId = AnyPartialObjectWithId>(table: string): DBQuery<ROW>;
53
+ static fromPlainObject<ROW extends PartialObjectWithId = AnyPartialObjectWithId>(obj: Partial<DBQuery<ROW>> & {
54
54
  table: string;
55
55
  }): DBQuery<ROW>;
56
56
  _filters: DBQueryFilter<ROW>[];
@@ -84,7 +84,7 @@ 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>, DBM extends ObjectWithId = Saved<BM>, TM extends AnyObject = BM> extends DBQuery<DBM> {
87
+ export declare class RunnableDBQuery<BM extends PartialObjectWithId, DBM extends PartialObjectWithId = BM, TM extends AnyObject = BM> extends DBQuery<DBM> {
88
88
  dao: CommonDao<BM, DBM, TM>;
89
89
  /**
90
90
  * Pass `table` to override table.
@@ -92,10 +92,10 @@ export declare class RunnableDBQuery<BM extends Partial<ObjectWithId>, DBM exten
92
92
  constructor(dao: CommonDao<BM, DBM, TM>, table?: string);
93
93
  runQuery(opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
94
94
  runQuerySingleColumn<T = any>(opt?: CommonDaoOptions): Promise<T[]>;
95
- runQueryAsDBM(opt?: CommonDaoOptions): Promise<DBM[]>;
95
+ runQueryAsDBM(opt?: CommonDaoOptions): Promise<Saved<DBM>[]>;
96
96
  runQueryAsTM(opt?: CommonDaoOptions): Promise<TM[]>;
97
97
  runQueryExtended(opt?: CommonDaoOptions): Promise<RunQueryResult<Saved<BM>>>;
98
- runQueryExtendedAsDBM(opt?: CommonDaoOptions): Promise<RunQueryResult<DBM>>;
98
+ runQueryExtendedAsDBM(opt?: CommonDaoOptions): Promise<RunQueryResult<Saved<DBM>>>;
99
99
  runQueryExtendedAsTM(opt?: CommonDaoOptions): Promise<RunQueryResult<TM>>;
100
100
  runQueryCount(opt?: CommonDaoOptions): Promise<number>;
101
101
  updateByQuery(patch: DBPatch<DBM>, opt?: CommonDaoOptions): Promise<number>;
@@ -2,14 +2,13 @@
2
2
  import { BaseDBEntity, Saved, JsonSchemaObject } from '@naturalcycles/js-lib';
3
3
  export declare const TEST_TABLE = "TEST_TABLE";
4
4
  export interface TestItemBM extends BaseDBEntity {
5
- id: string;
6
5
  k1: string;
7
6
  k2?: string | null;
8
7
  k3?: number;
9
8
  even?: boolean;
10
9
  b1?: Buffer;
11
10
  }
12
- export interface TestItemDBM extends Saved<TestItemBM> {
11
+ export interface TestItemDBM extends TestItemBM {
13
12
  }
14
13
  export interface TestItemTM {
15
14
  k1: string;
@@ -20,7 +19,7 @@ export declare const testItemDBMSchema: import("joi").ObjectSchema<TestItemDBM>;
20
19
  export declare const testItemTMSchema: import("joi").ObjectSchema<TestItemTM>;
21
20
  export declare const testItemBMJsonSchema: JsonSchemaObject<TestItemBM>;
22
21
  export declare const testItemDBMJsonSchema: JsonSchemaObject<TestItemDBM>;
23
- export declare function createTestItemDBM(num?: number): TestItemDBM;
22
+ export declare function createTestItemDBM(num?: number): Saved<TestItemDBM>;
24
23
  export declare function createTestItemBM(num?: number): Saved<TestItemBM>;
25
- export declare function createTestItemsDBM(count?: number): TestItemDBM[];
24
+ export declare function createTestItemsDBM(count?: number): Saved<TestItemDBM>[];
26
25
  export declare function createTestItemsBM(count?: number): Saved<TestItemBM>[];
@@ -18,7 +18,7 @@ exports.testItemDBMSchema = (0, nodejs_lib_1.objectSchema)({
18
18
  k3: nodejs_lib_1.numberSchema.optional(),
19
19
  even: nodejs_lib_1.booleanSchema.optional(),
20
20
  b1: nodejs_lib_1.binarySchema.optional(),
21
- }).concat(nodejs_lib_1.savedDBEntitySchema);
21
+ }).concat(nodejs_lib_1.baseDBEntitySchema);
22
22
  exports.testItemTMSchema = (0, nodejs_lib_1.objectSchema)({
23
23
  k1: nodejs_lib_1.stringSchema,
24
24
  even: nodejs_lib_1.booleanSchema.optional(),
@@ -1,4 +1,4 @@
1
- import { ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { PartialObjectWithId } from '@naturalcycles/js-lib';
2
2
  import type { CommonDB } from '../common.db';
3
3
  import { CommonDBOptions, CommonDBSaveOptions, DBTransaction } from '../db.model';
4
4
  /**
@@ -22,7 +22,7 @@ export declare class FakeDBTransaction implements DBTransaction {
22
22
  protected db: CommonDB;
23
23
  constructor(db: CommonDB);
24
24
  rollback(): Promise<void>;
25
- getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>;
26
- saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
25
+ getByIds<ROW extends PartialObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>;
26
+ saveBatch<ROW extends PartialObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
27
27
  deleteByIds(table: string, ids: string[], opt?: CommonDBOptions | undefined): Promise<number>;
28
28
  }
@@ -3,6 +3,6 @@ import { DBQuery, DBQueryFilter, DBQueryFilterOperator, DBQueryOrder } from '../
3
3
  export declare const commonDBOptionsSchema: import("joi").ObjectSchema<CommonDBOptions>;
4
4
  export declare const commonDBSaveOptionsSchema: import("joi").ObjectSchema<CommonDBSaveOptions<any>>;
5
5
  export declare const dbQueryFilterOperatorSchema: import("@naturalcycles/nodejs-lib/dist/validation/joi/string.extensions").StringSchema<DBQueryFilterOperator>;
6
- export declare const dbQueryFilterSchema: import("joi").ObjectSchema<DBQueryFilter<import("@naturalcycles/js-lib").AnyObjectWithId>>;
7
- export declare const dbQueryOrderSchema: import("joi").ObjectSchema<DBQueryOrder<import("@naturalcycles/js-lib").AnyObjectWithId>>;
6
+ export declare const dbQueryFilterSchema: import("joi").ObjectSchema<DBQueryFilter<import("@naturalcycles/js-lib").AnyPartialObjectWithId>>;
7
+ export declare const dbQueryOrderSchema: import("joi").ObjectSchema<DBQueryOrder<import("@naturalcycles/js-lib").AnyPartialObjectWithId>>;
8
8
  export declare const dbQuerySchema: import("joi").ObjectSchema<DBQuery<any>>;
package/package.json CHANGED
@@ -40,7 +40,7 @@
40
40
  "engines": {
41
41
  "node": ">=18.12"
42
42
  },
43
- "version": "9.2.1",
43
+ "version": "9.3.1",
44
44
  "description": "Lowest Common Denominator API to supported Databases",
45
45
  "keywords": [
46
46
  "db",
@@ -1,4 +1,4 @@
1
- import { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib'
1
+ import { CommonLogger, PartialObjectWithId } from '@naturalcycles/js-lib'
2
2
  import { CommonDB } from '../../common.db'
3
3
  import {
4
4
  CommonDBCreateOptions,
@@ -62,7 +62,7 @@ export interface CacheDBOptions extends CommonDBOptions {
62
62
  onlyCache?: boolean
63
63
  }
64
64
 
65
- export interface CacheDBSaveOptions<ROW extends Partial<ObjectWithId>>
65
+ export interface CacheDBSaveOptions<ROW extends PartialObjectWithId>
66
66
  extends CacheDBOptions,
67
67
  CommonDBSaveOptions<ROW> {}
68
68
 
@@ -3,9 +3,11 @@ import {
3
3
  _isTruthy,
4
4
  JsonSchemaObject,
5
5
  JsonSchemaRootObject,
6
- ObjectWithId,
6
+ PartialObjectWithId,
7
+ Saved,
7
8
  StringMap,
8
9
  } from '@naturalcycles/js-lib'
10
+ import { ReadableTyped } from '@naturalcycles/nodejs-lib'
9
11
  import { BaseCommonDB } from '../../base.common.db'
10
12
  import { CommonDB, commonDBFullSupport, CommonDBSupport } from '../../common.db'
11
13
  import { DBPatch, RunQueryResult } from '../../db.model'
@@ -57,13 +59,13 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
57
59
  return await this.cfg.downstreamDB.getTables()
58
60
  }
59
61
 
60
- override async getTableSchema<ROW extends ObjectWithId>(
62
+ override async getTableSchema<ROW extends PartialObjectWithId>(
61
63
  table: string,
62
64
  ): Promise<JsonSchemaRootObject<ROW>> {
63
65
  return await this.cfg.downstreamDB.getTableSchema<ROW>(table)
64
66
  }
65
67
 
66
- override async createTable<ROW extends ObjectWithId>(
68
+ override async createTable<ROW extends PartialObjectWithId>(
67
69
  table: string,
68
70
  schema: JsonSchemaObject<ROW>,
69
71
  opt: CacheDBCreateOptions = {},
@@ -77,13 +79,13 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
77
79
  }
78
80
  }
79
81
 
80
- override async getByIds<ROW extends ObjectWithId>(
82
+ override async getByIds<ROW extends PartialObjectWithId>(
81
83
  table: string,
82
- ids: ROW['id'][],
84
+ ids: string[],
83
85
  opt: CacheDBSaveOptions<ROW> = {},
84
- ): Promise<ROW[]> {
85
- const resultMap: StringMap<ROW> = {}
86
- const missingIds: ROW['id'][] = []
86
+ ): Promise<Saved<ROW>[]> {
87
+ const resultMap: StringMap<Saved<ROW>> = {}
88
+ const missingIds: string[] = []
87
89
 
88
90
  if (!opt.skipCache && !this.cfg.skipCache) {
89
91
  const results = await this.cfg.cacheDB.getByIds<ROW>(table, ids, opt)
@@ -123,7 +125,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
123
125
  return ids.map(id => resultMap[id]).filter(_isTruthy)
124
126
  }
125
127
 
126
- override async saveBatch<ROW extends Partial<ObjectWithId>>(
128
+ override async saveBatch<ROW extends PartialObjectWithId>(
127
129
  table: string,
128
130
  rows: ROW[],
129
131
  opt: CacheDBSaveOptions<ROW> = {},
@@ -152,10 +154,10 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
152
154
  }
153
155
  }
154
156
 
155
- override async runQuery<ROW extends ObjectWithId>(
157
+ override async runQuery<ROW extends PartialObjectWithId>(
156
158
  q: DBQuery<ROW>,
157
159
  opt: CacheDBSaveOptions<ROW> = {},
158
- ): Promise<RunQueryResult<ROW>> {
160
+ ): Promise<RunQueryResult<Saved<ROW>>> {
159
161
  if (!opt.onlyCache && !this.cfg.onlyCache) {
160
162
  const { rows, ...queryResult } = await this.cfg.downstreamDB.runQuery(q, opt)
161
163
 
@@ -182,7 +184,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
182
184
  return { rows, ...queryResult }
183
185
  }
184
186
 
185
- override async runQueryCount<ROW extends ObjectWithId>(
187
+ override async runQueryCount<ROW extends PartialObjectWithId>(
186
188
  q: DBQuery<ROW>,
187
189
  opt: CacheDBOptions = {},
188
190
  ): Promise<number> {
@@ -199,10 +201,10 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
199
201
  return count
200
202
  }
201
203
 
202
- override streamQuery<ROW extends ObjectWithId>(
204
+ override streamQuery<ROW extends PartialObjectWithId>(
203
205
  q: DBQuery<ROW>,
204
206
  opt: CacheDBStreamOptions = {},
205
- ): Readable {
207
+ ): ReadableTyped<Saved<ROW>> {
206
208
  if (!opt.onlyCache && !this.cfg.onlyCache) {
207
209
  const stream = this.cfg.downstreamDB.streamQuery<ROW>(q, opt)
208
210
 
@@ -238,7 +240,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
238
240
  return stream
239
241
  }
240
242
 
241
- override async deleteByQuery<ROW extends ObjectWithId>(
243
+ override async deleteByQuery<ROW extends PartialObjectWithId>(
242
244
  q: DBQuery<ROW>,
243
245
  opt: CacheDBOptions = {},
244
246
  ): Promise<number> {
@@ -270,7 +272,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
270
272
  return deletedIds
271
273
  }
272
274
 
273
- override async updateByQuery<ROW extends ObjectWithId>(
275
+ override async updateByQuery<ROW extends PartialObjectWithId>(
274
276
  q: DBQuery<ROW>,
275
277
  patch: DBPatch<ROW>,
276
278
  opt: CacheDBOptions = {},
@@ -1,11 +1,11 @@
1
- import { CommonLogger, ObjectWithId } from '@naturalcycles/js-lib'
1
+ import { CommonLogger, PartialObjectWithId, Saved } from '@naturalcycles/js-lib'
2
2
  import { DBSaveBatchOperation } from '../../db.model'
3
3
  import type { DBQueryOrder } from '../../query/dbQuery'
4
4
 
5
5
  export interface FileDBPersistencePlugin {
6
6
  ping: () => Promise<void>
7
7
  getTables: () => Promise<string[]>
8
- loadFile: <ROW extends ObjectWithId>(table: string) => Promise<ROW[]>
8
+ loadFile: <ROW extends PartialObjectWithId>(table: string) => Promise<Saved<ROW>[]>
9
9
  saveFiles: (ops: DBSaveBatchOperation<any>[]) => Promise<void>
10
10
  }
11
11
 
@@ -8,9 +8,9 @@ import {
8
8
  _stringMapValues,
9
9
  JsonSchemaRootObject,
10
10
  _filterUndefinedValues,
11
- ObjectWithId,
12
11
  _assert,
13
12
  Saved,
13
+ PartialObjectWithId,
14
14
  } from '@naturalcycles/js-lib'
15
15
  import { readableCreate, ReadableTyped, dimGrey } from '@naturalcycles/nodejs-lib'
16
16
  import {
@@ -74,16 +74,16 @@ export class FileDB extends BaseCommonDB implements CommonDB {
74
74
  return tables
75
75
  }
76
76
 
77
- override async getByIds<ROW extends ObjectWithId>(
77
+ override async getByIds<ROW extends PartialObjectWithId>(
78
78
  table: string,
79
- ids: ROW['id'][],
79
+ ids: string[],
80
80
  _opt?: CommonDBOptions,
81
- ): Promise<ROW[]> {
81
+ ): Promise<Saved<ROW>[]> {
82
82
  const byId = _by(await this.loadFile<ROW>(table), r => r.id)
83
83
  return ids.map(id => byId[id]!).filter(Boolean)
84
84
  }
85
85
 
86
- override async saveBatch<ROW extends Partial<ObjectWithId>>(
86
+ override async saveBatch<ROW extends PartialObjectWithId>(
87
87
  table: string,
88
88
  rows: ROW[],
89
89
  _opt?: CommonDBSaveOptions<ROW>,
@@ -111,23 +111,23 @@ export class FileDB extends BaseCommonDB implements CommonDB {
111
111
  }
112
112
  }
113
113
 
114
- override async runQuery<ROW extends ObjectWithId>(
114
+ override async runQuery<ROW extends PartialObjectWithId>(
115
115
  q: DBQuery<ROW>,
116
116
  _opt?: CommonDBOptions,
117
- ): Promise<RunQueryResult<ROW>> {
117
+ ): Promise<RunQueryResult<Saved<ROW>>> {
118
118
  return {
119
119
  rows: queryInMemory(q, await this.loadFile<ROW>(q.table)),
120
120
  }
121
121
  }
122
122
 
123
- override async runQueryCount<ROW extends ObjectWithId>(
123
+ override async runQueryCount<ROW extends PartialObjectWithId>(
124
124
  q: DBQuery<ROW>,
125
125
  _opt?: CommonDBOptions,
126
126
  ): Promise<number> {
127
127
  return (await this.loadFile(q.table)).length
128
128
  }
129
129
 
130
- override streamQuery<ROW extends ObjectWithId>(
130
+ override streamQuery<ROW extends PartialObjectWithId>(
131
131
  q: DBQuery<ROW>,
132
132
  opt?: CommonDBStreamOptions,
133
133
  ): ReadableTyped<ROW> {
@@ -141,7 +141,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
141
141
  return readable
142
142
  }
143
143
 
144
- override async deleteByQuery<ROW extends ObjectWithId>(
144
+ override async deleteByQuery<ROW extends PartialObjectWithId>(
145
145
  q: DBQuery<ROW>,
146
146
  _opt?: CommonDBOptions,
147
147
  ): Promise<number> {
@@ -181,7 +181,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
181
181
  return deleted
182
182
  }
183
183
 
184
- override async getTableSchema<ROW extends ObjectWithId>(
184
+ override async getTableSchema<ROW extends PartialObjectWithId>(
185
185
  table: string,
186
186
  ): Promise<JsonSchemaRootObject<ROW>> {
187
187
  const rows = await this.loadFile(table)
@@ -192,7 +192,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
192
192
  }
193
193
 
194
194
  // wrapper, to handle logging
195
- async loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]> {
195
+ async loadFile<ROW extends PartialObjectWithId>(table: string): Promise<Saved<ROW>[]> {
196
196
  const started = this.logStarted(`loadFile(${table})`)
197
197
  const rows = await this.cfg.plugin.loadFile<ROW>(table)
198
198
  this.logFinished(started, `loadFile(${table}) ${rows.length} row(s)`)
@@ -200,7 +200,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
200
200
  }
201
201
 
202
202
  // wrapper, to handle logging, sorting rows before saving
203
- async saveFile<ROW extends ObjectWithId>(table: string, _rows: ROW[]): Promise<void> {
203
+ async saveFile<ROW extends PartialObjectWithId>(table: string, _rows: ROW[]): Promise<void> {
204
204
  // if (!_rows.length) return // NO, it should be able to save file with 0 rows!
205
205
 
206
206
  // Sort the rows, if needed
@@ -212,7 +212,9 @@ export class FileDB extends BaseCommonDB implements CommonDB {
212
212
  this.logFinished(started, op)
213
213
  }
214
214
 
215
- async saveFiles<ROW extends ObjectWithId>(ops: DBSaveBatchOperation<ROW>[]): Promise<void> {
215
+ async saveFiles<ROW extends PartialObjectWithId>(
216
+ ops: DBSaveBatchOperation<ROW>[],
217
+ ): Promise<void> {
216
218
  if (!ops.length) return
217
219
  const op =
218
220
  `saveFiles ${ops.length} op(s):\n` + ops.map(o => `${o.table} (${o.rows.length})`).join('\n')
@@ -225,7 +227,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
225
227
  // return new FileDBTransaction(this)
226
228
  // }
227
229
 
228
- sortRows<ROW extends ObjectWithId>(rows: ROW[]): ROW[] {
230
+ sortRows<ROW extends PartialObjectWithId>(rows: ROW[]): ROW[] {
229
231
  rows = rows.map(r => _filterUndefinedValues(r))
230
232
 
231
233
  if (this.cfg.sortOnSave) {
@@ -2,7 +2,7 @@ import fs from 'node:fs'
2
2
  import fsp from 'node:fs/promises'
3
3
  import { Readable } from 'node:stream'
4
4
  import { createGzip, createUnzip } from 'node:zlib'
5
- import { pMap, ObjectWithId } from '@naturalcycles/js-lib'
5
+ import { pMap, PartialObjectWithId, Saved } from '@naturalcycles/js-lib'
6
6
  import {
7
7
  transformJsonParse,
8
8
  transformSplit,
@@ -48,7 +48,7 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
48
48
  .map(f => f.split('.ndjson')[0]!)
49
49
  }
50
50
 
51
- async loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]> {
51
+ async loadFile<ROW extends PartialObjectWithId>(table: string): Promise<Saved<ROW>[]> {
52
52
  await fs2.ensureDirAsync(this.cfg.storagePath)
53
53
  const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`
54
54
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`
@@ -57,7 +57,7 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
57
57
 
58
58
  const transformUnzip = this.cfg.gzip ? [createUnzip()] : []
59
59
 
60
- const rows: ROW[] = []
60
+ const rows: Saved<ROW>[] = []
61
61
 
62
62
  await _pipeline([
63
63
  fs.createReadStream(filePath),
@@ -74,7 +74,7 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
74
74
  await pMap(ops, async op => await this.saveFile(op.table, op.rows), { concurrency: 16 })
75
75
  }
76
76
 
77
- async saveFile<ROW extends ObjectWithId>(table: string, rows: ROW[]): Promise<void> {
77
+ async saveFile<ROW extends PartialObjectWithId>(table: string, rows: ROW[]): Promise<void> {
78
78
  await fs2.ensureDirAsync(this.cfg.storagePath)
79
79
  const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`
80
80
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`
@@ -1,4 +1,4 @@
1
- import { ObjectWithId } from '@naturalcycles/js-lib'
1
+ import { PartialObjectWithId, Saved } from '@naturalcycles/js-lib'
2
2
  import { DBSaveBatchOperation } from '../../db.model'
3
3
  import { FileDBPersistencePlugin } from './file.db.model'
4
4
 
@@ -9,7 +9,7 @@ export class NoopPersistencePlugin implements FileDBPersistencePlugin {
9
9
  return []
10
10
  }
11
11
 
12
- async loadFile<ROW extends ObjectWithId>(_table: string): Promise<ROW[]> {
12
+ async loadFile<ROW extends PartialObjectWithId>(_table: string): Promise<Saved<ROW>[]> {
13
13
  return []
14
14
  }
15
15
 
@@ -16,6 +16,8 @@ import {
16
16
  CommonLogger,
17
17
  _deepCopy,
18
18
  _assert,
19
+ PartialObjectWithId,
20
+ Saved,
19
21
  } from '@naturalcycles/js-lib'
20
22
  import {
21
23
  bufferReviver,
@@ -152,7 +154,7 @@ export class InMemoryDB implements CommonDB {
152
154
  return Object.keys(this.data).filter(t => t.startsWith(this.cfg.tablesPrefix))
153
155
  }
154
156
 
155
- async getTableSchema<ROW extends ObjectWithId>(
157
+ async getTableSchema<ROW extends PartialObjectWithId>(
156
158
  _table: string,
157
159
  ): Promise<JsonSchemaRootObject<ROW>> {
158
160
  const table = this.cfg.tablesPrefix + _table
@@ -162,7 +164,7 @@ export class InMemoryDB implements CommonDB {
162
164
  }
163
165
  }
164
166
 
165
- async createTable<ROW extends ObjectWithId>(
167
+ async createTable<ROW extends PartialObjectWithId>(
166
168
  _table: string,
167
169
  _schema: JsonSchemaObject<ROW>,
168
170
  opt: CommonDBCreateOptions = {},
@@ -175,17 +177,17 @@ export class InMemoryDB implements CommonDB {
175
177
  }
176
178
  }
177
179
 
178
- async getByIds<ROW extends ObjectWithId>(
180
+ async getByIds<ROW extends PartialObjectWithId>(
179
181
  _table: string,
180
- ids: ROW['id'][],
182
+ ids: string[],
181
183
  _opt?: CommonDBOptions,
182
- ): Promise<ROW[]> {
184
+ ): Promise<Saved<ROW>[]> {
183
185
  const table = this.cfg.tablesPrefix + _table
184
186
  this.data[table] ||= {}
185
- return ids.map(id => this.data[table]![id] as ROW).filter(Boolean)
187
+ return ids.map(id => this.data[table]![id] as Saved<ROW>).filter(Boolean)
186
188
  }
187
189
 
188
- async saveBatch<ROW extends Partial<ObjectWithId>>(
190
+ async saveBatch<ROW extends PartialObjectWithId>(
189
191
  _table: string,
190
192
  rows: ROW[],
191
193
  opt: CommonDBSaveOptions<ROW> = {},
@@ -216,13 +218,13 @@ export class InMemoryDB implements CommonDB {
216
218
  })
217
219
  }
218
220
 
219
- async deleteByQuery<ROW extends ObjectWithId>(
221
+ async deleteByQuery<ROW extends PartialObjectWithId>(
220
222
  q: DBQuery<ROW>,
221
223
  _opt?: CommonDBOptions,
222
224
  ): Promise<number> {
223
225
  const table = this.cfg.tablesPrefix + q.table
224
226
  if (!this.data[table]) return 0
225
- const ids = queryInMemory(q, Object.values(this.data[table]!) as ROW[]).map(r => r.id)
227
+ const ids = queryInMemory(q, Object.values(this.data[table]!) as Saved<ROW>[]).map(r => r.id)
226
228
  return await this.deleteByIds(q.table, ids)
227
229
  }
228
230
 
@@ -240,7 +242,7 @@ export class InMemoryDB implements CommonDB {
240
242
  return count
241
243
  }
242
244
 
243
- async updateByQuery<ROW extends ObjectWithId>(
245
+ async updateByQuery<ROW extends PartialObjectWithId>(
244
246
  q: DBQuery<ROW>,
245
247
  patch: DBPatch<ROW>,
246
248
  ): Promise<number> {
@@ -262,15 +264,15 @@ export class InMemoryDB implements CommonDB {
262
264
  return rows.length
263
265
  }
264
266
 
265
- async runQuery<ROW extends ObjectWithId>(
267
+ async runQuery<ROW extends PartialObjectWithId>(
266
268
  q: DBQuery<ROW>,
267
269
  _opt?: CommonDBOptions,
268
- ): Promise<RunQueryResult<ROW>> {
270
+ ): Promise<RunQueryResult<Saved<ROW>>> {
269
271
  const table = this.cfg.tablesPrefix + q.table
270
- return { rows: queryInMemory(q, Object.values(this.data[table] || {}) as ROW[]) }
272
+ return { rows: queryInMemory(q, Object.values(this.data[table] || {}) as Saved<ROW>[]) }
271
273
  }
272
274
 
273
- async runQueryCount<ROW extends ObjectWithId>(
275
+ async runQueryCount<ROW extends PartialObjectWithId>(
274
276
  q: DBQuery<ROW>,
275
277
  _opt?: CommonDBOptions,
276
278
  ): Promise<number> {
@@ -278,10 +280,10 @@ export class InMemoryDB implements CommonDB {
278
280
  return queryInMemory<any>(q, Object.values(this.data[table] || {})).length
279
281
  }
280
282
 
281
- streamQuery<ROW extends ObjectWithId>(
283
+ streamQuery<ROW extends PartialObjectWithId>(
282
284
  q: DBQuery<ROW>,
283
285
  _opt?: CommonDBOptions,
284
- ): ReadableTyped<ROW> {
286
+ ): ReadableTyped<Saved<ROW>> {
285
287
  const table = this.cfg.tablesPrefix + q.table
286
288
  return Readable.from(queryInMemory(q, Object.values(this.data[table] || {}) as ROW[]))
287
289
  }
@@ -388,7 +390,7 @@ export class InMemoryDBTransaction implements DBTransaction {
388
390
  // used to enforce forbidReadAfterWrite setting
389
391
  writeOperationHappened = false
390
392
 
391
- async getByIds<ROW extends ObjectWithId>(
393
+ async getByIds<ROW extends PartialObjectWithId>(
392
394
  table: string,
393
395
  ids: string[],
394
396
  opt?: CommonDBOptions,
@@ -403,7 +405,7 @@ export class InMemoryDBTransaction implements DBTransaction {
403
405
  return await this.db.getByIds(table, ids, opt)
404
406
  }
405
407
 
406
- async saveBatch<ROW extends Partial<ObjectWithId>>(
408
+ async saveBatch<ROW extends PartialObjectWithId>(
407
409
  table: string,
408
410
  rows: ROW[],
409
411
  opt?: CommonDBSaveOptions<ROW>,
@@ -1,4 +1,4 @@
1
- import { _pick, ObjectWithId } from '@naturalcycles/js-lib'
1
+ import { _pick, PartialObjectWithId } from '@naturalcycles/js-lib'
2
2
  import { DBQuery, DBQueryFilterOperator } from '../../query/dbQuery'
3
3
 
4
4
  type FilterFn = (v: any, val: any) => boolean
@@ -18,7 +18,10 @@ const FILTER_FNS: Record<DBQueryFilterOperator, FilterFn> = {
18
18
 
19
19
  // Important: q.table is not used in this function, so tablesPrefix is not needed.
20
20
  // But should be careful here..
21
- export function queryInMemory<ROW extends ObjectWithId>(q: DBQuery<ROW>, rows: ROW[] = []): ROW[] {
21
+ export function queryInMemory<ROW extends PartialObjectWithId>(
22
+ q: DBQuery<ROW>,
23
+ rows: ROW[] = [],
24
+ ): ROW[] {
22
25
  // .filter
23
26
  // eslint-disable-next-line unicorn/no-array-reduce
24
27
  rows = q._filters.reduce((rows, filter) => {
@@ -1,4 +1,9 @@
1
- import { JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib'
1
+ import {
2
+ JsonSchemaObject,
3
+ JsonSchemaRootObject,
4
+ PartialObjectWithId,
5
+ Saved,
6
+ } from '@naturalcycles/js-lib'
2
7
  import { ReadableTyped } from '@naturalcycles/nodejs-lib'
3
8
  import { CommonDB, CommonDBSupport, CommonDBType } from './common.db'
4
9
  import {
@@ -31,28 +36,31 @@ export class BaseCommonDB implements CommonDB {
31
36
  throw new Error('getTables is not implemented')
32
37
  }
33
38
 
34
- async getTableSchema<ROW extends ObjectWithId>(
39
+ async getTableSchema<ROW extends PartialObjectWithId>(
35
40
  table: string,
36
41
  ): Promise<JsonSchemaRootObject<ROW>> {
37
42
  throw new Error('getTableSchema is not implemented')
38
43
  }
39
44
 
40
- async createTable<ROW extends ObjectWithId>(
45
+ async createTable<ROW extends PartialObjectWithId>(
41
46
  table: string,
42
47
  schema: JsonSchemaObject<ROW>,
43
48
  ): Promise<void> {
44
49
  // no-op
45
50
  }
46
51
 
47
- async getByIds<ROW extends ObjectWithId>(table: string, ids: string[]): Promise<ROW[]> {
52
+ async getByIds<ROW extends PartialObjectWithId>(
53
+ table: string,
54
+ ids: string[],
55
+ ): Promise<Saved<ROW>[]> {
48
56
  throw new Error('getByIds is not implemented')
49
57
  }
50
58
 
51
- async deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<number> {
59
+ async deleteByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>): Promise<number> {
52
60
  throw new Error('deleteByQuery is not implemented')
53
61
  }
54
62
 
55
- async updateByQuery<ROW extends ObjectWithId>(
63
+ async updateByQuery<ROW extends PartialObjectWithId>(
56
64
  q: DBQuery<ROW>,
57
65
  patch: DBPatch<ROW>,
58
66
  opt?: CommonDBOptions,
@@ -60,15 +68,17 @@ export class BaseCommonDB implements CommonDB {
60
68
  throw new Error('updateByQuery is not implemented')
61
69
  }
62
70
 
63
- async runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<RunQueryResult<ROW>> {
71
+ async runQuery<ROW extends PartialObjectWithId>(
72
+ q: DBQuery<ROW>,
73
+ ): Promise<RunQueryResult<Saved<ROW>>> {
64
74
  throw new Error('runQuery is not implemented')
65
75
  }
66
76
 
67
- async runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>): Promise<number> {
77
+ async runQueryCount<ROW extends PartialObjectWithId>(q: DBQuery<ROW>): Promise<number> {
68
78
  throw new Error('runQueryCount is not implemented')
69
79
  }
70
80
 
71
- async saveBatch<ROW extends Partial<ObjectWithId>>(
81
+ async saveBatch<ROW extends PartialObjectWithId>(
72
82
  table: string,
73
83
  rows: ROW[],
74
84
  opt?: CommonDBSaveOptions<ROW>,
@@ -76,7 +86,7 @@ export class BaseCommonDB implements CommonDB {
76
86
  throw new Error('saveBatch is not implemented')
77
87
  }
78
88
 
79
- streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>): ReadableTyped<ROW> {
89
+ streamQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>): ReadableTyped<Saved<ROW>> {
80
90
  throw new Error('streamQuery is not implemented')
81
91
  }
82
92