backend-manager 3.0.43 → 3.0.45

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backend-manager",
3
- "version": "3.0.43",
3
+ "version": "3.0.45",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -23,9 +23,12 @@ Utilities.prototype.iterateCollection = function (callback, options) {
23
23
  options.orderBy = options.orderBy || null;
24
24
  options.startAt = options.startAt || null;
25
25
  options.startAfter = options.startAfter || null;
26
+ options.prefetchCursor = typeof options.prefetchCursor === 'undefined'
27
+ ? (!!options.startAt || !!options.startAfter) && !options.orderBy
28
+ : options.prefetchCursor;
26
29
  options.log = options.log;
27
30
 
28
- function listAllDocuments(nextPageToken) {
31
+ async function listAllDocuments(nextPageToken) {
29
32
  let query = admin.firestore().collection(options.collection)
30
33
 
31
34
  options.where
@@ -37,12 +40,24 @@ Utilities.prototype.iterateCollection = function (callback, options) {
37
40
  query = query.orderBy(options.orderBy);
38
41
  }
39
42
 
40
- if (batch === -1 && options.startAt) {
41
- query = query.startAt(options.startAt);
42
- }
43
+ if (batch === -1) {
44
+ let prefetchedCursor = null;
45
+
46
+ if (options.prefetchCursor) {
47
+ prefetchedCursor = await admin.firestore().doc(`${options.collection}/${options.startAt || options.startAfter}`)
48
+ .get()
49
+ .catch(e => e);
50
+
51
+ if (prefetchedCursor instanceof Error) {
52
+ return reject(prefetchedCursor);
53
+ }
54
+ }
43
55
 
44
- if (batch === -1 && options.startAfter) {
45
- query = query.startAfter(options.startAfter);
56
+ if (options.startAt) {
57
+ query = query.startAt(prefetchedCursor || options.startAt);
58
+ } else if (options.startAfter) {
59
+ query = query.startAfter(prefetchedCursor || options.startAfter);
60
+ }
46
61
  }
47
62
 
48
63
  // Start at next page
@@ -174,19 +189,41 @@ Utilities.prototype.get = function (docPath, options) {
174
189
  const self = this;
175
190
 
176
191
  return new Promise(function(resolve, reject) {
192
+ const Manager = self.Manager;
193
+ const { admin } = Manager.libraries;
177
194
 
178
195
  options = options || {};
179
196
  options.maxAge = options.maxAge || (1000 * 60 * 5); // 5 minutes
197
+ options.readTime = typeof options.readTime === 'undefined' ? null : options.readTime;
198
+ options.log = typeof options.log === 'undefined' ? false : options.log;
180
199
 
181
- self.cache = self.cache || self.Manager.storage({name: 'cache', temporary: true, clear: false});
200
+ self.cache = self.cache || Manager.storage({name: 'cache', temporary: true, clear: false});
182
201
 
183
202
  const item = self.cache.get(docPath).value();
184
203
  const age = item ? Date.now() - item.time : null;
185
204
 
186
- if (item && age && age < options.maxAge) {
205
+ if (options.readTime) {
206
+ const { Timestamp } = require('firebase-admin/firestore')
207
+ const time = Math.round(new Date(options.readTime).getTime() / 1000 / 60);
208
+ const logg = new Date(time * 1000 * 60);
209
+
210
+ if (options.log) {
211
+ console.log('Read time:', logg);
212
+ }
213
+
214
+ // loop docs
215
+ admin.firestore().runTransaction(
216
+ updateFunction => updateFunction.get(admin.firestore().doc(docPath)),
217
+ {readOnly: true, readTime: new Timestamp(time * 60, 0)}
218
+ )
219
+ .then(snap => {
220
+ return resolve(snap);
221
+ })
222
+ .catch(e => reject(e));
223
+ } else if (item && age && age < options.maxAge) {
187
224
  return resolve(item.doc);
188
225
  } else {
189
- self.Manager.libraries.admin.firestore().doc(docPath)
226
+ admin.firestore().doc(docPath)
190
227
  .get()
191
228
  .then(async (doc) => {
192
229
  const data = doc.data();