@naturalcycles/firestore-lib 1.1.9 → 1.1.12

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,10 +1,9 @@
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
  }
@@ -13,15 +12,15 @@ export interface FirestoreDBSaveOptions<ROW extends ObjectWithId = AnyObjectWith
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
21
  saveBatch<ROW extends 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>;
@@ -21,7 +21,7 @@ class FirestoreDB extends db_lib_1.BaseCommonDB {
21
21
  const doc = await this.cfg.firestore.collection(table).doc((0, firestore_util_1.escapeDocId)(id)).get();
22
22
  const data = doc.data();
23
23
  if (data === undefined)
24
- return;
24
+ return null;
25
25
  return {
26
26
  id,
27
27
  ...data,
@@ -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,19 +4,17 @@
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
- "@naturalcycles/nodejs-lib": "^12.4.0"
10
- },
11
- "peerDependencies": {
12
- "firebase-admin": ">=9.3.0"
10
+ "@naturalcycles/nodejs-lib": "^12.4.0",
11
+ "firebase-admin": "^10.0.0"
13
12
  },
14
13
  "devDependencies": {
15
14
  "@naturalcycles/dev-lib": "^12.1.2",
16
15
  "@naturalcycles/test-lib": "^1.0.3",
17
- "@types/node": "^16.7.1",
18
- "dotenv": "^10.0.0",
19
- "firebase-admin": "^10.0.0",
16
+ "@types/node": "^17.0.22",
17
+ "dotenv": "^16.0.0",
20
18
  "jest": "^27.0.6"
21
19
  },
22
20
  "files": [
@@ -37,9 +35,9 @@
37
35
  "url": "https://github.com/NaturalCycles/firestore-lib"
38
36
  },
39
37
  "engines": {
40
- "node": ">=12.13"
38
+ "node": ">=14.15.0"
41
39
  },
42
- "version": "1.1.9",
40
+ "version": "1.1.12",
43
41
  "description": "Firestore implementation of CommonDB interface",
44
42
  "author": "Natural Cycles Team",
45
43
  "license": "MIT"
package/readme.md CHANGED
@@ -8,10 +8,3 @@
8
8
  # Features
9
9
 
10
10
  - ...
11
-
12
- # Packaging
13
-
14
- - `engines.node >= 10.13`: Latest Node.js LTS
15
- - `main: dist/index.js`: commonjs, es2018
16
- - `types: dist/index.d.ts`: typescript types
17
- - `/src` folder with source `*.ts` files included
@@ -1,4 +1,4 @@
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,
@@ -19,12 +19,11 @@ import {
19
19
  AnyObjectWithId,
20
20
  } from '@naturalcycles/js-lib'
21
21
  import { ReadableTyped, transformMapSimple } from '@naturalcycles/nodejs-lib'
22
- import * as firebaseAdmin from 'firebase-admin'
23
22
  import { escapeDocId, unescapeDocId } from './firestore.util'
24
23
  import { dbQueryToFirestoreQuery } from './query.util'
25
24
 
26
25
  export interface FirestoreDBCfg {
27
- firestore: firebaseAdmin.firestore.Firestore
26
+ firestore: Firestore
28
27
  }
29
28
 
30
29
  export interface FirestoreDBOptions extends CommonDBOptions {}
@@ -39,7 +38,7 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
39
38
  // GET
40
39
  override async getByIds<ROW extends ObjectWithId>(
41
40
  table: string,
42
- ids: string[],
41
+ ids: ROW['id'][],
43
42
  opt?: FirestoreDBOptions,
44
43
  ): Promise<ROW[]> {
45
44
  // Oj, doesn't look like a very optimal implementation!
@@ -51,13 +50,13 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
51
50
 
52
51
  async getById<ROW extends ObjectWithId>(
53
52
  table: string,
54
- id: string,
53
+ id: ROW['id'],
55
54
  _opt?: FirestoreDBOptions,
56
- ): Promise<ROW | undefined> {
55
+ ): Promise<ROW | null> {
57
56
  const doc = await this.cfg.firestore.collection(table).doc(escapeDocId(id)).get()
58
57
 
59
58
  const data = doc.data()
60
- if (data === undefined) return
59
+ if (data === undefined) return null
61
60
 
62
61
  return {
63
62
  id,
@@ -157,9 +156,9 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
157
156
  return ids.length
158
157
  }
159
158
 
160
- override async deleteByIds(
159
+ override async deleteByIds<ROW extends ObjectWithId>(
161
160
  table: string,
162
- ids: string[],
161
+ ids: ROW['id'][],
163
162
  _opt?: FirestoreDBOptions,
164
163
  ): Promise<number> {
165
164
  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 }
package/CHANGELOG.md DELETED
@@ -1,140 +0,0 @@
1
- ## [1.1.9](https://github.com/NaturalCycles/firestore-lib/compare/v1.1.8...v1.1.9) (2021-10-19)
2
-
3
-
4
- ### Bug Fixes
5
-
6
- * adapt to db-lib ([45c850b](https://github.com/NaturalCycles/firestore-lib/commit/45c850b923d32ac439e00b0ed52629398146bd45))
7
-
8
- ## [1.1.8](https://github.com/NaturalCycles/firestore-lib/compare/v1.1.7...v1.1.8) (2021-10-18)
9
-
10
-
11
- ### Bug Fixes
12
-
13
- * correctly filter undefined values on save ([1eff2b4](https://github.com/NaturalCycles/firestore-lib/commit/1eff2b40fdb2ccfd137be268871d449ea2cbc74f))
14
-
15
- ## [1.1.7](https://github.com/NaturalCycles/firestore-lib/compare/v1.1.6...v1.1.7) (2021-10-18)
16
-
17
-
18
- ### Bug Fixes
19
-
20
- * stop removing null values on save ([03b8f21](https://github.com/NaturalCycles/firestore-lib/commit/03b8f218c9c030d55ff769a653a6332580ab44dc))
21
-
22
- ## [1.1.6](https://github.com/NaturalCycles/firestore-lib/compare/v1.1.5...v1.1.6) (2021-10-17)
23
-
24
-
25
- ### Bug Fixes
26
-
27
- * db-lib compat ([ad28580](https://github.com/NaturalCycles/firestore-lib/commit/ad28580cf772b9ff05d07cc89e6ee19be52981e3))
28
-
29
- ## [1.1.5](https://github.com/NaturalCycles/firestore-lib/compare/v1.1.4...v1.1.5) (2021-10-17)
30
-
31
-
32
- ### Bug Fixes
33
-
34
- * adapt to latest db-lib ([e64d4ca](https://github.com/NaturalCycles/firestore-lib/commit/e64d4cad792a2f24d3e592dbbff59492017a9d60))
35
-
36
- ## [1.1.4](https://github.com/NaturalCycles/firestore-lib/compare/v1.1.3...v1.1.4) (2021-10-01)
37
-
38
-
39
- ### Bug Fixes
40
-
41
- * adapt to db-lib ([05cbc49](https://github.com/NaturalCycles/firestore-lib/commit/05cbc4996efe5a3f1091835b016e2534b59befde))
42
-
43
- ## [1.1.3](https://github.com/NaturalCycles/firestore-lib/compare/v1.1.2...v1.1.3) (2021-08-25)
44
-
45
-
46
- ### Bug Fixes
47
-
48
- * deps ([541438a](https://github.com/NaturalCycles/firestore-lib/commit/541438aab99613d5b0c69b48b68008d6e0fbe06f))
49
-
50
- ## [1.1.2](https://github.com/NaturalCycles/firestore-lib/compare/v1.1.1...v1.1.2) (2021-02-27)
51
-
52
-
53
- ### Bug Fixes
54
-
55
- * deps ([741c1cb](https://github.com/NaturalCycles/firestore-lib/commit/741c1cbd2fbc913a32bdd26d394a14ea9e548f76))
56
-
57
- ## [1.1.1](https://github.com/NaturalCycles/firestore-lib/compare/v1.1.0...v1.1.1) (2020-11-05)
58
-
59
-
60
- ### Bug Fixes
61
-
62
- * adapt to db-lib ([032f821](https://github.com/NaturalCycles/firestore-lib/commit/032f82164f200d75fb5ed6c72ba12209fce45e9c))
63
-
64
- # [1.1.0](https://github.com/NaturalCycles/firestore-lib/compare/v1.0.9...v1.1.0) (2020-10-25)
65
-
66
-
67
- ### Features
68
-
69
- * adapt to current db-lib ([4c2f228](https://github.com/NaturalCycles/firestore-lib/commit/4c2f228c4f80711735a53b20fc75a9a74878ffb9))
70
-
71
- ## [1.0.9](https://github.com/NaturalCycles/firestore-lib/compare/v1.0.8...v1.0.9) (2020-03-22)
72
-
73
-
74
- ### Bug Fixes
75
-
76
- * deps ([53f70bb](https://github.com/NaturalCycles/firestore-lib/commit/53f70bbe9b746c500bb394d506bc0a707f09b4f2))
77
-
78
- ## [1.0.8](https://github.com/NaturalCycles/firestore-lib/compare/v1.0.7...v1.0.8) (2019-09-21)
79
-
80
-
81
- ### Bug Fixes
82
-
83
- * adapt to db-lib ([d046dd6](https://github.com/NaturalCycles/firestore-lib/commit/d046dd6))
84
-
85
- ## [1.0.7](https://github.com/NaturalCycles/firestore-lib/compare/v1.0.6...v1.0.7) (2019-09-20)
86
-
87
-
88
- ### Bug Fixes
89
-
90
- * adapt to new db-lib ([414b97c](https://github.com/NaturalCycles/firestore-lib/commit/414b97c))
91
-
92
- ## [1.0.6](https://github.com/NaturalCycles/firestore-lib/compare/v1.0.5...v1.0.6) (2019-08-24)
93
-
94
-
95
- ### Bug Fixes
96
-
97
- * missing index.ts ([b2b75cd](https://github.com/NaturalCycles/firestore-lib/commit/b2b75cd))
98
-
99
- ## [1.0.5](https://github.com/NaturalCycles/firestore-lib/compare/v1.0.4...v1.0.5) (2019-08-23)
100
-
101
-
102
- ### Bug Fixes
103
-
104
- * adopt to db-lib ([60800db](https://github.com/NaturalCycles/firestore-lib/commit/60800db))
105
-
106
- ## [1.0.4](https://github.com/NaturalCycles/firestore-lib/compare/v1.0.3...v1.0.4) (2019-08-20)
107
-
108
-
109
- ### Bug Fixes
110
-
111
- * return await ([6c6267d](https://github.com/NaturalCycles/firestore-lib/commit/6c6267d))
112
-
113
- ## [1.0.3](https://github.com/NaturalCycles/firestore-lib/compare/v1.0.2...v1.0.3) (2019-08-20)
114
-
115
-
116
- ### Bug Fixes
117
-
118
- * update to recent commondb interface ([d506916](https://github.com/NaturalCycles/firestore-lib/commit/d506916))
119
-
120
- ## [1.0.2](https://github.com/NaturalCycles/firestore-lib/compare/v1.0.1...v1.0.2) (2019-08-18)
121
-
122
-
123
- ### Bug Fixes
124
-
125
- * filterUndefinedValues ([57e0b24](https://github.com/NaturalCycles/firestore-lib/commit/57e0b24))
126
-
127
- ## [1.0.1](https://github.com/NaturalCycles/firestore-lib/compare/v1.0.0...v1.0.1) (2019-08-18)
128
-
129
-
130
- ### Bug Fixes
131
-
132
- * update to recent db-lib ([dbbb093](https://github.com/NaturalCycles/firestore-lib/commit/dbbb093))
133
-
134
- # 1.0.0 (2019-07-28)
135
-
136
-
137
- ### Features
138
-
139
- * first version ([6d129a9](https://github.com/NaturalCycles/firestore-lib/commit/6d129a9))
140
- * init project by create-module ([205703f](https://github.com/NaturalCycles/firestore-lib/commit/205703f))