@naturalcycles/firestore-lib 2.20.0 → 2.22.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.
@@ -10,17 +10,17 @@ export declare class FirestoreDB extends BaseCommonDB implements CommonDB {
10
10
  logger: CommonLogger;
11
11
  };
12
12
  support: CommonDBSupport;
13
- getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: FirestoreDBReadOptions): Promise<ROW[]>;
13
+ getByIds<ROW extends ObjectWithId>(table: string, ids: readonly string[], opt?: FirestoreDBReadOptions): Promise<ROW[]>;
14
14
  multiGet<ROW extends ObjectWithId>(map: StringMap<string[]>, opt?: CommonDBReadOptions): Promise<StringMap<ROW[]>>;
15
15
  runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBReadOptions): Promise<RunQueryResult<ROW>>;
16
16
  runFirestoreQuery<ROW extends ObjectWithId>(q: Query, opt?: FirestoreDBReadOptions): Promise<ROW[]>;
17
17
  runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: FirestoreDBReadOptions): Promise<number>;
18
18
  streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt_?: FirestoreDBStreamOptions): Pipeline<ROW>;
19
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: FirestoreDBSaveOptions<ROW>): Promise<void>;
19
+ saveBatch<ROW extends ObjectWithId>(table: string, rows: readonly ROW[], opt?: FirestoreDBSaveOptions<ROW>): Promise<void>;
20
20
  multiSave<ROW extends ObjectWithId>(map: StringMap<ROW[]>, opt?: FirestoreDBSaveOptions<ROW>): Promise<void>;
21
21
  patchById<ROW extends ObjectWithId>(table: string, id: string, patch: Partial<ROW>, opt?: FirestoreDBOptions): 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(table: string, ids: readonly string[], opt?: FirestoreDBOptions): Promise<number>;
24
24
  multiDelete(map: StringMap<string[]>, opt?: FirestoreDBOptions): Promise<number>;
25
25
  querySnapshotToArray<T = any>(qs: QuerySnapshot): T[];
26
26
  runInTransaction(fn: DBTransactionFn, opt?: CommonDBTransactionOptions): Promise<void>;
@@ -40,9 +40,9 @@ export declare class FirestoreDBTransaction implements DBTransaction {
40
40
  constructor(db: FirestoreDB, tx: Transaction);
41
41
  commit(): Promise<void>;
42
42
  rollback(): Promise<void>;
43
- getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions): Promise<ROW[]>;
44
- saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
45
- deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number>;
43
+ getByIds<ROW extends ObjectWithId>(table: string, ids: readonly string[], opt?: CommonDBOptions): Promise<ROW[]>;
44
+ saveBatch<ROW extends ObjectWithId>(table: string, rows: readonly ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
45
+ deleteByIds(table: string, ids: readonly string[], opt?: CommonDBOptions): Promise<number>;
46
46
  }
