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

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,10 +75,57 @@ 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/")) {
@@ -100,6 +159,13 @@ var getChangeSetMutations = (txn, changeSetRef) => {
100
159
  });
101
160
  return res;
102
161
  };
162
+ // src/functions/pageEntitiesByIndex.ts
163
+ var pageEntitiesByIndex = (txn, entity, field, value) => {
164
+ const { entitiesIndex } = txn.engineOpts;
165
+ const indexKey = `${entity}:${field}:${JSON.stringify(value)}`;
166
+ const refs = entitiesIndex.get(indexKey) ?? [];
167
+ return refs.map((ref) => txn.engineOpts.entities.get(ref));
168
+ };
103
169
  // src/functions/pageMutations.ts
104
170
  var pageMutations = (txn, {
105
171
  size = 30,
@@ -125,6 +191,14 @@ var pageMutations = (txn, {
125
191
  var readMutation = (engineOpts, ref) => {
126
192
  return engineOpts.mutations.get(ref);
127
193
  };
194
+ // src/functions/refByUniqueField.ts
195
+ var refByUniqueField = (txn, entity, field, fieldIndex, value) => {
196
+ const key = `${entity}:${field}:${JSON.stringify(value)}`;
197
+ const { entitiesUnique } = txn.engineOpts;
198
+ if (entitiesUnique.has(key)) {
199
+ return entitiesUnique.get(key);
200
+ }
201
+ };
128
202
  // src/createInMemoryAdapter.ts
129
203
  var createInMemoryAdapter = ({
130
204
  operations,
@@ -143,7 +217,9 @@ var createInMemoryAdapter = ({
143
217
  session
144
218
  }, {
145
219
  entities: new Map,
146
- mutations: new Map
220
+ mutations: new Map,
221
+ entitiesUnique: new Map,
222
+ entitiesIndex: new Map
147
223
  });
148
224
  };
149
225
  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.52",
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.52",
17
17
  "zod": "^3.0.0"
18
18
  },
19
19
  "keywords": [],