@naturalcycles/firestore-lib 1.1.10 → 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.
@@ -1,27 +1,26 @@
1
- import { Query } from '@google-cloud/firestore';
1
+ import { Firestore, Query } from '@google-cloud/firestore';
2
2
  import { BaseCommonDB, CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, DBQuery, DBTransaction, RunQueryResult } from '@naturalcycles/db-lib';
3
3
  import { ObjectWithId, AnyObjectWithId } from '@naturalcycles/js-lib';
4
4
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
5
- import * as firebaseAdmin from 'firebase-admin';
6
5
  export interface FirestoreDBCfg {
7
- firestore: firebaseAdmin.firestore.Firestore;
6
+ firestore: Firestore;
8
7
  }
9
8
  export interface FirestoreDBOptions extends CommonDBOptions {
10
9
  }
11
- export interface FirestoreDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId> extends CommonDBSaveOptions<ROW> {
10
+ export interface FirestoreDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObjectWithId> extends CommonDBSaveOptions<ROW> {
12
11
  }
13
12
  export declare class FirestoreDB extends BaseCommonDB implements CommonDB {
14
13
  cfg: FirestoreDBCfg;
15
14
  constructor(cfg: FirestoreDBCfg);
16
- getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: FirestoreDBOptions): Promise<ROW[]>;
17
- 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>;
18
17
  runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBOptions): Promise<RunQueryResult<ROW>>;
19
18
  runFirestoreQuery<ROW extends ObjectWithId>(q: Query, _opt?: FirestoreDBOptions): Promise<ROW[]>;
20
19
  runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBOptions): Promise<number>;
21
20
  streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBStreamOptions): ReadableTyped<ROW>;
22
- 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>;
23
22
  deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBOptions): Promise<number>;
24
- deleteByIds(table: string, ids: string[], _opt?: FirestoreDBOptions): Promise<number>;
23
+ deleteByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], _opt?: FirestoreDBOptions): Promise<number>;
25
24
  private querySnapshotToArray;
26
25
  commitTransaction(_tx: DBTransaction, _opt?: CommonDBSaveOptions): Promise<void>;
27
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/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
+ import { Firestore } from '@google-cloud/firestore';
1
2
  import { FirestoreDB, FirestoreDBCfg, FirestoreDBOptions, FirestoreDBSaveOptions } from './firestore.db';
2
3
  import { dbQueryToFirestoreQuery } from './query.util';
3
4
  export type { FirestoreDBCfg, FirestoreDBOptions, FirestoreDBSaveOptions };
4
- export { FirestoreDB, dbQueryToFirestoreQuery };
5
+ export { Firestore, FirestoreDB, dbQueryToFirestoreQuery };
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dbQueryToFirestoreQuery = exports.FirestoreDB = void 0;
3
+ exports.dbQueryToFirestoreQuery = exports.FirestoreDB = exports.Firestore = void 0;
4
+ const firestore_1 = require("@google-cloud/firestore");
5
+ Object.defineProperty(exports, "Firestore", { enumerable: true, get: function () { return firestore_1.Firestore; } });
4
6
  const firestore_db_1 = require("./firestore.db");
5
7
  Object.defineProperty(exports, "FirestoreDB", { enumerable: true, get: function () { return firestore_db_1.FirestoreDB; } });
6
8
  const query_util_1 = require("./query.util");
package/package.json CHANGED
@@ -4,6 +4,7 @@
4
4
  "prepare": "husky install"
5
5
  },
