@roc-db/indexed-db 0.2.0-pre.4 → 0.2.0-pre.41

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,21 +15,39 @@ 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,
21
+ pageMutations: () => pageMutations,
23
22
  getChangeSetMutations: () => getChangeSetMutations,
24
23
  findDebounceMutation: () => findDebounceMutation,
25
24
  end: () => end,
26
- deleteEntity: () => deleteEntity,
27
- createEntity: () => createEntity,
28
- begin: () => begin
25
+ commit: () => commit,
26
+ begin: () => begin,
27
+ batchReadEntities: () => batchReadEntities
29
28
  });
30
29
 
30
+ // src/functions/readEntity.ts
31
+ var readEntity = async (txn, ref) => {
32
+ const objectStore = txn.engineOpts.txn.objectStore("entities");
33
+ const request = objectStore.get(ref);
34
+ return new Promise((resolve, reject) => {
35
+ request.onsuccess = (event) => {
36
+ const document = event.target.result;
37
+ resolve(document);
38
+ };
39
+ request.onerror = (event) => {
40
+ reject(event.target.error);
41
+ };
42
+ });
43
+ };
44
+
45
+ // src/functions/batchReadEntities.ts
46
+ var batchReadEntities = (txn, refs) => {
47
+ return Promise.all(refs.map((ref) => readEntity(txn, ref)));
48
+ };
31
49
  // src/functions/begin.ts
32
- var begin = async (request, engineOpts) => {
50
+ var begin = async (engineOpts, callback) => {
33
51
  return new Promise((resolve, reject) => {
34
52
  let db;
35
53
  const idbRequest = indexedDB.open(engineOpts.dbName, 1);
@@ -45,63 +63,33 @@ var begin = async (request, engineOpts) => {
45
63
  };
46
64
  idbRequest.onsuccess = (event) => {
47
65
  db = idbRequest.result;
48
- resolve({
66
+ resolve(callback({
49
67
  ...engineOpts,
50
68
  db,
51
- txn: db.transaction(["entities", "mutations"], request.type === "write" ? "readwrite" : "readonly"),
52
- changeSet: request.changeSetRef ? {
53
- entities: new Map([]),
54
- mutations: new Map([])
55
- } : null
56
- });
69
+ txn: db.transaction(["entities", "mutations"], "readwrite")
70
+ }));
57
71
  };
58
72
  });
59
73
  };
60
- // src/functions/createEntity.ts
61
- import { entityFromRef } from "roc-db";
62
- var createEntity = (txn, ref, { data = {}, children = {}, ancestors = {}, parents = {} }) => {
74
+ // src/functions/commitCreate.ts
75
+ var commitCreate = (txn, document) => {
63
76
  const objectStore = txn.engineOpts.txn.objectStore("entities");
64
- const entity = entityFromRef(ref);
65
- const document = {
66
- ref,
67
- created: {
68
- mutationRef: txn.mutation.ref,
69
- timestamp: txn.mutation.timestamp
70
- },
71
- updated: {
72
- mutationRef: txn.mutation.ref,
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) => {
95
- console.error("Error creating entity:", document, event?.target?.error);
96
- reject(event?.target?.error);
97
- };
98
- });
99
- }
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
+ });
100
87
  };
101
- // src/functions/deleteEntity.ts
102
- import { DELETED_IN_CHANGE_SET_SYMBOL } from "roc-db";
103
- var deleteIndexed = (store, ref) => {
104
- 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);
105
93
  return new Promise((resolve, reject) => {
106
94
  request.onsuccess = () => {
107
95
  resolve(true);
@@ -112,29 +100,76 @@ var deleteIndexed = (store, ref) => {
112
100
  };
113
101
  });
114
102
  };
115
- var deleteEntity = async (txn, ref, cascade = false) => {
116
- if (txn.request.changeSetRef) {
117
- txn.engineOpts.changeSet.entities.set(ref, DELETED_IN_CHANGE_SET_SYMBOL);
118
- if (cascade) {
119
- throw new Error("TGOD");
120
- }
121
- 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);
122
125
  } else {
123
- const objectStore = txn.engineOpts.txn.objectStore("entities");
124
- await deleteIndexed(objectStore, ref);
125
- if (cascade) {
126
- 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");
127
149
  }
128
- 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);
129
160
  }
161
+ for (const ref of deleted) {
162
+ await commitDelete(txn, ref);
163
+ }
164
+ return mutation;
130
165
  };
131
166
  // src/functions/end.ts