47
47
  export interface FirestoreDBCfg {
48
48
  firestore: Firestore;
@@ -1,6 +1,6 @@
1
1
  import { Readable } from 'node:stream';
2
2
  import { FieldPath } from '@google-cloud/firestore';
3
- import { localTime } from '@naturalcycles/js-lib/datetime';
3
+ import { nowUnixMillis } from '@naturalcycles/js-lib/datetime';
4
4
  import { _ms } from '@naturalcycles/js-lib/datetime/time.util.js';
5
5
  import { _assert } from '@naturalcycles/js-lib/error/assert.js';
6
6
  import { createCommonLoggerAtLevel } from '@naturalcycles/js-lib/log';
@@ -76,7 +76,7 @@ export class FirestoreShardedReadable extends Readable {
76
76
  return;
77
77
  const { logger, table } = this;
78
78
  if (this.lastQueryDoneByShard[shard]) {
79
- this.totalWait += Date.now() - this.lastQueryDoneByShard[shard];
79
+ this.totalWait += nowUnixMillis() - this.lastQueryDoneByShard[shard];
80
80
  }
81
81
  this.queryIsRunningByShard[shard] = true;
82
82
  const limit = this.opt.batchSize;
@@ -107,7 +107,7 @@ export class FirestoreShardedReadable extends Readable {
107
107
  logger.debug(`${table} got ${rows.length} rows, ${this.rowsRetrieved} rowsRetrieved, totalWait: ${_ms(this.totalWait)}`);
108
108
  this.cursorByShard[shard] = lastDocId;
109
109
  this.queryIsRunningByShard[shard] = false; // ready to take more _reads
110
- this.lastQueryDoneByShard[shard] = localTime.nowUnixMillis();
110
+ this.lastQueryDoneByShard[shard] = nowUnixMillis();
111
111
  for (const row of rows) {
112
112
  this.push(row);
113
113
  }
@@ -1,6 +1,6 @@
1
1
  import { Readable } from 'node:stream';
2
2
  import { FieldPath } from '@google-cloud/firestore';
3
- import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js';
3
+ import { nowUnixMillis } from '@naturalcycles/js-lib/datetime/localTime.js';
4
4
  import { _ms } from '@naturalcycles/js-lib/datetime/time.util.js';
5
5
  import { TimeoutError } from '@naturalcycles/js-lib/error/error.util.js';
6
6
  import { createCommonLoggerAtLevel } from '@naturalcycles/js-lib/log';
@@ -89,9 +89,9 @@ export class FirestoreStreamReadable extends Readable {
89
89
  // rowsRetrieved: this.rowsRetrieved,
90
90
  // paused: this.paused,
91
91
  // })
92
- const started = localTime.nowUnixMillis();
92
+ const started = nowUnixMillis();
93
93
  const qs = await this.runQuery(q);
94
- const queryTook = Date.now() - started;
94
+ const queryTook = nowUnixMillis() - started;
95
95
  if (!qs) {
96
96
  // error already emitted in runQuery
97
97
  return;
@@ -1,11 +1,12 @@
1
1
  import { FieldPath, Timestamp } from '@google-cloud/firestore';
2
2
  import { _round } from '@naturalcycles/js-lib';
3
+ import { nowUnixMillis } from '@naturalcycles/js-lib/datetime/localTime.js';
3
4
  export function readAtToReadTime(opt) {
4
5
  if (!opt.readAt)
5
6
  return;
6
7
  // Same logic as Datastore: round to whole minutes, guard against future
7
8
  let readTimeMs = _round(opt.readAt, 60) * 1000;
8
- if (readTimeMs >= Date.now() - 1000) {
9
+ if (readTimeMs >= nowUnixMillis() - 1000) {
9
10
  readTimeMs -= 60_000;
10
11
  }
11
12
  return Timestamp.fromMillis(readTimeMs);
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "tslib": "^2"
10
10
  },
11
11
  "devDependencies": {
12
- "@naturalcycles/dev-lib": "20.66.0",
12
+ "@naturalcycles/dev-lib": "20.71.0",
13
13
  "@types/node": "^26",
14
14
  "typescript": "^7",
15
15
  "firebase-admin": "^14"
@@ -38,7 +38,7 @@
38
38
  "engines": {
39
39
  "node": ">=26.12.1"
40
40
  },
41
- "version": "2.20.0",
41
+ "version": "2.22.0",
42
42
  "description": "Firestore implementation of CommonDB interface",
43
43
  "author": "Natural Cycles Team",
44
44
  "license": "MIT",
@@ -59,7 +59,7 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
59
59
  // GET
60
60
  override async getByIds<ROW extends ObjectWithId>(
61
61
  table: string,
62
- ids: string[],
62
+ ids: readonly string[],
63
63
  opt: FirestoreDBReadOptions = {},
64
64
  ): Promise<ROW[]> {
65
65
  if (!ids.length) return []
@@ -218,7 +218,7 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
218
218
  // SAVE
219
219
  override async saveBatch<ROW extends ObjectWithId>(
220
220
  table: string,
221
- rows: ROW[],
221
+ rows: readonly ROW[],
222
222
  opt: FirestoreDBSaveOptions<ROW> = {},
223
223
  ): Promise<void> {
224
224
  const { firestore } = this.cfg
@@ -371,7 +371,7 @@ export class FirestoreDB extends BaseCommonDB implements CommonDB {
371
371
 
372
372
  override async deleteByIds(
373
373
  table: string,
374
- ids: string[],
374
+ ids: readonly string[],
375
375
  opt: FirestoreDBOptions = {},
376
376
  ): Promise<number> {
377
377
  const { firestore } = this.cfg
@@ -524,7 +524,7 @@ export class FirestoreDBTransaction implements DBTransaction {
524
524
 
525
525
  async getByIds<ROW extends ObjectWithId>(
526
526
  table: string,
527
- ids: string[],
527
+ ids: readonly string[],
528
528
  opt?: CommonDBOptions,
529
529
  ): Promise<ROW[]> {
530
530
  return await this.db.getByIds(table, ids, { ...opt, tx: this })
@@ -532,13 +532,13 @@ export class FirestoreDBTransaction implements DBTransaction {
532
532
 
533
533
  async saveBatch<ROW extends ObjectWithId>(
534
534
  table: string,
535
- rows: ROW[],
535
+ rows: readonly ROW[],
536
536
  opt?: CommonDBSaveOptions<ROW>,
537
537
  ): Promise<void> {
538
538
  await this.db.saveBatch(table, rows, { ...opt, tx: this })
539
539
  }
540
540
 
541
- async deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number> {
541
+ async deleteByIds(table: string, ids: readonly string[], opt?: CommonDBOptions): Promise<number> {
542
542
  return await this.db.deleteByIds(table, ids, { ...opt, tx: this })
543
543
  }
544
544
  }
@@ -2,7 +2,7 @@ import { Readable } from 'node:stream'
2
2
  import { FieldPath } from '@google-cloud/firestore'
3
3
  import type { Query, QuerySnapshot } from '@google-cloud/firestore'
4
4
  import type { DBQuery } from '@naturalcycles/db-lib'
5
- import { localTime } from '@naturalcycles/js-lib/datetime'
5
+ import { nowUnixMillis } from '@naturalcycles/js-lib/datetime'
6
6
  import { _ms } from '@naturalcycles/js-lib/datetime/time.util.js'
7
7
  import { _assert } from '@naturalcycles/js-lib/error/assert.js'
8
8
  import { createCommonLoggerAtLevel } from '@naturalcycles/js-lib/log'
@@ -107,7 +107,7 @@ export class FirestoreShardedReadable<T extends ObjectWithId = any>
107
107
  const { logger, table } = this
108
108
 
109
109
  if (this.lastQueryDoneByShard[shard]) {
110
- this.totalWait += Date.now() - this.lastQueryDoneByShard[shard]
110
+ this.totalWait += nowUnixMillis() - this.lastQueryDoneByShard[shard]
111
111
  }
112
112
 
113
113
  this.queryIsRunningByShard[shard] = true
@@ -151,7 +151,7 @@ export class FirestoreShardedReadable<T extends ObjectWithId = any>
151
151
 
152
152
  this.cursorByShard[shard] = lastDocId
153
153
  this.queryIsRunningByShard[shard] = false // ready to take more _reads
154
- this.lastQueryDoneByShard[shard] = localTime.nowUnixMillis()
154
+ this.lastQueryDoneByShard[shard] = nowUnixMillis()
155
155
 
156
156
  for (const row of rows) {
157
157
  this.push(row)
@@ -7,7 +7,7 @@ import type {
7
7
  Timestamp,
8
8
  } from '@google-cloud/firestore'
9
9
  import type { DBQuery } from '@naturalcycles/db-lib'
10
- import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js'
10
+ import { nowUnixMillis } from '@naturalcycles/js-lib/datetime/localTime.js'
11
11
  import { _ms } from '@naturalcycles/js-lib/datetime/time.util.js'
12
12
  import { TimeoutError } from '@naturalcycles/js-lib/error/error.util.js'
13
13
  import { createCommonLoggerAtLevel } from '@naturalcycles/js-lib/log'
@@ -125,9 +125,9 @@ export class FirestoreStreamReadable<T extends ObjectWithId = any>
125
125
  // paused: this.paused,
126
126
  // })
127
127
 
128
- const started = localTime.nowUnixMillis()
128
+ const started = nowUnixMillis()
129
129
  const qs = await this.runQuery(q)
130
- const queryTook = Date.now() - started
130
+ const queryTook = nowUnixMillis() - started
131
131
  if (!qs) {
132
132
  // error already emitted in runQuery
133
133
  return
package/src/query.util.ts CHANGED
@@ -2,6 +2,7 @@ import { FieldPath, Timestamp } from '@google-cloud/firestore'
2
2
  import type { Query, WhereFilterOp } from '@google-cloud/firestore'
3
3
  import type { CommonDBReadOptions, DBQuery, DBQueryFilterOperator } from '@naturalcycles/db-lib'
4
4
  import { _round } from '@naturalcycles/js-lib'
5
+ import { nowUnixMillis } from '@naturalcycles/js-lib/datetime/localTime.js'
5
6
  import type { ObjectWithId } from '@naturalcycles/js-lib/types'
6
7
 
7
8
  export function readAtToReadTime(opt: CommonDBReadOptions): Timestamp | undefined {
@@ -9,7 +10,7 @@ export function readAtToReadTime(opt: CommonDBReadOptions): Timestamp | undefine
9
10
 
10
11
  // Same logic as Datastore: round to whole minutes, guard against future
11
12
  let readTimeMs = _round(opt.readAt, 60) * 1000
12
- if (readTimeMs >= Date.now() - 1000) {
13
+ if (readTimeMs >= nowUnixMillis() - 1000) {
13
14
  readTimeMs -= 60_000
14
15
  }
15
16