@roc-db/in-memory 0.2.0-pre.51 → 0.2.0-pre.53

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
@@ -19,9 +19,11 @@ import {
19
19
  var exports_functions = {};
20
20
  __export(exports_functions, {
21
21
  saveMutation: () => saveMutation,
22
+ refByUniqueField: () => refByUniqueField,
22
23
  readMutation: () => readMutation,
23
24
  readEntity: () => readEntity,
24
25
  pageMutations: () => pageMutations,
26
+ pageEntitiesByIndex: () => pageEntitiesByIndex,
25
27
  getChangeSetMutations: () => getChangeSetMutations,
26
28
  findDebounceMutation: () => findDebounceMutation,
27
29
  commit: () => commit,
@@ -35,6 +37,11 @@ var readEntity = (txn, ref) => txn.engineOpts.entities.get(ref);
35
37
  var batchReadEntities = (txn, refs) => {
36
38
  return refs.map((ref) => readEntity(txn, ref));
37
39
  };
40
+ // src/functions/commit.ts
41
+ import {
42
+ createUniqueConstraintConflictError
43
+ } from "roc-db";
44
+
38
45
  // src/functions/saveMutation.ts
39
46
  var saveMutation = (txn, finalizedMutation) => {
40
47
  txn.engineOpts.mutations.set(finalizedMutation.ref, finalizedMutation);
@@ -42,6 +49,11 @@ var saveMutation = (txn, finalizedMutation) => {
42
49
  };
43
50
 
44
51
  // src/functions/commit.ts
52
+ var findAddedAndRemovedEntries = (oldArr, newArr) => {
53
+ const removed = newArr?.filter(([k1, v1]) => !oldArr.some(([k2, v2]) => k1 === k2 && v1 === v2));
54
+ const added = oldArr?.filter(([k1, v1]) => !newArr.some(([k2, v2]) => k1 === k2 && v1 === v2));
55
+ return [added, removed];
56
+ };
45
57
  var commit = (txn, mutation, {
46
58
  created,
47
59
  updated,
@@ -63,15 +75,76 @@ var commit = (txn, mutation, {
63
75
  if (txn.request.changeSetRef)
64
76
  return mutation;
65
77
  for (const doc of created) {
78
+ if (doc.__.unique?.length) {
79
+ doc.__.unique.forEach(([key, value]) => {
80
+ const uniqueKey = `${doc.entity}:${key}:${JSON.stringify(value)}`;
81
+ if (txn.engineOpts.entitiesUnique.has(uniqueKey))
82
+ throw createUniqueConstraintConflictError(doc.entity);
83
+ txn.engineOpts.entitiesUnique.set(uniqueKey, doc.ref);
84
+ });
85
+ }
86
+ if (doc.__.index?.length) {
87
+ doc.__.index.forEach(([key, value]) => {
88
+ const indexKey = `${doc.entity}:${key}:${JSON.stringify(value)}`;
89
+ const arr = txn.engineOpts.entitiesIndex.get(indexKey) ?? [];
90
+ txn.engineOpts.entitiesIndex.set(indexKey, [...arr, doc.ref]);
91
+ });
92
+ }
66
93
  txn.engineOpts.entities.set(doc.ref, doc);
67
94
  }
68
- for (const doc of updated) {
69
- txn.engineOpts.entities.set(doc.ref, doc);
95
+ for (const updatedDocument of updated) {
96
+ const existingDocument = txn.engineOpts.entities.get(updatedDocument.ref);
97
+ if (updatedDocument.__.unique?.length || existingDocument.__.unique?.length) {
98
+ const { ref, entity } = updatedDocument;
99
+ const [added, removed] = findAddedAndRemovedEntries(updatedDocument.__.unique, existingDocument.__.unique);
100
+ removed.forEach(([key, value]) => {
101
+ const uniqueKey = `${updatedDocument.entity}:${key}:${JSON.stringify(value)}`;
102
+ txn.engineOpts.entitiesUnique.delete(uniqueKey);
103
+ });
104
+ added.forEach(([key, value]) => {
105
+ const uniqueKey = `${updatedDocument.entity}:${key}:${JSON.stringify(value)}`;
106
+ if (txn.engineOpts.entitiesUnique.has(uniqueKey))
107
+ throw createUniqueConstraintConflictError(entity);
108
+ txn.engineOpts.entitiesUnique.set(uniqueKey, ref);
109
+ });
110
+ }
111
+ if (updatedDocument.__.index?.length || existingDocument.__.index?.length) {
112
+ const { ref, entity } = updatedDocument;
113
+ const [added, removed] = findAddedAndRemovedEntries(updatedDocument.__.index, existingDocument.__.index);
114
+ removed.forEach(([key, value]) => {
115
+ const indexKey = `${updatedDocument.entity}:${key}:${JSON.stringify(value)}`;
116
+ const arr = txn.engineOpts.entitiesIndex.get(indexKey);
117
+ txn.engineOpts.entitiesIndex.set(indexKey, arr.filter((ref2) => ref2 !== updatedDocument.ref));
118
+ });
119
+ added.forEach(([key, value]) => {
120
+ const indexKey = `${updatedDocument.entity}:${key}:${JSON.stringify(value)}`;
121
+ const arr = txn.engineOpts.entitiesIndex.get(indexKey) ?? [];
122
+ txn.engineOpts.entitiesIndex.set(indexKey, [
123
+ ...arr,
124
+ updatedDocument.ref
125
+ ]);
126
+ });
127
+ }
128
+ txn.engineOpts.entities.set(updatedDocument.ref, updatedDocument);
70
129
  }
71
130
  for (const ref of deleted) {
72
131
  if (ref.startsWith("Mutation/")) {
73
132
  txn.engineOpts.mutations.delete(ref);
74
133
  } else {
134
+ const existingDocument = txn.engineOpts.entities.get(ref);
135
+ if (existingDocument.__.unique?.length) {
136
+ existingDocument.__.unique.forEach(([key, value]) => {
137
+ const uniqueKey = `${existingDocument.entity}:${key}:${JSON.stringify(value)}`;
138
+ txn.engineOpts.entitiesUnique.delete(uniqueKey);
139
+ });
140
+ }
141
+ if (existingDocument.__.index?.length) {
142
+ existingDocument.__.index.forEach(([key, value]) => {
143
+ const indexKey = `${existingDocument.entity}:${key}:${JSON.stringify(value)}`;
144
+ const arr = txn.engineOpts.entitiesIndex.get(indexKey) ?? [];
145
+ txn.engineOpts.entitiesIndex.set(indexKey, arr.filter((ref2) => ref2 !== existingDocument.ref));
146
+ });
147
+ }
75
148
  txn.engineOpts.entities.delete(ref);
76
149
  }
77
150
  }
@@ -100,6 +173,13 @@ var getChangeSetMutations = (txn, changeSetRef) => {
100
173
  });
101
174
  return res;
102
175
  };
176
+ // src/functions/pageEntitiesByIndex.ts
177
+ var pageEntitiesByIndex = (txn, entity, field, value) => {
178
+ const { entitiesIndex } = txn.engineOpts;
179
+ const indexKey = `${entity}:${field}:${JSON.stringify(value)}`;
180
+ const refs = entitiesIndex.get(indexKey) ?? [];
181
+ return refs.map((ref) => txn.engineOpts.entities.get(ref));
182
+ };
103
183
  // src/functions/pageMutations.ts
104
184
  var pageMutations = (txn, {
105
185
  size = 30,
@@ -125,6 +205,14 @@ var pageMutations = (txn, {
125
205
  var readMutation = (engineOpts, ref) => {
126
206
  return engineOpts.mutations.get(ref);
127
207
  };
208
+ // src/functions/refByUniqueField.ts
209
+ var refByUniqueField = (txn, entity, field, fieldIndex, value) => {
210
+ const key = `${entity}:${field}:${JSON.stringify(value)}`;
211
+ const { entitiesUnique } = txn.engineOpts;
212
+ if (entitiesUnique.has(key)) {
213
+ return entitiesUnique.get(key);
214
+ }
215
+ };
128
216
  // src/createInMemoryAdapter.ts
129
217
  var createInMemoryAdapter = ({
130
218
  operations,
@@ -143,7 +231,9 @@ var createInMemoryAdapter = ({
143
231
  session
144
232
  }, {
145
233
  entities: new Map,
146
- mutations: new Map
234
+ mutations: new Map,
235
+ entitiesUnique: new Map,
236
+ entitiesIndex: new Map
147
237
  });
148
238
  };
149
239
  export {
@@ -1,4 +1,4 @@
1
- import type { Entity, Mutation, Ref, WriteTransaction } from "roc-db";
1
+ import { type Entity, type Mutation, type Ref, type WriteTransaction } from "roc-db";
2
2
  export declare const commit: (txn: WriteTransaction, mutation: Mutation, { created, updated, deleted, }: {
3
3
  created: Entity[];
4
4
  updated: Entity[];
@@ -2,7 +2,9 @@ export { batchReadEntities } from "./batchReadEntities";
2
2
  export { commit } from "./commit";
3
3
  export { findDebounceMutation } from "./findDebounceMutation";
4
4
  export { getChangeSetMutations } from "./getChangeSetMutations";
5
+ export { pageEntitiesByIndex } from "./pageEntitiesByIndex";
5
6
  export { pageMutations } from "./pageMutations";
6
7
  export { readEntity } from "./readEntity";
7
8
  export { readMutation } from "./readMutation";
9
+ export { refByUniqueField } from "./refByUniqueField";
8
10
  export { saveMutation } from "./saveMutation";
@@ -0,0 +1 @@
1
+ export declare const pageEntitiesByIndex: (txn: any, entity: any, field: any, value: any) => any;
@@ -0,0 +1 @@
1
+ export declare const refByUniqueField: (txn: WriteTransaction, entity: string, field: string, fieldIndex: number, value: any) => any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@roc-db/in-memory",
3
- "version": "0.2.0-pre.51",
3
+ "version": "0.2.0-pre.53",
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.51",
16
+ "roc-db": "0.2.0-pre.53",
17
17
  "zod": "^3.0.0"
18
18
  },
19
19
  "keywords": [],