@sprucelabs/data-stores 18.0.7 → 18.0.10

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.
@@ -3,8 +3,9 @@ export default class DatabaseFactory {
3
3
  private static cache;
4
4
  private constructor();
5
5
  static Database(options: {
6
- dbName: string;
6
+ dbName?: string;
7
7
  dbConnectionString: string;
8
8
  }): Database;
9
+ private static generateCacheKey;
9
10
  static reset(): void;
10
11
  }
@@ -13,7 +13,7 @@ export default class DatabaseFactory {
13
13
  friendlyMessage: "DatabaseFactory can't connect to the database! Setting env.DB_CONNECTION_STRING may help!",
14
14
  });
15
15
  }
16
- const cacheKey = options.dbName + options.dbConnectionString;
16
+ const cacheKey = this.generateCacheKey(options);
17
17
  if (!this.cache[cacheKey]) {
18
18
  if (dbConnectionString.startsWith('memory')) {
19
19
  database = new NeDbDatabase();
@@ -25,6 +25,15 @@ export default class DatabaseFactory {
25
25
  }
26
26
  return this.cache[cacheKey];
27
27
  }
28
+ static generateCacheKey(options) {
29
+ if (!options.dbName && options.dbConnectionString.includes('memory')) {
30
+ const key = Object.keys(this.cache).find((k) => k.startsWith('memory'));
31
+ if (key) {
32
+ return key;
33
+ }
34
+ }
35
+ return options.dbName + options.dbConnectionString;
36
+ }
28
37
  static reset() {
29
38
  this.cache = {};
30
39
  }
@@ -9,6 +9,7 @@ export default class StoreLoader {
9
9
  private static instance;
10
10
  private static defaultStoreDir;
11
11
  private static defaultDb;
12
+ private factory?;
12
13
  private constructor();
13
14
  static setStoreDir(dir: string): void;
14
15
  static setDatabase(db: Database): void;
@@ -64,12 +64,19 @@ export default class StoreLoader {
64
64
  }
65
65
  loadStoresAndErrors() {
66
66
  return __awaiter(this, void 0, void 0, function* () {
67
- const { stores, errors } = yield this.loadStoreClassesWithErrors();
68
- const factory = StoreFactory.Factory(this.db);
69
- for (const store of stores) {
70
- factory.setStoreClass(namesUtil.toCamel(store.namePascal), store.Class);
67
+ let errors = [];
68
+ if (!this.factory) {
69
+ const { stores, errors: loadedErrors } = yield this.loadStoreClassesWithErrors();
70
+ errors = loadedErrors;
71
+ const factory = StoreFactory.Factory(this.db);
72
+ for (const store of stores) {
73
+ factory.setStoreClass(namesUtil.toCamel(store.namePascal), store.Class);
74
+ }
75
+ if (errors.length === 0) {
76
+ this.factory = factory;
77
+ }
71
78
  }
72
- return { factory, errors };
79
+ return { factory: this.factory, errors };
73
80
  });
74
81
  }
75
82
  loadStoreClassesWithErrors() {
@@ -3,8 +3,9 @@ export default class DatabaseFactory {
3
3
  private static cache;
4
4
  private constructor();
5
5
  static Database(options: {
6
- dbName: string;
6
+ dbName?: string;
7
7
  dbConnectionString: string;
8
8
  }): Database;
9
+ private static generateCacheKey;
9
10
  static reset(): void;
10
11
  }
@@ -18,7 +18,7 @@ class DatabaseFactory {
18
18
  friendlyMessage: "DatabaseFactory can't connect to the database! Setting env.DB_CONNECTION_STRING may help!",
19
19
  });
20
20
  }
21
- const cacheKey = options.dbName + options.dbConnectionString;
21
+ const cacheKey = this.generateCacheKey(options);
22
22
  if (!this.cache[cacheKey]) {
23
23
  if (dbConnectionString.startsWith('memory')) {
24
24
  database = new NeDbDatabase_1.default();
@@ -30,6 +30,15 @@ class DatabaseFactory {
30
30
  }
31
31
  return this.cache[cacheKey];
32
32
  }
33
+ static generateCacheKey(options) {
34
+ if (!options.dbName && options.dbConnectionString.includes('memory')) {
35
+ const key = Object.keys(this.cache).find((k) => k.startsWith('memory'));
36
+ if (key) {
37
+ return key;
38
+ }
39
+ }
40
+ return options.dbName + options.dbConnectionString;
41
+ }
33
42
  static reset() {
34
43
  this.cache = {};
35
44
  }
@@ -9,6 +9,7 @@ export default class StoreLoader {
9
9
  private static instance;
10
10
  private static defaultStoreDir;
11
11
  private static defaultDb;
12
+ private factory?;
12
13
  private constructor();
13
14
  static setStoreDir(dir: string): void;
14
15
  static setDatabase(db: Database): void;
@@ -53,12 +53,19 @@ class StoreLoader {
53
53
  return factory;
54
54
  }
55
55
  async loadStoresAndErrors() {
56
- const { stores, errors } = await this.loadStoreClassesWithErrors();
57
- const factory = StoreFactory_1.default.Factory(this.db);
58
- for (const store of stores) {
59
- factory.setStoreClass(spruce_skill_utils_1.namesUtil.toCamel(store.namePascal), store.Class);
56
+ let errors = [];
57
+ if (!this.factory) {
58
+ const { stores, errors: loadedErrors } = await this.loadStoreClassesWithErrors();
59
+ errors = loadedErrors;
60
+ const factory = StoreFactory_1.default.Factory(this.db);
61
+ for (const store of stores) {
62
+ factory.setStoreClass(spruce_skill_utils_1.namesUtil.toCamel(store.namePascal), store.Class);
63
+ }
64
+ if (errors.length === 0) {
65
+ this.factory = factory;
66
+ }
60
67
  }
61
- return { factory, errors };
68
+ return { factory: this.factory, errors };
62
69
  }
63
70
  async loadStoreClassesWithErrors() {
64
71
  var _a, _b;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "18.0.7",
6
+ "version": "18.0.10",
7
7
  "files": [
8
8
  "build/**/*",
9
9
  "!build/__tests__",