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