@naturalcycles/firestore-lib 1.1.12 → 1.2.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.
@@ -7,7 +7,7 @@ export interface FirestoreDBCfg {
7
7
  }
8
8
  export interface FirestoreDBOptions extends CommonDBOptions {
9
9
  }
10
- export interface FirestoreDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId> extends CommonDBSaveOptions<ROW> {
10
+ export interface FirestoreDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObjectWithId> extends CommonDBSaveOptions<ROW> {
11
11
  }
12
12
  export declare class FirestoreDB extends BaseCommonDB implements CommonDB {
13
13
  cfg: FirestoreDBCfg;
@@ -18,7 +18,7 @@ export declare class FirestoreDB extends BaseCommonDB implements CommonDB {
18
18
  runFirestoreQuery<ROW extends ObjectWithId>(q: Query, _opt?: FirestoreDBOptions): Promise<ROW[]>;
19
19
  runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBOptions): Promise<number>;
20
20
  streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBStreamOptions): ReadableTyped<ROW>;
21
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], _opt?: FirestoreDBSaveOptions<ROW>): Promise<void>;
21
+ saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rows: ROW[], opt?: FirestoreDBSaveOptions<ROW>): Promise<void>;
22
22
  deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBOptions): Promise<number>;
23
23
  deleteByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], _opt?: FirestoreDBOptions): Promise<number>;
24
24
  private querySnapshotToArray;
@@ -6,6 +6,11 @@ const js_lib_1 = require("@naturalcycles/js-lib");
6
6
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
7
7
  const firestore_util_1 = require("./firestore.util");
8
8
  const query_util_1 = require("./query.util");
9
+ const methodMap = {
10
+ insert: 'create',
11
+ update: 'update',
12
+ upsert: 'set',
13
+ };
9
14
  class FirestoreDB extends db_lib_1.BaseCommonDB {
10
15
  constructor(cfg) {
11
16
  super();
@@ -54,12 +59,14 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
54
59
  }));
55
60
  }
56
61
  // SAVE
57
- async saveBatch(table, rows, _opt) {
62
+ async saveBatch(table, rows, opt = {}) {
63
+ const method = methodMap[opt.saveMethod] || 'set';
58
64
  // Firestore allows max 500 items in one batch
59
65
  await (0, js_lib_1.pMap)((0, js_lib_1._chunk)(rows, 500), async (chunk) => {
60
66
  const batch = this.cfg.firestore.batch();
61
67
  chunk.forEach(row => {
62
- batch.set(this.cfg.firestore.collection(table).doc((0, firestore_util_1.escapeDocId)(row.id)), (0, js_lib_1._filterUndefinedValues)(row));
68
+ (0, js_lib_1._assert)(row.id, `firestore-db doesn't support id auto-generation, but empty id was provided in saveBatch`);
69
+ batch[method](this.cfg.firestore.collection(table).doc((0, firestore_util_1.escapeDocId)(row.id)), (0, js_lib_1._filterUndefinedValues)(row));
63
70
  });
64
71
  await batch.commit();
65
72
  }, { concurrency: 1 });
package/package.json CHANGED
@@ -37,7 +37,7 @@
37
37
  "engines": {
38
38
  "node": ">=14.15.0"
39
39
  },
40
- "version": "1.1.12",
40
+ "version": "1.2.0",
41
41
  "description": "Firestore implementation of CommonDB interface",
42
42
  "author": "Natural Cycles Team",
43
43
  "license": "MIT"
@@ -3,6 +3,7 @@ import {
3
3
  BaseCommonDB,
4
4
  CommonDB,
5
5
  CommonDBOptions,
6
+ CommonDBSaveMethod,
6
7
  CommonDBSaveOptions,
7
8
  CommonDBStreamOptions,
8
9
  DBQuery,
@@ -17,6 +18,7 @@ import {
17
18
  _filterUndefinedValues,
18
19
  ObjectWithId,
19
20
  AnyObjectWithId,
21
+ _assert,
20
22
  } from '@naturalcycles/js-lib'
21
23
  import { ReadableTyped, transformMapSimple } from '@naturalcycles/nodejs-lib'
22
24
  import { escapeDocId, unescapeDocId } from './firestore.util'
@@ -27,9 +29,15 @@ export interface FirestoreDBCfg {
27
29
  }
28
30
 
29
31
  export interface FirestoreDBOptions extends CommonDBOptions {}
30
- export interface FirestoreDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId>
32
+ export interface FirestoreDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObjectWithId>
31
33
  extends CommonDBSaveOptions<ROW> {}
32
34
 
35
+ const methodMap: Record<CommonDBSaveMethod, string> = {
36
+ insert: 'create',
37
+ update: 'update',
38
+ upsert: 'set',
39
+ }
40
+
33
41
  export class FirestoreDB extends BaseCommonDB implements CommonDB {
34
42
  constructor(public cfg: FirestoreDBCfg) {
35
43
  super()
@@ -116,11 +124,13 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
116
124
  }
117
125
 
118
126
  // SAVE
119
- override async saveBatch<ROW extends ObjectWithId>(
127
+ override async saveBatch<ROW extends Partial<ObjectWithId>>(
120
128
  table: string,
121
129
  rows: ROW[],
122
- _opt?: FirestoreDBSaveOptions<ROW>,
130
+ opt: FirestoreDBSaveOptions<ROW> = {},
123
131
  ): Promise<void> {
132
+ const method = methodMap[opt.saveMethod!] || 'set'
133
+
124
134
  // Firestore allows max 500 items in one batch
125
135
  await pMap(
126
136
  _chunk(rows, 500),
@@ -128,7 +138,12 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
128
138
  const batch = this.cfg.firestore.batch()
129
139
 
130
140
  chunk.forEach(row => {
131
- batch.set(
141
+ _assert(
142
+ row.id,
143
+ `firestore-db doesn't support id auto-generation, but empty id was provided in saveBatch`,
144
+ )
145
+
146
+ batch[method](
132
147
  this.cfg.firestore.collection(table).doc(escapeDocId(row.id)),
133
148
  _filterUndefinedValues(row),
134
149
  )