@roc-db/indexed-db 0.2.0-pre.6 → 0.2.0-pre.60
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 +250 -171
- package/dist/types/createIndexedDBAdapter.d.ts +8 -0
- package/dist/types/functions/batchReadEntities.d.ts +2 -0
- package/dist/types/functions/begin.d.ts +1 -0
- package/dist/types/functions/commit.d.ts +5 -0
- package/dist/types/functions/commitCreate.d.ts +3 -0
- package/dist/types/functions/commitDelete.d.ts +2 -0
- package/dist/types/functions/commitUpdate.d.ts +1 -0
- package/dist/types/functions/end.d.ts +5 -0
- package/dist/types/functions/findDebounceMutation.d.ts +3 -0
- package/dist/types/functions/getChangeSetMutations.d.ts +3 -0
- package/dist/types/functions/index.d.ts +13 -0
- package/dist/types/functions/pageEntities.d.ts +2 -0
- package/dist/types/functions/pageEntitiesByIndex.d.ts +2 -0
- package/dist/types/functions/pageMutations.d.ts +2 -0
- package/dist/types/functions/readEntity.d.ts +1 -0
- package/dist/types/functions/readMutation.d.ts +1 -0
- package/dist/types/functions/refByUniqueField.d.ts +1 -0
- package/dist/types/functions/saveMutation.d.ts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/lib/documentFromDBRow.d.ts +11 -0
- package/dist/types/lib/documentToDBRow.d.ts +1 -0
- package/dist/types/types/IndexedDBEngine.d.ts +5 -0
- package/dist/types/types/IndexedDBReadTransaction.d.ts +4 -0
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -15,27 +15,72 @@ import { createAdapter, Snowflake } from "roc-db";
|
|
|
15
15
|
// src/functions/index.ts
|
|
16
16
|
var exports_functions = {};
|
|
17
17
|
__export(exports_functions, {
|
|
18
|
-
updateEntity: () => updateEntity,
|
|
19
18
|
saveMutation: () => saveMutation,
|
|
19
|
+
refByUniqueField: () => refByUniqueField,
|
|
20
20
|
readMutation: () => readMutation,
|
|
21
21
|
readEntity: () => readEntity,
|
|
22
|
-
|
|
22
|
+
pageMutations: () => pageMutations,
|
|
23
|
+
pageEntitiesByIndex: () => pageEntitiesByIndex,
|
|
24
|
+
pageEntities: () => pageEntities,
|
|
23
25
|
getChangeSetMutations: () => getChangeSetMutations,
|
|
24
26
|
findDebounceMutation: () => findDebounceMutation,
|
|
25
27
|
end: () => end,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
commit: () => commit,
|
|
29
|
+
begin: () => begin,
|
|
30
|
+
batchReadEntities: () => batchReadEntities
|
|
29
31
|
});
|
|
30
32
|
|
|
33
|
+
// src/lib/documentFromDBRow.ts
|
|
34
|
+
var documentFromDBRow = ({
|
|
35
|
+
unique_constraint_0,
|
|
36
|
+
unique_constraint_1,
|
|
37
|
+
index_entries,
|
|
38
|
+
...document
|
|
39
|
+
}) => {
|
|
40
|
+
return {
|
|
41
|
+
...document,
|
|
42
|
+
__: {
|
|
43
|
+
uniqueExisting: [],
|
|
44
|
+
indexExisting: []
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// src/functions/readEntity.ts
|
|
50
|
+
var readEntity = async (txn, ref) => {
|
|
51
|
+
const objectStore = txn.engineOpts.txn.objectStore("entities");
|
|
52
|
+
const request = objectStore.get(ref);
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
request.onsuccess = (event) => {
|
|
55
|
+
const result = event.target.result;
|
|
56
|
+
if (!result) {
|
|
57
|
+
return resolve(undefined);
|
|
58
|
+
}
|
|
59
|
+
resolve(documentFromDBRow(result));
|
|
60
|
+
};
|
|
61
|
+
request.onerror = (event) => {
|
|
62
|
+
reject(event.target.error);
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// src/functions/batchReadEntities.ts
|
|
68
|
+
var batchReadEntities = (txn, refs) => {
|
|
69
|
+
return Promise.all(refs.map((ref) => readEntity(txn, ref)));
|
|
70
|
+
};
|
|
31
71
|
// src/functions/begin.ts
|
|
32
|
-
var begin = async (
|
|
72
|
+
var begin = async (engineOpts, callback) => {
|
|
33
73
|
return new Promise((resolve, reject) => {
|
|
34
74
|
let db;
|
|
35
75
|
const idbRequest = indexedDB.open(engineOpts.dbName, 1);
|
|
36
76
|
idbRequest.onupgradeneeded = (event) => {
|
|
37
77
|
const entitiesObjectStore = idbRequest.result.createObjectStore("entities", { keyPath: "ref" });
|
|
38
78
|
const mutationsObjectStore = idbRequest.result.createObjectStore("mutations", { keyPath: "ref" });
|
|
79
|
+
entitiesObjectStore.createIndex("unique_constraint_0", ["entity", "unique_constraint_0"], { unique: true });
|
|
80
|
+
entitiesObjectStore.createIndex("unique_constraint_1", ["entity", "unique_constraint_1"], { unique: true });
|
|
81
|
+
entitiesObjectStore.createIndex("byIndexEntry", "index_entries", {
|
|
82
|
+
multiEntry: true
|
|
83
|
+
});
|
|
39
84
|
mutationsObjectStore.createIndex("timestamp", "timestamp", {
|
|
40
85
|
unique: false
|
|
41
86
|
});
|
|
@@ -45,96 +90,142 @@ var begin = async (request, engineOpts) => {
|
|
|
45
90
|
};
|
|
46
91
|
idbRequest.onsuccess = (event) => {
|
|
47
92
|
db = idbRequest.result;
|
|
48
|
-
resolve({
|
|
93
|
+
resolve(callback({
|
|
49
94
|
...engineOpts,
|
|
50
95
|
db,
|
|
51
|
-
txn: db.transaction(["entities", "mutations"],
|
|
52
|
-
|
|
53
|
-
entities: new Map([]),
|
|
54
|
-
mutations: new Map([])
|
|
55
|
-
} : null
|
|
56
|
-
});
|
|
96
|
+
txn: db.transaction(["entities", "mutations"], "readwrite")
|
|
97
|
+
}));
|
|
57
98
|
};
|
|
58
99
|
});
|
|
59
100
|
};
|
|
60
|
-
// src/functions/
|
|
61
|
-
import {
|
|
62
|
-
|
|
101
|
+
// src/functions/commitCreate.ts
|
|
102
|
+
import {
|
|
103
|
+
createUniqueConstraintConflictError
|
|
104
|
+
} from "roc-db";
|
|
105
|
+
var commitCreate = (txn, document) => {
|
|
63
106
|
const objectStore = txn.engineOpts.txn.objectStore("entities");
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
timestamp: txn.mutation.timestamp
|
|
74
|
-
},
|
|
75
|
-
entity,
|
|
76
|
-
data,
|
|
77
|
-
children,
|
|
78
|
-
ancestors,
|
|
79
|
-
parents
|
|
80
|
-
};
|
|
81
|
-
if (txn.request.changeSetRef) {
|
|
82
|
-
if (txn.engineOpts.changeSet.entities.has(ref)) {
|
|
83
|
-
throw new Error("Entity already exists in changeSet");
|
|
84
|
-
} else {
|
|
85
|
-
txn.engineOpts.changeSet.entities.set(ref, document);
|
|
86
|
-
return document;
|
|
87
|
-
}
|
|
88
|
-
} else {
|
|
89
|
-
const request = objectStore.add(document);
|
|
90
|
-
return new Promise((resolve, reject) => {
|
|
91
|
-
request.onsuccess = (event) => {
|
|
92
|
-
resolve(document);
|
|
93
|
-
};
|
|
94
|
-
request.onerror = (event) => {
|
|
107
|
+
const request = objectStore.add(document);
|
|
108
|
+
return new Promise((resolve, reject) => {
|
|
109
|
+
request.onsuccess = (event) => {
|
|
110
|
+
resolve(document);
|
|
111
|
+
};
|
|
112
|
+
request.onerror = (event) => {
|
|
113
|
+
if (event?.target?.error?.name === "ConstraintError") {
|
|
114
|
+
reject(createUniqueConstraintConflictError(document.entity));
|
|
115
|
+
} else {
|
|
95
116
|
console.error("Error creating entity:", document, event?.target?.error);
|
|
96
117
|
reject(event?.target?.error);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
});
|
|
100
121
|
};
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
122
|
+
|
|
123
|
+
// src/functions/commitDelete.ts
|
|
124
|
+
import { entityFromRef } from "roc-db";
|
|
125
|
+
var commitDelete = async (txn, ref) => {
|
|
126
|
+
const entity = entityFromRef(ref);
|
|
127
|
+
let objectStore;
|
|
128
|
+
if (entity === "Mutation") {
|
|
129
|
+
objectStore = txn.engineOpts.txn.objectStore("mutations");
|
|
130
|
+
} else {
|
|
131
|
+
objectStore = txn.engineOpts.txn.objectStore("entities");
|
|
132
|
+
}
|
|
133
|
+
const request = objectStore.delete(ref);
|
|
105
134
|
return new Promise((resolve, reject) => {
|
|
106
135
|
request.onsuccess = () => {
|
|
107
136
|
resolve(true);
|
|
108
137
|
};
|
|
109
138
|
request.onerror = (event) => {
|
|
110
|
-
console.error("Error
|
|
139
|
+
console.error("Error deleting document", event?.target?.error);
|
|
111
140
|
reject(event?.target?.error);
|
|
112
141
|
};
|
|
113
142
|
});
|
|
114
143
|
};
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
144
|
+
|
|
145
|
+
// src/functions/commitUpdate.ts
|
|
146
|
+
var commitUpdate = async (txn, document) => {
|
|
147
|
+
const objectStore = txn.engineOpts.txn.objectStore("entities");
|
|
148
|
+
const request = objectStore.put(document);
|
|
149
|
+
return new Promise((resolve, reject) => {
|
|
150
|
+
request.onsuccess = (event) => {
|
|
151
|
+
resolve(document);
|
|
152
|
+
};
|
|
153
|
+
request.onerror = (event) => {
|
|
154
|
+
console.error("Error updating document", event?.target?.error);
|
|
155
|
+
reject(event?.target?.error);
|
|
156
|
+
};
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
// src/functions/saveMutation.ts
|
|
161
|
+
var saveMutation = async (txn, finalizedMutation) => {
|
|
162
|
+
const objectStore = txn.engineOpts.txn.objectStore("mutations");
|
|
163
|
+
let request;
|
|
164
|
+
if (finalizedMutation.debounceCount > 0 || finalizedMutation.appliedAt) {
|
|
165
|
+
request = objectStore.put(finalizedMutation);
|
|
122
166
|
} else {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
167
|
+
request = objectStore.add(finalizedMutation);
|
|
168
|
+
}
|
|
169
|
+
return new Promise((resolve, reject) => {
|
|
170
|
+
request.onsuccess = (event) => {
|
|
171
|
+
resolve(finalizedMutation);
|
|
172
|
+
};
|
|
173
|
+
request.onerror = (event) => {
|
|
174
|
+
console.error("Error saving mutation", event?.target?.error);
|
|
175
|
+
reject(event?.target?.error);
|
|
176
|
+
};
|
|
177
|
+
});
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
// src/lib/documentToDBRow.ts
|
|
181
|
+
var stringifyIndexEntry = (entity, [k, v]) => `${entity}:${k}:${JSON.stringify(v)}`;
|
|
182
|
+
var stringifyUniqueEntry = ([k, v]) => `${k}:${JSON.stringify(v)}`;
|
|
183
|
+
var stringifyIndexEntries = (entity, arr) => {
|
|
184
|
+
return arr.map((pair) => stringifyIndexEntry(entity, pair));
|
|
185
|
+
};
|
|
186
|
+
var documentToDBRow = (document) => {
|
|
187
|
+
return {
|
|
188
|
+
...document,
|
|
189
|
+
index_entries: document.__.index ? stringifyIndexEntries(document.entity, document.__.index) : [],
|
|
190
|
+
unique_constraint_0: document.__?.unique?.[0] ? stringifyUniqueEntry(document.__?.unique?.[0]) : null,
|
|
191
|
+
unique_constraint_1: document.__?.unique?.[1] ? stringifyUniqueEntry(document.__?.unique?.[1]) : null
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// src/functions/commit.ts
|
|
196
|
+
var commit = async (txn, mutation, { created, updated, deleted }) => {
|
|
197
|
+
const currentMutation = await txn.readMutation(txn.mutation.ref, false);
|
|
198
|
+
if (currentMutation) {
|
|
199
|
+
if (currentMutation && mutation.appliedAt === txn.timestamp) {
|
|
200
|
+
await saveMutation(txn, mutation);
|
|
201
|
+
} else if (currentMutation && mutation.debounceCount > currentMutation.debounceCount) {
|
|
202
|
+
await saveMutation(txn, mutation);
|
|
203
|
+
} else {
|
|
204
|
+
throw new Error("Mutation already exists");
|
|
127
205
|
}
|
|
128
|
-
|
|
206
|
+
} else {
|
|
207
|
+
await saveMutation(txn, mutation);
|
|
129
208
|
}
|
|
209
|
+
if (txn.request.changeSetRef)
|
|
210
|
+
return mutation;
|
|
211
|
+
for (const doc of created) {
|
|
212
|
+
await commitCreate(txn, documentToDBRow(doc));
|
|
213
|
+
}
|
|
214
|
+
for (const doc of updated) {
|
|
215
|
+
await commitUpdate(txn, documentToDBRow(doc));
|
|
216
|
+
}
|
|
217
|
+
for (const ref of deleted) {
|
|
218
|
+
await commitDelete(txn, ref);
|
|
219
|
+
}
|
|
220
|
+
return mutation;
|
|
130
221
|
};
|
|
131
222
|
// src/functions/end.ts
|
|
132
|
-
var end = (
|
|
223
|
+
var end = (engineOpts) => {
|
|
133
224
|
return new Promise((resolve, reject) => {
|
|
134
|
-
|
|
225
|
+
engineOpts.txn.oncomplete = () => {
|
|
135
226
|
resolve(undefined);
|
|
136
227
|
};
|
|
137
|
-
|
|
228
|
+
engineOpts.txn.onerror = (event) => {
|
|
138
229
|
reject(event.target.error);
|
|
139
230
|
};
|
|
140
231
|
});
|
|
@@ -159,11 +250,10 @@ var getChangeSetMutations = async (txn, changeSetRef) => {
|
|
|
159
250
|
});
|
|
160
251
|
};
|
|
161
252
|
// src/functions/findDebounceMutation.ts
|
|
162
|
-
var findDebounceMutation = async (request, engine, now) => {
|
|
163
|
-
const mutationName = request.schema.shape.name.value;
|
|
253
|
+
var findDebounceMutation = async (request, engine, now, mutationName) => {
|
|
164
254
|
const objectStore = engine.txn.objectStore("mutations");
|
|
165
255
|
const index = objectStore.index("timestamp");
|
|
166
|
-
const timestamp = new Date(now - request.
|
|
256
|
+
const timestamp = new Date(now - request.operation.debounce * 1000).toISOString();
|
|
167
257
|
const range = IDBKeyRange.lowerBound(timestamp, true);
|
|
168
258
|
const idbRequest = index.openCursor(range);
|
|
169
259
|
return new Promise((resolve, reject) => {
|
|
@@ -172,7 +262,7 @@ var findDebounceMutation = async (request, engine, now) => {
|
|
|
172
262
|
const cursor = idbRequest.result;
|
|
173
263
|
if (cursor) {
|
|
174
264
|
const doc = cursor.value;
|
|
175
|
-
if (doc.name === mutationName && doc.payload.ref === request.payload.ref && doc.changeSetRef === request.changeSetRef) {
|
|
265
|
+
if (doc.operation.name === mutationName && doc.payload.ref === request.payload.ref && doc.changeSetRef === request.changeSetRef) {
|
|
176
266
|
results.push(doc);
|
|
177
267
|
}
|
|
178
268
|
cursor.continue();
|
|
@@ -189,67 +279,83 @@ var findDebounceMutation = async (request, engine, now) => {
|
|
|
189
279
|
};
|
|
190
280
|
});
|
|
191
281
|
};
|
|
192
|
-
// src/functions/
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
// src/functions/readEntity.ts
|
|
196
|
-
import { DELETED_IN_CHANGE_SET_SYMBOL as DELETED_IN_CHANGE_SET_SYMBOL2 } from "roc-db";
|
|
197
|
-
var readEntity = (txn, ref) => {
|
|
198
|
-
if (txn.request.changeSetRef) {
|
|
199
|
-
const doc = txn.engineOpts.changeSet.entities.get(ref);
|
|
200
|
-
if (doc === DELETED_IN_CHANGE_SET_SYMBOL2)
|
|
201
|
-
return;
|
|
202
|
-
if (doc)
|
|
203
|
-
return doc;
|
|
204
|
-
}
|
|
282
|
+
// src/functions/pageEntities.ts
|
|
283
|
+
var pageEntities = async (txn, args) => {
|
|
284
|
+
const { size, entities, childrenOf } = args;
|
|
205
285
|
const objectStore = txn.engineOpts.txn.objectStore("entities");
|
|
206
|
-
const
|
|
286
|
+
const idbRequest = objectStore.openCursor();
|
|
207
287
|
return new Promise((resolve, reject) => {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
288
|
+
const results = [];
|
|
289
|
+
idbRequest.onsuccess = () => {
|
|
290
|
+
if (results.length >= size) {
|
|
291
|
+
resolve(results);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const cursor = idbRequest.result;
|
|
295
|
+
if (cursor) {
|
|
296
|
+
if (entities && entities !== "*" && !entities.includes(cursor.value.entity)) {
|
|
297
|
+
cursor.continue();
|
|
298
|
+
} else {
|
|
299
|
+
if (childrenOf && childrenOf.length > 0 && !childrenOf.some((childRef) => cursor.value.__.parentRefs.includes(childRef))) {
|
|
300
|
+
cursor.continue();
|
|
301
|
+
} else {
|
|
302
|
+
results.push(cursor.value);
|
|
303
|
+
cursor.continue();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
} else {
|
|
307
|
+
resolve(results);
|
|
308
|
+
}
|
|
211
309
|
};
|
|
212
|
-
|
|
213
|
-
|
|
310
|
+
});
|
|
311
|
+
};
|
|
312
|
+
// src/functions/pageEntitiesByIndex.ts
|
|
313
|
+
var pageEntitiesByIndex = async (txn, entity, field, value) => {
|
|
314
|
+
const objectStore = txn.engineOpts.txn.objectStore("entities");
|
|
315
|
+
const index = objectStore.index("byIndexEntry");
|
|
316
|
+
const entry = `${entity}:${field}:${JSON.stringify(value)}`;
|
|
317
|
+
const range = IDBKeyRange.only(entry);
|
|
318
|
+
const idbRequest = index.openCursor(range);
|
|
319
|
+
return new Promise((resolve, reject) => {
|
|
320
|
+
const results = [];
|
|
321
|
+
idbRequest.onsuccess = () => {
|
|
322
|
+
const cursor = idbRequest.result;
|
|
323
|
+
if (cursor) {
|
|
324
|
+
results.push(cursor.value);
|
|
325
|
+
cursor.continue();
|
|
326
|
+
} else {
|
|
327
|
+
resolve(results);
|
|
328
|
+
}
|
|
214
329
|
};
|
|
215
330
|
});
|
|
216
331
|
};
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
timestamp: txn.mutation.timestamp,
|
|
227
|
-
mutationRef: txn.mutation.ref
|
|
228
|
-
},
|
|
229
|
-
data: deepPatch(existingDocument.data, data),
|
|
230
|
-
children: deepPatch(existingDocument.children, children),
|
|
231
|
-
parents: deepPatch(existingDocument.parents, parents),
|
|
232
|
-
ancestors: deepPatch(existingDocument.ancestors, ancestors)
|
|
233
|
-
};
|
|
234
|
-
if (txn.request.changeSetRef) {
|
|
235
|
-
txn.engineOpts.changeSet.entities.set(ref, updatedDocument);
|
|
236
|
-
return updatedDocument;
|
|
332
|
+
// src/functions/pageMutations.ts
|
|
333
|
+
var pageMutations = async (txn, args) => {
|
|
334
|
+
const { size, changeSetRef, skip } = args;
|
|
335
|
+
const objectStore = txn.engineOpts.txn.objectStore("mutations");
|
|
336
|
+
let idbRequest;
|
|
337
|
+
if (changeSetRef) {
|
|
338
|
+
const index = objectStore.index("byChangeSetRef");
|
|
339
|
+
const range = IDBKeyRange.only(changeSetRef);
|
|
340
|
+
idbRequest = index.openCursor(range);
|
|
237
341
|
} else {
|
|
238
|
-
|
|
239
|
-
const request = objectStore.put(updatedDocument);
|
|
240
|
-
return new Promise((resolve, reject) => {
|
|
241
|
-
request.onsuccess = (event) => {
|
|
242
|
-
resolve(updatedDocument);
|
|
243
|
-
};
|
|
244
|
-
request.onerror = (event) => {
|
|
245
|
-
console.error("Error patching document", event?.target?.error);
|
|
246
|
-
reject(event?.target?.error);
|
|
247
|
-
};
|
|
248
|
-
});
|
|
342
|
+
idbRequest = objectStore.openCursor();
|
|
249
343
|
}
|
|
344
|
+
return new Promise((resolve, reject) => {
|
|
345
|
+
const results = [];
|
|
346
|
+
idbRequest.onsuccess = () => {
|
|
347
|
+
const cursor = idbRequest.result;
|
|
348
|
+
if (cursor) {
|
|
349
|
+
results.push(cursor.value);
|
|
350
|
+
cursor.continue();
|
|
351
|
+
} else {
|
|
352
|
+
resolve(results);
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
});
|
|
250
356
|
};
|
|
251
357
|
// src/functions/readMutation.ts
|
|
252
|
-
var readMutation = (engineOpts, ref) => {
|
|
358
|
+
var readMutation = async (engineOpts, ref) => {
|
|
253
359
|
const objectStore = engineOpts.txn.objectStore("mutations");
|
|
254
360
|
const request = objectStore.get(ref);
|
|
255
361
|
return new Promise((resolve, reject) => {
|
|
@@ -262,66 +368,39 @@ var readMutation = (engineOpts, ref) => {
|
|
|
262
368
|
};
|
|
263
369
|
});
|
|
264
370
|
};
|
|
265
|
-
// src/functions/
|
|
266
|
-
var
|
|
267
|
-
const objectStore = txn.engineOpts.txn.objectStore("
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
371
|
+
// src/functions/refByUniqueField.ts
|
|
372
|
+
var refByUniqueField = async (txn, entity, field, fieldIndex, value) => {
|
|
373
|
+
const objectStore = txn.engineOpts.txn.objectStore("entities");
|
|
374
|
+
if (fieldIndex > 1)
|
|
375
|
+
throw new Error("Field index must be 0 or 1");
|
|
376
|
+
const index = objectStore.index(`unique_constraint_${fieldIndex}`);
|
|
377
|
+
const range = IDBKeyRange.only([
|
|
378
|
+
entity,
|
|
379
|
+
`${field}:${JSON.stringify(value)}`
|
|
380
|
+
]);
|
|
381
|
+
const idbRequest = index.openCursor(range);
|
|
274
382
|
return new Promise((resolve, reject) => {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
request.onerror = (event) => {
|
|
279
|
-
console.error("Error saving mutation", event?.target?.error);
|
|
280
|
-
reject(event?.target?.error);
|
|
383
|
+
idbRequest.onsuccess = () => {
|
|
384
|
+
const cursor = idbRequest.result;
|
|
385
|
+
resolve(cursor?.value?.ref);
|
|
281
386
|
};
|
|
282
387
|
});
|
|
283
388
|
};
|
|
284
|
-
// src/functions/updateEntity.ts
|
|
285
|
-
var updateEntity = async (txn, ref, { data = {}, children = {}, parents = {}, ancestors = {} }) => {
|
|
286
|
-
const currentEntity = await readEntity(txn, ref);
|
|
287
|
-
const objectStore = txn.engineOpts.txn.objectStore("entities");
|
|
288
|
-
const document = {
|
|
289
|
-
...currentEntity,
|
|
290
|
-
updated: {
|
|
291
|
-
mutationRef: txn.mutation.ref,
|
|
292
|
-
timestamp: txn.mutation.timestamp
|
|
293
|
-
},
|
|
294
|
-
data,
|
|
295
|
-
children,
|
|
296
|
-
parents,
|
|
297
|
-
ancestors
|
|
298
|
-
};
|
|
299
|
-
if (txn.request.changeSetRef) {
|
|
300
|
-
throw new Error("TODO support changeSetRef");
|
|
301
|
-
} else {
|
|
302
|
-
const request = objectStore.put(document);
|
|
303
|
-
return new Promise((resolve, reject) => {
|
|
304
|
-
request.onsuccess = (event) => {
|
|
305
|
-
resolve(document);
|
|
306
|
-
};
|
|
307
|
-
request.onerror = (event) => {
|
|
308
|
-
console.error("Error updating document", event?.target?.error);
|
|
309
|
-
reject(event?.target?.error);
|
|
310
|
-
};
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
};
|
|
314
389
|
// src/createIndexedDBAdapter.ts
|
|
315
390
|
var createIndexedDBAdapter = ({
|
|
316
391
|
operations,
|
|
317
392
|
entities,
|
|
318
|
-
|
|
393
|
+
session,
|
|
394
|
+
dbName = "roc-db",
|
|
395
|
+
optimistic = false
|
|
319
396
|
}) => {
|
|
320
397
|
return createAdapter({
|
|
321
398
|
name: "indexed-db",
|
|
322
399
|
operations,
|
|
323
400
|
entities,
|
|
324
401
|
functions: exports_functions,
|
|
402
|
+
optimistic,
|
|
403
|
+
session,
|
|
325
404
|
snowflake: new Snowflake(1, 1),
|
|
326
405
|
async: true
|
|
327
406
|
}, {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Adapter } from "roc-db";
|
|
2
|
+
export declare const createIndexedDBAdapter: ({ operations, entities, session, dbName, optimistic, }: {
|
|
3
|
+
operations: any;
|
|
4
|
+
entities: any;
|
|
5
|
+
session: any;
|
|
6
|
+
dbName?: string | undefined;
|
|
7
|
+
optimistic?: boolean | undefined;
|
|
8
|
+
}) => Adapter;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const begin: (engineOpts: any, callback: any) => Promise<unknown>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const commitUpdate: (txn: any, document: any) => Promise<unknown>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { batchReadEntities } from "./batchReadEntities";
|
|
2
|
+
export { begin } from "./begin";
|
|
3
|
+
export { commit } from "./commit";
|
|
4
|
+
export { end } from "./end";
|
|
5
|
+
export { getChangeSetMutations } from "./getChangeSetMutations";
|
|
6
|
+
export { findDebounceMutation } from "./findDebounceMutation";
|
|
7
|
+
export { pageEntities } from "./pageEntities";
|
|
8
|
+
export { pageEntitiesByIndex } from "./pageEntitiesByIndex";
|
|
9
|
+
export { pageMutations } from "./pageMutations";
|
|
10
|
+
export { readEntity } from "./readEntity";
|
|
11
|
+
export { readMutation } from "./readMutation";
|
|
12
|
+
export { refByUniqueField } from "./refByUniqueField";
|
|
13
|
+
export { saveMutation } from "./saveMutation";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const readEntity: (txn: any, ref: any) => Promise<unknown>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const readMutation: (engineOpts: any, ref: any) => Promise<unknown>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const refByUniqueField: (txn: any, entity: any, field: any, fieldIndex: any, value: any) => Promise<unknown>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const saveMutation: (txn: any, finalizedMutation: any) => Promise<unknown>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createIndexedDBAdapter } from "./createIndexedDBAdapter";
|
|
@@ -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.60",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"sideEffects": false,
|
|
15
15
|
"dependencies": {},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"roc-db": "0.2.0-pre.
|
|
18
|
-
"zod": "3.
|
|
17
|
+
"roc-db": "0.2.0-pre.60",
|
|
18
|
+
"zod": "^3.0.0"
|
|
19
19
|
},
|
|
20
20
|
"keywords": [],
|
|
21
21
|
"author": "Eigil Sagafos",
|