@roc-db/in-memory 0.2.0-pre.33 → 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,110 +15,56 @@ 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
- deleteEntity: () => deleteEntity,
27
- createEntity: () => createEntity,
28
- begin: () => begin,
24
+ commit: () => commit,
29
25
  batchReadEntities: () => batchReadEntities
30
26
  });
31
27
 
32
28
  // src/functions/readEntity.ts
33
- import { DELETED_IN_CHANGE_SET_SYMBOL } from "roc-db";
34
- var readEntity = (txn, ref) => {
35
- if (txn.request.changeSetRef) {
36
- const doc = txn.engineOpts.changeSet.entities.get(ref);
37
- if (doc === DELETED_IN_CHANGE_SET_SYMBOL)
38
- return;
39
- if (doc)
40
- return doc;
41
- }
42
- return txn.engineOpts.entities.get(ref);
43
- };
29
+ var readEntity = (txn, ref) => txn.engineOpts.entities.get(ref);
44
30
 
45
31
  // src/functions/batchReadEntities.ts
46
32
  var batchReadEntities = (txn, refs) => {
47
33
  return refs.map((ref) => readEntity(txn, ref));
48
34
  };
49
- // src/functions/begin.ts
50
- var begin = (request, engineOpts, callback) => {
51
- return callback({
52
- ...engineOpts,
53
- changeSet: request.changeSetRef ? {
54
- entities: new Map([]),
55
- mutations: new Map([])
56
- } : null
57
- });
35
+ // src/functions/saveMutation.ts
36
+ var saveMutation = (txn, finalizedMutation) => {
37
+ txn.engineOpts.mutations.set(finalizedMutation.ref, finalizedMutation);
38
+ return finalizedMutation;
58
39
  };
59
- // src/functions/createEntity.ts
60
- import { entityFromRef } from "roc-db";
61
- var createEntity = (txn, ref, {
62
- data = {},
63
- indexedData = {},
64
- children = {},
65
- parents = {},
66
- ancestors = {}
67
- }) => {
68
- const entity = entityFromRef(ref);
69
- const document = {
70
- ref,
71
- entity,
72
- created: {
73
- timestamp: txn.mutation.timestamp,
74
- mutationRef: txn.mutation.ref
75
- },
76
- updated: {
77
- timestamp: txn.mutation.timestamp,
78
- mutationRef: txn.mutation.ref
79
- },
80
- data: {
81
- ...data,
82
- ...indexedData
83
- },
84
- children,
85
- parents,
86
- ancestors
87
- };
88
- if (txn.request.changeSetRef) {
89
- if (txn.engineOpts.changeSet.entities.has(ref)) {
90
- throw new Error("Entity already exists in changeSet");
40
+
41
+ // src/functions/commit.ts
42
+ var commit = (txn, mutation, { created, updated, deleted }) => {
43
+ const { mutations } = txn.engineOpts;
44
+ const currentMutation = mutations.get(txn.mutation.ref);
45
+ if (currentMutation) {
46
+ if (currentMutation && mutation.appliedAt === txn.timestamp) {
47
+ saveMutation(txn, mutation);
48
+ } else if (currentMutation && mutation.debounceCount > currentMutation.debounceCount) {
49
+ saveMutation(txn, mutation);
91
50
  } else {
92
- txn.engineOpts.changeSet.entities.set(ref, document);
93
- return document;
51
+ throw new Error("Mutation already exists");
94
52
  }
95
53
  } else {
96
- txn.engineOpts.entities.set(ref, document);
97
- return document;
54
+ saveMutation(txn, mutation);
98
55
  }
99
- };
100
- // src/functions/deleteEntity.ts
101
- import { DELETED_IN_CHANGE_SET_SYMBOL as DELETED_IN_CHANGE_SET_SYMBOL2 } from "roc-db";
102
- var deleteEntity = (txn, ref) => {
103
- if (txn.request.changeSetRef) {
104
- txn.engineOpts.changeSet.entities.set(ref, DELETED_IN_CHANGE_SET_SYMBOL2);
105
- return true;
106
- } else {
56
+ if (txn.request.changeSetRef)
57
+ return mutation;
58
+ for (const doc of created) {
59
+ txn.engineOpts.entities.set(doc.ref, doc);
60
+ }
61
+ for (const doc of updated) {
62
+ txn.engineOpts.entities.set(doc.ref, doc);
63
+ }
64
+ for (const ref of deleted) {
107
65
  txn.engineOpts.entities.delete(ref);
108
- return true;
109
66
  }
110
- };
111
- // src/functions/getChangeSetMutations.ts
112
- var getChangeSetMutations = (txn, changeSetRef) => {
113
- if (!changeSetRef)
114
- throw new Error("changeSetRef is required");
115
- const res = [];
116
- txn.engineOpts.mutations.forEach((mutation) => {
117
- if (mutation.changeSetRef === changeSetRef) {
118
- res.push(mutation);
119
- }
120
- });
121
- return res;
67
+ return mutation;
122
68
  };
123
69
  // src/functions/findDebounceMutation.ts
124
70
  var findDebounceMutation = (request, engine, now) => {
@@ -132,6 +78,18 @@ var findDebounceMutation = (request, engine, now) => {
132
78
  });
133
79
  return res;
134
80
  };
81
+ // src/functions/getChangeSetMutations.ts
82
+ var getChangeSetMutations = (txn, changeSetRef) => {
83
+ if (!changeSetRef)
84
+ throw new Error("changeSetRef is required");
85
+ const res = [];
86
+ txn.engineOpts.mutations.forEach((mutation) => {
87
+ if (mutation.changeSetRef === changeSetRef) {
88
+ res.push(mutation);
89
+ }
90
+ });
91
+ return res;
92
+ };
135
93
  // src/functions/pageMutations.ts
136
94
  var pageMutations = (txn, {
137
95
  size = 30,
@@ -153,64 +111,10 @@ var pageMutations = (txn, {
153
111
  }
154
112
  return res;
155
113
  };
156
- // src/functions/patchEntity.ts
157
- import { deepPatch, NotFoundError } from "roc-db";
158
- var patchEntity = (txn, ref, { data = {}, children = {}, parents = {}, ancestors = {} }) => {
159
- const existingDocument = readEntity(txn, ref);
160
- if (!existingDocument)
161
- throw new NotFoundError(ref);
162
- const updatedDocument = {
163
- ...existingDocument,
164
- updated: {
165
- timestamp: txn.mutation.timestamp,
166
- mutationRef: txn.mutation.ref
167
- },
168
- data: deepPatch(existingDocument.data, data),
169
- children: deepPatch(existingDocument.children, children),
170
- parents: deepPatch(existingDocument.parents, parents),
171
- ancestors: deepPatch(existingDocument.ancestors, ancestors)
172
- };
173
- if (txn.request.changeSetRef) {
174
- txn.engineOpts.changeSet.entities.set(ref, updatedDocument);
175
- return updatedDocument;
176
- } else {
177
- txn.engineOpts.entities.set(ref, updatedDocument);
178
- return updatedDocument;
179
- }
180
- };
181
114
  // src/functions/readMutation.ts
182
115
  var readMutation = (engineOpts, ref) => {
183
116
  return engineOpts.mutations.get(ref);
184
117
  };
185
- // src/functions/saveMutation.ts
186
- var saveMutation = (txn, finalizedMutation) => {
187
- txn.engineOpts.mutations.set(finalizedMutation.ref, finalizedMutation);
188
- return finalizedMutation;
189
- };
190
- // src/functions/updateEntity.ts
191
- import { NotFoundError as NotFoundError2 } from "roc-db";
192
- var updateEntity = (txn, ref, { data = {}, children = {}, parents = {}, ancestors = {} }) => {
193
- const currentEntity = readEntity(txn, ref);
194
- if (!currentEntity)
195
- throw new NotFoundError2(ref);
196
- const document = {
197
- ...currentEntity,
198
- updated: {
199
- mutationRef: txn.mutation.ref,
200
- timestamp: txn.mutation.timestamp
201
- },
202
- data,
203
- children,
204
- parents,
205
- ancestors
206
- };
207
- if (txn.request.changeSetRef) {
208
- throw new Error("TODO support changeSetRef");
209
- } else {
210
- txn.engineOpts.entities.set(ref, document);
211
- return document;
212
- }
213
- };
214
118
  // src/createInMemoryAdapter.ts
215
119
  var createInMemoryAdapter = ({
216
120
  operations,
@@ -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
+ }) => any;
@@ -1,12 +1,8 @@
1
1
  export { batchReadEntities } from "./batchReadEntities";
2
- export { begin } from "./begin";
3
- export { createEntity } from "./createEntity";
4
- export { deleteEntity } from "./deleteEntity";
5
- export { getChangeSetMutations } from "./getChangeSetMutations";
2
+ export { commit } from "./commit";
6
3
  export { findDebounceMutation } from "./findDebounceMutation";
4
+ export { getChangeSetMutations } from "./getChangeSetMutations";
7
5
  export { pageMutations } from "./pageMutations";
8
- export { patchEntity } from "./patchEntity";
9
6
  export { readEntity } from "./readEntity";
10
7
  export { readMutation } from "./readMutation";
11
8
  export { saveMutation } from "./saveMutation";
12
- export { updateEntity } from "./updateEntity";
@@ -4,4 +4,4 @@ export declare const pageMutations: (txn: ReadTransaction<any, InMemoryEngine, a
4
4
  size?: number;
5
5
  skip?: number;
6
6
  changeSetRef?: string;
7
- }) => import("roc-db").Mutation[];
7
+ }) => any[];
@@ -1 +1,2 @@
1
- export declare const readEntity: (txn: any, ref: any) => any;
1
+ import type { Ref, Transaction } from "roc-db";
2
+ export declare const readEntity: (txn: Transaction, ref: Ref) => any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@roc-db/in-memory",
3
- "version": "0.2.0-pre.33",
3
+ "version": "0.2.0-pre.34",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -13,7 +13,7 @@
13
13
  ],
14
14
  "sideEffects": false,
15
15
  "peerDependencies": {
16
- "roc-db": "0.2.0-pre.33",
16
+ "roc-db": "0.2.0-pre.34",
17
17
  "zod": "^3.0.0"
18
18
  },
19
19
  "keywords": [],
@@ -1 +0,0 @@
1
- export declare const begin: (request: any, engineOpts: any, callback: any) => any;
@@ -1,24 +0,0 @@
1
- import { type Ref } from "roc-db";
2
- import type { InMemoryWriteTransaction } from "../types/InMemoryWriteTransaction";
3
- export declare const createEntity: (txn: InMemoryWriteTransaction, ref: Ref, { data, indexedData, children, parents, ancestors, }: {
4
- data?: {} | undefined;
5
- indexedData?: {} | undefined;
6
- children?: {} | undefined;
7
- parents?: {} | undefined;
8
- ancestors?: {} | undefined;
9
- }) => {
10
- ref: `${string}/${string}`;
11
- entity: string;
12
- created: {
13
- timestamp: any;
14
- mutationRef: any;
15
- };
16
- updated: {
17
- timestamp: any;
18
- mutationRef: any;
19
- };
20
- data: {};
21
- children: {};
22
- parents: {};
23
- ancestors: {};
24
- };
@@ -1 +0,0 @@
1
- export declare const deleteEntity: (txn: any, ref: any) => boolean;
@@ -1,6 +0,0 @@
1
- export declare const patchEntity: (txn: any, ref: any, { data, children, parents, ancestors }: {
2
- data?: {} | undefined;
3
- children?: {} | undefined;
4
- parents?: {} | undefined;
5
- ancestors?: {} | undefined;
6
- }) => 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
- }) => any;