@naturalcycles/db-lib 8.18.0 → 8.19.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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [8.19.0](https://github.com/NaturalCycles/db-lib/compare/v8.18.0...v8.19.0) (2021-10-17)
2
+
3
+
4
+ ### Features
5
+
6
+ * better-typed Queries, excludeFromIndexes ([b7a37ba](https://github.com/NaturalCycles/db-lib/commit/b7a37ba0ed28d5047f4cea5a5f5900f7d1fe5da4))
7
+
1
8
  # [8.18.0](https://github.com/NaturalCycles/db-lib/compare/v8.17.0...v8.18.0) (2021-10-15)
2
9
 
3
10
 
@@ -23,12 +23,12 @@ export declare class CacheDB extends BaseCommonDB implements CommonDB {
23
23
  */
24
24
  getTables(): Promise<string[]>;
25
25
  getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
26
- createTable(table: string, schema: JsonSchemaObject, opt?: CacheDBCreateOptions): Promise<void>;
27
- getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CacheDBOptions): Promise<ROW[]>;
28
- deleteByIds(table: string, ids: string[], opt?: CacheDBOptions): Promise<number>;
29
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CacheDBOptions): Promise<void>;
30
- runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<RunQueryResult<ROW>>;
31
- runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>;
32
- streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBStreamOptions): Readable;
33
- deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>;
26
+ createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CacheDBCreateOptions<ROW>): Promise<void>;
27
+ getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CacheDBOptions<ROW>): Promise<ROW[]>;
28
+ deleteByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CacheDBOptions<ROW>): Promise<number>;
29
+ saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CacheDBOptions<ROW>): Promise<void>;
30
+ runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions<ROW>): Promise<RunQueryResult<ROW>>;
31
+ runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions<ROW>): Promise<number>;
32
+ streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBStreamOptions<ROW>): Readable;
33
+ deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions<ROW>): Promise<number>;
34
34
  }
@@ -1,5 +1,5 @@
1
1
  import { CommonDB } from '../../common.db';
