@acodeninja/persist 2.2.3 → 2.3.0

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": "@acodeninja/persist",
3
- "version": "2.2.3",
3
+ "version": "2.3.0",
4
4
  "description": "A JSON based data modelling and persistence module with alternate storage mechanisms.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -10,6 +10,7 @@ import lunr from 'lunr';
10
10
  */
11
11
  class Engine {
12
12
  static configuration = undefined;
13
+ static _searchCache = undefined;
13
14
 
14
15
  /**
15
16
  * Retrieves a model by its ID. This method must be implemented by subclasses.
@@ -113,16 +114,24 @@ class Engine {
113
114
  * Performs a search query on a model's index and returns the matching models.
114
115
  *
115
116
  * @param {Model.constructor} model - The model class.
116
- * @param {object} query - The search query string.
117
- * @returns {Array<Model>} An array of models matching the search query.
117
+ * @param {string} query - The search query string.
118
+ * @returns {Promise<Array<Model>>} An array of models matching the search query.
118
119
  * @throws {EngineError} Throws if the search index is not available for the model.
119
120
  */
120
121
  static async search(model, query) {
121
122
  this.checkConfiguration();
122
123
 
123
- const index = await this.getSearchIndexCompiled(model).catch(() => {
124
- throw new EngineError(`The model ${model.toString()} does not have a search index available.`);
125
- });
124
+ const index =
125
+ (this._searchCache && this.configuration?.cache?.search && Date.now() - this._searchCache[0] < this.configuration.cache.search) ?
126
+ this._searchCache[1] :
127
+ await this.getSearchIndexCompiled(model)
128
+ .then(i => {
129
+ this._searchCache = [Date.now(), i];
130
+ return i;
131
+ })
132
+ .catch(error => {
133
+ throw new EngineError(`The model ${model.toString()} does not have a search index available.`, error);
134
+ });
126
135
 
127
136
  const searchIndex = lunr.Index.load(index);
128
137
 
@@ -317,7 +326,7 @@ class Engine {
317
326
  static configuration = configuration;
318
327
  }
319
328
 
320
- Object.defineProperty(ConfiguredStore, 'name', { value: `${this.toString()}` });
329
+ Object.defineProperty(ConfiguredStore, 'name', {value: `${this.toString()}`});
321
330
 
322
331
  return ConfiguredStore;
323
332
  }
@@ -329,7 +338,7 @@ class Engine {
329
338
  * @abstract
330
339
  */
331
340
  static checkConfiguration() {
332
- // Implemented in extending Engine class
341
+ // Implemented in extending Engine class
333
342
  }
334
343
 
335
344
  /**
package/.deepsource.toml DELETED
@@ -1,10 +0,0 @@
1
- version = 1
2
-
3
- [[analyzers]]
4
- name = "javascript"
5
-
6
- [analyzers.meta]
7
- environment = [
8
- "nodejs",
9
- "browser"
10
- ]