@acodeninja/persist 3.0.0-next.10 → 3.0.0-next.11
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
@@ -0,0 +1,43 @@
|
|
1
|
+
import lunr from 'lunr';
|
2
|
+
|
3
|
+
export default class SearchIndex {
|
4
|
+
#index;
|
5
|
+
#model;
|
6
|
+
#compiledIndex;
|
7
|
+
|
8
|
+
constructor(model, index) {
|
9
|
+
this.#index = index;
|
10
|
+
this.#model = model;
|
11
|
+
if (model.searchProperties().length === 0) {
|
12
|
+
throw new NoIndexAvailableSearchIndexError(this.#model);
|
13
|
+
}
|
14
|
+
|
15
|
+
this.#compiledIndex = lunr(function () {
|
16
|
+
this.ref('id');
|
17
|
+
|
18
|
+
for (const field of model.searchProperties()) {
|
19
|
+
this.field(field);
|
20
|
+
}
|
21
|
+
|
22
|
+
Object.values(index).forEach(function (doc) {
|
23
|
+
this.add(doc);
|
24
|
+
}, this);
|
25
|
+
});
|
26
|
+
}
|
27
|
+
|
28
|
+
search(query) {
|
29
|
+
return this.#compiledIndex.search(query).map(doc => ({
|
30
|
+
score: doc.score,
|
31
|
+
model: this.#model.fromData(this.#index[doc.ref]),
|
32
|
+
}));
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
export class SearchIndexError extends Error {
|
37
|
+
}
|
38
|
+
|
39
|
+
export class NoIndexAvailableSearchIndexError extends SearchIndexError {
|
40
|
+
constructor(model) {
|
41
|
+
super(`The model ${model.name} has no search properties`);
|
42
|
+
}
|
43
|
+
}
|
@@ -163,9 +163,9 @@ export default class StorageEngine {
|
|
163
163
|
.map(async ([constructor, updatableModels]) => [
|
164
164
|
constructor,
|
165
165
|
await Promise.all(updatableModels.map(async m => {
|
166
|
-
const
|
167
|
-
modelCache[
|
168
|
-
return
|
166
|
+
const upToDateModel = modelCache[m.id] ?? await this.get(m.id);
|
167
|
+
modelCache[upToDateModel.id] = upToDateModel;
|
168
|
+
return upToDateModel;
|
169
169
|
})),
|
170
170
|
]),
|
171
171
|
))).flat(1)
|
@@ -193,8 +193,8 @@ export default class StorageEngine {
|
|
193
193
|
}),
|
194
194
|
);
|
195
195
|
|
196
|
-
for (const
|
197
|
-
await processModel(
|
196
|
+
for (const modelToBeProcessed of modelsToProcess) {
|
197
|
+
await processModel(modelToBeProcessed);
|
198
198
|
}
|
199
199
|
};
|
200
200
|
|