@roc-db/in-memory 0.1.0-pre.2

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 ADDED
@@ -0,0 +1,213 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, {
5
+ get: all[name],
6
+ enumerable: true,
7
+ configurable: true,
8
+ set: (newValue) => all[name] = () => newValue
9
+ });
10
+ };
11
+
12
+ // src/createInMemoryAdapter.ts
13
+ import { createAdapter, Snowflake } from "roc-db";
14
+
15
+ // src/functions/index.ts
16
+ var exports_functions = {};
17
+ __export(exports_functions, {
18
+ updateEntity: () => updateEntity,
19
+ saveMutation: () => saveMutation,
20
+ readMutation: () => readMutation,
21
+ readEntity: () => readEntity,
22
+ patchEntity: () => patchEntity,
23
+ pageMutations: () => pageMutations,
24
+ getChangeSetMutations: () => getChangeSetMutations,
25
+ findDebounceMutation: () => findDebounceMutation,
26
+ deleteEntity: () => deleteEntity,
27
+ createEntity: () => createEntity,
28
+ begin: () => begin
29
+ });
30
+
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
+ };
40
+ };
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");
73
+ } else {
74
+ txn.engineOpts.changeSet.entities.set(ref, document);
75
+ return document;
76
+ }
77
+ } else {
78
+ txn.engineOpts.entities.set(ref, document);
79
+ return document;
80
+ }
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 {
89
+ txn.engineOpts.entities.delete(ref);
90
+ return true;
91
+ }
92
+ };
93
+ // src/functions/getChangeSetMutations.ts
94
+ var getChangeSetMutations = (txn, changeSetRef) => {
95
+ if (!changeSetRef)
96
+ throw new Error("changeSetRef is required");
97
+ const res = [];
98
+ txn.engineOpts.mutations.forEach((mutation) => {
99
+ if (mutation.changeSetRef === changeSetRef) {
100
+ res.push(mutation);
101
+ }
102
+ });
103
+ return res;
104
+ };
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
+ // 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;
159
+ }
160
+ };
161
+ // src/functions/readMutation.ts
162
+ var readMutation = (engineOpts, ref) => {
163
+ return engineOpts.mutations.get(ref);
164
+ };
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
+ // src/createInMemoryAdapter.ts
195
+ var createInMemoryAdapter = ({
196
+ operations,
197
+ entities,
198
+ snowflake = new Snowflake(1, 1)
199
+ }) => {
200
+ return createAdapter({
201
+ name: "in-memory",
202
+ operations,
203
+ entities,
204
+ functions: exports_functions,
205
+ snowflake
206
+ }, {
207
+ entities: new Map,
208
+ mutations: new Map
209
+ });
210
+ };
211
+ export {
212
+ createInMemoryAdapter
213
+ };
@@ -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, }: {
7
+ operations: Operations;
8
+ entities: Entities;
9
+ snowflake?: Snowflake;
10
+ }) => Adapter;
11
+ export {};
@@ -0,0 +1 @@
1
+ export declare const begin: (request: any, engineOpts: any) => any;
@@ -0,0 +1,24 @@
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
+ };
@@ -0,0 +1 @@
1
+ export declare const deleteEntity: (txn: any, ref: any) => boolean;
@@ -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) => import("roc-db").Mutation | undefined;
@@ -0,0 +1 @@
1
+ export declare const getChangeSetMutations: (txn: any, changeSetRef: any) => any[];
@@ -0,0 +1,11 @@
1
+ export { begin } from "./begin";
2
+ export { createEntity } from "./createEntity";
3
+ export { deleteEntity } from "./deleteEntity";
4
+ export { getChangeSetMutations } from "./getChangeSetMutations";
5
+ export { findDebounceMutation } from "./findDebounceMutation";
6
+ export { pageMutations } from "./pageMutations";
7
+ export { patchEntity } from "./patchEntity";
8
+ export { readEntity } from "./readEntity";
9
+ export { readMutation } from "./readMutation";
10
+ export { saveMutation } from "./saveMutation";
11
+ export { updateEntity } from "./updateEntity";
@@ -0,0 +1,6 @@
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 }?: {
4
+ size?: number;
5
+ skip?: number;
6
+ }) => import("roc-db").Mutation[];
@@ -0,0 +1,6 @@
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;
@@ -0,0 +1 @@
1
+ export declare const readEntity: (txn: any, ref: any) => 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,6 @@
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;
@@ -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 ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@roc-db/in-memory",
3
+ "version": "0.1.0-pre.2",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": "./src/index.ts"
7
+ },
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "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.1.0-pre.1",
19
+ "roc-db": "0.1.0-pre.1",
20
+ "zod": "3.24.2"
21
+ },
22
+ "peerDependencies": {
23
+ "roc-db": "0.1.0-pre.1",
24
+ "zod": "3.24.2"
25
+ },
26
+ "keywords": [],
27
+ "author": "Eigil Sagafos",
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/eigilsagafos/roc-db.git"
31
+ },
32
+ "license": "MIT"
33
+ }