@naturalcycles/db-lib 8.43.2 → 8.43.4

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.
@@ -25,7 +25,7 @@ export declare class CacheDB extends BaseCommonDB implements CommonDB {
25
25
  createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CacheDBCreateOptions): Promise<void>;
26
26
  getByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], opt?: CacheDBSaveOptions<ROW>): Promise<ROW[]>;
27
27
  deleteByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], opt?: CacheDBOptions): Promise<number>;
28
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CacheDBSaveOptions<ROW>): Promise<void>;
28
+ saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CacheDBSaveOptions<ROW>): Promise<void>;
29
29
  runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBSaveOptions<ROW>): Promise<RunQueryResult<ROW>>;
30
30
  runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBOptions): Promise<number>;
31
31
  streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBStreamOptions): Readable;
@@ -47,7 +47,7 @@ export interface CacheDBOptions {
47
47
  */
48
48
  onlyCache?: boolean;
49
49
  }
50
- export interface CacheDBSaveOptions<ROW extends ObjectWithId> extends CacheDBOptions, CommonDBSaveOptions<ROW> {
50
+ export interface CacheDBSaveOptions<ROW extends Partial<ObjectWithId>> extends CacheDBOptions, CommonDBSaveOptions<ROW> {
51
51
  }
52
52
  export interface CacheDBStreamOptions extends CacheDBOptions, CommonDBStreamOptions {
53
53
  }
@@ -22,7 +22,7 @@ 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: ROW['id'][], _opt?: CommonDBOptions): Promise<ROW[]>;
25
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], _opt?: CommonDBSaveOptions<ROW>): Promise<void>;
25
+ saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], _opt?: CommonDBSaveOptions<ROW>): Promise<void>;
26
26
  /**
27
27
  * Implementation is optimized for loading/saving _whole files_.
28
28
  */
