backend-manager 2.3.16 → 2.3.17

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": "2.3.16",
3
+ "version": "2.3.17",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -66,4 +66,4 @@
66
66
  "src/",
67
67
  "templates/"
68
68
  ]
69
- }
69
+ }
@@ -15,6 +15,10 @@ function Manager(exporter, options) {
15
15
  self.libraries = {};
16
16
  self.handlers = {};
17
17
 
18
+ self._internal = {
19
+ storage: {},
20
+ };
21
+
18
22
  return self;
19
23
  }
20
24
 
@@ -362,21 +366,7 @@ Manager.prototype.init = function (exporter, options) {
362
366
 
363
367
  // Setup LocalDatabase
364
368
  if (options.setupLocalDatabase) {
365
- const low = require('lowdb');
366
- const FileSync = require('lowdb/adapters/FileSync');
367
- // const dbPath = path.resolve(process.cwd(), './.data/db.json');
368
- const dbPath = './.data/db.json';
369
- const adapter = new FileSync(dbPath);
370
- const jetpack = require('fs-jetpack');
371
-
372
- try {
373
- if (!jetpack.exists(dbPath)) {
374
- jetpack.write(dbPath, {});
375
- }
376
- self.libraries.localDatabase = low(adapter);
377
- } catch (e) {
378
- console.error('Could not load .data', e);
379
- }
369
+ self.storage();
380
370
  }
381
371
 
382
372
  return self;
@@ -478,6 +468,49 @@ Manager.prototype.ApiManager = function () {
478
468
  return new self.libraries.ApiManager(self, ...arguments);
479
469
  };
480
470
 
471
+ Manager.prototype.storage = function (options) {
472
+ const self = this;
473
+ options = options || {};
474
+ options.name = options.name || 'main';
475
+
476
+ if (!self._internal.storage[options.name]) {
477
+ const low = require('lowdb');
478
+ const FileSync = require('lowdb/adapters/FileSync');
479
+ const dbPath = `./.data/${options.name}.json`;
480
+ const adapter = new FileSync(dbPath);
481
+ const jetpack = require('fs-jetpack');
482
+
483
+ options.clearInvalid = typeof options.clearInvalid === 'undefined'
484
+ ? true
485
+ : options.clearInvalid;
486
+
487
+ function _setup() {
488
+ if (!jetpack.exists(dbPath)) {
489
+ jetpack.write(dbPath, {});
490
+ }
491
+ self._internal.storage[options.name] = low(adapter);
492
+ }
493
+
494
+ try {
495
+ _setup()
496
+ } catch (e) {
497
+ console.error(`Could not storage: ${dbPath}`, e);
498
+
499
+ try {
500
+ if (options.clearInvalid) {
501
+ console.log(`Clearing invalud storage: ${dbPath}`);
502
+ jetpack.write(dbPath, {});
503
+ }
504
+ _setup()
505
+ } catch (e) {
506
+ console.error(`Failed to clear invalid storage: ${dbPath}`, e);
507
+ }
508
+ }
509
+ }
510
+
511
+ return self._internal.storage[options.name]
512
+ };
513
+
481
514
  // Manager.prototype.LocalDatabase = function () {
482
515
  // const self = this;
483
516
  // if (!self.libraries.LocalDatabase) {