@roc-db/in-memory 0.2.0-pre.4 → 0.2.0-pre.40

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,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
- deleteEntity: () => deleteEntity,
27
- createEntity: () => createEntity,
28
- begin: () => begin
24
+ commit: () => commit,
25
+ batchReadEntities: () => batchReadEntities
29
26
  });
30
27
 
31
- // src/functions/begin.ts
32
- var begin = (request, engineOpts) => {
33
- return {
34
- ...engineOpts,
35
- changeSet: request.changeSetRef ? {
36
- entities: new Map([]),
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/createEntity.ts
42
- import { entityFromRef } from "roc-db";
43
- var createEntity = (txn, ref, {
44
- data = {},
45
- indexedData = {},
46
- children = {},
47
- parents = {},
48
- ancestors = {}
49
- }) => {
50
- const entity = entityFromRef(ref);
51
- const document = {
52
- ref,
53
- entity,
54
- created: {
55
- timestamp: txn.mutation.timestamp,
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
- txn.engineOpts.changeSet.entities.set(ref, document);
75
- return document;
51
+ throw new Error("Mutation already exists");
76
52
  }
77
53
  } else {
78
- txn.engineOpts.entities.set(ref, document);
79
- return document;
54
+ saveMutation(txn, mutation);
80
55
  }
81
- };
82
- // src/functions/deleteEntity.ts
83
- import { DELETED_IN_CHANGE_SET_SYMBOL } from "roc-db";
84
- var deleteEntity = (txn, ref) => {
85
- if (txn.request.changeSetRef) {
86
- txn.engineOpts.changeSet.entities.set(ref, DELETED_IN_CHANGE_SET_SYMBOL);
87
- return true;
88
- } 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) {
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, { size = 30, skip = 0 } = {}) => {
119
- return txn.engineOpts.mutations.values().drop(skip).take(size).toArray();
120
- };
121
- // src/functions/patchEntity.ts
122
- import { deepPatch, NotFoundError } from "roc-db";
123
-
124
- // src/functions/readEntity.ts
125
- import { DELETED_IN_CHANGE_SET_SYMBOL as DELETED_IN_CHANGE_SET_SYMBOL2 } from "roc-db";
126
- var readEntity = (txn, ref) => {
127
- if (txn.request.changeSetRef) {
128
- const doc = txn.engineOpts.changeSet.entities.get(ref);
129
- if (doc === DELETED_IN_CHANGE_SET_SYMBOL2)
130
- return;
131
- if (doc)
132
- return doc;
133
- }
134
- return txn.engineOpts.entities.get(ref);
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,2 @@
1
+ import type { Ref } from "roc-db";
2
+ export declare const batchReadEntities: (txn: any, refs: Ref[]) => any[];
@@ -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;
@@ -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,2 @@
1
+ import type { Ref, Transaction } from "roc-db";
2
+ export declare const readEntity: (txn: Transaction, ref: Ref) => any;
@@ -0,0 +1 @@
1
+ export declare const readMutation: (engineOpts: any, ref: any) => any;
@@ -0,0 +1,2 @@
1
+ import type { InMemoryWriteTransaction } from "../types/InMemoryWriteTransaction";
2
+ export declare const saveMutation: (txn: InMemoryWriteTransaction, finalizedMutation: any) => any;
@@ -0,0 +1 @@
1
+ export { createInMemoryAdapter } from "./createInMemoryAdapter";
@@ -0,0 +1,5 @@
1
+ import type { Entity, Ref, Mutation } from "roc-db";
2
+ export type InMemoryEngine = {
3
+ entities: Map<Ref, Entity>;
4
+ mutations: Map<Ref, Mutation>;
5
+ };
@@ -0,0 +1,3 @@
1
+ import type { WriteTransaction } from "roc-db";
2
+ import type { InMemoryEngine } from "./InMemoryEngine";
3
+ export type InMemoryWriteTransaction = WriteTransaction<any, InMemoryEngine, any, any>;
package/package.json CHANGED
@@ -1,27 +1,20 @@
1
1
  {
2
2
  "name": "@roc-db/in-memory",
3
- "version": "0.2.0-pre.4",
3
+ "version": "0.2.0-pre.40",
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
- "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.4",
24
- "zod": "3.24.2"
16
+ "roc-db": "0.2.0-pre.40",
17
+ "zod": "^3.0.0"
25
18
  },
26
19
  "keywords": [],
27
20
  "author": "Eigil Sagafos",