@signaldb/core 2.0.0-beta.13 → 2.0.0-beta.14
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/dist/.vite/manifest.json +72 -46
- package/dist/AsyncDataAdapter.d.ts +11 -2
- package/dist/Collection/Cursor.d.ts +11 -1
- package/dist/Collection/Observer.d.ts +31 -0
- package/dist/DataAdapter.d.ts +15 -2
- package/dist/WorkerDataAdapter.d.ts +25 -2
- package/dist/WorkerDataAdapterHost.d.ts +2 -0
- package/dist/index.cjs.js +15 -15
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +15 -15
- package/dist/index10.cjs.js +7 -19
- package/dist/index10.mjs +7 -19
- package/dist/index11.cjs.js +19 -41
- package/dist/index11.mjs +19 -41
- package/dist/index12.cjs.js +41 -18
- package/dist/index12.mjs +41 -18
- package/dist/index13.cjs.js +18 -41
- package/dist/index13.mjs +18 -41
- package/dist/index14.cjs.js +39 -80
- package/dist/index14.mjs +39 -80
- package/dist/index15.cjs.js +82 -11
- package/dist/index15.mjs +82 -11
- package/dist/index16.cjs.js +11 -132
- package/dist/index16.mjs +11 -132
- package/dist/index17.cjs.js +127 -38
- package/dist/index17.mjs +127 -38
- package/dist/index18.cjs.js +43 -11
- package/dist/index18.mjs +43 -11
- package/dist/index19.cjs.js +10 -18
- package/dist/index19.mjs +11 -19
- package/dist/index20.cjs.js +19 -33
- package/dist/index20.mjs +19 -33
- package/dist/index21.cjs.js +33 -31
- package/dist/index21.mjs +33 -31
- package/dist/index22.cjs.js +31 -21
- package/dist/index22.mjs +31 -21
- package/dist/index23.cjs.js +16 -14
- package/dist/index23.mjs +16 -14
- package/dist/index24.cjs.js +15 -338
- package/dist/index24.mjs +15 -338
- package/dist/index25.cjs.js +122 -554
- package/dist/index25.mjs +121 -554
- package/dist/index26.cjs.js +34 -7
- package/dist/index26.mjs +34 -7
- package/dist/index27.cjs.js +339 -7
- package/dist/index27.mjs +339 -7
- package/dist/index28.cjs.js +550 -83
- package/dist/index28.mjs +550 -82
- package/dist/index29.cjs.js +8 -422
- package/dist/index29.mjs +8 -422
- package/dist/index30.cjs.js +7 -68
- package/dist/index30.mjs +7 -68
- package/dist/index31.cjs.js +86 -28
- package/dist/index31.mjs +85 -28
- package/dist/index32.cjs.js +446 -278
- package/dist/index32.mjs +446 -278
- package/dist/index33.cjs.js +68 -17
- package/dist/index33.mjs +68 -17
- package/dist/index34.cjs.js +460 -304
- package/dist/index34.mjs +460 -304
- package/dist/index35.cjs.js +14 -532
- package/dist/index35.mjs +14 -532
- package/dist/index36.cjs.js +389 -0
- package/dist/index36.mjs +389 -0
- package/dist/index37.cjs.js +539 -0
- package/dist/index37.mjs +539 -0
- package/dist/index4.cjs.js +199 -140
- package/dist/index4.mjs +195 -140
- package/dist/index5.cjs.js +152 -253
- package/dist/index5.mjs +152 -253
- package/dist/index6.cjs.js +267 -89
- package/dist/index6.mjs +267 -89
- package/dist/index7.cjs.js +121 -29
- package/dist/index7.mjs +121 -29
- package/dist/index8.cjs.js +29 -8
- package/dist/index8.mjs +29 -8
- package/dist/index9.cjs.js +8 -7
- package/dist/index9.mjs +8 -7
- package/dist/utils/incrementalQueryUpdate.d.ts +60 -0
- package/dist/utils/projectItems.d.ts +12 -0
- package/dist/utils/queryDelta.d.ts +83 -0
- package/package.json +1 -1
package/dist/index26.cjs.js
CHANGED
|
@@ -1,11 +1,38 @@
|
|
|
1
|
-
//#region src/
|
|
1
|
+
//#region src/utils/queryId.ts
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* Returns true when `options` is effectively "empty" for the purpose of queryId generation.
|
|
4
|
+
* Treats `undefined`/`null` as empty, and only considers plain objects with zero own enumerable keys as empty.
|
|
5
|
+
* Arrays and non-object values are not considered empty.
|
|
6
|
+
* @param options - Query options to test.
|
|
7
|
+
* @returns `true` if `options` is `null`/`undefined` or a plain object with no own keys; otherwise `false`.
|
|
6
8
|
*/
|
|
7
|
-
function
|
|
8
|
-
return
|
|
9
|
+
function isEmptyOptions(options) {
|
|
10
|
+
if (options == null) return true;
|
|
11
|
+
if (typeof options !== "object") return false;
|
|
12
|
+
if (Array.isArray(options)) return false;
|
|
13
|
+
return Object.keys(options).length === 0;
|
|
14
|
+
}
|
|
15
|
+
var noOptions = {};
|
|
16
|
+
var cache = /* @__PURE__ */ new WeakMap();
|
|
17
|
+
/**
|
|
18
|
+
* Generates a unique identifier for a query based on its selector and options.
|
|
19
|
+
* @param selector - The selector object.
|
|
20
|
+
* @param options - The query options object (optional).
|
|
21
|
+
* @returns A unique identifier string for the query.
|
|
22
|
+
*/
|
|
23
|
+
function queryId(selector, options) {
|
|
24
|
+
if (!(selector != null && typeof selector === "object" && (options == null || typeof options === "object"))) {
|
|
25
|
+
const optionsId = isEmptyOptions(options) ? -1 : JSON.stringify(options);
|
|
26
|
+
return `${JSON.stringify(selector)}:${optionsId}`;
|
|
27
|
+
}
|
|
28
|
+
const optionsKey = options ?? noOptions;
|
|
29
|
+
const cachedForSelector = cache.get(selector);
|
|
30
|
+
const cached = cachedForSelector?.get(optionsKey);
|
|
31
|
+
if (cached != null) return cached;
|
|
32
|
+
const id = `${JSON.stringify(selector)}:${isEmptyOptions(options) ? -1 : JSON.stringify(options)}`;
|
|
33
|
+
if (cachedForSelector) cachedForSelector.set(optionsKey, id);
|
|
34
|
+
else cache.set(selector, new WeakMap([[optionsKey, id]]));
|
|
35
|
+
return id;
|
|
9
36
|
}
|
|
10
37
|
//#endregion
|
|
11
|
-
exports.default =
|
|
38
|
+
exports.default = queryId;
|
package/dist/index26.mjs
CHANGED
|
@@ -1,11 +1,38 @@
|
|
|
1
|
-
//#region src/
|
|
1
|
+
//#region src/utils/queryId.ts
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* Returns true when `options` is effectively "empty" for the purpose of queryId generation.
|
|
4
|
+
* Treats `undefined`/`null` as empty, and only considers plain objects with zero own enumerable keys as empty.
|
|
5
|
+
* Arrays and non-object values are not considered empty.
|
|
6
|
+
* @param options - Query options to test.
|
|
7
|
+
* @returns `true` if `options` is `null`/`undefined` or a plain object with no own keys; otherwise `false`.
|
|
6
8
|
*/
|
|
7
|
-
function
|
|
8
|
-
return
|
|
9
|
+
function isEmptyOptions(options) {
|
|
10
|
+
if (options == null) return true;
|
|
11
|
+
if (typeof options !== "object") return false;
|
|
12
|
+
if (Array.isArray(options)) return false;
|
|
13
|
+
return Object.keys(options).length === 0;
|
|
14
|
+
}
|
|
15
|
+
var noOptions = {};
|
|
16
|
+
var cache = /* @__PURE__ */ new WeakMap();
|
|
17
|
+
/**
|
|
18
|
+
* Generates a unique identifier for a query based on its selector and options.
|
|
19
|
+
* @param selector - The selector object.
|
|
20
|
+
* @param options - The query options object (optional).
|
|
21
|
+
* @returns A unique identifier string for the query.
|
|
22
|
+
*/
|
|
23
|
+
function queryId(selector, options) {
|
|
24
|
+
if (!(selector != null && typeof selector === "object" && (options == null || typeof options === "object"))) {
|
|
25
|
+
const optionsId = isEmptyOptions(options) ? -1 : JSON.stringify(options);
|
|
26
|
+
return `${JSON.stringify(selector)}:${optionsId}`;
|
|
27
|
+
}
|
|
28
|
+
const optionsKey = options ?? noOptions;
|
|
29
|
+
const cachedForSelector = cache.get(selector);
|
|
30
|
+
const cached = cachedForSelector?.get(optionsKey);
|
|
31
|
+
if (cached != null) return cached;
|
|
32
|
+
const id = `${JSON.stringify(selector)}:${isEmptyOptions(options) ? -1 : JSON.stringify(options)}`;
|
|
33
|
+
if (cachedForSelector) cachedForSelector.set(optionsKey, id);
|
|
34
|
+
else cache.set(selector, new WeakMap([[optionsKey, id]]));
|
|
35
|
+
return id;
|
|
9
36
|
}
|
|
10
37
|
//#endregion
|
|
11
|
-
export {
|
|
38
|
+
export { queryId as default };
|
package/dist/index27.cjs.js
CHANGED
|
@@ -1,11 +1,343 @@
|
|
|
1
|
-
|
|
1
|
+
const require_isEqual = require("./index2.cjs.js");
|
|
2
|
+
const require_queryDelta = require("./index4.cjs.js");
|
|
3
|
+
const require_EventEmitter = require("./index7.cjs.js");
|
|
4
|
+
const require_serializeValue = require("./index13.cjs.js");
|
|
5
|
+
const require_createIndex = require("./index15.cjs.js");
|
|
6
|
+
const require_getIndexInfo = require("./index17.cjs.js");
|
|
7
|
+
const require_deepClone = require("./index18.cjs.js");
|
|
8
|
+
const require_match = require("./index19.cjs.js");
|
|
9
|
+
const require_modify = require("./index20.cjs.js");
|
|
10
|
+
const require_projectItems = require("./index23.cjs.js");
|
|
11
|
+
const require_sortItems = require("./index24.cjs.js");
|
|
12
|
+
const require_incrementalQueryUpdate = require("./index25.cjs.js");
|
|
13
|
+
const require_queryId = require("./index26.cjs.js");
|
|
14
|
+
//#region src/DefaultDataAdapter.ts
|
|
2
15
|
/**
|
|
3
|
-
*
|
|
4
|
-
* @
|
|
5
|
-
* @
|
|
16
|
+
* Checks if there are any pending updates in the given changeset.
|
|
17
|
+
* @template T - The type of the items in the changeset.
|
|
18
|
+
* @param pendingUpdates - The changeset to check for pending updates.
|
|
19
|
+
* @returns `true` if there are pending updates, otherwise `false`.
|
|
6
20
|
*/
|
|
7
|
-
function
|
|
8
|
-
return
|
|
21
|
+
function hasPendingUpdates(pendingUpdates) {
|
|
22
|
+
return pendingUpdates.added.length > 0 || pendingUpdates.modified.length > 0 || pendingUpdates.removed.length > 0;
|
|
9
23
|
}
|
|
24
|
+
var DefaultDataAdapter = class {
|
|
25
|
+
items = /* @__PURE__ */ new Map();
|
|
26
|
+
options;
|
|
27
|
+
storageAdapters = /* @__PURE__ */ new Map();
|
|
28
|
+
collections = /* @__PURE__ */ new Set();
|
|
29
|
+
indices = /* @__PURE__ */ new Map();
|
|
30
|
+
activeQueries = /* @__PURE__ */ new Map();
|
|
31
|
+
queryEmitters = /* @__PURE__ */ new Map();
|
|
32
|
+
queuedQueryUpdates = /* @__PURE__ */ new Map();
|
|
33
|
+
cachedQueryResults = /* @__PURE__ */ new Map();
|
|
34
|
+
constructor(options) {
|
|
35
|
+
this.options = options || {};
|
|
36
|
+
}
|
|
37
|
+
ensureStorageAdapter(name) {
|
|
38
|
+
if (this.storageAdapters.get(name)) return;
|
|
39
|
+
if (!this.options.storage) return;
|
|
40
|
+
const adapter = this.options.storage(name);
|
|
41
|
+
if (!adapter) return;
|
|
42
|
+
this.storageAdapters.set(name, adapter);
|
|
43
|
+
}
|
|
44
|
+
rebuildIndices(collection) {
|
|
45
|
+
const items = this.items.get(collection.name);
|
|
46
|
+
if (!items) throw new Error(`Items not found for collection ${collection.name}`);
|
|
47
|
+
const indices = this.indices.get(collection.name);
|
|
48
|
+
if (!indices) throw new Error(`Indices not found for collection ${collection.name}`);
|
|
49
|
+
indices.forEach((index) => index.rebuild([...items.values()]));
|
|
50
|
+
}
|
|
51
|
+
async setupStorageAdapter(collection) {
|
|
52
|
+
const storageAdapter = this.storageAdapters.get(collection.name);
|
|
53
|
+
if (!storageAdapter) return;
|
|
54
|
+
return storageAdapter.setup().then(async () => {
|
|
55
|
+
await this.fetchItemsFromStorage(collection);
|
|
56
|
+
}).catch((error) => {
|
|
57
|
+
if (!this.options.onError) {
|
|
58
|
+
console.error(`Error during data persistence operation in collection ${collection.name}`, error);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
this.options.onError(collection.name, error instanceof Error ? error : new Error(error));
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
getIndexInfo(collection, selector) {
|
|
65
|
+
if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") return {
|
|
66
|
+
matched: true,
|
|
67
|
+
ids: [require_serializeValue.default(selector.id)],
|
|
68
|
+
optimizedSelector: {}
|
|
69
|
+
};
|
|
70
|
+
if (selector == null) return {
|
|
71
|
+
matched: false,
|
|
72
|
+
ids: [],
|
|
73
|
+
optimizedSelector: {}
|
|
74
|
+
};
|
|
75
|
+
return require_getIndexInfo.default((this.indices.get(collection.name) ?? []).map((i) => i.query.bind(i)), selector);
|
|
76
|
+
}
|
|
77
|
+
applyIndexDeltas(collection, changes) {
|
|
78
|
+
const indices = this.indices.get(collection.name) ?? [];
|
|
79
|
+
for (const index of indices) {
|
|
80
|
+
if (changes.modified) index.update(changes.modified);
|
|
81
|
+
if (changes.added && changes.added.length > 0) index.insert(changes.added);
|
|
82
|
+
if (changes.removed && changes.removed.length > 0) index.remove(changes.removed);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
getItem(collection, selector) {
|
|
86
|
+
const memory = this.items.get(collection.name) ?? /* @__PURE__ */ new Map();
|
|
87
|
+
const indexInfo = this.getIndexInfo(collection, selector);
|
|
88
|
+
return (indexInfo.matched ? indexInfo.ids.map((id) => memory.get(require_serializeValue.default(id))).filter((i) => i != null) : [...memory.values()]).find((document) => require_match.default(document, selector));
|
|
89
|
+
}
|
|
90
|
+
queryItems(collection, selector) {
|
|
91
|
+
const indexInfo = this.getIndexInfo(collection, selector);
|
|
92
|
+
const matchItems = (item) => {
|
|
93
|
+
if (indexInfo.optimizedSelector == null) return true;
|
|
94
|
+
if (Object.keys(indexInfo.optimizedSelector).length <= 0) return true;
|
|
95
|
+
return require_match.default(item, indexInfo.optimizedSelector);
|
|
96
|
+
};
|
|
97
|
+
const items = this.items.get(collection.name);
|
|
98
|
+
if (!indexInfo.matched) {
|
|
99
|
+
if (require_isEqual.default(selector, {})) return [...items.values()];
|
|
100
|
+
return [...items.values()].filter(matchItems);
|
|
101
|
+
}
|
|
102
|
+
const foundItems = indexInfo.ids.map((ids) => items.get(require_serializeValue.default(ids))).filter((i) => i != null);
|
|
103
|
+
if (require_isEqual.default(indexInfo.optimizedSelector, {})) return foundItems;
|
|
104
|
+
return foundItems.filter(matchItems);
|
|
105
|
+
}
|
|
106
|
+
executeQuery(collection, selector, options) {
|
|
107
|
+
const items = this.queryItems(collection, selector || {});
|
|
108
|
+
const { sort, skip, limit, fields } = options || {};
|
|
109
|
+
const sorted = sort ? require_sortItems.default(items, sort) : items;
|
|
110
|
+
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
111
|
+
return require_projectItems.default(limit ? skipped.slice(0, limit) : skipped, fields);
|
|
112
|
+
}
|
|
113
|
+
flushQueuedQueryUpdates(collection) {
|
|
114
|
+
if (!this.queuedQueryUpdates.get(collection.name)) return;
|
|
115
|
+
const changes = this.queuedQueryUpdates.get(collection.name);
|
|
116
|
+
if (!changes || !hasPendingUpdates(changes)) return;
|
|
117
|
+
this.queuedQueryUpdates.set(collection.name, {
|
|
118
|
+
added: [],
|
|
119
|
+
modified: [],
|
|
120
|
+
removed: []
|
|
121
|
+
});
|
|
122
|
+
const changeset = {
|
|
123
|
+
upserts: [...changes.added, ...changes.modified],
|
|
124
|
+
deletes: changes.removed.map((item) => item.id)
|
|
125
|
+
};
|
|
126
|
+
const flatItems = [
|
|
127
|
+
...changes.added,
|
|
128
|
+
...changes.modified,
|
|
129
|
+
...changes.removed
|
|
130
|
+
];
|
|
131
|
+
const itemIds = new Set(flatItems.map((i) => i.id));
|
|
132
|
+
[...this.activeQueries.get(collection.name)?.values() ?? []].filter(({ selector, options }) => {
|
|
133
|
+
if ((this.cachedQueryResults.get(collection.name)?.get(require_queryId.default(selector, options))?.map((i) => i.id) ?? []).some((id) => itemIds.has(id))) return true;
|
|
134
|
+
return flatItems.some((item) => require_match.default(item, selector));
|
|
135
|
+
}).forEach(({ selector, options }) => {
|
|
136
|
+
this.executeAndCacheQuery(collection, selector, options, changeset);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
executeAndCacheQuery(collection, selector, options, changes) {
|
|
140
|
+
const cached = this.cachedQueryResults.get(collection.name)?.get(require_queryId.default(selector, options));
|
|
141
|
+
const result = (changes && cached ? require_incrementalQueryUpdate.default(cached, selector, options, changes) : null) ?? this.executeQuery(collection, selector, options);
|
|
142
|
+
const delta = cached ? require_queryDelta.diffQueryResults(cached, result) : void 0;
|
|
143
|
+
this.cachedQueryResults.set(collection.name, this.cachedQueryResults.get(collection.name) || /* @__PURE__ */ new Map());
|
|
144
|
+
this.cachedQueryResults.get(collection.name)?.set(require_queryId.default(selector, options), result);
|
|
145
|
+
const emitter = this.queryEmitters.get(collection.name);
|
|
146
|
+
if (!emitter) return;
|
|
147
|
+
if (delta && require_queryDelta.isEmptyQueryDelta(delta)) return;
|
|
148
|
+
emitter.emit("change", selector, options, "complete", delta);
|
|
149
|
+
}
|
|
150
|
+
updateQueries(collection, changes) {
|
|
151
|
+
this.queuedQueryUpdates.set(collection.name, this.queuedQueryUpdates.get(collection.name) || {
|
|
152
|
+
added: [],
|
|
153
|
+
modified: [],
|
|
154
|
+
removed: []
|
|
155
|
+
});
|
|
156
|
+
this.queuedQueryUpdates.get(collection.name)?.added.push(...changes.added);
|
|
157
|
+
this.queuedQueryUpdates.get(collection.name)?.modified.push(...changes.modified);
|
|
158
|
+
this.queuedQueryUpdates.get(collection.name)?.removed.push(...changes.removed);
|
|
159
|
+
this.flushQueuedQueryUpdates(collection);
|
|
160
|
+
}
|
|
161
|
+
createCollectionBackend(collection, indices) {
|
|
162
|
+
this.collections.add(collection);
|
|
163
|
+
this.ensureStorageAdapter(collection.name);
|
|
164
|
+
this.items.set(collection.name, this.items.get(collection.name) ?? /* @__PURE__ */ new Map());
|
|
165
|
+
this.queryEmitters.set(collection.name, this.queryEmitters.get(collection.name) ?? new require_EventEmitter.default());
|
|
166
|
+
this.queryEmitters.get(collection.name)?.setMaxListeners(Infinity);
|
|
167
|
+
this.activeQueries.set(collection.name, this.activeQueries.get(collection.name) || /* @__PURE__ */ new Map());
|
|
168
|
+
this.indices.set(collection.name, indices.map((field) => require_createIndex.default(field)));
|
|
169
|
+
this.rebuildIndices(collection);
|
|
170
|
+
const persistenceReadyPromise = this.setupStorageAdapter(collection);
|
|
171
|
+
const backend = {
|
|
172
|
+
insert: async (newItem) => {
|
|
173
|
+
const items = this.items.get(collection.name);
|
|
174
|
+
if (!items) throw new Error(`Items not found for collection ${collection.name}`);
|
|
175
|
+
if (items.has(require_serializeValue.default(newItem.id))) throw new Error(`Item with id '${newItem.id}' already exists`);
|
|
176
|
+
this.items.get(collection.name)?.set(require_serializeValue.default(newItem.id), newItem);
|
|
177
|
+
await this.storageAdapters.get(collection.name)?.insert([newItem]);
|
|
178
|
+
this.applyIndexDeltas(collection, { added: [newItem] });
|
|
179
|
+
this.updateQueries(collection, {
|
|
180
|
+
added: [],
|
|
181
|
+
modified: [newItem],
|
|
182
|
+
removed: []
|
|
183
|
+
});
|
|
184
|
+
return newItem;
|
|
185
|
+
},
|
|
186
|
+
updateOne: async (selector, modifier) => {
|
|
187
|
+
const { $setOnInsert, ...restModifier } = modifier;
|
|
188
|
+
const item = this.getItem(collection, selector);
|
|
189
|
+
if (item == null) return [];
|
|
190
|
+
const modifiedItem = require_modify.default(require_deepClone.default(item), restModifier);
|
|
191
|
+
if (item.id !== modifiedItem.id && this.getItem(collection, { id: modifiedItem.id }) != null) throw new Error(`Item with id '${modifiedItem.id}' already exists`);
|
|
192
|
+
this.items.get(collection.name)?.set(require_serializeValue.default(modifiedItem.id), modifiedItem);
|
|
193
|
+
await this.storageAdapters.get(collection.name)?.replace([modifiedItem]);
|
|
194
|
+
this.applyIndexDeltas(collection, { modified: [{
|
|
195
|
+
oldItem: item,
|
|
196
|
+
newItem: modifiedItem
|
|
197
|
+
}] });
|
|
198
|
+
this.updateQueries(collection, {
|
|
199
|
+
added: [],
|
|
200
|
+
modified: [modifiedItem],
|
|
201
|
+
removed: []
|
|
202
|
+
});
|
|
203
|
+
return [modifiedItem];
|
|
204
|
+
},
|
|
205
|
+
updateMany: async (selector, modifier) => {
|
|
206
|
+
const { $setOnInsert, ...restModifier } = modifier;
|
|
207
|
+
const items = this.executeQuery(collection, selector, {});
|
|
208
|
+
const changedItems = items.map((item) => {
|
|
209
|
+
const modifiedItem = require_modify.default(require_deepClone.default(item), restModifier);
|
|
210
|
+
if (item.id !== modifiedItem.id && this.getItem(collection, { id: modifiedItem.id }) != null) throw new Error(`Item with id '${modifiedItem.id}' already exists`);
|
|
211
|
+
return modifiedItem;
|
|
212
|
+
});
|
|
213
|
+
changedItems.forEach((item) => {
|
|
214
|
+
this.items.get(collection.name)?.set(require_serializeValue.default(item.id), item);
|
|
215
|
+
});
|
|
216
|
+
await this.storageAdapters.get(collection.name)?.replace(changedItems);
|
|
217
|
+
const pairs = items.map((oldItem, index) => ({
|
|
218
|
+
oldItem,
|
|
219
|
+
newItem: changedItems[index]
|
|
220
|
+
}));
|
|
221
|
+
this.applyIndexDeltas(collection, { modified: pairs });
|
|
222
|
+
this.updateQueries(collection, {
|
|
223
|
+
added: [],
|
|
224
|
+
modified: changedItems,
|
|
225
|
+
removed: []
|
|
226
|
+
});
|
|
227
|
+
return changedItems;
|
|
228
|
+
},
|
|
229
|
+
replaceOne: async (selector, replacement) => {
|
|
230
|
+
const item = this.getItem(collection, selector);
|
|
231
|
+
if (item == null) return [];
|
|
232
|
+
if (item.id !== replacement.id && replacement.id != null && this.getItem(collection, { id: replacement.id }) != null) throw new Error(`Item with id '${replacement.id}' already exists`);
|
|
233
|
+
const modifiedItem = {
|
|
234
|
+
id: item.id,
|
|
235
|
+
...replacement
|
|
236
|
+
};
|
|
237
|
+
this.items.get(collection.name)?.set(require_serializeValue.default(modifiedItem.id), modifiedItem);
|
|
238
|
+
await this.storageAdapters.get(collection.name)?.replace([modifiedItem]);
|
|
239
|
+
this.applyIndexDeltas(collection, { modified: [{
|
|
240
|
+
oldItem: item,
|
|
241
|
+
newItem: modifiedItem
|
|
242
|
+
}] });
|
|
243
|
+
this.updateQueries(collection, {
|
|
244
|
+
added: [],
|
|
245
|
+
modified: [modifiedItem],
|
|
246
|
+
removed: []
|
|
247
|
+
});
|
|
248
|
+
return [modifiedItem];
|
|
249
|
+
},
|
|
250
|
+
removeOne: async (selector) => {
|
|
251
|
+
const item = this.getItem(collection, selector);
|
|
252
|
+
if (item == null) return [];
|
|
253
|
+
this.items.get(collection.name)?.delete(require_serializeValue.default(item.id));
|
|
254
|
+
await this.storageAdapters.get(collection.name)?.remove([item]);
|
|
255
|
+
this.applyIndexDeltas(collection, { removed: [item] });
|
|
256
|
+
this.updateQueries(collection, {
|
|
257
|
+
added: [],
|
|
258
|
+
modified: [],
|
|
259
|
+
removed: [item]
|
|
260
|
+
});
|
|
261
|
+
return [item];
|
|
262
|
+
},
|
|
263
|
+
removeMany: async (selector) => {
|
|
264
|
+
const items = backend.getQueryResult(selector, {});
|
|
265
|
+
items.forEach((item) => {
|
|
266
|
+
this.items.get(collection.name)?.delete(require_serializeValue.default(item.id));
|
|
267
|
+
});
|
|
268
|
+
await this.storageAdapters.get(collection.name)?.remove(items);
|
|
269
|
+
this.applyIndexDeltas(collection, { removed: items });
|
|
270
|
+
this.updateQueries(collection, {
|
|
271
|
+
added: [],
|
|
272
|
+
modified: [],
|
|
273
|
+
removed: items
|
|
274
|
+
});
|
|
275
|
+
return items;
|
|
276
|
+
},
|
|
277
|
+
registerQuery: (selector, options) => {
|
|
278
|
+
this.activeQueries.set(collection.name, this.activeQueries.get(collection.name) || /* @__PURE__ */ new Map());
|
|
279
|
+
this.activeQueries.get(collection.name)?.set(require_queryId.default(selector, options), {
|
|
280
|
+
selector,
|
|
281
|
+
options
|
|
282
|
+
});
|
|
283
|
+
this.executeAndCacheQuery(collection, selector, options);
|
|
284
|
+
},
|
|
285
|
+
unregisterQuery: (selector, options) => {
|
|
286
|
+
if (!this.activeQueries.get(collection.name)) return;
|
|
287
|
+
this.activeQueries.get(collection.name)?.delete(require_queryId.default(selector, options));
|
|
288
|
+
},
|
|
289
|
+
getQueryState: () => "complete",
|
|
290
|
+
onQueryStateChange: (selector, options, callback) => {
|
|
291
|
+
const emitter = this.queryEmitters.get(collection.name);
|
|
292
|
+
if (!emitter) throw new Error(`Query emitter not found for collection ${collection.name}`);
|
|
293
|
+
const handler = (querySelector, queryOptions, state, delta) => {
|
|
294
|
+
if (require_queryId.default(querySelector, queryOptions) !== require_queryId.default(selector, options)) return;
|
|
295
|
+
require_queryDelta.callWithDelta(callback, state, delta);
|
|
296
|
+
};
|
|
297
|
+
emitter.on("change", handler);
|
|
298
|
+
return () => {
|
|
299
|
+
emitter.off("change", handler);
|
|
300
|
+
};
|
|
301
|
+
},
|
|
302
|
+
getQueryError: () => null,
|
|
303
|
+
getQueryResult: (selector, options) => {
|
|
304
|
+
if (this.activeQueries.get(collection.name)?.has(require_queryId.default(selector, options))) {
|
|
305
|
+
const results = this.cachedQueryResults.get(collection.name)?.get(require_queryId.default(selector, options));
|
|
306
|
+
if (!results) throw new Error("Cached query results are not defined!");
|
|
307
|
+
return results;
|
|
308
|
+
}
|
|
309
|
+
return this.executeQuery(collection, selector, options);
|
|
310
|
+
},
|
|
311
|
+
executeQuery: (selector, options) => Promise.resolve(this.executeQuery(collection, selector, options)),
|
|
312
|
+
dispose: async () => {
|
|
313
|
+
const adapter = this.storageAdapters.get(collection.name);
|
|
314
|
+
if (adapter?.teardown) await adapter.teardown();
|
|
315
|
+
this.storageAdapters.delete(collection.name);
|
|
316
|
+
this.items.delete(collection.name);
|
|
317
|
+
this.indices.delete(collection.name);
|
|
318
|
+
this.activeQueries.delete(collection.name);
|
|
319
|
+
this.queryEmitters.delete(collection.name);
|
|
320
|
+
this.queuedQueryUpdates.delete(collection.name);
|
|
321
|
+
this.cachedQueryResults.delete(collection.name);
|
|
322
|
+
},
|
|
323
|
+
isReady: () => persistenceReadyPromise
|
|
324
|
+
};
|
|
325
|
+
return backend;
|
|
326
|
+
}
|
|
327
|
+
async fetchItemsFromStorage(collection) {
|
|
328
|
+
if (!collection) {
|
|
329
|
+
await Promise.all([...this.collections].map((currentCollection) => this.fetchItemsFromStorage(currentCollection)));
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const storageAdapter = this.storageAdapters.get(collection.name);
|
|
333
|
+
if (!storageAdapter) return;
|
|
334
|
+
const items = await storageAdapter.readAll();
|
|
335
|
+
this.items.set(collection.name, items.reduce((map, item) => {
|
|
336
|
+
map.set(require_serializeValue.default(item.id), item);
|
|
337
|
+
return map;
|
|
338
|
+
}, /* @__PURE__ */ new Map()));
|
|
339
|
+
this.rebuildIndices(collection);
|
|
340
|
+
}
|
|
341
|
+
};
|
|
10
342
|
//#endregion
|
|
11
|
-
exports.default =
|
|
343
|
+
exports.default = DefaultDataAdapter;
|