2
- import { CommonDBCreateOptions, CommonDBSaveOptions, CommonDBStreamOptions } from '../../db.model';
2
+ import { CommonDBCreateOptions, CommonDBSaveOptions, CommonDBStreamOptions, ObjectWithId } from '../../db.model';
3
3
  export interface CacheDBCfg {
4
4
  name: string;
5
5
  cacheDB: CommonDB;
@@ -32,7 +32,7 @@ export interface CacheDBCfg {
32
32
  */
33
33
  logDownstream?: boolean;
34
34
  }
35
- export interface CacheDBOptions extends CommonDBSaveOptions {
35
+ export interface CacheDBOptions<ROW extends ObjectWithId> extends CommonDBSaveOptions<ROW> {
36
36
  /**
37
37
  * @default false
38
38
  */
@@ -42,7 +42,7 @@ export interface CacheDBOptions extends CommonDBSaveOptions {
42
42
  */
43
43
  onlyCache?: boolean;
44
44
  }
45
- export interface CacheDBStreamOptions extends CacheDBOptions, CommonDBStreamOptions {
45
+ export interface CacheDBStreamOptions<ROW extends ObjectWithId> extends CacheDBOptions<ROW>, CommonDBStreamOptions {
46
46
  }
47
- export interface CacheDBCreateOptions extends CacheDBOptions, CommonDBCreateOptions {
47
+ export interface CacheDBCreateOptions<ROW extends ObjectWithId> extends CacheDBOptions<ROW>, CommonDBCreateOptions {
48
48
  }
@@ -22,11 +22,11 @@ export declare class FileDB extends BaseCommonDB implements CommonDB {
22
22
  ping(): Promise<void>;
23
23
  getTables(): Promise<string[]>;
24
24
  getByIds<ROW extends ObjectWithId>(table: string, ids: string[], _opt?: CommonDBOptions): Promise<ROW[]>;
25
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], _opt?: CommonDBSaveOptions): Promise<void>;
25
+ saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], _opt?: CommonDBSaveOptions<ROW>): Promise<void>;
26
26
  /**
27
27
  * Implementation is optimized for loading/saving _whole files_.
28
28
  */
29
- commitTransaction(tx: DBTransaction, _opt?: CommonDBSaveOptions): Promise<void>;
29
+ commitTransaction(tx: DBTransaction, _opt?: CommonDBOptions): Promise<void>;
30
30
  runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<RunQueryResult<ROW>>;
31
31
  runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
32
32
  streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBStreamOptions): ReadableTyped<ROW>;
@@ -35,7 +35,7 @@ export declare class FileDB extends BaseCommonDB implements CommonDB {
35
35
  getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
36
36
  loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]>;
37
37
  saveFile<ROW extends ObjectWithId>(table: string, _rows: ROW[]): Promise<void>;
38
- saveFiles(ops: DBSaveBatchOperation[]): Promise<void>;
38
+ saveFiles<ROW extends ObjectWithId>(ops: DBSaveBatchOperation<ROW>[]): Promise<void>;
39
39
  /**
40
40
  * Mutates
41
41
  */
@@ -47,15 +47,15 @@ export declare class InMemoryDB implements CommonDB {
47
47
  resetCache(_table?: string): Promise<void>;
48
48
  getTables(): Promise<string[]>;
49
49
  getTableSchema<ROW extends ObjectWithId>(_table: string): Promise<JsonSchemaRootObject<ROW>>;
50
- createTable(_table: string, _schema: JsonSchemaObject, opt?: CommonDBCreateOptions): Promise<void>;
50
+ createTable<ROW extends ObjectWithId>(_table: string, _schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
51
51
  getByIds<ROW extends ObjectWithId>(_table: string, ids: string[], _opt?: CommonDBOptions): Promise<ROW[]>;
52
- saveBatch<ROW extends ObjectWithId>(_table: string, rows: ROW[], _opt?: CommonDBSaveOptions): Promise<void>;
52
+ saveBatch<ROW extends ObjectWithId>(_table: string, rows: ROW[], _opt?: CommonDBSaveOptions<ROW>): Promise<void>;
53
53
  deleteByIds(_table: string, ids: string[], _opt?: CommonDBOptions): Promise<number>;
54
54
  deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
55
55
  runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<RunQueryResult<ROW>>;
56
56
  runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
57
57
  streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): ReadableTyped<ROW>;
58
- commitTransaction(tx: DBTransaction, opt?: CommonDBSaveOptions): Promise<void>;
58
+ commitTransaction(tx: DBTransaction, opt?: CommonDBOptions): Promise<void>;
59
59
  /**
60
60
  * Flushes all tables (all namespaces) at once.
61
61
  */
@@ -1,7 +1,7 @@
1
1
  import { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib';
2
2
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
3
  import { CommonDB } from './common.db';
4
- import { CommonDBSaveOptions, ObjectWithId, RunQueryResult } from './db.model';
4
+ import { CommonDBOptions, ObjectWithId, RunQueryResult } from './db.model';
5
5
  import { DBQuery } from './query/dbQuery';
6
6
  import { DBTransaction } from './transaction/dbTransaction';
7
7
  /**
@@ -12,7 +12,7 @@ export declare class BaseCommonDB implements CommonDB {
12
12
  ping(): Promise<void>;
13
13
  getTables(): Promise<string[]>;
14
14
  getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
15
- createTable(_table: string, _schema: JsonSchemaObject): Promise<void>;
15
+ createTable<ROW extends ObjectWithId>(_table: string, _schema: JsonSchemaObject<ROW>): Promise<void>;
16
16
  deleteByIds(_table: string, _ids: string[]): Promise<number>;
17
17
  deleteByQuery<ROW extends ObjectWithId>(_q: DBQuery<ROW>): Promise<number>;
18
18
  getByIds<ROW extends ObjectWithId>(_table: string, _ids: string[]): Promise<ROW[]>;
@@ -24,5 +24,5 @@ export declare class BaseCommonDB implements CommonDB {
24
24
  * Naive implementation.
25
25
  * To be extended.
26
26
  */
27
- commitTransaction(tx: DBTransaction, opt?: CommonDBSaveOptions): Promise<void>;
27
+ commitTransaction(tx: DBTransaction, opt?: CommonDBOptions): Promise<void>;
28
28
  }
@@ -26,7 +26,7 @@ export interface CommonDB {
26
26
  * Will do like `create table ...` for mysql.
27
27
  * Caution! dropIfExists defaults to false. If set to true - will actually DROP the table!
28
28
  */
29
- createTable(table: string, schema: JsonSchemaObject, opt?: CommonDBCreateOptions): Promise<void>;
29
+ createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
30
30
  /**
31
31
  * Order of items returned is not guaranteed to match order of ids.
32
32
  * (Such limitation exists because Datastore doesn't support it).
@@ -38,7 +38,7 @@ export interface CommonDB {
38
38
  runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions): Promise<RunQueryResult<ROW>>;
39
39
  runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBOptions): Promise<number>;
40
40
  streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBStreamOptions): ReadableTyped<ROW>;
41
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions): Promise<void>;
41
+ saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
42
42
  /**
43
43
  * @returns number of deleted items.
44
44
  * Not supported by all implementations (e.g Datastore will always return same number as number of ids).
@@ -49,5 +49,5 @@ export interface CommonDB {
49
49
  * Should be implemented as a Transaction (best effort), which means that
50
50
  * either ALL or NONE of the operations should be applied.
51
51
  */
52
- commitTransaction(tx: DBTransaction, opt?: CommonDBSaveOptions): Promise<void>;
52
+ commitTransaction(tx: DBTransaction, opt?: CommonDBOptions): Promise<void>;
53
53
  }
@@ -32,8 +32,8 @@ export declare class CommonDao<BM extends Partial<ObjectWithId>, DBM extends Obj
32
32
  * Throws if readOnly is true
33
33
  */
34
34
  private requireWriteAccess;
35
- getBy(by: string, value: any, limit?: number, opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
36
- getOneBy(by: string, value: any, opt?: CommonDaoOptions): Promise<Saved<BM> | null>;
35
+ getBy(by: keyof DBM, value: any, limit?: number, opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
36
+ getOneBy(by: keyof DBM, value: any, opt?: CommonDaoOptions): Promise<Saved<BM> | null>;
37
37
  getAll(opt?: CommonDaoOptions): Promise<Saved<BM>[]>;
38
38
  /**
39
39
  * Pass `table` to override table
@@ -74,7 +74,7 @@ export declare class CommonDao<BM extends Partial<ObjectWithId>, DBM extends Obj
74
74
  /**
75
75
  * Mutates with id, created, updated
76
76
  */
77
- save(bm: BM, opt?: CommonDaoSaveOptions): Promise<Saved<BM>>;
77
+ save(bm: BM, opt?: CommonDaoSaveOptions<DBM>): Promise<Saved<BM>>;
78
78
  /**
79
79
  * Mutates id if needed
80
80
  */
@@ -87,11 +87,11 @@ export declare class CommonDao<BM extends Partial<ObjectWithId>, DBM extends Obj
87
87
  *
88
88
  * Convenience method to replace 3 operations (loading+patching+saving) with one.
89
89
  */
90
- patch(id: string, patch: Partial<BM>, opt?: CommonDaoSaveOptions): Promise<Saved<BM>>;
91
- patchAsDBM(id: string, patch: Partial<DBM>, opt?: CommonDaoSaveOptions): Promise<DBM>;
92
- saveAsDBM(dbm: DBM, opt?: CommonDaoSaveOptions): Promise<DBM>;
93
- saveBatch(bms: BM[], opt?: CommonDaoSaveOptions): Promise<Saved<BM>[]>;
94
- saveBatchAsDBM(dbms: DBM[], opt?: CommonDaoSaveOptions): Promise<DBM[]>;
90
+ patch(id: string, patch: Partial<BM>, opt?: CommonDaoSaveOptions<DBM>): Promise<Saved<BM>>;
91
+ patchAsDBM(id: string, patch: Partial<DBM>, opt?: CommonDaoSaveOptions<DBM>): Promise<DBM>;
92
+ saveAsDBM(dbm: DBM, opt?: CommonDaoSaveOptions<DBM>): Promise<DBM>;
93
+ saveBatch(bms: BM[], opt?: CommonDaoSaveOptions<DBM>): Promise<Saved<BM>[]>;
94
+ saveBatchAsDBM(dbms: DBM[], opt?: CommonDaoSaveOptions<DBM>): Promise<DBM[]>;
95
95
  /**
96
96
  * @returns number of deleted items
97
97
  */
@@ -155,10 +155,10 @@ class CommonDao {
155
155
  }
156
156
  }
157
157
  async getBy(by, value, limit = 0, opt) {
158
- return await this.query().filter(by, '==', value).limit(limit).runQuery(opt);
158
+ return await this.query().filterEq(by, value).limit(limit).runQuery(opt);
159
159
  }
160
160
  async getOneBy(by, value, opt) {
161
- const [bm] = await this.query().filter(by, '==', value).limit(1).runQuery(opt);
161
+ const [bm] = await this.query().filterEq(by, value).limit(1).runQuery(opt);
162
162
  return bm || null;
163
163
  }
164
164
  async getAll(opt) {
@@ -47,7 +47,7 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends Obje
47
47
  dbmSchema?: ObjectSchemaTyped<DBM> | AjvSchema<DBM>;
48
48
  bmSchema?: ObjectSchemaTyped<BM> | AjvSchema<BM>;
49
49
  tmSchema?: ObjectSchemaTyped<TM> | AjvSchema<TM>;
50
- excludeFromIndexes?: string[];
50
+ excludeFromIndexes?: (keyof DBM)[];
51
51
  /**
52
52
  * @default to false
53
53
  * Set to true to limit DB writing (will throw an error is such case).
@@ -116,7 +116,7 @@ export interface CommonDaoOptions extends CommonDBOptions {
116
116
  /**
117
117
  * All properties default to undefined.
118
118
  */
119
- export interface CommonDaoSaveOptions extends CommonDaoOptions, CommonDBSaveOptions {
119
+ export interface CommonDaoSaveOptions<DBM extends ObjectWithId> extends CommonDaoOptions, CommonDBSaveOptions<DBM> {
120
120
  /**
121
121
  * @default false
122
122
  *
@@ -1,11 +1,12 @@
1
+ import { AnyObject } from '@naturalcycles/js-lib';
1
2
  import { CommonDB } from './common.db';
2
3
  export interface CommonDBOptions {
3
4
  }
4
5
  /**
5
6
  * All properties default to undefined.
6
7
  */
7
- export interface CommonDBSaveOptions extends CommonDBOptions {
8
- excludeFromIndexes?: string[];
8
+ export interface CommonDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId> extends CommonDBOptions {
9
+ excludeFromIndexes?: (keyof ROW)[];
9
10
  }
10
11
  export declare type CommonDBStreamOptions = CommonDBOptions;
11
12
  export interface CommonDBCreateOptions extends CommonDBOptions {
@@ -20,7 +21,7 @@ export interface RunQueryResult<T> {
20
21
  endCursor?: string;
21
22
  }
22
23
  export declare type DBOperation = DBSaveBatchOperation | DBDeleteByIdsOperation;
23
- export interface DBSaveBatchOperation<ROW extends ObjectWithId = any> {
24
+ export interface DBSaveBatchOperation<ROW extends ObjectWithId = AnyObjectWithId> {
24
25
  type: 'saveBatch';
25
26
  table: string;
26
27
  rows: ROW[];
@@ -49,6 +50,8 @@ export interface CreatedUpdatedId extends CreatedUpdated {
49
50
  export interface ObjectWithId {
50
51
  id: string;
51
52
  }
53
+ export interface AnyObjectWithId extends AnyObject, ObjectWithId {
54
+ }
52
55
  /**
53
56
  * Interface for a module (lib) that implements CommonDB.
54
57
  *
package/dist/index.d.ts CHANGED
@@ -6,7 +6,7 @@ import { DBLibError } from './cnst';
6
6
  import { CommonDB } from './common.db';
7
7
  import { CommonDao } from './commondao/common.dao';
8
8
  import { CommonDaoAnonymizeHook, CommonDaoBeforeBMToDBMHook, CommonDaoBeforeBMToTMHook, CommonDaoBeforeCreateHook, CommonDaoBeforeDBMToBMHook, CommonDaoBeforeDBMValidateHook, CommonDaoBeforeTMToBMHook, CommonDaoCfg, CommonDaoCreateIdHook, CommonDaoCreateOptions, CommonDaoLogLevel, CommonDaoOptions, CommonDaoParseNaturalIdHook, CommonDaoSaveOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions } from './commondao/common.dao.model';
9
- import { CommonDBAdapter, CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, CreatedUpdated, CreatedUpdatedId, DBDeleteByIdsOperation, DBModelType, DBOperation, DBRelation, DBSaveBatchOperation, ObjectWithId, RunQueryResult } from './db.model';
9
+ import { AnyObjectWithId, CommonDBAdapter, CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, CreatedUpdated, CreatedUpdatedId, DBDeleteByIdsOperation, DBModelType, DBOperation, DBRelation, DBSaveBatchOperation, ObjectWithId, RunQueryResult } from './db.model';
10
10
  import { getDB } from './getDB';
11
11
  import { CommonKeyValueDao, CommonKeyValueDaoCfg } from './kv/commonKeyValueDao';
12
12
  import { CommonKeyValueDB, KeyValueDBTuple } from './kv/commonKeyValueDB';
@@ -17,5 +17,5 @@ import { dbPipelineRestore, DBPipelineRestoreOptions } from './pipeline/dbPipeli
17
17
  import { DBQuery, DBQueryFilter, DBQueryFilterOperator, dbQueryFilterOperatorValues, DBQueryOrder, RunnableDBQuery } from './query/dbQuery';
18
18
  import { DBTransaction, RunnableDBTransaction } from './transaction/dbTransaction';
19
19
  import { commitDBTransactionSimple, mergeDBOperations } from './transaction/dbTransaction.util';
20
- export type { DBQueryFilterOperator, DBQueryFilter, DBQueryOrder, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoSaveOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, CommonDBCreateOptions, CommonDB, RunQueryResult, CreatedUpdated, CreatedUpdatedId, ObjectWithId, CommonDaoCfg, CommonDaoCreateIdHook, CommonDaoParseNaturalIdHook, CommonDaoBeforeCreateHook, CommonDaoBeforeDBMValidateHook, CommonDaoBeforeDBMToBMHook, CommonDaoBeforeBMToDBMHook, CommonDaoBeforeTMToBMHook, CommonDaoBeforeBMToTMHook, CommonDaoAnonymizeHook, InMemoryDBCfg, InMemoryKeyValueDBCfg, DBPipelineBackupOptions, DBPipelineRestoreOptions, DBPipelineCopyOptions, CommonDBAdapter, DBOperation, DBSaveBatchOperation, DBDeleteByIdsOperation, CommonKeyValueDB, CommonKeyValueDaoCfg, KeyValueDBTuple, };
20
+ export type { DBQueryFilterOperator, DBQueryFilter, DBQueryOrder, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoSaveOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, CommonDBCreateOptions, CommonDB, RunQueryResult, CreatedUpdated, CreatedUpdatedId, AnyObjectWithId, ObjectWithId, CommonDaoCfg, CommonDaoCreateIdHook, CommonDaoParseNaturalIdHook, CommonDaoBeforeCreateHook, CommonDaoBeforeDBMValidateHook, CommonDaoBeforeDBMToBMHook, CommonDaoBeforeBMToDBMHook, CommonDaoBeforeTMToBMHook, CommonDaoBeforeBMToTMHook, CommonDaoAnonymizeHook, InMemoryDBCfg, InMemoryKeyValueDBCfg, DBPipelineBackupOptions, DBPipelineRestoreOptions, DBPipelineCopyOptions, CommonDBAdapter, DBOperation, DBSaveBatchOperation, DBDeleteByIdsOperation, CommonKeyValueDB, CommonKeyValueDaoCfg, KeyValueDBTuple, };
21
21
  export { DBQuery, dbQueryFilterOperatorValues, RunnableDBQuery, CommonDaoLogLevel, DBRelation, DBModelType, CommonDao, createdUpdatedFields, createdUpdatedIdFields, idField, InMemoryDB, InMemoryKeyValueDB, queryInMemory, serializeJsonField, deserializeJsonField, dbPipelineBackup, dbPipelineRestore, dbPipelineCopy, getDB, DBLibError, BaseCommonDB, DBTransaction, RunnableDBTransaction, mergeDBOperations, commitDBTransactionSimple, CommonKeyValueDao, };
@@ -2,7 +2,7 @@ import { AsyncMapper, Saved } from '@naturalcycles/js-lib';
2
2
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
3
3
  import { CommonDaoOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions } from '..';
4
4
  import { CommonDao } from '../commondao/common.dao';
5
- import { ObjectWithId, RunQueryResult } from '../db.model';
5
+ import { AnyObjectWithId, ObjectWithId, RunQueryResult } from '../db.model';
6
6
  /**
7
7
  * Modeled after Firestore operators (WhereFilterOp type)
8
8
  *
@@ -24,13 +24,13 @@ import { ObjectWithId, RunQueryResult } from '../db.model';
24
24
  */
25
25
  export declare type DBQueryFilterOperator = '<' | '<=' | '==' | '>=' | '>' | 'in' | 'not-in' | 'array-contains' | 'array-contains-any';
26
26
  export declare const dbQueryFilterOperatorValues: string[];
27
- export interface DBQueryFilter {
28
- name: string;
27
+ export interface DBQueryFilter<ROW extends ObjectWithId = AnyObjectWithId> {
28
+ name: keyof ROW;
29
29
  op: DBQueryFilterOperator;
30
30
  val: any;
31
31
  }
32
- export interface DBQueryOrder {
33
- name: string;
32
+ export interface DBQueryOrder<ROW extends ObjectWithId = AnyObjectWithId> {
33
+ name: keyof ROW;
34
34
  descending?: boolean;
35
35
  }
36
36
  /**
@@ -43,33 +43,33 @@ export interface DBQueryOrder {
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> {
46
+ export declare class DBQuery<ROW extends ObjectWithId = AnyObjectWithId> {
47
47
  table: string;
48
48
  constructor(table: string);
49
49
  /**
50
50
  * Convenience method.
51
51
  */
52
- static create<ROW extends ObjectWithId = any>(table: string): DBQuery<ROW>;
53
- static fromPlainObject<ROW extends ObjectWithId = any>(obj: Partial<DBQuery<ROW>> & {
52
+ static create<ROW extends ObjectWithId = AnyObjectWithId>(table: string): DBQuery<ROW>;
53
+ static fromPlainObject<ROW extends ObjectWithId = AnyObjectWithId>(obj: Partial<DBQuery<ROW>> & {
54
54
  table: string;
55
55
  }): DBQuery<ROW>;
56
- _filters: DBQueryFilter[];
56
+ _filters: DBQueryFilter<ROW>[];
57
57
  _limitValue: number;
58
58
  _offsetValue: number;
59
- _orders: DBQueryOrder[];
59
+ _orders: DBQueryOrder<ROW>[];
60
60
  _startCursor?: string;
61
61
  _endCursor?: string;
62
62
  /**
63
63
  * If defined - only those fields will be selected.
64
64
  * In undefined - all fields (*) will be returned.
65
65
  */
66
- _selectedFieldNames?: string[];
67
- filter(name: string, op: DBQueryFilterOperator, val: any): this;
68
- filterEq(name: string, val: any): this;
66
+ _selectedFieldNames?: (keyof ROW)[];
67
+ filter(name: keyof ROW, op: DBQueryFilterOperator, val: any): this;
68
+ filterEq(name: keyof ROW, val: any): this;
69
69
  limit(limit: number): this;
70
70
  offset(offset: number): this;
71
- order(name: string, descending?: boolean): this;
72
- select(fieldNames: string[]): this;
71
+ order(name: keyof ROW, descending?: boolean): this;
72
+ select(fieldNames: (keyof ROW)[]): this;
73
73
  startCursor(startCursor?: string): this;
74
74
  endCursor(endCursor?: string): this;
75
75
  clone(): DBQuery<ROW>;
@@ -13,17 +13,6 @@ exports.dbQueryFilterOperatorValues = [
13
13
  'array-contains',
14
14
  'array-contains-any',
15
15
  ];
16
- // export interface DBQueryData {
17
- // _filters: DBQueryFilter[]
18
- // _limitValue: number
19
- // _orders: DBQueryOrder[]
20
- //
21
- // /**
22
- // * If defined - only those fields will be selected.
23
- // * In undefined - all fields (*) will be returned.
24
- // */
25
- // _selectedFieldNames?: string[]
26
- // }
27
16
  /**
28
17
  * Lowest Common Denominator Query object.
29
18
  * To be executed by CommonDao / CommonDB.
@@ -66,7 +66,7 @@ class CommonTimeSeriesDao {
66
66
  dbq.filter('ts', '>=', q.fromIncl);
67
67
  if (q.toExcl)
68
68
  dbq.filter('ts', '<', q.toExcl);
69
- const { rows } = await this.cfg.db.runQuery(dbq);
69
+ const rows = (await this.cfg.db.runQuery(dbq)).rows;
70
70
  // todo: query from aggregated tables when step is above 'hour'
71
71
  return rows
72
72
  .filter(r => r.v !== null && r.v !== undefined) // can be 0
@@ -1,11 +1,11 @@
1
1
  import type { CommonDB } from '../common.db';
2
- import type { CommonDBSaveOptions, DBOperation, ObjectWithId } from '../db.model';
2
+ import type { AnyObjectWithId, CommonDBSaveOptions, DBOperation, ObjectWithId } from '../db.model';
3
3
  /**
4
4
  * Convenience class that stores the list of DBOperations and provides a fluent API to add them.
5
5
  */
6
6
  export declare class DBTransaction {
7
7
  ops: DBOperation[];
8
- saveBatch<ROW extends ObjectWithId = any>(table: string, rows: ROW[]): this;
8
+ saveBatch<ROW extends ObjectWithId = AnyObjectWithId>(table: string, rows: ROW[]): this;
9
9
  deleteByIds(table: string, ids: string[]): this;
10
10
  }
11
11
  /**
@@ -15,5 +15,5 @@ export declare class DBTransaction {
15
15
  export declare class RunnableDBTransaction extends DBTransaction {
16
16
  db: CommonDB;
17
17
  constructor(db: CommonDB);
18
- commit(opt?: CommonDBSaveOptions): Promise<void>;
18
+ commit<ROW extends ObjectWithId>(opt?: CommonDBSaveOptions<ROW>): Promise<void>;
19
19
  }
@@ -1,8 +1,8 @@
1
1
  import { CommonDBOptions, CommonDBSaveOptions } from '../db.model';
2
2
  import { DBQuery, DBQueryFilter, DBQueryOrder } from '../query/dbQuery';
3
3
  export declare const commonDBOptionsSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<CommonDBOptions, CommonDBOptions>;
4
- export declare const commonDBSaveOptionsSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<CommonDBSaveOptions, CommonDBSaveOptions>;
4
+ export declare const commonDBSaveOptionsSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<CommonDBSaveOptions<import("../db.model").AnyObjectWithId>, CommonDBSaveOptions<import("../db.model").AnyObjectWithId>>;
5
5
  export declare const dbQueryFilterOperatorSchema: import("@naturalcycles/nodejs-lib/dist/validation/joi/string.extensions").ExtendedStringSchema;
6
- export declare const dbQueryFilterSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryFilter, DBQueryFilter>;
7
- export declare const dbQueryOrderSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryOrder, DBQueryOrder>;
6
+ export declare const dbQueryFilterSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryFilter<import("../db.model").AnyObjectWithId>, DBQueryFilter<import("../db.model").AnyObjectWithId>>;
7
+ export declare const dbQueryOrderSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryOrder<import("../db.model").AnyObjectWithId>, DBQueryOrder<import("../db.model").AnyObjectWithId>>;
8
8
  export declare const dbQuerySchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQuery<any>, DBQuery<any>>;
package/package.json CHANGED
@@ -42,7 +42,7 @@
42
42
  "engines": {
43
43
  "node": ">=12.13"
44
44
  },
45
- "version": "8.18.0",
45
+ "version": "8.19.0",
46
46
  "description": "Lowest Common Denominator API to supported Databases",
47
47
  "keywords": [
48
48
  "db",
@@ -1,5 +1,10 @@
1
1
  import { CommonDB } from '../../common.db'
2
- import { CommonDBCreateOptions, CommonDBSaveOptions, CommonDBStreamOptions } from '../../db.model'
2
+ import {
3
+ CommonDBCreateOptions,
4
+ CommonDBSaveOptions,
5
+ CommonDBStreamOptions,
6
+ ObjectWithId,
7
+ } from '../../db.model'
3
8
 
4
9
  export interface CacheDBCfg {
5
10
  name: string
@@ -39,7 +44,7 @@ export interface CacheDBCfg {
39
44
  logDownstream?: boolean
40
45
  }
41
46
 
42
- export interface CacheDBOptions extends CommonDBSaveOptions {
47
+ export interface CacheDBOptions<ROW extends ObjectWithId> extends CommonDBSaveOptions<ROW> {
43
48
  /**
44
49
  * @default false
45
50
  */
@@ -51,5 +56,9 @@ export interface CacheDBOptions extends CommonDBSaveOptions {
51
56
  onlyCache?: boolean
52
57
  }
53
58
 
54
- export interface CacheDBStreamOptions extends CacheDBOptions, CommonDBStreamOptions {}
55
- export interface CacheDBCreateOptions extends CacheDBOptions, CommonDBCreateOptions {}
59
+ export interface CacheDBStreamOptions<ROW extends ObjectWithId>
60
+ extends CacheDBOptions<ROW>,
61
+ CommonDBStreamOptions {}
62
+ export interface CacheDBCreateOptions<ROW extends ObjectWithId>
63
+ extends CacheDBOptions<ROW>,
64
+ CommonDBCreateOptions {}
@@ -49,10 +49,10 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
49
49
  return await this.cfg.downstreamDB.getTableSchema<ROW>(table)
50
50
  }
51
51
 
52
- override async createTable(
52
+ override async createTable<ROW extends ObjectWithId>(
53
53
  table: string,
54
- schema: JsonSchemaObject,
55
- opt: CacheDBCreateOptions = {},
54
+ schema: JsonSchemaObject<ROW>,
55
+ opt: CacheDBCreateOptions<ROW> = {},
56
56
  ): Promise<void> {
57
57
  if (!opt.onlyCache && !this.cfg.onlyCache) {
58
58
  await this.cfg.downstreamDB.createTable(table, schema, opt)
@@ -66,7 +66,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
66
66
  override async getByIds<ROW extends ObjectWithId>(
67
67
  table: string,
68
68
  ids: string[],
69
- opt: CacheDBOptions = {},
69
+ opt: CacheDBOptions<ROW> = {},
70
70
  ): Promise<ROW[]> {
71
71
  const resultMap: Record<string, ROW> = {}
72
72
  const missingIds: string[] = []
@@ -109,10 +109,10 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
109
109
  return ids.map(id => resultMap[id]!).filter(Boolean)
110
110
  }
111
111
 
112
- override async deleteByIds(
112
+ override async deleteByIds<ROW extends ObjectWithId>(
113
113
  table: string,
114
114
  ids: string[],
115
- opt: CacheDBOptions = {},
115
+ opt: CacheDBOptions<ROW> = {},
116
116
  ): Promise<number> {
117
117
  let deletedIds = 0
118
118
 
@@ -139,7 +139,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
139
139
  override async saveBatch<ROW extends ObjectWithId>(
140
140
  table: string,
141
141
  rows: ROW[],
142
- opt: CacheDBOptions = {},
142
+ opt: CacheDBOptions<ROW> = {},
143
143
  ): Promise<void> {
144
144
  if (!opt.onlyCache && !this.cfg.onlyCache) {
145
145
  await this.cfg.downstreamDB.saveBatch(table, rows, opt)
@@ -167,7 +167,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
167
167
 
168
168
  override async runQuery<ROW extends ObjectWithId>(
169
169
  q: DBQuery<ROW>,
170
- opt: CacheDBOptions = {},
170
+ opt: CacheDBOptions<ROW> = {},
171
171
  ): Promise<RunQueryResult<ROW>> {
172
172
  if (!opt.onlyCache && !this.cfg.onlyCache) {
173
173
  const { rows, ...queryResult } = await this.cfg.downstreamDB.runQuery(q, opt)
@@ -197,7 +197,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
197
197
 
198
198
  override async runQueryCount<ROW extends ObjectWithId>(
199
199
  q: DBQuery<ROW>,
200
- opt: CacheDBOptions = {},
200
+ opt: CacheDBOptions<ROW> = {},
201
201
  ): Promise<number> {
202
202
  if (!opt.onlyCache && !this.cfg.onlyCache) {
203
203
  return await this.cfg.downstreamDB.runQueryCount(q, opt)
@@ -214,7 +214,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
214
214
 
215
215
  override streamQuery<ROW extends ObjectWithId>(
216
216
  q: DBQuery<ROW>,
217
- opt: CacheDBStreamOptions = {},
217
+ opt: CacheDBStreamOptions<ROW> = {},
218
218
  ): Readable {
219
219
  if (!opt.onlyCache && !this.cfg.onlyCache) {
220
220
  const stream = this.cfg.downstreamDB.streamQuery<ROW>(q, opt)
@@ -253,7 +253,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
253
253
 
254
254
  override async deleteByQuery<ROW extends ObjectWithId>(
255
255
  q: DBQuery<ROW>,
256
- opt: CacheDBOptions = {},
256
+ opt: CacheDBOptions<ROW> = {},
257
257
  ): Promise<number> {
258
258
  if (!opt.onlyCache && !this.cfg.onlyCache) {
259
259
  const deletedIds = await this.cfg.downstreamDB.deleteByQuery(q, opt)
@@ -13,7 +13,13 @@ import {
13
13
  } from '@naturalcycles/js-lib'
14
14
  import { Debug, readableCreate, ReadableTyped } from '@naturalcycles/nodejs-lib'
15
15
  import { dimGrey } from '@naturalcycles/nodejs-lib/dist/colors'
16
- import { BaseCommonDB, DBSaveBatchOperation, ObjectWithId, queryInMemory } from '../..'
16
+ import {
17
+ AnyObjectWithId,
18
+ BaseCommonDB,
19
+ DBSaveBatchOperation,
20
+ ObjectWithId,
21
+ queryInMemory,
22
+ } from '../..'
17
23
  import { CommonDB } from '../../common.db'
18
24
  import {
19
25
  CommonDBOptions,
@@ -72,7 +78,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
72
78
  override async saveBatch<ROW extends ObjectWithId>(
73
79
  table: string,
74
80
  rows: ROW[],
75
- _opt?: CommonDBSaveOptions,
81
+ _opt?: CommonDBSaveOptions<ROW>,
76
82
  ): Promise<void> {
77
83
  if (!rows.length) return // save some api calls
78
84
 
@@ -98,7 +104,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
98
104
  /**
99
105
  * Implementation is optimized for loading/saving _whole files_.
100
106
  */
101
- override async commitTransaction(tx: DBTransaction, _opt?: CommonDBSaveOptions): Promise<void> {
107
+ override async commitTransaction(tx: DBTransaction, _opt?: CommonDBOptions): Promise<void> {
102
108
  // data[table][id] => row
103
109
  const data: StringMap<StringMap<ObjectWithId>> = {}
104
110
 
@@ -131,7 +137,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
131
137
  return {
132
138
  type: 'saveBatch',
133
139
  table,
134
- rows: this.sortRows(Object.values(data[table]!)),
140
+ rows: this.sortRows(Object.values(data[table]!) as AnyObjectWithId[]),
135
141
  }
136
142
  })
137
143
 
@@ -242,7 +248,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
242
248
  this.logFinished(started, op)
243
249
  }
244
250
 
245
- async saveFiles(ops: DBSaveBatchOperation[]): Promise<void> {
251
+ async saveFiles<ROW extends ObjectWithId>(ops: DBSaveBatchOperation<ROW>[]): Promise<void> {
246
252
  if (!ops.length) return
247
253
  const op =
248
254
  `saveFiles ${ops.length} op(s):\n` + ops.map(o => `${o.table} (${o.rows.length})`).join('\n')
@@ -122,9 +122,9 @@ export class InMemoryDB implements CommonDB {
122
122
  }
123
123
  }
124
124
 
125
- async createTable(
125
+ async createTable<ROW extends ObjectWithId>(
126
126
  _table: string,
127
- _schema: JsonSchemaObject,
127
+ _schema: JsonSchemaObject<ROW>,
128
128
  opt: CommonDBCreateOptions = {},
129
129
  ): Promise<void> {
130
130
  const table = this.cfg.tablesPrefix + _table
@@ -148,7 +148,7 @@ export class InMemoryDB implements CommonDB {
148
148
  async saveBatch<ROW extends ObjectWithId>(
149
149
  _table: string,
150
150
  rows: ROW[],
151
- _opt?: CommonDBSaveOptions,
151
+ _opt?: CommonDBSaveOptions<ROW>,
152
152
  ): Promise<void> {
153
153
  const table = this.cfg.tablesPrefix + _table
154
154
  this.data[table] = this.data[table] || {}
@@ -220,7 +220,7 @@ export class InMemoryDB implements CommonDB {
220
220
  return Readable.from(queryInMemory(q, Object.values(this.data[table] || {}) as ROW[]))
221
221
  }
222
222
 
223
- async commitTransaction(tx: DBTransaction, opt?: CommonDBSaveOptions): Promise<void> {
223
+ async commitTransaction(tx: DBTransaction, opt?: CommonDBOptions): Promise<void> {
224
224
  for await (const op of tx.ops) {
225
225
  if (op.type === 'saveBatch') {
226
226
  await this.saveBatch(op.table, op.rows, opt)
@@ -2,7 +2,7 @@ import { Readable } from 'stream'
2
2
  import { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib'
3
3
  import { ReadableTyped } from '@naturalcycles/nodejs-lib'
4
4
  import { CommonDB } from './common.db'
5
- import { CommonDBSaveOptions, ObjectWithId, RunQueryResult } from './db.model'
5
+ import { CommonDBOptions, ObjectWithId, RunQueryResult } from './db.model'
6
6
  import { DBQuery } from './query/dbQuery'
7
7
  import { DBTransaction } from './transaction/dbTransaction'
8
8
  import { commitDBTransactionSimple } from './transaction/dbTransaction.util'
@@ -30,7 +30,10 @@ export class BaseCommonDB implements CommonDB {
30
30
  }
31
31
  }
32
32
 
33
- async createTable(_table: string, _schema: JsonSchemaObject): Promise<void> {}
33
+ async createTable<ROW extends ObjectWithId>(
34
+ _table: string,
35
+ _schema: JsonSchemaObject<ROW>,
36
+ ): Promise<void> {}
34
37
 
35
38
  async deleteByIds(_table: string, _ids: string[]): Promise<number> {
36
39
  return 0
@@ -62,7 +65,7 @@ export class BaseCommonDB implements CommonDB {
62
65
  * Naive implementation.
63
66
  * To be extended.
64
67
  */
65
- async commitTransaction(tx: DBTransaction, opt?: CommonDBSaveOptions): Promise<void> {
68
+ async commitTransaction(tx: DBTransaction, opt?: CommonDBOptions): Promise<void> {
66
69
  await commitDBTransactionSimple(this, tx, opt)
67
70
  }
68
71
  }
package/src/common.db.ts CHANGED
@@ -37,7 +37,11 @@ export interface CommonDB {
37
37
  * Will do like `create table ...` for mysql.
38
38
  * Caution! dropIfExists defaults to false. If set to true - will actually DROP the table!
39
39
  */
40
- createTable(table: string, schema: JsonSchemaObject, opt?: CommonDBCreateOptions): Promise<void>
40
+ createTable<ROW extends ObjectWithId>(
41
+ table: string,
42
+ schema: JsonSchemaObject<ROW>,
43
+ opt?: CommonDBCreateOptions,
44
+ ): Promise<void>
41
45
 
42
46
  // GET
43
47
  /**
@@ -70,7 +74,7 @@ export interface CommonDB {
70
74
  saveBatch<ROW extends ObjectWithId>(
71
75
  table: string,
72
76
  rows: ROW[],
73
- opt?: CommonDBSaveOptions,
77
+ opt?: CommonDBSaveOptions<ROW>,
74
78
  ): Promise<void>
75
79
 
76
80
  // DELETE
@@ -87,5 +91,5 @@ export interface CommonDB {
87
91
  * Should be implemented as a Transaction (best effort), which means that
88
92
  * either ALL or NONE of the operations should be applied.
89
93
  */
90
- commitTransaction(tx: DBTransaction, opt?: CommonDBSaveOptions): Promise<void>
94
+ commitTransaction(tx: DBTransaction, opt?: CommonDBOptions): Promise<void>
91
95
  }
@@ -67,7 +67,7 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends Obje
67
67
  bmSchema?: ObjectSchemaTyped<BM> | AjvSchema<BM>
68
68
  tmSchema?: ObjectSchemaTyped<TM> | AjvSchema<TM>
69
69
 
70
- excludeFromIndexes?: string[]
70
+ excludeFromIndexes?: (keyof DBM)[]
71
71
 
72
72
  /**
73
73
  * @default to false
@@ -149,7 +149,9 @@ export interface CommonDaoOptions extends CommonDBOptions {
149
149
  /**
150
150
  * All properties default to undefined.
151
151
  */
152
- export interface CommonDaoSaveOptions extends CommonDaoOptions, CommonDBSaveOptions {
152
+ export interface CommonDaoSaveOptions<DBM extends ObjectWithId>
153
+ extends CommonDaoOptions,
154
+ CommonDBSaveOptions<DBM> {
153
155
  /**
154
156
  * @default false
155
157
  *
@@ -216,12 +216,12 @@ export class CommonDao<
216
216
  }
217
217
  }
218
218
 
219
- async getBy(by: string, value: any, limit = 0, opt?: CommonDaoOptions): Promise<Saved<BM>[]> {
220
- return await this.query().filter(by, '==', value).limit(limit).runQuery(opt)
219
+ async getBy(by: keyof DBM, value: any, limit = 0, opt?: CommonDaoOptions): Promise<Saved<BM>[]> {
220
+ return await this.query().filterEq(by, value).limit(limit).runQuery(opt)
221
221
  }
222
222
 
223
- async getOneBy(by: string, value: any, opt?: CommonDaoOptions): Promise<Saved<BM> | null> {
224
- const [bm] = await this.query().filter(by, '==', value).limit(1).runQuery(opt)
223
+ async getOneBy(by: keyof DBM, value: any, opt?: CommonDaoOptions): Promise<Saved<BM> | null> {
224
+ const [bm] = await this.query().filterEq(by, value).limit(1).runQuery(opt)
225
225
  return bm || null
226
226
  }
227
227
 
@@ -531,7 +531,7 @@ export class CommonDao<
531
531
  /**
532
532
  * Mutates with id, created, updated
533
533
  */
534
- async save(bm: BM, opt: CommonDaoSaveOptions = {}): Promise<Saved<BM>> {
534
+ async save(bm: BM, opt: CommonDaoSaveOptions<DBM> = {}): Promise<Saved<BM>> {
535
535
  this.requireWriteAccess()
536
536
  const idWasGenerated = !bm.id
537
537
  this.assignIdCreatedUpdated(bm, opt) // mutates
@@ -572,7 +572,11 @@ export class CommonDao<
572
572
  *
573
573
  * Convenience method to replace 3 operations (loading+patching+saving) with one.
574
574
  */
575
- async patch(id: string, patch: Partial<BM>, opt: CommonDaoSaveOptions = {}): Promise<Saved<BM>> {
575
+ async patch(
576
+ id: string,
577
+ patch: Partial<BM>,
578
+ opt: CommonDaoSaveOptions<DBM> = {},
579
+ ): Promise<Saved<BM>> {
576
580
  return await this.save(
577
581
  {
578
582
  ...(await this.getByIdOrCreate(id, patch, opt)),
@@ -582,7 +586,11 @@ export class CommonDao<
582
586
  )
583
587
  }
584
588
 
585
- async patchAsDBM(id: string, patch: Partial<DBM>, opt: CommonDaoSaveOptions = {}): Promise<DBM> {
589
+ async patchAsDBM(
590
+ id: string,
591
+ patch: Partial<DBM>,
592
+ opt: CommonDaoSaveOptions<DBM> = {},
593
+ ): Promise<DBM> {
586
594
  const dbm =
587
595
  (await this.getByIdAsDBM(id, opt)) ||
588
596
  (this.create({ ...patch, id } as Partial<BM>, opt) as any as DBM)
@@ -596,7 +604,7 @@ export class CommonDao<
596
604
  )
597
605
  }
598
606
 
599
- async saveAsDBM(dbm: DBM, opt: CommonDaoSaveOptions = {}): Promise<DBM> {
607
+ async saveAsDBM(dbm: DBM, opt: CommonDaoSaveOptions<DBM> = {}): Promise<DBM> {
600
608
  this.requireWriteAccess()
601
609
  const table = opt.table || this.cfg.table
602
610
 
@@ -618,7 +626,7 @@ export class CommonDao<
618
626
  return dbm
619
627
  }
620
628
 
621
- async saveBatch(bms: BM[], opt: CommonDaoSaveOptions = {}): Promise<Saved<BM>[]> {
629
+ async saveBatch(bms: BM[], opt: CommonDaoSaveOptions<DBM> = {}): Promise<Saved<BM>[]> {
622
630
  this.requireWriteAccess()
623
631
  const table = opt.table || this.cfg.table
624
632
  bms.forEach(bm => this.assignIdCreatedUpdated(bm, opt))
@@ -643,7 +651,7 @@ export class CommonDao<
643
651
  return bms as any[]
644
652
  }
645
653
 
646
- async saveBatchAsDBM(dbms: DBM[], opt: CommonDaoSaveOptions = {}): Promise<DBM[]> {
654
+ async saveBatchAsDBM(dbms: DBM[], opt: CommonDaoSaveOptions<DBM> = {}): Promise<DBM[]> {
647
655
  this.requireWriteAccess()
648
656
  const table = opt.table || this.cfg.table
649
657
  if (!opt.raw) {
package/src/db.model.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { AnyObject } from '@naturalcycles/js-lib'
1
2
  import { CommonDB } from './common.db'
2
3
 
3
4
  // eslint-disable-next-line @typescript-eslint/no-empty-interface
@@ -6,8 +7,9 @@ export interface CommonDBOptions {}
6
7
  /**
7
8
  * All properties default to undefined.
8
9
  */
9
- export interface CommonDBSaveOptions extends CommonDBOptions {
10
- excludeFromIndexes?: string[]
10
+ export interface CommonDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId>
11
+ extends CommonDBOptions {
12
+ excludeFromIndexes?: (keyof ROW)[]
11
13
  }
12
14
 
13
15
  export type CommonDBStreamOptions = CommonDBOptions
@@ -27,7 +29,7 @@ export interface RunQueryResult<T> {
27
29
 
28
30
  export type DBOperation = DBSaveBatchOperation | DBDeleteByIdsOperation
29
31
 
30
- export interface DBSaveBatchOperation<ROW extends ObjectWithId = any> {
32
+ export interface DBSaveBatchOperation<ROW extends ObjectWithId = AnyObjectWithId> {
31
33
  type: 'saveBatch'
32
34
  table: string
33
35
  rows: ROW[]
@@ -63,6 +65,8 @@ export interface ObjectWithId {
63
65
  id: string
64
66
  }
65
67
 
68
+ export interface AnyObjectWithId extends AnyObject, ObjectWithId {}
69
+
66
70
  /**
67
71
  * Interface for a module (lib) that implements CommonDB.
68
72
  *
package/src/index.ts CHANGED
@@ -24,6 +24,7 @@ import {
24
24
  CommonDaoStreamOptions,
25
25
  } from './commondao/common.dao.model'
26
26
  import {
27
+ AnyObjectWithId,
27
28
  CommonDBAdapter,
28
29
  CommonDBCreateOptions,
29
30
  CommonDBOptions,
@@ -80,6 +81,7 @@ export type {
80
81
  RunQueryResult,
81
82
  CreatedUpdated,
82
83
  CreatedUpdatedId,
84
+ AnyObjectWithId,
83
85
  ObjectWithId,
84
86
  CommonDaoCfg,
85
87
  CommonDaoCreateIdHook,
@@ -2,7 +2,7 @@ import { AsyncMapper, _truncate, Saved } from '@naturalcycles/js-lib'
2
2
  import { ReadableTyped } from '@naturalcycles/nodejs-lib'
3
3
  import { CommonDaoOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions } from '..'
4
4
  import { CommonDao } from '../commondao/common.dao'
5
- import { ObjectWithId, RunQueryResult } from '../db.model'
5
+ import { AnyObjectWithId, ObjectWithId, RunQueryResult } from '../db.model'
6
6
 
7
7
  /**
8
8
  * Modeled after Firestore operators (WhereFilterOp type)
@@ -46,29 +46,17 @@ export const dbQueryFilterOperatorValues = [
46
46
  'array-contains-any',
47
47
  ]
48
48
 
49
- export interface DBQueryFilter {
50
- name: string
49
+ export interface DBQueryFilter<ROW extends ObjectWithId = AnyObjectWithId> {
50
+ name: keyof ROW
51
51
  op: DBQueryFilterOperator
52
52
  val: any
53
53
  }
54
54
 
55
- export interface DBQueryOrder {
56
- name: string
55
+ export interface DBQueryOrder<ROW extends ObjectWithId = AnyObjectWithId> {
56
+ name: keyof ROW
57
57
  descending?: boolean
58
58
  }
59
59
 
60
- // export interface DBQueryData {
61
- // _filters: DBQueryFilter[]
62
- // _limitValue: number
63
- // _orders: DBQueryOrder[]
64
- //
65
- // /**
66
- // * If defined - only those fields will be selected.
67
- // * In undefined - all fields (*) will be returned.
68
- // */
69
- // _selectedFieldNames?: string[]
70
- // }
71
-
72
60
  /**
73
61
  * Lowest Common Denominator Query object.
74
62
  * To be executed by CommonDao / CommonDB.
@@ -79,26 +67,26 @@ export interface DBQueryOrder {
79
67
  *
80
68
  * <DBM> is the type of **queried** object (so e.g `key of DBM` can be used), not **returned** object.
81
69
  */
82
- export class DBQuery<ROW extends ObjectWithId> {
70
+ export class DBQuery<ROW extends ObjectWithId = AnyObjectWithId> {
83
71
  constructor(public table: string) {}
84
72
 
85
73
  /**
86
74
  * Convenience method.
87
75
  */
88
- static create<ROW extends ObjectWithId = any>(table: string): DBQuery<ROW> {
76
+ static create<ROW extends ObjectWithId = AnyObjectWithId>(table: string): DBQuery<ROW> {
89
77
  return new DBQuery(table)
90
78
  }
91
79
 
92
- static fromPlainObject<ROW extends ObjectWithId = any>(
80
+ static fromPlainObject<ROW extends ObjectWithId = AnyObjectWithId>(
93
81
  obj: Partial<DBQuery<ROW>> & { table: string },
94
82
  ): DBQuery<ROW> {
95
83
  return Object.assign(new DBQuery<ROW>(obj.table), obj)
96
84
  }
97
85
 
98
- _filters: DBQueryFilter[] = []
86
+ _filters: DBQueryFilter<ROW>[] = []
99
87
  _limitValue = 0 // 0 means "no limit"
100
88
  _offsetValue = 0 // 0 means "no offset"
101
- _orders: DBQueryOrder[] = []
89
+ _orders: DBQueryOrder<ROW>[] = []
102
90
 
103
91
  _startCursor?: string
104
92
  _endCursor?: string
@@ -107,14 +95,14 @@ export class DBQuery<ROW extends ObjectWithId> {
107
95
  * If defined - only those fields will be selected.
108
96
  * In undefined - all fields (*) will be returned.
109
97
  */
110
- _selectedFieldNames?: string[]
98
+ _selectedFieldNames?: (keyof ROW)[]
111
99
 
112
- filter(name: string, op: DBQueryFilterOperator, val: any): this {
100
+ filter(name: keyof ROW, op: DBQueryFilterOperator, val: any): this {
113
101
  this._filters.push({ name, op, val })
114
102
  return this
115
103
  }
116
104
 
117
- filterEq(name: string, val: any): this {
105
+ filterEq(name: keyof ROW, val: any): this {
118
106
  this._filters.push({ name, op: '==', val })
119
107
  return this
120
108
  }
@@ -129,7 +117,7 @@ export class DBQuery<ROW extends ObjectWithId> {
129
117
  return this
130
118
  }
131
119
 
132
- order(name: string, descending?: boolean): this {
120
+ order(name: keyof ROW, descending?: boolean): this {
133
121
  this._orders.push({
134
122
  name,
135
123
  descending,
@@ -137,7 +125,7 @@ export class DBQuery<ROW extends ObjectWithId> {
137
125
  return this
138
126
  }
139
127
 
140
- select(fieldNames: string[]): this {
128
+ select(fieldNames: (keyof ROW)[]): this {
141
129
  this._selectedFieldNames = fieldNames
142
130
  return this
143
131
  }
@@ -5,6 +5,7 @@ import {
5
5
  CommonTimeSeriesDaoCfg,
6
6
  TimeSeriesDataPoint,
7
7
  TimeSeriesQuery,
8
+ TimeSeriesRow,
8
9
  TimeSeriesSaveBatchOp,
9
10
  } from './timeSeries.model'
10
11
 
@@ -84,7 +85,7 @@ export class CommonTimeSeriesDao {
84
85
  if (q.fromIncl) dbq.filter('ts', '>=', q.fromIncl)
85
86
  if (q.toExcl) dbq.filter('ts', '<', q.toExcl)
86
87
 
87
- const { rows } = await this.cfg.db.runQuery(dbq)
88
+ const rows = (await this.cfg.db.runQuery(dbq)).rows as any as TimeSeriesRow[]
88
89
 
89
90
  // todo: query from aggregated tables when step is above 'hour'
90
91
 
@@ -1,5 +1,5 @@
1
1
  import type { CommonDB } from '../common.db'
2
- import type { CommonDBSaveOptions, DBOperation, ObjectWithId } from '../db.model'
2
+ import type { AnyObjectWithId, CommonDBSaveOptions, DBOperation, ObjectWithId } from '../db.model'
3
3
 
4
4
  /**
5
5
  * Convenience class that stores the list of DBOperations and provides a fluent API to add them.
@@ -7,7 +7,7 @@ import type { CommonDBSaveOptions, DBOperation, ObjectWithId } from '../db.model
7
7
  export class DBTransaction {
8
8
  public ops: DBOperation[] = []
9
9
 
10
- saveBatch<ROW extends ObjectWithId = any>(table: string, rows: ROW[]): this {
10
+ saveBatch<ROW extends ObjectWithId = AnyObjectWithId>(table: string, rows: ROW[]): this {
11
11
  this.ops.push({
12
12
  type: 'saveBatch',
13
13
  table,
@@ -35,7 +35,7 @@ export class RunnableDBTransaction extends DBTransaction {
35
35
  super()
36
36
  }
37
37
 
38
- async commit(opt?: CommonDBSaveOptions): Promise<void> {
38
+ async commit<ROW extends ObjectWithId>(opt?: CommonDBSaveOptions<ROW>): Promise<void> {
39
39
  await this.db.commitTransaction(this, opt)
40
40
  }
41
41
  }