@naturalcycles/firestore-lib 1.1.11 → 1.2.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.
@@ -7,20 +7,20 @@ 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;
14
14
  constructor(cfg: FirestoreDBCfg);
15
- getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: FirestoreDBOptions): Promise<ROW[]>;
16
- getById<ROW extends ObjectWithId>(table: string, id: string, _opt?: FirestoreDBOptions): Promise<ROW | undefined>;
15
+ getByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], opt?: FirestoreDBOptions): Promise<ROW[]>;
16
+ getById<ROW extends ObjectWithId>(table: string, id: ROW['id'], _opt?: FirestoreDBOptions): Promise<ROW | null>;
17
17
  runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBOptions): Promise<RunQueryResult<ROW>>;
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
- deleteByIds(table: string, ids: string[], _opt?: FirestoreDBOptions): Promise<number>;
23
+ deleteByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], _opt?: FirestoreDBOptions): Promise<number>;
24
24
  private querySnapshotToArray;
25
25
  commitTransaction(_tx: DBTransaction, _opt?: CommonDBSaveOptions): Promise<void>;
26
26
  ping(): Promise<void>;
@@ -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();
@@ -21,7 +26,7 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
21
26
  const doc = await this.cfg.firestore.collection(table).doc((0, firestore_util_1.escapeDocId)(id)).get();
22
27
  const data = doc.data();
23
28
  if (data === undefined)
24
- return;
29
+ return null;
25
30
  return {
26
31
  id,
27
32
  ...data,
@@ -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 });
@@ -1,2 +1,2 @@
1
- export declare function escapeDocId(docId: string): string;
1
+ export declare function escapeDocId(docId: string | number): string;
2
2
  export declare function unescapeDocId(docId: string): string;
@@ -4,10 +4,13 @@ exports.unescapeDocId = exports.escapeDocId = void 0;
4
4
  const js_lib_1 = require("@naturalcycles/js-lib");
5
5
  const SLASH = '_SLASH_';
6
6
  function escapeDocId(docId) {
7
+ if (typeof docId === 'number')
8
+ return String(docId);
7
9
  return (0, js_lib_1._replaceAll)(docId, '/', SLASH);
8
10
  }
9
11
  exports.escapeDocId = escapeDocId;
10
12
  function unescapeDocId(docId) {
13
+ // if (typeof docId === 'number') return docId
11
14
  return (0, js_lib_1._replaceAll)(docId, SLASH, '/');
12
15
  }
13
16
  exports.unescapeDocId = unescapeDocId;
package/package.json CHANGED
@@ -4,18 +4,18 @@
4
4
  "prepare": "husky install"
5
5
  },
6
6
  "dependencies": {
7
- "@google-cloud/firestore": "^4.5.0",
7
+ "@google-cloud/firestore": "^5.0.2",
8
8
  "@naturalcycles/db-lib": "^8.2.0",
9
9
  "@naturalcycles/js-lib": "^14.6.0",
10
10
  "@naturalcycles/nodejs-lib": "^12.4.0",
11
- "firebase-admin": "^10.0.0"
11
+ "firebase-admin": "^11.0.0"
12
12
  },
13
13
  "devDependencies": {
14
- "@naturalcycles/dev-lib": "^12.1.2",
14
+ "@naturalcycles/dev-lib": "^13.0.1",
15
15
  "@naturalcycles/test-lib": "^1.0.3",
16
- "@types/node": "^17.0.22",
16
+ "@types/node": "^18.0.0",
17
17
  "dotenv": "^16.0.0",
18
- "jest": "^27.0.6"
18
+ "jest": "^28.1.0"
19
19
  },
20
20
  "files": [
21
21
  "dist",
@@ -37,7 +37,7 @@
37
37
  "engines": {
38
38
  "node": ">=14.15.0"
39
39
  },
40
- "version": "1.1.11",
40
+ "version": "1.2.1",
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()
@@ -38,7 +46,7 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
38
46
  // GET
39
47
  override async getByIds<ROW extends ObjectWithId>(
40
48
  table: string,
41
- ids: string[],
49
+ ids: ROW['id'][],
42
50
  opt?: FirestoreDBOptions,
43
51
  ): Promise<ROW[]> {
44
52
  // Oj, doesn't look like a very optimal implementation!
@@ -50,13 +58,13 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
50
58
 
51
59
  async getById<ROW extends ObjectWithId>(
52
60
  table: string,
53
- id: string,
61
+ id: ROW['id'],
54
62
  _opt?: FirestoreDBOptions,
55
- ): Promise<ROW | undefined> {
63
+ ): Promise<ROW | null> {
56
64
  const doc = await this.cfg.firestore.collection(table).doc(escapeDocId(id)).get()
57
65
 
58
66
  const data = doc.data()
59
- if (data === undefined) return
67
+ if (data === undefined) return null
60
68
 
61
69
  return {
62
70
  id,
@@ -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,11 @@ 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
+ ;(batch as any)[method](
132
146
  this.cfg.firestore.collection(table).doc(escapeDocId(row.id)),
133
147
  _filterUndefinedValues(row),
134
148
  )
@@ -156,9 +170,9 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
156
170
  return ids.length
157
171
  }
158
172
 
159
- override async deleteByIds(
173
+ override async deleteByIds<ROW extends ObjectWithId>(
160
174
  table: string,
161
- ids: string[],
175
+ ids: ROW['id'][],
162
176
  _opt?: FirestoreDBOptions,
163
177
  ): Promise<number> {
164
178
  await pMap(_chunk(ids, 500), async chunk => {
@@ -2,10 +2,12 @@ import { _replaceAll } from '@naturalcycles/js-lib'
2
2
 
3
3
  const SLASH = '_SLASH_'
4
4
 
5
- export function escapeDocId(docId: string): string {
5
+ export function escapeDocId(docId: string | number): string {
6
+ if (typeof docId === 'number') return String(docId)
6
7
  return _replaceAll(docId, '/', SLASH)
7
8
  }
8
9
 
9
10
  export function unescapeDocId(docId: string): string {
11
+ // if (typeof docId === 'number') return docId
10
12
  return _replaceAll(docId, SLASH, '/')
11
13
  }