@naturalcycles/db-lib 8.51.1 → 8.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { AnyObject, AsyncMapper, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, Saved, Unsaved, ZodSchema } from '@naturalcycles/js-lib';
1
+ import { AnyObject, AsyncMapper, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, Promisable, Saved, Unsaved, ZodSchema } from '@naturalcycles/js-lib';
2
2
  import { AjvSchema, ObjectSchemaTyped, ReadableTyped } from '@naturalcycles/nodejs-lib';
3
3
  import { DBDeleteByIdsOperation, DBModelType, DBOperation, DBPatch, DBSaveBatchOperation, RunQueryResult } from '../db.model';
4
4
  import { DBQuery, RunnableDBQuery } from '../query/dbQuery';
@@ -85,9 +85,9 @@ export declare class CommonDao<BM extends Partial<ObjectWithId<ID>>, DBM extends
85
85
  assignIdCreatedUpdated(obj: Unsaved<BM>, opt?: CommonDaoOptions): Saved<BM>;
86
86
  tx: {
87
87
  save: (bm: Unsaved<BM>, opt?: CommonDaoSaveOptions<DBM>) => Promise<DBSaveBatchOperation>;
88
- saveBatch: (bms: Unsaved<BM>[], opt?: CommonDaoSaveOptions<DBM>) => Promise<DBSaveBatchOperation>;
89
- deleteByIds: (ids: ID[], opt?: CommonDaoOptions) => Promise<DBDeleteByIdsOperation>;
90
- deleteById: (id: ID, opt?: CommonDaoOptions) => Promise<DBDeleteByIdsOperation>;
88
+ saveBatch: (bms: Unsaved<BM>[], opt?: CommonDaoSaveOptions<DBM>) => Promise<DBSaveBatchOperation | undefined>;
89
+ deleteByIds: (ids: ID[], opt?: CommonDaoOptions) => Promise<DBDeleteByIdsOperation | undefined>;
90
+ deleteById: (id: ID | null | undefined, opt?: CommonDaoOptions) => Promise<DBDeleteByIdsOperation | undefined>;
91
91
  };
92
92
  /**
93
93
  * Mutates with id, created, updated
@@ -155,7 +155,7 @@ export declare class CommonDao<BM extends Partial<ObjectWithId<ID>>, DBM extends
155
155
  * Proxy to this.cfg.db.ping
156
156
  */
157
157
  ping(): Promise<void>;
158
- runInTransaction(ops: Promise<DBOperation>[]): Promise<void>;
158
+ runInTransaction(ops: Promisable<DBOperation | undefined>[]): Promise<void>;
159
159
  protected logResult(started: number, op: string, res: any, table: string): void;
160
160
  protected logSaveResult(started: number, op: string, table: string): void;
161
161
  protected logStarted(op: string, table: string, force?: boolean): number;
@@ -34,6 +34,8 @@ class CommonDao {
34
34
  };
35
35
  },
36
36
  saveBatch: async (bms, opt = {}) => {
37
+ if (!bms.length)
38
+ return;
37
39
  const rows = (await this.saveBatch(bms, { ...opt, tx: true }));
38
40
  return {
39
41
  type: 'saveBatch',
@@ -46,6 +48,8 @@ class CommonDao {
46
48
  };
47
49
  },
48
50
  deleteByIds: async (ids, opt = {}) => {
51
+ if (!ids.length)
52
+ return;
49
53
  return {
50
54
  type: 'deleteByIds',
51
55
  table: this.cfg.table,
@@ -54,6 +58,8 @@ class CommonDao {
54
58
  };
55
59
  },
56
60
  deleteById: async (id, opt = {}) => {
61
+ if (!id)
62
+ return;
57
63
  return {
58
64
  type: 'deleteByIds',
59
65
  table: this.cfg.table,
@@ -879,9 +885,9 @@ class CommonDao {
879
885
  await this.cfg.db.ping();
880
886
  }
881
887
  async runInTransaction(ops) {
882
- if (!ops.length)
888
+ const resolvedOps = (await Promise.all(ops)).filter(js_lib_1._isTruthy);
889
+ if (!resolvedOps.length)
883
890
  return;
884
- const resolvedOps = await Promise.all(ops);
885
891
  await this.cfg.db.commitTransaction(dbTransaction_1.DBTransaction.create(resolvedOps));
886
892
  }
887
893
  logResult(started, op, res, table) {
package/package.json CHANGED
@@ -41,7 +41,7 @@
41
41
  "engines": {
42
42
  "node": ">=18.12"
43
43
  },
44
- "version": "8.51.1",
44
+ "version": "8.52.0",
45
45
  "description": "Lowest Common Denominator API to supported Databases",
46
46
  "keywords": [
47
47
  "db",
@@ -2,6 +2,7 @@ import {
2
2
  _assert,
3
3
  _filterNullishValues,
4
4
  _filterUndefinedValues,
5
+ _isTruthy,
5
6
  _passthroughPredicate,
6
7
  _since,
7
8
  _truncate,
@@ -14,6 +15,7 @@ import {
14
15
  JsonSchemaRootObject,
15
16
  ObjectWithId,
16
17
  pMap,
18
+ Promisable,
17
19
  Saved,
18
20
  Unsaved,
19
21
  ZodSchema,
@@ -633,7 +635,8 @@ export class CommonDao<
633
635
  saveBatch: async (
634
636
  bms: Unsaved<BM>[],
635
637
  opt: CommonDaoSaveOptions<DBM> = {},
636
- ): Promise<DBSaveBatchOperation> => {
638
+ ): Promise<DBSaveBatchOperation | undefined> => {
639
+ if (!bms.length) return
637
640
  const rows: DBM[] = (await this.saveBatch(bms, { ...opt, tx: true })) as any
638
641
 
639
642
  return {
@@ -646,7 +649,11 @@ export class CommonDao<
646
649
  },
647
650
  }
648
651
  },
649
- deleteByIds: async (ids: ID[], opt: CommonDaoOptions = {}): Promise<DBDeleteByIdsOperation> => {
652
+ deleteByIds: async (
653
+ ids: ID[],
654
+ opt: CommonDaoOptions = {},
655
+ ): Promise<DBDeleteByIdsOperation | undefined> => {
656
+ if (!ids.length) return
650
657
  return {
651
658
  type: 'deleteByIds',
652
659
  table: this.cfg.table,
@@ -654,7 +661,11 @@ export class CommonDao<
654
661
  opt,
655
662
  }
656
663
  },
657
- deleteById: async (id: ID, opt: CommonDaoOptions = {}): Promise<DBDeleteByIdsOperation> => {
664
+ deleteById: async (
665
+ id: ID | null | undefined,
666
+ opt: CommonDaoOptions = {},
667
+ ): Promise<DBDeleteByIdsOperation | undefined> => {
668
+ if (!id) return
658
669
  return {
659
670
  type: 'deleteByIds',
660
671
  table: this.cfg.table,
@@ -1164,10 +1175,9 @@ export class CommonDao<
1164
1175
  await this.cfg.db.ping()
1165
1176
  }
1166
1177
 
1167
- async runInTransaction(ops: Promise<DBOperation>[]): Promise<void> {
1168
- if (!ops.length) return
1169
-
1170
- const resolvedOps = await Promise.all(ops)
1178
+ async runInTransaction(ops: Promisable<DBOperation | undefined>[]): Promise<void> {
1179
+ const resolvedOps = (await Promise.all(ops)).filter(_isTruthy)
1180
+ if (!resolvedOps.length) return
1171
1181
 
1172
1182
  await this.cfg.db.commitTransaction(DBTransaction.create(resolvedOps))
1173
1183
  }