@roc-db/in-memory 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 +70 -143
- package/dist/types/createInMemoryAdapter.d.ts +11 -0
- package/dist/types/functions/batchReadEntities.d.ts +2 -0
- package/dist/types/functions/commit.d.ts +5 -0
- package/dist/types/functions/findDebounceMutation.d.ts +3 -0
- package/dist/types/functions/getChangeSetMutations.d.ts +1 -0
- package/dist/types/functions/index.d.ts +8 -0
- package/dist/types/functions/pageMutations.d.ts +7 -0
- package/dist/types/functions/readEntity.d.ts +2 -0
- package/dist/types/functions/readMutation.d.ts +1 -0
- package/dist/types/functions/saveMutation.d.ts +2 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/types/InMemoryEngine.d.ts +5 -0
- package/dist/types/types/InMemoryWriteTransaction.d.ts +3 -0
- package/package.json +7 -14
- package/dist/tarball/roc-db-in-memory-0.2.0-pre.4.tgz +0 -0
package/dist/index.js
CHANGED
|
@@ -15,80 +15,67 @@ 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
|
-
|
|
27
|
-
|
|
28
|
-
begin: () => begin
|
|
24
|
+
commit: () => commit,
|
|
25
|
+
batchReadEntities: () => batchReadEntities
|
|
29
26
|
});
|
|
30
27
|
|
|
31
|
-
// src/functions/
|
|
32
|
-
var
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
mutations: new Map([])
|
|
38
|
-
} : null
|
|
39
|
-
};
|
|
28
|
+
// src/functions/readEntity.ts
|
|
29
|
+
var readEntity = (txn, ref) => txn.engineOpts.entities.get(ref);
|
|
30
|
+
|
|
31
|
+
// src/functions/batchReadEntities.ts
|
|
32
|
+
var batchReadEntities = (txn, refs) => {
|
|
33
|
+
return refs.map((ref) => readEntity(txn, ref));
|
|
40
34
|
};
|
|
41
|
-
// src/functions/
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
mutationRef: txn.mutation.ref
|
|
57
|
-
},
|
|
58
|
-
updated: {
|
|
59
|
-
timestamp: txn.mutation.timestamp,
|
|
60
|
-
mutationRef: txn.mutation.ref
|
|
61
|
-
},
|
|
62
|
-
data: {
|
|
63
|
-
...data,
|
|
64
|
-
...indexedData
|
|
65
|
-
},
|
|
66
|
-
children,
|
|
67
|
-
parents,
|
|
68
|
-
ancestors
|
|
69
|
-
};
|
|
70
|
-
if (txn.request.changeSetRef) {
|
|
71
|
-
if (txn.engineOpts.changeSet.entities.has(ref)) {
|
|
72
|
-
throw new Error("Entity already exists in changeSet");
|
|
35
|
+
// src/functions/saveMutation.ts
|
|
36
|
+
var saveMutation = (txn, finalizedMutation) => {
|
|
37
|
+
txn.engineOpts.mutations.set(finalizedMutation.ref, finalizedMutation);
|
|
38
|
+
return finalizedMutation;
|
|
39
|
+
};
|
|
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);
|
|
73
50
|
} else {
|
|
74
|
-
|
|
75
|
-
return document;
|
|
51
|
+
throw new Error("Mutation already exists");
|
|
76
52
|
}
|
|
77
53
|
} else {
|
|
78
|
-
txn
|
|
79
|
-
return document;
|
|
54
|
+
saveMutation(txn, mutation);
|
|
80
55
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
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) {
|
|
89
65
|
txn.engineOpts.entities.delete(ref);
|
|
90
|
-
return true;
|
|
91
66
|
}
|
|
67
|
+
return mutation;
|
|
68
|
+
};
|
|
69
|
+
// src/functions/findDebounceMutation.ts
|
|
70
|
+
var findDebounceMutation = (request, engine, now, mutationName) => {
|
|
71
|
+
const debounceTime = request.settings.debounce;
|
|
72
|
+
const thresholdTime = new Date(now - debounceTime * 1000).toISOString();
|
|
73
|
+
const res = engine.mutations.values().find((mutation) => {
|
|
74
|
+
if (mutation.operation.name === mutationName && mutation.timestamp > thresholdTime && mutation.payload.ref === request.payload.ref) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
return res;
|
|
92
79
|
};
|
|
93
80
|
// src/functions/getChangeSetMutations.ts
|
|
94
81
|
var getChangeSetMutations = (txn, changeSetRef) => {
|
|
@@ -102,107 +89,47 @@ var getChangeSetMutations = (txn, changeSetRef) => {
|
|
|
102
89
|
});
|
|
103
90
|
return res;
|
|
104
91
|
};
|
|
105
|
-
// src/functions/findDebounceMutation.ts
|
|
106
|
-
var findDebounceMutation = (request, engine, now) => {
|
|
107
|
-
const debounceTime = request.settings.debounce;
|
|
108
|
-
const mutationName = request.schema.shape.name.value;
|
|
109
|
-
const thresholdTime = new Date(now - debounceTime * 1000).toISOString();
|
|
110
|
-
const res = engine.mutations.values().find((mutation) => {
|
|
111
|
-
if (mutation.name === mutationName && mutation.timestamp > thresholdTime && mutation.payload.ref === request.payload.ref) {
|
|
112
|
-
return true;
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
return res;
|
|
116
|
-
};
|
|
117
92
|
// src/functions/pageMutations.ts
|
|
118
|
-
var pageMutations = (txn, {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
// src/functions/patchEntity.ts
|
|
138
|
-
var patchEntity = (txn, ref, { data = {}, children = {}, parents = {}, ancestors = {} }) => {
|
|
139
|
-
const existingDocument = readEntity(txn, ref);
|
|
140
|
-
if (!existingDocument)
|
|
141
|
-
throw new NotFoundError(ref);
|
|
142
|
-
const updatedDocument = {
|
|
143
|
-
...existingDocument,
|
|
144
|
-
updated: {
|
|
145
|
-
timestamp: txn.mutation.timestamp,
|
|
146
|
-
mutationRef: txn.mutation.ref
|
|
147
|
-
},
|
|
148
|
-
data: deepPatch(existingDocument.data, data),
|
|
149
|
-
children: deepPatch(existingDocument.children, children),
|
|
150
|
-
parents: deepPatch(existingDocument.parents, parents),
|
|
151
|
-
ancestors: deepPatch(existingDocument.ancestors, ancestors)
|
|
152
|
-
};
|
|
153
|
-
if (txn.request.changeSetRef) {
|
|
154
|
-
txn.engineOpts.changeSet.entities.set(ref, updatedDocument);
|
|
155
|
-
return updatedDocument;
|
|
156
|
-
} else {
|
|
157
|
-
txn.engineOpts.entities.set(ref, updatedDocument);
|
|
158
|
-
return updatedDocument;
|
|
93
|
+
var pageMutations = (txn, {
|
|
94
|
+
size = 30,
|
|
95
|
+
skip = 0,
|
|
96
|
+
changeSetRef
|
|
97
|
+
} = {}) => {
|
|
98
|
+
const res = [];
|
|
99
|
+
const iterator = txn.engineOpts.mutations.values().drop(skip);
|
|
100
|
+
let next = iterator.next();
|
|
101
|
+
while (!next.done && res.length < size) {
|
|
102
|
+
if (changeSetRef) {
|
|
103
|
+
if (next.value.changeSetRef === changeSetRef) {
|
|
104
|
+
res.push(next.value);
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
res.push(next.value);
|
|
108
|
+
}
|
|
109
|
+
next = iterator.next();
|
|
159
110
|
}
|
|
111
|
+
return res;
|
|
160
112
|
};
|
|
161
113
|
// src/functions/readMutation.ts
|
|
162
114
|
var readMutation = (engineOpts, ref) => {
|
|
163
115
|
return engineOpts.mutations.get(ref);
|
|
164
116
|
};
|
|
165
|
-
// src/functions/saveMutation.ts
|
|
166
|
-
var saveMutation = (txn, finalizedMutation) => {
|
|
167
|
-
txn.engineOpts.mutations.set(finalizedMutation.ref, finalizedMutation);
|
|
168
|
-
return finalizedMutation;
|
|
169
|
-
};
|
|
170
|
-
// src/functions/updateEntity.ts
|
|
171
|
-
import { NotFoundError as NotFoundError2 } from "roc-db";
|
|
172
|
-
var updateEntity = (txn, ref, { data = {}, children = {}, parents = {}, ancestors = {} }) => {
|
|
173
|
-
const currentEntity = readEntity(txn, ref);
|
|
174
|
-
if (!currentEntity)
|
|
175
|
-
throw new NotFoundError2(ref);
|
|
176
|
-
const document = {
|
|
177
|
-
...currentEntity,
|
|
178
|
-
updated: {
|
|
179
|
-
mutationRef: txn.mutation.ref,
|
|
180
|
-
timestamp: txn.mutation.timestamp
|
|
181
|
-
},
|
|
182
|
-
data,
|
|
183
|
-
children,
|
|
184
|
-
parents,
|
|
185
|
-
ancestors
|
|
186
|
-
};
|
|
187
|
-
if (txn.request.changeSetRef) {
|
|
188
|
-
throw new Error("TODO support changeSetRef");
|
|
189
|
-
} else {
|
|
190
|
-
txn.engineOpts.entities.set(ref, document);
|
|
191
|
-
return document;
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
117
|
// src/createInMemoryAdapter.ts
|
|
195
118
|
var createInMemoryAdapter = ({
|
|
196
119
|
operations,
|
|
197
120
|
entities,
|
|
198
|
-
snowflake = new Snowflake(1, 1)
|
|
121
|
+
snowflake = new Snowflake(1, 1),
|
|
122
|
+
session,
|
|
123
|
+
optimistic = true
|
|
199
124
|
}) => {
|
|
200
125
|
return createAdapter({
|
|
201
126
|
name: "in-memory",
|
|
202
127
|
operations,
|
|
203
128
|
entities,
|
|
204
129
|
functions: exports_functions,
|
|
205
|
-
snowflake
|
|
130
|
+
snowflake,
|
|
131
|
+
session,
|
|
132
|
+
optimistic
|
|
206
133
|
}, {
|
|
207
134
|
entities: new Map,
|
|
208
135
|
mutations: new Map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Snowflake, type Adapter, type Operation } from "roc-db";
|
|
2
|
+
import type { z } from "zod";
|
|
3
|
+
type Entity = z.ZodObject<{
|
|
4
|
+
entity: z.ZodLiteral<string>;
|
|
5
|
+
}>;
|
|
6
|
+
export declare const createInMemoryAdapter: <const Name extends string, const Operations extends readonly Operation[], const Entities extends readonly Entity[]>({ operations, entities, snowflake, session, optimistic, }: {
|
|
7
|
+
operations: Operations;
|
|
8
|
+
entities: Entities;
|
|
9
|
+
snowflake?: Snowflake;
|
|
10
|
+
}) => Adapter;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { WriteOperation } from "roc-db";
|
|
2
|
+
import type { InMemoryEngine } from "../types/InMemoryEngine";
|
|
3
|
+
export declare const findDebounceMutation: (request: WriteOperation, engine: InMemoryEngine, now: any, mutationName: string) => import("roc-db").Mutation | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getChangeSetMutations: (txn: any, changeSetRef: any) => any[];
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { batchReadEntities } from "./batchReadEntities";
|
|
2
|
+
export { commit } from "./commit";
|
|
3
|
+
export { findDebounceMutation } from "./findDebounceMutation";
|
|
4
|
+
export { getChangeSetMutations } from "./getChangeSetMutations";
|
|
5
|
+
export { pageMutations } from "./pageMutations";
|
|
6
|
+
export { readEntity } from "./readEntity";
|
|
7
|
+
export { readMutation } from "./readMutation";
|
|
8
|
+
export { saveMutation } from "./saveMutation";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ReadTransaction } from "roc-db";
|
|
2
|
+
import type { InMemoryEngine } from "../types/InMemoryEngine";
|
|
3
|
+
export declare const pageMutations: (txn: ReadTransaction<any, InMemoryEngine, any, any>, { size, skip, changeSetRef, }?: {
|
|
4
|
+
size?: number;
|
|
5
|
+
skip?: number;
|
|
6
|
+
changeSetRef?: string;
|
|
7
|
+
}) => any[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const readMutation: (engineOpts: any, ref: any) => any;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createInMemoryAdapter } from "./createInMemoryAdapter";
|
package/package.json
CHANGED
|
@@ -1,27 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@roc-db/in-memory",
|
|
3
|
-
"version": "0.2.0-pre.
|
|
3
|
+
"version": "0.2.0-pre.41",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
|
-
".":
|
|
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
|
-
"devDependencies": {
|
|
18
|
-
"@roc-db/test-utils": "0.2.0-pre.4",
|
|
19
|
-
"roc-db": "0.2.0-pre.4",
|
|
20
|
-
"zod": "3.24.2"
|
|
21
|
-
},
|
|
22
15
|
"peerDependencies": {
|
|
23
|
-
"roc-db": "0.2.0-pre.
|
|
24
|
-
"zod": "3.
|
|
16
|
+
"roc-db": "0.2.0-pre.41",
|
|
17
|
+
"zod": "^3.0.0"
|
|
25
18
|
},
|
|
26
19
|
"keywords": [],
|
|
27
20
|
"author": "Eigil Sagafos",
|
|
Binary file
|