132
- var end = (txn) => {
167
+ var end = (engineOpts) => {
133
168
  return new Promise((resolve, reject) => {
134
- txn.engineOpts.txn.oncomplete = () => {
169
+ engineOpts.txn.oncomplete = () => {
135
170
  resolve(undefined);
136
171
  };
137
- txn.engineOpts.txn.onerror = (event) => {
172
+ engineOpts.txn.onerror = (event) => {
138
173
  reject(event.target.error);
139
174
  };
140
175
  });
@@ -159,8 +194,7 @@ var getChangeSetMutations = async (txn, changeSetRef) => {
159
194
  });
160
195
  };
161
196
  // src/functions/findDebounceMutation.ts
162
- var findDebounceMutation = async (request, engine, now) => {
163
- const mutationName = request.schema.shape.name.value;
197
+ var findDebounceMutation = async (request, engine, now, mutationName) => {
164
198
  const objectStore = engine.txn.objectStore("mutations");
165
199
  const index = objectStore.index("timestamp");
166
200
  const timestamp = new Date(now - request.settings.debounce * 1000).toISOString();
@@ -172,7 +206,7 @@ var findDebounceMutation = async (request, engine, now) => {
172
206
  const cursor = idbRequest.result;
173
207
  if (cursor) {
174
208
  const doc = cursor.value;
175
- if (doc.name === mutationName && doc.payload.ref === request.payload.ref && doc.changeSetRef === request.changeSetRef) {
209
+ if (doc.operation.name === mutationName && doc.payload.ref === request.payload.ref && doc.changeSetRef === request.changeSetRef) {
176
210
  results.push(doc);
177
211
  }
178
212
  cursor.continue();
@@ -189,67 +223,33 @@ var findDebounceMutation = async (request, engine, now) => {
189
223
  };
190
224
  });
191
225
  };
192
- // src/functions/patchEntity.ts
193
- import { deepPatch, NotFoundError } from "roc-db";
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;
226
+ // src/functions/pageMutations.ts
227
+ var pageMutations = async (txn, args) => {
228
+ const { size, changeSetRef, skip } = args;
229
+ const objectStore = txn.engineOpts.txn.objectStore("mutations");
230
+ let idbRequest;
231
+ if (changeSetRef) {
232
+ const index = objectStore.index("byChangeSetRef");
233
+ const range = IDBKeyRange.only(changeSetRef);
234
+ idbRequest = index.openCursor(range);
235
+ } else {
236
+ idbRequest = objectStore.openCursor();
204
237
  }
205
- const objectStore = txn.engineOpts.txn.objectStore("entities");
206
- const request = objectStore.get(ref);
207
238
  return new Promise((resolve, reject) => {
208
- request.onsuccess = (event) => {
209
- const document = event.target.result;
210
- resolve(document);
211
- };
212
- request.onerror = (event) => {
213
- reject(event.target.error);
239
+ const results = [];
240
+ idbRequest.onsuccess = () => {
241
+ const cursor = idbRequest.result;
242
+ if (cursor) {
243
+ results.push(cursor.value);
244
+ cursor.continue();
245
+ } else {
246
+ resolve(results);
247
+ }
214
248
  };
215
249
  });
216
250
  };
217
-
218
- // src/functions/patchEntity.ts
219
- var patchEntity = async (txn, ref, { data = {}, children = {}, parents = {}, ancestors = {} }) => {
220
- const existingDocument = await readEntity(txn, ref);
221
- if (!existingDocument)
222
- throw new NotFoundError(ref);
223
- const updatedDocument = {
224
- ...existingDocument,
225
- updated: {
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;
237
- } else {
238
- const objectStore = txn.engineOpts.txn.objectStore("entities");
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
- });
249
- }
250
- };
251
251
  // src/functions/readMutation.ts
252
- var readMutation = (engineOpts, ref) => {
252
+ var readMutation = async (engineOpts, ref) => {
253
253
  const objectStore = engineOpts.txn.objectStore("mutations");
254
254
  const request = objectStore.get(ref);
255
255
  return new Promise((resolve, reject) => {
@@ -262,66 +262,21 @@ var readMutation = (engineOpts, ref) => {
262
262
  };
263
263
  });
264
264
  };
265
- // src/functions/saveMutation.ts
266
- var saveMutation = (txn, finalizedMutation) => {
267
- const objectStore = txn.engineOpts.txn.objectStore("mutations");
268
- let request;
269
- if (finalizedMutation.debounceCount > 0) {
270
- request = objectStore.put(finalizedMutation);
271
- } else {
272
- request = objectStore.add(finalizedMutation);
273
- }
274
- return new Promise((resolve, reject) => {
275
- request.onsuccess = (event) => {
276
- resolve(finalizedMutation);
277
- };
278
- request.onerror = (event) => {
279
- console.error("Error saving mutation", event?.target?.error);
280
- reject(event?.target?.error);
281
- };
282
- });
283
- };
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
265
  // src/createIndexedDBAdapter.ts
315
266
  var createIndexedDBAdapter = ({
316
267
  operations,
317
268
  entities,
318
- dbName = "roc-db"
269
+ session,
270
+ dbName = "roc-db",
271
+ optimistic = false
319
272
  }) => {
320
273
  return createAdapter({
321
274
  name: "indexed-db",
322
275
  operations,
323
276
  entities,
324
277
  functions: exports_functions,
278
+ optimistic,
279
+ session,
325
280
  snowflake: new Snowflake(1, 1),
326
281
  async: true
327
282
  }, {
@@ -0,0 +1,7 @@
1
+ export declare const createIndexedDBAdapter: ({ operations, entities, session, dbName, optimistic, }: {
2
+ operations: any;
3
+ entities: any;
4
+ session: any;
5
+ dbName?: string | undefined;
6
+ optimistic?: boolean | undefined;
7
+ }) => Adapter;
@@ -0,0 +1,2 @@
1
+ import type { Ref } from "roc-db";
2
+ export declare const batchReadEntities: (txn: any, refs: Ref[]) => Promise<unknown[]>;
@@ -0,0 +1 @@
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>;
@@ -0,0 +1,5 @@
1
+ import type { EndFunction } from "roc-db";
2
+ import type { IndexedDBEngine } from "../types/IndexedDBEngine";
3
+ import type { WriteRequest } from "roc-db/src/types/WriteRequest";
4
+ export declare const end: EndFunction<WriteRequest, IndexedDBEngine, [
5
+ ], any>;
@@ -0,0 +1,3 @@
1
+ import type { WriteRequest } from "roc-db";
2
+ import type { IndexedDBEngine } from "../types/IndexedDBEngine";
3
+ export declare const findDebounceMutation: (request: WriteRequest, engine: IndexedDBEngine, now: number, mutationName: string) => Promise<unknown>;
@@ -0,0 +1,3 @@
1
+ import type { Ref } from "roc-db";
2
+ import type { IndexedDBReadTransaction } from "../types/IndexedDBReadTransaction";
3
+ export declare const getChangeSetMutations: (txn: IndexedDBReadTransaction, changeSetRef: Ref) => Promise<unknown>;
@@ -0,0 +1,10 @@
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 { pageMutations } from "./pageMutations";
8
+ export { readEntity } from "./readEntity";
9
+ export { readMutation } from "./readMutation";
10
+ export { saveMutation } from "./saveMutation";
@@ -0,0 +1,2 @@
1
+ import type { IndexedDBReadTransaction } from "../types/IndexedDBReadTransaction";
2
+ export declare const pageMutations: (txn: IndexedDBReadTransaction, args: any) => Promise<unknown>;
@@ -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 saveMutation: (txn: any, finalizedMutation: any) => Promise<unknown>;
@@ -0,0 +1 @@
1
+ export { createIndexedDBAdapter } from "./createIndexedDBAdapter";
@@ -0,0 +1,5 @@
1
+ export type IndexedDBEngine = {
2
+ dbName: string;
3
+ db: IDBDatabase;
4
+ txn: IDBTransaction;
5
+ };
@@ -0,0 +1,4 @@
1
+ import type { ReadTransaction } from "roc-db";
2
+ import type { IndexedDBEngine } from "./IndexedDBEngine";
3
+ export type IndexedDBReadTransaction = ReadTransaction<any, IndexedDBEngine, [
4
+ ], any>;
package/package.json CHANGED
@@ -1,29 +1,21 @@
1
1
  {
2
2
  "name": "@roc-db/indexed-db",
3
- "version": "0.2.0-pre.4",
3
+ "version": "0.2.0-pre.41",
4
4
  "type": "module",
5
5
  "exports": {
6
- ".": "./src/index.ts"
6
+ ".": {
7
+ "import": "./dist/index.js",
8
+ "types": "./dist/types/index.d.ts"
9
+ }
7
10
  },
8
11
  "files": [
9
12
  "dist"
10
13
  ],
11
14
  "sideEffects": false,
12
- "scripts": {
13
- "build": "NODE_ENV=production bun run scripts/build.ts",
14
- "build:types": "rm -rf dist/types && bun run tsc --noCheck",
15
- "test": "bun test"
16
- },
17
15
  "dependencies": {},
18
- "devDependencies": {
19
- "@roc-db/test-utils": "0.2.0-pre.4",
20
- "fake-indexeddb": "6.0.0",
21
- "roc-db": "0.2.0-pre.4",
22
- "zod": "3.24.2"
23
- },
24
16
  "peerDependencies": {
25
- "roc-db": "0.2.0-pre.4",
26
- "zod": "3.24.2"
17
+ "roc-db": "0.2.0-pre.41",
18
+ "zod": "^3.0.0"
27
19
  },
28
20
  "keywords": [],
29
21
  "author": "Eigil Sagafos",