@naturalcycles/firestore-lib 2.10.1 → 2.11.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,7 +1,7 @@
|
|
|
1
1
|
import { Readable } from 'node:stream';
|
|
2
2
|
import { FieldPath } from '@google-cloud/firestore';
|
|
3
3
|
import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js';
|
|
4
|
-
import {
|
|
4
|
+
import { _ms } from '@naturalcycles/js-lib/datetime/time.util.js';
|
|
5
5
|
import { pRetry } from '@naturalcycles/js-lib/promise/pRetry.js';
|
|
6
6
|
import { unescapeDocId } from './firestore.util.js';
|
|
7
7
|
export class FirestoreStreamReadable extends Readable {
|
|
@@ -56,7 +56,6 @@ export class FirestoreStreamReadable extends Readable {
|
|
|
56
56
|
}
|
|
57
57
|
if (this.queryIsRunning) {
|
|
58
58
|
this.logger.log(`_read #${this.countReads}, queryIsRunning: true, doing nothing`);
|
|
59
|
-
// todo: check if this can cause a "hang", if no more _reads would come later and we get stuck?
|
|
60
59
|
return;
|
|
61
60
|
}
|
|
62
61
|
void this.runNextQuery().catch(err => {
|
|
@@ -73,7 +72,6 @@ export class FirestoreStreamReadable extends Readable {
|
|
|
73
72
|
if (this.originalLimit) {
|
|
74
73
|
limit = Math.min(this.opt.batchSize, this.originalLimit - this.rowsRetrieved);
|
|
75
74
|
}
|
|
76
|
-
// console.log(`limit: ${limit}`)
|
|
77
75
|
// We have to orderBy documentId, to be able to use id as a cursor
|
|
78
76
|
let q = this.q.orderBy(FieldPath.documentId()).limit(limit);
|
|
79
77
|
if (this.endCursor) {
|
|
@@ -85,7 +83,7 @@ export class FirestoreStreamReadable extends Readable {
|
|
|
85
83
|
// })
|
|
86
84
|
const started = localTime.nowUnixMillis();
|
|
87
85
|
const qs = await this.runQuery(q);
|
|
88
|
-
|
|
86
|
+
const queryTook = Date.now() - started;
|
|
89
87
|
if (!qs) {
|
|
90
88
|
// error already emitted in runQuery
|
|
91
89
|
return;
|
|
@@ -100,7 +98,7 @@ export class FirestoreStreamReadable extends Readable {
|
|
|
100
98
|
});
|
|
101
99
|
}
|
|
102
100
|
this.rowsRetrieved += rows.length;
|
|
103
|
-
logger.log(`${table} got ${rows.length} rows, ${this.rowsRetrieved} rowsRetrieved`);
|
|
101
|
+
logger.log(`${table} got ${rows.length} rows in ${_ms(queryTook)}, ${this.rowsRetrieved} rowsRetrieved`);
|
|
104
102
|
this.endCursor = lastDocId;
|
|
105
103
|
this.queryIsRunning = false; // ready to take more _reads
|
|
106
104
|
let shouldContinue = false;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@ import { Readable } from 'node:stream'
|
|
|
2
2
|
import { FieldPath, type Query, type QuerySnapshot } from '@google-cloud/firestore'
|
|
3
3
|
import type { DBQuery } from '@naturalcycles/db-lib'
|
|
4
4
|
import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js'
|
|
5
|
-
import {
|
|
5
|
+
import { _ms } from '@naturalcycles/js-lib/datetime/time.util.js'
|
|
6
6
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
7
7
|
import { pRetry } from '@naturalcycles/js-lib/promise/pRetry.js'
|
|
8
8
|
import type { ObjectWithId } from '@naturalcycles/js-lib/types'
|
|
@@ -78,7 +78,6 @@ export class FirestoreStreamReadable<T extends ObjectWithId = any>
|
|
|
78
78
|
|
|
79
79
|
if (this.queryIsRunning) {
|
|
80
80
|
this.logger.log(`_read #${this.countReads}, queryIsRunning: true, doing nothing`)
|
|
81
|
-
// todo: check if this can cause a "hang", if no more _reads would come later and we get stuck?
|
|
82
81
|
return
|
|
83
82
|
}
|
|
84
83
|
|
|
@@ -100,7 +99,6 @@ export class FirestoreStreamReadable<T extends ObjectWithId = any>
|
|
|
100
99
|
limit = Math.min(this.opt.batchSize, this.originalLimit - this.rowsRetrieved)
|
|
101
100
|
}
|
|
102
101
|
|
|
103
|
-
// console.log(`limit: ${limit}`)
|
|
104
102
|
// We have to orderBy documentId, to be able to use id as a cursor
|
|
105
103
|
let q = this.q.orderBy(FieldPath.documentId()).limit(limit)
|
|
106
104
|
if (this.endCursor) {
|
|
@@ -114,7 +112,7 @@ export class FirestoreStreamReadable<T extends ObjectWithId = any>
|
|
|
114
112
|
|
|
115
113
|
const started = localTime.nowUnixMillis()
|
|
116
114
|
const qs = await this.runQuery(q)
|
|
117
|
-
|
|
115
|
+
const queryTook = Date.now() - started
|
|
118
116
|
if (!qs) {
|
|
119
117
|
// error already emitted in runQuery
|
|
120
118
|
return
|
|
@@ -132,7 +130,9 @@ export class FirestoreStreamReadable<T extends ObjectWithId = any>
|
|
|
132
130
|
}
|
|
133
131
|
|
|
134
132
|
this.rowsRetrieved += rows.length
|
|
135
|
-
logger.log(
|
|
133
|
+
logger.log(
|
|
134
|
+
`${table} got ${rows.length} rows in ${_ms(queryTook)}, ${this.rowsRetrieved} rowsRetrieved`,
|
|
135
|
+
)
|
|
136
136
|
|
|
137
137
|
this.endCursor = lastDocId
|
|
138
138
|
this.queryIsRunning = false // ready to take more _reads
|