@@ -53,7 +53,7 @@ export declare class InMemoryDB implements CommonDB {
53
53
  getTableSchema<ROW extends ObjectWithId>(_table: string): Promise<JsonSchemaRootObject<ROW>>;
54
54
  createTable<ROW extends ObjectWithId>(_table: string, _schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
55
55
  getByIds<ROW extends ObjectWithId>(_table: string, ids: ROW['id'][], _opt?: CommonDBOptions): Promise<ROW[]>;
56
- saveBatch<ROW extends ObjectWithId>(_table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
56
+ saveBatch<ROW extends Partial<ObjectWithId>>(_table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
57
57
  deleteByIds<ROW extends ObjectWithId>(_table: string, ids: ROW['id'][], _opt?: CommonDBOptions): Promise<number>;
58
58
  deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
59
59
  runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<RunQueryResult<ROW>>;
@@ -18,7 +18,7 @@ export declare class BaseCommonDB implements CommonDB {
18
18
  getByIds<ROW extends ObjectWithId>(_table: string, _ids: ROW['id'][]): Promise<ROW[]>;
19
19
  runQuery<ROW extends ObjectWithId>(_q: DBQuery<ROW>): Promise<RunQueryResult<ROW>>;
20
20
  runQueryCount<ROW extends ObjectWithId>(_q: DBQuery<ROW>): Promise<number>;
21
- saveBatch<ROW extends ObjectWithId>(_table: string, _rows: ROW[], _opt?: CommonDBSaveOptions<ROW>): Promise<void>;
21
+ saveBatch<ROW extends Partial<ObjectWithId>>(_table: string, _rows: ROW[], _opt?: CommonDBSaveOptions<ROW>): Promise<void>;
22
22
  streamQuery<ROW extends ObjectWithId>(_q: DBQuery<ROW>): ReadableTyped<ROW>;
23
23
  /**
24
24
  * Naive implementation.
@@ -41,7 +41,7 @@ export interface CommonDB {
41
41
  /**
42
42
  * rows can have missing ids only if DB supports auto-generating them (like mysql auto_increment).
43
43
  */
44
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
44
+ saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
45
45
  /**
46
46
  * Returns number of deleted items.
47
47
  * Not supported by all implementations (e.g Datastore will always return same number as number of ids).
@@ -10,7 +10,7 @@ import { CommonDaoCfg, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoSaveOp
10
10
  * BM = Backend model (optimized for API access)
11
11
  * TM = Transport model (optimized to be sent over the wire)
12
12
  */
13
- export declare class CommonDao<BM extends Partial<ObjectWithId<ID>>, DBM extends ObjectWithId<ID> = Saved<BM>, TM extends AnyObject = BM, ID extends string | number = string> {
13
+ export declare class CommonDao<BM extends Partial<ObjectWithId<ID>>, DBM extends ObjectWithId<ID> = Saved<BM>, TM extends AnyObject = BM, ID extends string | number = NonNullable<BM['id']>> {
14
14
  cfg: CommonDaoCfg<BM, DBM, TM, ID>;
15
15
  constructor(cfg: CommonDaoCfg<BM, DBM, TM, ID>);
16
16
  create(part?: Partial<BM>, opt?: CommonDaoOptions): Saved<BM>;
@@ -1,4 +1,4 @@
1
- import { AnyObjectWithId, ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { ObjectWithId } from '@naturalcycles/js-lib';
2
2
  /**
3
3
  * Similar to SQL INSERT, UPDATE.
4
4
  * Insert will fail if row already exists.
@@ -13,7 +13,7 @@ export interface CommonDBOptions {
13
13
  /**
14
14
  * All properties default to undefined.
15
15
  */
16
- export interface CommonDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId> extends CommonDBOptions {
16
+ export interface CommonDBSaveOptions<ROW extends Partial<ObjectWithId> = any> extends CommonDBOptions {
17
17
  excludeFromIndexes?: (keyof ROW)[];
18
18
  /**
19
19
  * Default is `upsert`
@@ -41,7 +41,7 @@ export interface RunQueryResult<T> {
41
41
  endCursor?: string;
42
42
  }
43
43
  export declare type DBOperation = DBSaveBatchOperation | DBDeleteByIdsOperation;
44
- export interface DBSaveBatchOperation<ROW extends ObjectWithId = any> {
44
+ export interface DBSaveBatchOperation<ROW extends Partial<ObjectWithId> = any> {
45
45
  type: 'saveBatch';
46
46
  table: string;
47
47
  rows: ROW[];
@@ -11,8 +11,8 @@ export declare class DBTransaction {
11
11
  * Convenience method.
12
12
  */
13
13
  static create(ops?: DBOperation[]): DBTransaction;
14
- save<ROW extends ObjectWithId>(table: string, row: ROW): this;
15
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[]): this;
14
+ save<ROW extends Partial<ObjectWithId>>(table: string, row: ROW): this;
15
+ saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[]): this;
16
16
  deleteById(table: string, id: string): this;
17
17
  deleteByIds(table: string, ids: string[]): this;
18
18
  }
@@ -23,5 +23,5 @@ export declare class DBTransaction {
23
23
  export declare class RunnableDBTransaction extends DBTransaction {
24
24
  db: CommonDB;
25
25
  constructor(db: CommonDB);
26
- commit<ROW extends ObjectWithId>(opt?: CommonDBSaveOptions<ROW>): Promise<void>;
26
+ commit<ROW extends Partial<ObjectWithId>>(opt?: CommonDBSaveOptions<ROW>): Promise<void>;
27
27
  }
@@ -1,7 +1,7 @@
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<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>, CommonDBSaveOptions<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>>;
4
+ export declare const commonDBSaveOptionsSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<CommonDBSaveOptions<any>, CommonDBSaveOptions<any>>;
5
5
  export declare const dbQueryFilterOperatorSchema: import("@naturalcycles/nodejs-lib/dist/validation/joi/string.extensions").ExtendedStringSchema;
6
6
  export declare const dbQueryFilterSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryFilter<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>, DBQueryFilter<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>>;
7
7
  export declare const dbQueryOrderSchema: import("@naturalcycles/nodejs-lib").ObjectSchemaTyped<DBQueryOrder<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>, DBQueryOrder<import("@naturalcycles/js-lib").AnyObjectWithId<string | number>>>;
package/package.json CHANGED
@@ -41,7 +41,7 @@
41
41
  "engines": {
42
42
  "node": ">=14.15"
43
43
  },
44
- "version": "8.43.2",
44
+ "version": "8.43.4",
45
45
  "description": "Lowest Common Denominator API to supported Databases",
46
46
  "keywords": [
47
47
  "db",
@@ -57,7 +57,7 @@ export interface CacheDBOptions {
57
57
  onlyCache?: boolean
58
58
  }
59
59
 
60
- export interface CacheDBSaveOptions<ROW extends ObjectWithId>
60
+ export interface CacheDBSaveOptions<ROW extends Partial<ObjectWithId>>
61
61
  extends CacheDBOptions,
62
62
  CommonDBSaveOptions<ROW> {}
63
63
 
@@ -147,7 +147,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
147
147
  return deletedIds
148
148
  }
149
149
 
150
- override async saveBatch<ROW extends ObjectWithId>(
150
+ override async saveBatch<ROW extends Partial<ObjectWithId>>(
151
151
  table: string,
152
152
  rows: ROW[],
153
153
  opt: CacheDBSaveOptions<ROW> = {},
@@ -15,6 +15,7 @@ import {
15
15
  _assert,
16
16
  _deepCopy,
17
17
  _stringMapEntries,
18
+ Saved,
18
19
  } from '@naturalcycles/js-lib'
19
20
  import { readableCreate, ReadableTyped } from '@naturalcycles/nodejs-lib'
20
21
  import { dimGrey } from '@naturalcycles/nodejs-lib/dist/colors'
@@ -73,7 +74,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
73
74
  return ids.map(id => byId[id]!).filter(Boolean)
74
75
  }
75
76
 
76
- override async saveBatch<ROW extends ObjectWithId>(
77
+ override async saveBatch<ROW extends Partial<ObjectWithId>>(
77
78
  table: string,
78
79
  rows: ROW[],
79
80
  _opt?: CommonDBSaveOptions<ROW>,
@@ -81,7 +82,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
81
82
  if (!rows.length) return // save some api calls
82
83
 
83
84
  // 1. Load the whole file
84
- const byId = _by(await this.loadFile<ROW>(table), r => r.id)
85
+ const byId = _by(await this.loadFile<Saved<ROW>>(table), r => r.id)
85
86
 
86
87
  // 2. Merge with new data (using ids)
87
88
  let saved = 0
@@ -152,7 +152,7 @@ export class InMemoryDB implements CommonDB {
152
152
  return ids.map(id => this.data[table]![id]).filter(Boolean) as ROW[]
153
153
  }
154
154
 
155
- async saveBatch<ROW extends ObjectWithId>(
155
+ async saveBatch<ROW extends Partial<ObjectWithId>>(
156
156
  _table: string,
157
157
  rows: ROW[],
158
158
  opt: CommonDBSaveOptions<ROW> = {},
@@ -55,7 +55,7 @@ export class BaseCommonDB implements CommonDB {
55
55
  return 0
56
56
  }
57
57
 
58
- async saveBatch<ROW extends ObjectWithId>(
58
+ async saveBatch<ROW extends Partial<ObjectWithId>>(
59
59
  _table: string,
60
60
  _rows: ROW[],
61
61
  _opt?: CommonDBSaveOptions<ROW>,
package/src/common.db.ts CHANGED
@@ -73,7 +73,7 @@ export interface CommonDB {
73
73
  /**
74
74
  * rows can have missing ids only if DB supports auto-generating them (like mysql auto_increment).
75
75
  */
76
- saveBatch<ROW extends ObjectWithId>(
76
+ saveBatch<ROW extends Partial<ObjectWithId>>(
77
77
  table: string,
78
78
  rows: ROW[],
79
79
  opt?: CommonDBSaveOptions<ROW>,
@@ -71,7 +71,7 @@ export class CommonDao<
71
71
  BM extends Partial<ObjectWithId<ID>>,
72
72
  DBM extends ObjectWithId<ID> = Saved<BM>,
73
73
  TM extends AnyObject = BM,
74
- ID extends string | number = string,
74
+ ID extends string | number = NonNullable<BM['id']>,
75
75
  > {
76
76
  constructor(public cfg: CommonDaoCfg<BM, DBM, TM, ID>) {
77
77
  this.cfg = {
package/src/db.model.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AnyObjectWithId, ObjectWithId } from '@naturalcycles/js-lib'
1
+ import { ObjectWithId } from '@naturalcycles/js-lib'
2
2
 
3
3
  /**
4
4
  * Similar to SQL INSERT, UPDATE.
@@ -16,7 +16,7 @@ export interface CommonDBOptions {}
16
16
  /**
17
17
  * All properties default to undefined.
18
18
  */
19
- export interface CommonDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId>
19
+ export interface CommonDBSaveOptions<ROW extends Partial<ObjectWithId> = any>
20
20
  extends CommonDBOptions {
21
21
  excludeFromIndexes?: (keyof ROW)[]
22
22
 
@@ -52,7 +52,7 @@ export interface RunQueryResult<T> {
52
52
 
53
53
  export type DBOperation = DBSaveBatchOperation | DBDeleteByIdsOperation
54
54
 
55
- export interface DBSaveBatchOperation<ROW extends ObjectWithId = any> {
55
+ export interface DBSaveBatchOperation<ROW extends Partial<ObjectWithId> = any> {
56
56
  type: 'saveBatch'
57
57
  table: string
58
58
  rows: ROW[]
@@ -15,7 +15,7 @@ export class DBTransaction {
15
15
  return new DBTransaction(ops)
16
16
  }
17
17
 
18
- save<ROW extends ObjectWithId>(table: string, row: ROW): this {
18
+ save<ROW extends Partial<ObjectWithId>>(table: string, row: ROW): this {
19
19
  this.ops.push({
20
20
  type: 'saveBatch',
21
21
  table,
@@ -24,7 +24,7 @@ export class DBTransaction {
24
24
  return this
25
25
  }
26
26
 
27
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[]): this {
27
+ saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[]): this {
28
28
  this.ops.push({
29
29
  type: 'saveBatch',
30
30
  table,
@@ -61,7 +61,7 @@ export class RunnableDBTransaction extends DBTransaction {
61
61
  super()
62
62
  }
63
63
 
64
- async commit<ROW extends ObjectWithId>(opt?: CommonDBSaveOptions<ROW>): Promise<void> {
64
+ async commit<ROW extends Partial<ObjectWithId>>(opt?: CommonDBSaveOptions<ROW>): Promise<void> {
65
65
  await this.db.commitTransaction(this, opt)
66
66
  }
67
67
  }