@roc-db/indexed-db 0.2.0-pre.51 → 0.2.0-pre.53
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/index.js +92 -6
- package/dist/types/functions/commitCreate.d.ts +1 -1
- package/dist/types/functions/index.d.ts +2 -0
- package/dist/types/functions/pageEntitiesByIndex.d.ts +2 -0
- package/dist/types/functions/refByUniqueField.d.ts +1 -0
- package/dist/types/lib/documentFromDBRow.d.ts +11 -0
- package/dist/types/lib/documentToDBRow.d.ts +1 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -16,9 +16,11 @@ import { createAdapter, Snowflake } from "roc-db";
|
|
|
16
16
|
var exports_functions = {};
|
|
17
17
|
__export(exports_functions, {
|
|
18
18
|
saveMutation: () => saveMutation,
|
|
19
|
+
refByUniqueField: () => refByUniqueField,
|
|
19
20
|
readMutation: () => readMutation,
|
|
20
21
|
readEntity: () => readEntity,
|
|
21
22
|
pageMutations: () => pageMutations,
|
|
23
|
+
pageEntitiesByIndex: () => pageEntitiesByIndex,
|
|
22
24
|
getChangeSetMutations: () => getChangeSetMutations,
|
|
23
25
|
findDebounceMutation: () => findDebounceMutation,
|
|
24
26
|
end: () => end,
|
|
@@ -27,14 +29,33 @@ __export(exports_functions, {
|
|
|
27
29
|
batchReadEntities: () => batchReadEntities
|
|
28
30
|
});
|
|
29
31
|
|
|
32
|
+
// src/lib/documentFromDBRow.ts
|
|
33
|
+
var documentFromDBRow = ({
|
|
34
|
+
unique_constraint_0,
|
|
35
|
+
unique_constraint_1,
|
|
36
|
+
index_entries,
|
|
37
|
+
...document
|
|
38
|
+
}) => {
|
|
39
|
+
return {
|
|
40
|
+
...document,
|
|
41
|
+
__: {
|
|
42
|
+
uniqueExisting: [],
|
|
43
|
+
indexExisting: []
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
30
48
|
// src/functions/readEntity.ts
|
|
31
49
|
var readEntity = async (txn, ref) => {
|
|
32
50
|
const objectStore = txn.engineOpts.txn.objectStore("entities");
|
|
33
51
|
const request = objectStore.get(ref);
|
|
34
52
|
return new Promise((resolve, reject) => {
|
|
35
53
|
request.onsuccess = (event) => {
|
|
36
|
-
const
|
|
37
|
-
|
|
54
|
+
const result = event.target.result;
|
|
55
|
+
if (!result) {
|
|
56
|
+
return resolve(undefined);
|
|
57
|
+
}
|
|
58
|
+
resolve(documentFromDBRow(result));
|
|
38
59
|
};
|
|
39
60
|
request.onerror = (event) => {
|
|
40
61
|
reject(event.target.error);
|
|
@@ -54,6 +75,11 @@ var begin = async (engineOpts, callback) => {
|
|
|
54
75
|
idbRequest.onupgradeneeded = (event) => {
|
|
55
76
|
const entitiesObjectStore = idbRequest.result.createObjectStore("entities", { keyPath: "ref" });
|
|
56
77
|
const mutationsObjectStore = idbRequest.result.createObjectStore("mutations", { keyPath: "ref" });
|
|
78
|
+
entitiesObjectStore.createIndex("unique_constraint_0", ["entity", "unique_constraint_0"], { unique: true });
|
|
79
|
+
entitiesObjectStore.createIndex("unique_constraint_1", ["entity", "unique_constraint_1"], { unique: true });
|
|
80
|
+
entitiesObjectStore.createIndex("byIndexEntry", "index_entries", {
|
|
81
|
+
multiEntry: true
|
|
82
|
+
});
|
|
57
83
|
mutationsObjectStore.createIndex("timestamp", "timestamp", {
|
|
58
84
|
unique: false
|
|
59
85
|
});
|
|
@@ -72,6 +98,9 @@ var begin = async (engineOpts, callback) => {
|
|
|
72
98
|
});
|
|
73
99
|
};
|
|
74
100
|
// src/functions/commitCreate.ts
|
|
101
|
+
import {
|
|
102
|
+
createUniqueConstraintConflictError
|
|
103
|
+
} from "roc-db";
|
|
75
104
|
var commitCreate = (txn, document) => {
|
|
76
105
|
const objectStore = txn.engineOpts.txn.objectStore("entities");
|
|
77
106
|
const request = objectStore.add(document);
|
|
@@ -80,8 +109,12 @@ var commitCreate = (txn, document) => {
|
|
|
80
109
|
resolve(document);
|
|
81
110
|
};
|
|
82
111
|
request.onerror = (event) => {
|
|
83
|
-
|
|
84
|
-
|
|
112
|
+
if (event?.target?.error?.name === "ConstraintError") {
|
|
113
|
+
reject(createUniqueConstraintConflictError(document.entity));
|
|
114
|
+
} else {
|
|
115
|
+
console.error("Error creating entity:", document, event?.target?.error);
|
|
116
|
+
reject(event?.target?.error);
|
|
117
|
+
}
|
|
85
118
|
};
|
|
86
119
|
});
|
|
87
120
|
};
|
|
@@ -143,6 +176,21 @@ var saveMutation = async (txn, finalizedMutation) => {
|
|
|
143
176
|
});
|
|
144
177
|
};
|
|
145
178
|
|
|
179
|
+
// src/lib/documentToDBRow.ts
|
|
180
|
+
var stringifyIndexEntry = (entity, [k, v]) => `${entity}:${k}:${JSON.stringify(v)}`;
|
|
181
|
+
var stringifyUniqueEntry = ([k, v]) => `${k}:${JSON.stringify(v)}`;
|
|
182
|
+
var stringifyIndexEntries = (entity, arr) => {
|
|
183
|
+
return arr.map((pair) => stringifyIndexEntry(entity, pair));
|
|
184
|
+
};
|
|
185
|
+
var documentToDBRow = (document) => {
|
|
186
|
+
return {
|
|
187
|
+
...document,
|
|
188
|
+
index_entries: document.__.index ? stringifyIndexEntries(document.entity, document.__.index) : [],
|
|
189
|
+
unique_constraint_0: document.__?.unique?.[0] ? stringifyUniqueEntry(document.__?.unique?.[0]) : null,
|
|
190
|
+
unique_constraint_1: document.__?.unique?.[1] ? stringifyUniqueEntry(document.__?.unique?.[1]) : null
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
|
|
146
194
|
// src/functions/commit.ts
|
|
147
195
|
var commit = async (txn, mutation, { created, updated, deleted }) => {
|
|
148
196
|
const currentMutation = await txn.readMutation(txn.mutation.ref, false);
|
|
@@ -160,10 +208,10 @@ var commit = async (txn, mutation, { created, updated, deleted }) => {
|
|
|
160
208
|
if (txn.request.changeSetRef)
|
|
161
209
|
return mutation;
|
|
162
210
|
for (const doc of created) {
|
|
163
|
-
await commitCreate(txn, doc);
|
|
211
|
+
await commitCreate(txn, documentToDBRow(doc));
|
|
164
212
|
}
|
|
165
213
|
for (const doc of updated) {
|
|
166
|
-
await commitUpdate(txn, doc);
|
|
214
|
+
await commitUpdate(txn, documentToDBRow(doc));
|
|
167
215
|
}
|
|
168
216
|
for (const ref of deleted) {
|
|
169
217
|
await commitDelete(txn, ref);
|
|
@@ -230,6 +278,26 @@ var findDebounceMutation = async (request, engine, now, mutationName) => {
|
|
|
230
278
|
};
|
|
231
279
|
});
|
|
232
280
|
};
|
|
281
|
+
// src/functions/pageEntitiesByIndex.ts
|
|
282
|
+
var pageEntitiesByIndex = async (txn, entity, field, value) => {
|
|
283
|
+
const objectStore = txn.engineOpts.txn.objectStore("entities");
|
|
284
|
+
const index = objectStore.index("byIndexEntry");
|
|
285
|
+
const entry = `${entity}:${field}:${JSON.stringify(value)}`;
|
|
286
|
+
const range = IDBKeyRange.only(entry);
|
|
287
|
+
const idbRequest = index.openCursor(range);
|
|
288
|
+
return new Promise((resolve, reject) => {
|
|
289
|
+
const results = [];
|
|
290
|
+
idbRequest.onsuccess = () => {
|
|
291
|
+
const cursor = idbRequest.result;
|
|
292
|
+
if (cursor) {
|
|
293
|
+
results.push(cursor.value);
|
|
294
|
+
cursor.continue();
|
|
295
|
+
} else {
|
|
296
|
+
resolve(results);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
});
|
|
300
|
+
};
|
|
233
301
|
// src/functions/pageMutations.ts
|
|
234
302
|
var pageMutations = async (txn, args) => {
|
|
235
303
|
const { size, changeSetRef, skip } = args;
|
|
@@ -269,6 +337,24 @@ var readMutation = async (engineOpts, ref) => {
|
|
|
269
337
|
};
|
|
270
338
|
});
|
|
271
339
|
};
|
|
340
|
+
// src/functions/refByUniqueField.ts
|
|
341
|
+
var refByUniqueField = async (txn, entity, field, fieldIndex, value) => {
|
|
342
|
+
const objectStore = txn.engineOpts.txn.objectStore("entities");
|
|
343
|
+
if (fieldIndex > 1)
|
|
344
|
+
throw new Error("Field index must be 0 or 1");
|
|
345
|
+
const index = objectStore.index(`unique_constraint_${fieldIndex}`);
|
|
346
|
+
const range = IDBKeyRange.only([
|
|
347
|
+
entity,
|
|
348
|
+
`${field}:${JSON.stringify(value)}`
|
|
349
|
+
]);
|
|
350
|
+
const idbRequest = index.openCursor(range);
|
|
351
|
+
return new Promise((resolve, reject) => {
|
|
352
|
+
idbRequest.onsuccess = () => {
|
|
353
|
+
const cursor = idbRequest.result;
|
|
354
|
+
resolve(cursor?.value?.ref);
|
|
355
|
+
};
|
|
356
|
+
});
|
|
357
|
+
};
|
|
272
358
|
// src/createIndexedDBAdapter.ts
|
|
273
359
|
var createIndexedDBAdapter = ({
|
|
274
360
|
operations,
|
|
@@ -4,7 +4,9 @@ export { commit } from "./commit";
|
|
|
4
4
|
export { end } from "./end";
|
|
5
5
|
export { getChangeSetMutations } from "./getChangeSetMutations";
|
|
6
6
|
export { findDebounceMutation } from "./findDebounceMutation";
|
|
7
|
+
export { pageEntitiesByIndex } from "./pageEntitiesByIndex";
|
|
7
8
|
export { pageMutations } from "./pageMutations";
|
|
8
9
|
export { readEntity } from "./readEntity";
|
|
9
10
|
export { readMutation } from "./readMutation";
|
|
11
|
+
export { refByUniqueField } from "./refByUniqueField";
|
|
10
12
|
export { saveMutation } from "./saveMutation";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const refByUniqueField: (txn: any, entity: any, field: any, fieldIndex: any, value: any) => Promise<unknown>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const documentFromDBRow: ({ unique_constraint_0, unique_constraint_1, index_entries, ...document }: {
|
|
2
|
+
[x: string]: any;
|
|
3
|
+
unique_constraint_0: any;
|
|
4
|
+
unique_constraint_1: any;
|
|
5
|
+
index_entries: any;
|
|
6
|
+
}) => {
|
|
7
|
+
__: {
|
|
8
|
+
uniqueExisting: never[];
|
|
9
|
+
indexExisting: never[];
|
|
10
|
+
};
|
|
11
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const documentToDBRow: (document: any) => any;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@roc-db/indexed-db",
|
|
3
|
-
"version": "0.2.0-pre.
|
|
3
|
+
"version": "0.2.0-pre.53",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"sideEffects": false,
|
|
15
15
|
"dependencies": {},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"roc-db": "0.2.0-pre.
|
|
17
|
+
"roc-db": "0.2.0-pre.53",
|
|
18
18
|
"zod": "^3.0.0"
|
|
19
19
|
},
|
|
20
20
|
"keywords": [],
|