@roc-db/indexed-db 0.2.0-pre.32 → 0.2.0-pre.34

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 CHANGED
@@ -15,31 +15,20 @@ 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,
20
19
  readMutation: () => readMutation,
21
20
  readEntity: () => readEntity,
22
- patchEntity: () => patchEntity,
23
21
  pageMutations: () => pageMutations,
24
22
  getChangeSetMutations: () => getChangeSetMutations,
25
23
  findDebounceMutation: () => findDebounceMutation,
26
24
  end: () => end,
27
- deleteEntity: () => deleteEntity,
28
- createEntity: () => createEntity,
25
+ commit: () => commit,
29
26
  begin: () => begin,
30
27
  batchReadEntities: () => batchReadEntities
31
28
  });
32
29
 
33
30
  // src/functions/readEntity.ts
34
- import { DELETED_IN_CHANGE_SET_SYMBOL } from "roc-db";
35
31
  var readEntity = async (txn, ref) => {
36
- if (txn.request.changeSetRef) {
37
- const doc = txn.engineOpts.changeSet.entities.get(ref);
38
- if (doc === DELETED_IN_CHANGE_SET_SYMBOL)
39
- return;
40
- if (doc)
41
- return doc;
42
- }
43
32
  const objectStore = txn.engineOpts.txn.objectStore("entities");
44
33
  const request = objectStore.get(ref);
45
34
  return new Promise((resolve, reject) => {
@@ -58,7 +47,7 @@ var batchReadEntities = (txn, refs) => {
58
47
  return Promise.all(refs.map((ref) => readEntity(txn, ref)));
59
48
  };
60
49
  // src/functions/begin.ts
61
- var begin = async (request, engineOpts, callback) => {
50
+ var begin = async (engineOpts, callback) => {
62
51
  return new Promise((resolve, reject) => {
63
52
  let db;
64
53
  const idbRequest = indexedDB.open(engineOpts.dbName, 1);
@@ -77,60 +66,30 @@ var begin = async (request, engineOpts, callback) => {
77
66
  resolve(callback({
78
67
  ...engineOpts,
79
68
  db,
80
- txn: db.transaction(["entities", "mutations"], request.type === "write" ? "readwrite" : "readonly"),
81
- changeSet: request.changeSetRef ? {
82
- entities: new Map([]),
83
- mutations: new Map([])
84
- } : null
69
+ txn: db.transaction(["entities", "mutations"], "readwrite")
85
70
  }));
86
71
  };
87
72
  });
88
73
  };
89
- // src/functions/createEntity.ts
90
- import { entityFromRef } from "roc-db";
91
- var createEntity = (txn, ref, { data = {}, children = {}, ancestors = {}, parents = {} }) => {
74
+ // src/functions/commitCreate.ts
75
+ var commitCreate = (txn, document) => {
92
76
  const objectStore = txn.engineOpts.txn.objectStore("entities");
93
- const entity = entityFromRef(ref);
94
- const document = {
95
- ref,
96
- created: {
97
- mutationRef: txn.mutation.ref,
98
- timestamp: txn.mutation.timestamp
99
- },
100
- updated: {
101
- mutationRef: txn.mutation.ref,
102
- timestamp: txn.mutation.timestamp
103
- },
104
- entity,
105
- data,
106
- children,
107
- ancestors,
108
- parents
109
- };
110
- if (txn.request.changeSetRef) {
111
- if (txn.engineOpts.changeSet.entities.has(ref)) {
112
- throw new Error("Entity already exists in changeSet");
113
- } else {
114
- txn.engineOpts.changeSet.entities.set(ref, document);
115
- return document;
116
- }
117
- } else {
118
- const request = objectStore.add(document);
119
- return new Promise((resolve, reject) => {
120
- request.onsuccess = (event) => {
121
- resolve(document);
122
- };
123
- request.onerror = (event) => {
124
- console.error("Error creating entity:", document, event?.target?.error);
125
- reject(event?.target?.error);
126
- };
127
- });
128
- }
77
+ const request = objectStore.add(document);
78
+ return new Promise((resolve, reject) => {
79
+ request.onsuccess = (event) => {
80
+ resolve(document);
81
+ };
82
+ request.onerror = (event) => {
83
+ console.error("Error creating entity:", document, event?.target?.error);
84
+ reject(event?.target?.error);
85
+ };
86
+ });
129
87
  };
130
- // src/functions/deleteEntity.ts
131
- import { DELETED_IN_CHANGE_SET_SYMBOL as DELETED_IN_CHANGE_SET_SYMBOL2 } from "roc-db";
132
- var deleteIndexed = (store, ref) => {
133
- const request = store.delete(ref);
88
+
89
+ // src/functions/commitDelete.ts
90
+ var commitDelete = async (txn, ref, cascade = false) => {
91
+ const objectStore = txn.engineOpts.txn.objectStore("entities");
92
+ const request = objectStore.delete(ref);
134
93
  return new Promise((resolve, reject) => {
135
94
  request.onsuccess = () => {
136
95
  resolve(true);
@@ -141,29 +100,76 @@ var deleteIndexed = (store, ref) => {
141
100
  };
142
101
  });
143
102
  };
144
- var deleteEntity = async (txn, ref, cascade = false) => {
145
- if (txn.request.changeSetRef) {
146
- txn.engineOpts.changeSet.entities.set(ref, DELETED_IN_CHANGE_SET_SYMBOL2);
147
- if (cascade) {
148
- throw new Error("TGOD");
149
- }
150
- return true;
103
+
104
+ // src/functions/commitUpdate.ts
105
+ var commitUpdate = async (txn, document) => {
106
+ const objectStore = txn.engineOpts.txn.objectStore("entities");
107
+ const request = objectStore.put(document);
108
+ return new Promise((resolve, reject) => {
109
+ request.onsuccess = (event) => {
110
+ resolve(document);
111
+ };
112
+ request.onerror = (event) => {
113
+ console.error("Error updating document", event?.target?.error);
114
+ reject(event?.target?.error);
115
+ };
116
+ });
117
+ };
118
+
119
+ // src/functions/saveMutation.ts
120
+ var saveMutation = async (txn, finalizedMutation) => {
121
+ const objectStore = txn.engineOpts.txn.objectStore("mutations");
122
+ let request;
123
+ if (finalizedMutation.debounceCount > 0 || finalizedMutation.appliedAt) {
124
+ request = objectStore.put(finalizedMutation);
151
125
  } else {
152
- const objectStore = txn.engineOpts.txn.objectStore("entities");
153
- await deleteIndexed(objectStore, ref);
154
- if (cascade) {
155
- throw new Error("TGOD");
126
+ request = objectStore.add(finalizedMutation);
127
+ }
128
+ return new Promise((resolve, reject) => {
129
+ request.onsuccess = (event) => {
130
+ resolve(finalizedMutation);
131
+ };
132
+ request.onerror = (event) => {
133
+ console.error("Error saving mutation", event?.target?.error);
134
+ reject(event?.target?.error);
135
+ };
136
+ });
137
+ };
138
+
139
+ // src/functions/commit.ts
140
+ var commit = async (txn, mutation, { created, updated, deleted }) => {
141
+ const currentMutation = await txn.readMutation(txn.mutation.ref, false);
142
+ if (currentMutation) {
143
+ if (currentMutation && mutation.appliedAt === txn.timestamp) {
144
+ await saveMutation(txn, mutation);
145
+ } else if (currentMutation && mutation.debounceCount > currentMutation.debounceCount) {
146
+ await saveMutation(txn, mutation);
147
+ } else {
148
+ throw new Error("Mutation already exists");
156
149
  }
157
- return true;
150
+ } else {
151
+ await saveMutation(txn, mutation);
152
+ }
153
+ if (txn.request.changeSetRef)
154
+ return mutation;
155
+ for (const doc of created) {
156
+ await commitCreate(txn, doc);
157
+ }
158
+ for (const doc of updated) {
159
+ await commitUpdate(txn, doc);
158
160
  }
161
+ for (const ref of deleted) {
162
+ await commitDelete(txn, ref);
163
+ }
164
+ return mutation;
159
165
  };
160
166
  // src/functions/end.ts
161
- var end = (txn) => {
167
+ var end = (engineOpts) => {
162
168
  return new Promise((resolve, reject) => {
163
- txn.engineOpts.txn.oncomplete = () => {
169
+ engineOpts.txn.oncomplete = () => {
164
170
  resolve(undefined);
165
171
  };
166
- txn.engineOpts.txn.onerror = (event) => {
172
+ engineOpts.txn.onerror = (event) => {
167
173
  reject(event.target.error);
168
174
  };
169
175
  });
@@ -221,12 +227,15 @@ var findDebounceMutation = async (request, engine, now) => {
221
227
  // src/functions/pageMutations.ts
222
228
  var pageMutations = async (txn, args) => {
223
229
  const { size, changeSetRef, skip } = args;
224
- if (!changeSetRef)
225
- throw new Error("changeSetRef is required");
226
230
  const objectStore = txn.engineOpts.txn.objectStore("mutations");
227
- const index = objectStore.index("byChangeSetRef");
228
- const range = IDBKeyRange.only(changeSetRef);
229
- const idbRequest = index.openCursor(range);
231
+ let idbRequest;
232
+ if (changeSetRef) {
233
+ const index = objectStore.index("byChangeSetRef");
234
+ const range = IDBKeyRange.only(changeSetRef);
235
+ idbRequest = index.openCursor(range);
236
+ } else {
237
+ idbRequest = objectStore.openCursor();
238
+ }
230
239
  return new Promise((resolve, reject) => {
231
240
  const results = [];
232
241
  idbRequest.onsuccess = () => {
@@ -240,40 +249,6 @@ var pageMutations = async (txn, args) => {
240
249
  };
241
250
  });
242
251
  };
243
- // src/functions/patchEntity.ts
244
- import { deepPatch, NotFoundError } from "roc-db";
245
- var patchEntity = async (txn, ref, { data = {}, children = {}, parents = {}, ancestors = {} }) => {
246
- const existingDocument = await readEntity(txn, ref);
247
- if (!existingDocument)
248
- throw new NotFoundError(ref);
249
- const updatedDocument = {
250
- ...existingDocument,
251
- updated: {
252
- timestamp: txn.mutation.timestamp,
253
- mutationRef: txn.mutation.ref
254
- },
255
- data: deepPatch(existingDocument.data, data),
256
- children: deepPatch(existingDocument.children, children),
257
- parents: deepPatch(existingDocument.parents, parents),
258
- ancestors: deepPatch(existingDocument.ancestors, ancestors)
259
- };
260
- if (txn.request.changeSetRef) {
261
- txn.engineOpts.changeSet.entities.set(ref, updatedDocument);
262
- return updatedDocument;
263
- } else {
264
- const objectStore = txn.engineOpts.txn.objectStore("entities");
265
- const request = objectStore.put(updatedDocument);
266
- return new Promise((resolve, reject) => {
267
- request.onsuccess = (event) => {
268
- resolve(updatedDocument);
269
- };
270
- request.onerror = (event) => {
271
- console.error("Error patching document", event?.target?.error);
272
- reject(event?.target?.error);
273
- };
274
- });
275
- }
276
- };
277
252
  // src/functions/readMutation.ts
278
253
  var readMutation = async (engineOpts, ref) => {
279
254
  const objectStore = engineOpts.txn.objectStore("mutations");
@@ -288,55 +263,6 @@ var readMutation = async (engineOpts, ref) => {
288
263
  };
289
264
  });
290
265
  };
291
- // src/functions/saveMutation.ts
292
- var saveMutation = async (txn, finalizedMutation) => {
293
- const objectStore = txn.engineOpts.txn.objectStore("mutations");
294
- let request;
295
- if (finalizedMutation.debounceCount > 0) {
296
- request = objectStore.put(finalizedMutation);
297
- } else {
298
- request = objectStore.add(finalizedMutation);
299
- }
300
- return new Promise((resolve, reject) => {
301
- request.onsuccess = (event) => {
302
- resolve(finalizedMutation);
303
- };
304
- request.onerror = (event) => {
305
- console.error("Error saving mutation", event?.target?.error);
306
- reject(event?.target?.error);
307
- };
308
- });
309
- };
310
- // src/functions/updateEntity.ts
311
- var updateEntity = async (txn, ref, { data = {}, children = {}, parents = {}, ancestors = {} }) => {
312
- const currentEntity = await readEntity(txn, ref);
313
- const objectStore = txn.engineOpts.txn.objectStore("entities");
314
- const document = {
315
- ...currentEntity,
316
- updated: {
317
- mutationRef: txn.mutation.ref,
318
- timestamp: txn.mutation.timestamp
319
- },
320
- data,
321
- children,
322
- parents,
323
- ancestors
324
- };
325
- if (txn.request.changeSetRef) {
326
- throw new Error("TODO support changeSetRef");
327
- } else {
328
- const request = objectStore.put(document);
329
- return new Promise((resolve, reject) => {
330
- request.onsuccess = (event) => {
331
- resolve(document);
332
- };
333
- request.onerror = (event) => {
334
- console.error("Error updating document", event?.target?.error);
335
- reject(event?.target?.error);
336
- };
337
- });
338
- }
339
- };
340
266
  // src/createIndexedDBAdapter.ts
341
267
  var createIndexedDBAdapter = ({
342
268
  operations,
@@ -1,2 +1,2 @@
1
1
  import type { Ref } from "roc-db";
2
- export declare const batchReadEntities: (txn: any, refs: Ref[]) => Promise<any[]>;
2
+ export declare const batchReadEntities: (txn: any, refs: Ref[]) => Promise<unknown[]>;
@@ -1 +1 @@
1
- export declare const begin: (request: any, engineOpts: any, callback: any) => Promise<unknown>;
1
+ export declare const begin: (engineOpts: any, callback: any) => Promise<unknown>;
@@ -0,0 +1,5 @@
1
+ export declare const commit: (txn: any, mutation: any, { created, updated, deleted }: {
2
+ created: any;
3
+ updated: any;
4
+ deleted: any;
5
+ }) => Promise<any>;
@@ -0,0 +1,3 @@
1
+ import type { CreateEntityFunction } from "roc-db";
2
+ import type { IndexedDBEngine } from "../types/IndexedDBEngine";
3
+ export declare const commitCreate: CreateEntityFunction<IndexedDBEngine>;
@@ -0,0 +1 @@
1
+ export declare const commitDelete: (txn: any, ref: any, cascade?: boolean) => 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, [], any>;
4
+ export declare const end: EndFunction<WriteRequest, IndexedDBEngine, [
5
+ ], any>;
@@ -1,13 +1,10 @@
1
1
  export { batchReadEntities } from "./batchReadEntities";
2
2
  export { begin } from "./begin";
3
- export { createEntity } from "./createEntity";
4
- export { deleteEntity } from "./deleteEntity";
3
+ export { commit } from "./commit";
5
4
  export { end } from "./end";
6
5
  export { getChangeSetMutations } from "./getChangeSetMutations";
7
6
  export { findDebounceMutation } from "./findDebounceMutation";
8
7
  export { pageMutations } from "./pageMutations";
9
- export { patchEntity } from "./patchEntity";
10
8
  export { readEntity } from "./readEntity";
11
9
  export { readMutation } from "./readMutation";
12
10
  export { saveMutation } from "./saveMutation";
13
- export { updateEntity } from "./updateEntity";
@@ -1 +1 @@
1
- export declare const readEntity: (txn: any, ref: any) => Promise<any>;
1
+ export declare const readEntity: (txn: any, ref: any) => Promise<unknown>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@roc-db/indexed-db",
3
- "version": "0.2.0-pre.32",
3
+ "version": "0.2.0-pre.34",
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.32",
17
+ "roc-db": "0.2.0-pre.34",
18
18
  "zod": "^3.0.0"
19
19
  },
20
20
  "keywords": [],
@@ -1,3 +0,0 @@
1
- import { type CreateEntityFunction } from "roc-db";
2
- import type { IndexedDBEngine } from "../types/IndexedDBEngine";
3
- export declare const createEntity: CreateEntityFunction<IndexedDBEngine>;
@@ -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>;
@@ -1,6 +0,0 @@
1
- export declare const updateEntity: (txn: any, ref: any, { data, children, parents, ancestors }: {
2
- data?: {} | undefined;
3
- children?: {} | undefined;
4
- parents?: {} | undefined;
5
- ancestors?: {} | undefined;
6
- }) => Promise<unknown>;