@naturalcycles/firestore-lib 2.16.1 → 2.17.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.
@@ -2,6 +2,7 @@ import { Readable } from 'node:stream';
2
2
  import { FieldPath } from '@google-cloud/firestore';
3
3
  import { localTime } from '@naturalcycles/js-lib/datetime';
4
4
  import { _ms } from '@naturalcycles/js-lib/datetime/time.util.js';
5
+ import { _assert } from '@naturalcycles/js-lib/error/assert.js';
5
6
  import { createCommonLoggerAtLevel } from '@naturalcycles/js-lib/log';
6
7
  import { pRetry } from '@naturalcycles/js-lib/promise/pRetry.js';
7
8
  import { unescapeDocId } from './firestore.util.js';
@@ -37,6 +38,9 @@ export class FirestoreShardedReadable extends Readable {
37
38
  batchSize: 3000,
38
39
  ...opt,
39
40
  };
41
+ // Offset cannot be supported here: the query is split into concurrent per-shard queries,
42
+ // and a global offset would be (wrongly) re-applied to each of them.
43
+ _assert(!dbQuery._offsetValue, 'offset is not supported by experimentalShardedStream');
40
44
  this.originalLimit = dbQuery._limitValue;
41
45
  this.table = dbQuery.table;
42
46
  const logger = createCommonLoggerAtLevel(opt.logger, opt.logLevel);
@@ -80,7 +80,9 @@ export class FirestoreStreamReadable extends Readable {
80
80
  // We have to orderBy documentId, to be able to use id as a cursor
81
81
  let q = this.q.orderBy(FieldPath.documentId()).limit(limit);
82
82
  if (this.endCursor) {
83
- q = q.startAfter(this.endCursor);
83
+ // Offset (if any) is already "consumed" by the first query,
84
+ // subsequent queries continue from the cursor and must not skip rows again.
85
+ q = q.startAfter(this.endCursor).offset(0);
84
86
  }
85
87
  // logger.log(`runNextQuery`, {
86
88
  // rowsRetrieved: this.rowsRetrieved,
@@ -32,6 +32,10 @@ export function dbQueryToFirestoreQuery(dbQuery, emptyQuery) {
32
32
  if (dbQuery._limitValue) {
33
33
  q = q.limit(dbQuery._limitValue);
34
34
  }
35
+ // offset
36
+ if (dbQuery._offsetValue) {
37
+ q = q.offset(dbQuery._offsetValue);
38
+ }
35
39
  // selectedFields
36
40
  if (dbQuery._selectedFieldNames) {
37
41
  // id is filtered out, because in Firestore it's not a "property",
package/package.json CHANGED
@@ -9,10 +9,10 @@
9
9
  "tslib": "^2"
10
10
  },
11
11
  "devDependencies": {
12
- "@types/node": "^25",
13
- "@typescript/native-preview": "beta",
14
- "firebase-admin": "^14",
15
- "@naturalcycles/dev-lib": "18.4.2"
12
+ "@naturalcycles/dev-lib": "0.0.0",
13
+ "@types/node": "^26",
14
+ "typescript": "^7",
15
+ "firebase-admin": "^14"
16
16
  },
17
17
  "exports": {
18
18
  ".": "./dist/index.js"
@@ -38,7 +38,7 @@
38
38
  "engines": {
39
39
  "node": ">=24.10.0"
40
40
  },
41
- "version": "2.16.1",
41
+ "version": "2.17.0",
42
42
  "description": "Firestore implementation of CommonDB interface",
43
43
  "author": "Natural Cycles Team",
44
44
  "license": "MIT",
@@ -4,6 +4,7 @@ import type { Query, QuerySnapshot } from '@google-cloud/firestore'
4
4
  import type { DBQuery } from '@naturalcycles/db-lib'
5
5
  import { localTime } from '@naturalcycles/js-lib/datetime'
6
6
  import { _ms } from '@naturalcycles/js-lib/datetime/time.util.js'
7
+ import { _assert } from '@naturalcycles/js-lib/error/assert.js'
7
8
  import { createCommonLoggerAtLevel } from '@naturalcycles/js-lib/log'
8
9
  import type { CommonLogger } from '@naturalcycles/js-lib/log'
9
10
  import { pRetry } from '@naturalcycles/js-lib/promise/pRetry.js'
@@ -58,6 +59,10 @@ export class FirestoreShardedReadable<T extends ObjectWithId = any>
58
59
  ...opt,
59
60
  }
60
61
 
62
+ // Offset cannot be supported here: the query is split into concurrent per-shard queries,
63
+ // and a global offset would be (wrongly) re-applied to each of them.
64
+ _assert(!dbQuery._offsetValue, 'offset is not supported by experimentalShardedStream')
65
+
61
66
  this.originalLimit = dbQuery._limitValue
62
67
  this.table = dbQuery.table
63
68
  const logger = createCommonLoggerAtLevel(opt.logger, opt.logLevel)
@@ -114,7 +114,9 @@ export class FirestoreStreamReadable<T extends ObjectWithId = any>
114
114
  // We have to orderBy documentId, to be able to use id as a cursor
115
115
  let q = this.q.orderBy(FieldPath.documentId()).limit(limit)
116
116
  if (this.endCursor) {
117
- q = q.startAfter(this.endCursor)
117
+ // Offset (if any) is already "consumed" by the first query,
118
+ // subsequent queries continue from the cursor and must not skip rows again.
119
+ q = q.startAfter(this.endCursor).offset(0)
118
120
  }
119
121
 
120
122
  // logger.log(`runNextQuery`, {
package/src/query.util.ts CHANGED
@@ -31,7 +31,7 @@ export function dbQueryToFirestoreQuery<ROW extends ObjectWithId>(
31
31
 
32
32
  // filter
33
33
  for (const f of dbQuery._filters) {
34
- q = q.where(mapName(f.name), OP_MAP[f.op] || (f.op as WhereFilterOp), f.val)
34
+ q = q.where(mapName(f.name), OP_MAP[f.op] || f.op, f.val)
35
35
  }
36
36
 
37
37
  // order
@@ -46,6 +46,11 @@ export function dbQueryToFirestoreQuery<ROW extends ObjectWithId>(
46
46
  q = q.limit(dbQuery._limitValue)
47
47
  }
48
48
 
49
+ // offset
50
+ if (dbQuery._offsetValue) {
51
+ q = q.offset(dbQuery._offsetValue)
52
+ }
53
+
49
54
  // selectedFields
50
55
  if (dbQuery._selectedFieldNames) {
51
56
  // id is filtered out, because in Firestore it's not a "property",