@roc-db/in-memory 0.2.0-pre.97 → 0.2.0-pre.99

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
@@ -55,11 +55,7 @@ var findAddedAndRemovedEntries = (oldArr, newArr) => {
55
55
  const added = oldArr?.filter(([k1, v1]) => !newArr.some(([k2, v2]) => k1 === k2 && v1 === v2));
56
56
  return [added, removed];
57
57
  };
58
- var commit = (txn, mutation, {
59
- created,
60
- updated,
61
- deleted
62
- }) => {
58
+ var commit = (txn, mutation, { created, updated, deleted }) => {
63
59
  const { mutations } = txn.engineOpts;
64
60
  const currentMutation = mutations.get(txn.mutation.ref);
65
61
  if (currentMutation) {
@@ -76,6 +72,9 @@ var commit = (txn, mutation, {
76
72
  if (txn.request.changeSetRef)
77
73
  return mutation;
78
74
  for (const doc of created) {
75
+ if (txn.adapter.models?.[doc.entity]?.singleton && txn.engineOpts.entities.has(doc.ref)) {
76
+ throw createUniqueConstraintConflictError(doc.entity);
77
+ }
79
78
  if (doc.__.unique?.length) {
80
79
  doc.__.unique.forEach(([key, value]) => {
81
80
  const uniqueKey = `${doc.entity}:${key}:${JSON.stringify(value)}`;
@@ -152,11 +151,12 @@ var commit = (txn, mutation, {
152
151
  return mutation;
153
152
  };
154
153
  // src/functions/findDebounceMutation.ts
155
- var findDebounceMutation = (request, engine, now, mutationName) => {
154
+ var findDebounceMutation = (request, engine, now, mutationName, identityRef) => {
156
155
  const debounceTime = request.operation.debounce;
157
- const thresholdTime = new Date(now - debounceTime * 1000).toISOString();
156
+ const thresholdTime = new Date(now.getTime() - debounceTime * 1000).toISOString();
157
+ const payloadRef = request.payload?.ref;
158
158
  const res = engine.mutations.values().find((mutation) => {
159
- if (mutation.operation.name === mutationName && mutation.timestamp > thresholdTime && mutation.payload.ref === request.payload.ref) {
159
+ if (mutation.operation.name === mutationName && mutation.timestamp > thresholdTime && mutation.payload?.ref === payloadRef && mutation.identityRef === identityRef) {
160
160
  return true;
161
161
  }
162
162
  });
@@ -186,7 +186,7 @@ var pageEntities = (txn, args) => {
186
186
  continue;
187
187
  } else {
188
188
  if (childrenOf && childrenOf.length > 0) {
189
- if (!childrenOf.some((childRef) => doc.__.parentRefs.includes(childRef))) {
189
+ if (!childrenOf.some((childRef) => doc.__?.parentRefs?.includes(childRef))) {
190
190
  continue;
191
191
  }
192
192
  }
@@ -203,11 +203,7 @@ var pageEntitiesByIndex = (txn, entity, field, value) => {
203
203
  return refs.map((ref) => txn.engineOpts.entities.get(ref));
204
204
  };
205
205
  // src/functions/pageMutations.ts
206
- var pageMutations = (txn, {
207
- size = 30,
208
- skip = 0,
209
- changeSetRef
210
- } = {}) => {
206
+ var pageMutations = (txn, { size = 30, skip = 0, changeSetRef } = {}) => {
211
207
  const res = [];
212
208
  const iterator = txn.engineOpts.mutations.values().drop(skip);
213
209
  let next = iterator.next();
@@ -241,7 +237,8 @@ var createInMemoryAdapter = ({
241
237
  entities,
242
238
  snowflake = new Snowflake(1, 1),
243
239
  optimistic = true,
244
- session
240
+ session,
241
+ engine
245
242
  }) => {
246
243
  return createAdapter({
247
244
  name: "in-memory",
@@ -251,7 +248,7 @@ var createInMemoryAdapter = ({
251
248
  snowflake,
252
249
  optimistic,
253
250
  session
254
- }, {
251
+ }, engine ?? {
255
252
  entities: new Map,
256
253
  mutations: new Map,
257
254
  entitiesUnique: new Map,
@@ -1,13 +1,14 @@
1
- import { Snowflake, type Adapter, type Operation, type Session } 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, optimistic, session, }: {
1
+ import { Snowflake, type Adapter, type Entity, type Operation, type Session } from "roc-db";
2
+ export declare const createInMemoryAdapter: <const Name extends string, const Operations extends readonly Operation[], const Entities extends readonly Entity<any>[]>({ operations, entities, snowflake, optimistic, session, engine, }: {
7
3
  operations: Operations;
8
4
  entities: Entities;
9
5
  snowflake?: Snowflake;
10
6
  optimistic?: boolean;
11
7
  session: Session;
8
+ engine?: {
9
+ entities: Map<string, any>;
10
+ mutations: Map<string, any>;
11
+ entitiesUnique: Map<string, any>;
12
+ entitiesIndex: Map<string, any>;
13
+ };
12
14
  }) => Adapter;
13
- export {};
@@ -1,2 +1,3 @@
1
- import type { Ref } from "roc-db";
2
- export declare const batchReadEntities: (txn: any, refs: Ref[]) => any[];
1
+ import type { Ref, Transaction } from "roc-db";
2
+ import type { InMemoryEngine } from "../types/InMemoryEngine";
3
+ export declare const batchReadEntities: (txn: Transaction<InMemoryEngine>, refs: Ref[]) => any[];
@@ -1,6 +1,3 @@
1
- import { type Entity, type Mutation, type Ref, type WriteTransaction } from "roc-db";
2
- export declare const commit: (txn: WriteTransaction, mutation: Mutation, { created, updated, deleted, }: {
3
- created: Entity[];
4
- updated: Entity[];
5
- deleted: Ref[];
6
- }) => Mutation;
1
+ import { type CommitFunction } from "roc-db";
2
+ import type { InMemoryEngine } from "../types/InMemoryEngine";
3
+ export declare const commit: CommitFunction<InMemoryEngine>;
@@ -1,3 +1,3 @@
1
- import type { WriteRequest } from "roc-db";
1
+ import type { FindDebounceMutationFunction } from "roc-db";
2
2
  import type { InMemoryEngine } from "../types/InMemoryEngine";
3
- export declare const findDebounceMutation: (request: WriteRequest, engine: InMemoryEngine, now: number, mutationName: string) => import("roc-db").Mutation | undefined;
3
+ export declare const findDebounceMutation: FindDebounceMutationFunction<InMemoryEngine>;
@@ -1 +1,3 @@
1
- export declare const getChangeSetMutations: (txn: any, changeSetRef: any) => any[];
1
+ import type { GetChangeSetMutationsFunction } from "roc-db";
2
+ import type { InMemoryEngine } from "../types/InMemoryEngine";
3
+ export declare const getChangeSetMutations: GetChangeSetMutationsFunction<InMemoryEngine>;
@@ -1 +1,3 @@
1
- export declare const pageEntities: (txn: any, args: any) => any[];
1
+ import type { PageEntitiesFunction } from "roc-db";
2
+ import type { InMemoryEngine } from "../types/InMemoryEngine";
3
+ export declare const pageEntities: PageEntitiesFunction<InMemoryEngine>;
@@ -1 +1,3 @@
1
- export declare const pageEntitiesByIndex: (txn: any, entity: any, field: any, value: any) => any;
1
+ import type { PageEntitiesByIndexFunction } from "roc-db";
2
+ import type { InMemoryEngine } from "../types/InMemoryEngine";
3
+ export declare const pageEntitiesByIndex: PageEntitiesByIndexFunction<InMemoryEngine>;
@@ -1,7 +1,3 @@
1
- import type { ReadTransaction } from "roc-db";
1
+ import type { PageMutationsFunction } from "roc-db";
2
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[];
3
+ export declare const pageMutations: PageMutationsFunction<InMemoryEngine>;
@@ -1,2 +1,3 @@
1
- import type { Ref, Transaction } from "roc-db";
2
- export declare const readEntity: (txn: Transaction, ref: Ref) => any;
1
+ import type { ReadEntityFunctiun } from "roc-db";
2
+ import type { InMemoryEngine } from "../types/InMemoryEngine";
3
+ export declare const readEntity: ReadEntityFunctiun<InMemoryEngine>;
@@ -1 +1,3 @@
1
- export declare const readMutation: (engineOpts: any, ref: any) => any;
1
+ import type { ReadMutationFunction } from "roc-db";
2
+ import type { InMemoryEngine } from "../types/InMemoryEngine";
3
+ export declare const readMutation: ReadMutationFunction<InMemoryEngine>;
@@ -1 +1,3 @@
1
- export declare const refByUniqueField: (txn: WriteTransaction, entity: string, field: string, fieldIndex: number, value: any) => any;
1
+ import type { RefByUniqueFieldFunction } from "roc-db";
2
+ import type { InMemoryEngine } from "../types/InMemoryEngine";
3
+ export declare const refByUniqueField: RefByUniqueFieldFunction<InMemoryEngine>;
@@ -1,2 +1,3 @@
1
- import type { InMemoryWriteTransaction } from "../types/InMemoryWriteTransaction";
2
- export declare const saveMutation: (txn: InMemoryWriteTransaction, finalizedMutation: any) => any;
1
+ import type { SaveMutationFunction } from "roc-db";
2
+ import type { InMemoryEngine } from "../types/InMemoryEngine";
3
+ export declare const saveMutation: SaveMutationFunction<InMemoryEngine>;
@@ -1,5 +1,7 @@
1
- import type { Entity, Ref, Mutation } from "roc-db";
1
+ import type { EntityDocument, Mutation, Ref } from "roc-db";
2
2
  export type InMemoryEngine = {
3
- entities: Map<Ref, Entity>;
3
+ entities: Map<Ref, EntityDocument>;
4
4
  mutations: Map<Ref, Mutation>;
5
+ entitiesUnique: Map<string, Ref>;
6
+ entitiesIndex: Map<string, Ref[]>;
5
7
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@roc-db/in-memory",
3
- "version": "0.2.0-pre.97",
3
+ "version": "0.2.0-pre.99",
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.97",
16
+ "roc-db": ">=0.2.0-pre.98 <1.0.0",
17
17
  "zod": ">=4.0.0"
18
18
  },
19
19
  "keywords": [],
@@ -22,5 +22,9 @@
22
22
  "type": "git",
23
23
  "url": "git+https://github.com/eigilsagafos/roc-db.git"
24
24
  },
25
- "license": "MIT"
25
+ "license": "MIT",
26
+ "publishConfig": {
27
+ "access": "public",
28
+ "registry": "https://registry.npmjs.org/"
29
+ }
26
30
  }
@@ -1,3 +0,0 @@
1
- import type { WriteTransaction } from "roc-db";
2
- import type { InMemoryEngine } from "./InMemoryEngine";
3
- export type InMemoryWriteTransaction = WriteTransaction<any, InMemoryEngine, any, any>;