backend-manager 3.0.44 → 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.44",
3
+ "version": "3.0.45",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -189,19 +189,41 @@ Utilities.prototype.get = function (docPath, options) {
189
189
  const self = this;
190
190
 
191
191
  return new Promise(function(resolve, reject) {
192
+ const Manager = self.Manager;
193
+ const { admin } = Manager.libraries;
192
194
 
193
195
  options = options || {};
194
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;
195
199
 
196
- 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});
197
201
 
198
202
  const item = self.cache.get(docPath).value();
199
203
  const age = item ? Date.now() - item.time : null;
200
204
 
201
- 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) {
202
224
  return resolve(item.doc);
203
225
  } else {
204
- self.Manager.libraries.admin.firestore().doc(docPath)
226
+ admin.firestore().doc(docPath)
205
227
  .get()
206
228
  .then(async (doc) => {
207
229
  const data = doc.data();