@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 +1 -1
- package/src/engine/Engine.js +16 -7
- package/.deepsource.toml +0 -10
package/package.json
CHANGED
package/src/engine/Engine.js
CHANGED
@@ -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 {
|
117
|
-
* @returns {Array<Model
|
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 =
|
124
|
-
|
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', {
|
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
|
-
|
341
|
+
// Implemented in extending Engine class
|
333
342
|
}
|
334
343
|
|
335
344
|
/**
|