6
6
  "dependencies": {
7
+ "@google-cloud/firestore": "^4.5.0",
7
8
  "@naturalcycles/db-lib": "^8.2.0",
8
9
  "@naturalcycles/js-lib": "^14.6.0",
9
10
  "@naturalcycles/nodejs-lib": "^12.4.0",
@@ -12,8 +13,8 @@
12
13
  "devDependencies": {
13
14
  "@naturalcycles/dev-lib": "^12.1.2",
14
15
  "@naturalcycles/test-lib": "^1.0.3",
15
- "@types/node": "^16.7.1",
16
- "dotenv": "^10.0.0",
16
+ "@types/node": "^17.0.22",
17
+ "dotenv": "^16.0.0",
17
18
  "jest": "^27.0.6"
18
19
  },
19
20
  "files": [
@@ -36,7 +37,7 @@
36
37
  "engines": {
37
38
  "node": ">=14.15.0"
38
39
  },
39
- "version": "1.1.10",
40
+ "version": "1.2.0",
40
41
  "description": "Firestore implementation of CommonDB interface",
41
42
  "author": "Natural Cycles Team",
42
43
  "license": "MIT"
@@ -1,8 +1,9 @@
1
- import { Query, QueryDocumentSnapshot, QuerySnapshot } from '@google-cloud/firestore'
1
+ import { Firestore, Query, QueryDocumentSnapshot, QuerySnapshot } from '@google-cloud/firestore'
2
2
  import {
3
3
  BaseCommonDB,
4
4
  CommonDB,
5
5
  CommonDBOptions,
6
+ CommonDBSaveMethod,
6
7
  CommonDBSaveOptions,
7
8
  CommonDBStreamOptions,
8
9
  DBQuery,
@@ -17,20 +18,26 @@ 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
- import * as firebaseAdmin from 'firebase-admin'
23
24
  import { escapeDocId, unescapeDocId } from './firestore.util'
24
25
  import { dbQueryToFirestoreQuery } from './query.util'
25
26
 
26
27
  export interface FirestoreDBCfg {
27
- firestore: firebaseAdmin.firestore.Firestore
28
+ firestore: Firestore
28
29
  }
29
30
 
30
31
  export interface FirestoreDBOptions extends CommonDBOptions {}
31
- export interface FirestoreDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId>
32
+ export interface FirestoreDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObjectWithId>
32
33
  extends CommonDBSaveOptions<ROW> {}
33
34
 
35
+ const methodMap: Record<CommonDBSaveMethod, string> = {
36
+ insert: 'create',
37
+ update: 'update',
38
+ upsert: 'set',
39
+ }
40
+
34
41
  export class FirestoreDB extends BaseCommonDB implements CommonDB {
35
42
  constructor(public cfg: FirestoreDBCfg) {
36
43
  super()
@@ -39,7 +46,7 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
39
46
  // GET
40
47
  override async getByIds<ROW extends ObjectWithId>(
41
48
  table: string,
42
- ids: string[],
49
+ ids: ROW['id'][],
43
50
  opt?: FirestoreDBOptions,
44
51
  ): Promise<ROW[]> {
45
52
  // Oj, doesn't look like a very optimal implementation!
@@ -51,13 +58,13 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
51
58
 
52
59
  async getById<ROW extends ObjectWithId>(
53
60
  table: string,
54
- id: string,
61
+ id: ROW['id'],
55
62
  _opt?: FirestoreDBOptions,
56
- ): Promise<ROW | undefined> {
63
+ ): Promise<ROW | null> {
57
64
  const doc = await this.cfg.firestore.collection(table).doc(escapeDocId(id)).get()
58
65
 
59
66
  const data = doc.data()
60
- if (data === undefined) return
67
+ if (data === undefined) return null
61
68
 
62
69
  return {
63
70
  id,
@@ -117,11 +124,13 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
117
124
  }
118
125
 
119
126
  // SAVE
120
- override async saveBatch<ROW extends ObjectWithId>(
127
+ override async saveBatch<ROW extends Partial<ObjectWithId>>(
121
128
  table: string,
122
129
  rows: ROW[],
123
- _opt?: FirestoreDBSaveOptions<ROW>,
130
+ opt: FirestoreDBSaveOptions<ROW> = {},
124
131
  ): Promise<void> {
132
+ const method = methodMap[opt.saveMethod!] || 'set'
133
+
125
134
  // Firestore allows max 500 items in one batch
126
135
  await pMap(
127
136
  _chunk(rows, 500),
@@ -129,7 +138,12 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
129
138
  const batch = this.cfg.firestore.batch()
130
139
 
131
140
  chunk.forEach(row => {
132
- 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](
133
147
  this.cfg.firestore.collection(table).doc(escapeDocId(row.id)),
134
148
  _filterUndefinedValues(row),
135
149
  )
@@ -157,9 +171,9 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
157
171
  return ids.length
158
172
  }
159
173
 
160
- override async deleteByIds(
174
+ override async deleteByIds<ROW extends ObjectWithId>(
161
175
  table: string,
162
- ids: string[],
176
+ ids: ROW['id'][],
163
177
  _opt?: FirestoreDBOptions,
164
178
  ): Promise<number> {
165
179
  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
  }
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Firestore } from '@google-cloud/firestore'
1
2
  import {
2
3
  FirestoreDB,
3
4
  FirestoreDBCfg,
@@ -8,4 +9,4 @@ import { dbQueryToFirestoreQuery } from './query.util'
8
9
 
9
10
  export type { FirestoreDBCfg, FirestoreDBOptions, FirestoreDBSaveOptions }
10
11
 
11
- export { FirestoreDB, dbQueryToFirestoreQuery }
12
+ export { Firestore, FirestoreDB, dbQueryToFirestoreQuery }