backend-manager 3.2.60 → 3.2.62
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.2.
|
|
3
|
+
"version": "3.2.62",
|
|
4
4
|
"description": "Quick tools for developing Firebase functions",
|
|
5
5
|
"main": "src/manager/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -71,4 +71,4 @@
|
|
|
71
71
|
"wonderful-log": "^1.0.5",
|
|
72
72
|
"yargs": "^17.7.2"
|
|
73
73
|
}
|
|
74
|
-
}
|
|
74
|
+
}
|
|
@@ -73,6 +73,8 @@ Module.prototype.clearFirestore = function() {
|
|
|
73
73
|
const assistant = self.assistant;
|
|
74
74
|
const context = self.context;
|
|
75
75
|
|
|
76
|
+
const { admin } = libraries;
|
|
77
|
+
|
|
76
78
|
return new Promise(async function(resolve, reject) {
|
|
77
79
|
// Log status
|
|
78
80
|
assistant.log(`cron/daily/reset-usage() [firestore]: Starting...`);
|
|
@@ -166,7 +168,7 @@ Module.prototype.clearFirestore = function() {
|
|
|
166
168
|
// Clear temporary/usage in firestore by deleting the doc
|
|
167
169
|
admin.firestore().collection('temporary').listDocuments()
|
|
168
170
|
.then((snap) => {
|
|
169
|
-
const chunks = []
|
|
171
|
+
const chunks = [];
|
|
170
172
|
for (let i = 0; i < snap.length; i += 500) {
|
|
171
173
|
chunks.push(snap.slice(i, i + 500))
|
|
172
174
|
}
|
|
@@ -196,32 +196,41 @@ Utilities.prototype.get = function (docPath, options) {
|
|
|
196
196
|
options.maxAge = options.maxAge || (1000 * 60 * 5); // 5 minutes
|
|
197
197
|
options.readTime = typeof options.readTime === 'undefined' ? null : options.readTime;
|
|
198
198
|
options.log = typeof options.log === 'undefined' ? false : options.log;
|
|
199
|
+
options.format = typeof options.format === 'undefined' ? 'raw' : options.format;
|
|
199
200
|
|
|
200
201
|
self.cache = self.cache || Manager.storage({name: 'cache', temporary: true, clear: false});
|
|
201
202
|
|
|
202
203
|
const item = self.cache.get(docPath).value();
|
|
203
204
|
const age = item ? Date.now() - item.time : null;
|
|
204
205
|
|
|
206
|
+
function _format(doc) {
|
|
207
|
+
if (options.format === 'raw') {
|
|
208
|
+
return doc;
|
|
209
|
+
} else if (options.format === 'data') {
|
|
210
|
+
return doc.data();
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Log
|
|
205
215
|
if (options.readTime) {
|
|
206
216
|
const { Timestamp } = require('firebase-admin/firestore')
|
|
207
217
|
const time = Math.round(new Date(options.readTime).getTime() / 1000 / 60);
|
|
208
|
-
const
|
|
218
|
+
const timeLog = new Date(time * 1000 * 60);
|
|
209
219
|
|
|
220
|
+
// Log
|
|
210
221
|
if (options.log) {
|
|
211
|
-
console.log('Read time:',
|
|
222
|
+
console.log('Read time:', timeLog);
|
|
212
223
|
}
|
|
213
224
|
|
|
214
|
-
//
|
|
225
|
+
// Loop docs
|
|
215
226
|
admin.firestore().runTransaction(
|
|
216
227
|
updateFunction => updateFunction.get(admin.firestore().doc(docPath)),
|
|
217
228
|
{readOnly: true, readTime: new Timestamp(time * 60, 0)}
|
|
218
229
|
)
|
|
219
|
-
.then(snap =>
|
|
220
|
-
|
|
221
|
-
})
|
|
222
|
-
.catch(e => reject(e));
|
|
230
|
+
.then((snap) => resolve(_format(snap)))
|
|
231
|
+
.catch((e) => reject(e));
|
|
223
232
|
} else if (item && age && age < options.maxAge) {
|
|
224
|
-
return resolve(item.doc);
|
|
233
|
+
return resolve(_format(item.doc));
|
|
225
234
|
} else {
|
|
226
235
|
admin.firestore().doc(docPath)
|
|
227
236
|
.get()
|
|
@@ -236,9 +245,9 @@ Utilities.prototype.get = function (docPath, options) {
|
|
|
236
245
|
.write();
|
|
237
246
|
}
|
|
238
247
|
|
|
239
|
-
return resolve(doc);
|
|
248
|
+
return resolve(_format(doc));
|
|
240
249
|
})
|
|
241
|
-
.catch(e => reject(e));
|
|
250
|
+
.catch((e) => reject(e));
|
|
242
251
|
}
|
|
243
252
|
});
|
|
244
253
|
};
|