rads-db 3.0.33 → 3.0.36

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.cjs CHANGED
@@ -22,7 +22,14 @@ function generateValidators(schema) {
22
22
  const result = {};
23
23
  for (const key in zodSchemas) {
24
24
  result[key] = (value) => {
25
- value = zodSchemas[key].parse(value);
25
+ try {
26
+ value = zodSchemas[key].parse(value);
27
+ } catch (error) {
28
+ if (error instanceof zod.ZodError)
29
+ throw new ValidationError(error);
30
+ else
31
+ throw error;
32
+ }
26
33
  fillDefaultValues(schema, key, value);
27
34
  return value;
28
35
  };
@@ -100,6 +107,13 @@ function getFieldZodSchemaBase(zodSchemas, schema, field, shouldBeLazy) {
100
107
  }
101
108
  throw new Error(`Unknown type: ${field.type}`);
102
109
  }
110
+ class ValidationError extends Error {
111
+ constructor(error) {
112
+ super(error.errors[0].message);
113
+ this.message = error.errors[0].message;
114
+ this.issues = error.errors;
115
+ }
116
+ }
103
117
 
104
118
  const operatorFns = {
105
119
  eq: (x, w) => x === w,
package/dist/index.d.ts CHANGED
@@ -209,7 +209,7 @@ interface TypeDefinition {
209
209
  handle?: string;
210
210
  handlePlural?: string;
211
211
  isExtending?: string;
212
- keepHistoryFields: string[];
212
+ keepHistoryFields?: string[];
213
213
  }
214
214
  interface FileUploadResult {
215
215
  url: string;
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { z } from 'zod';
1
+ import { z, ZodError } from 'zod';
2
2
  import _ from 'lodash';
3
3
  import { v4 } from 'uuid';
4
4
  import createMerge from '@fastify/deepmerge';
@@ -14,7 +14,14 @@ function generateValidators(schema) {
14
14
  const result = {};
15
15
  for (const key in zodSchemas) {
16
16
  result[key] = (value) => {
17
- value = zodSchemas[key].parse(value);
17
+ try {
18
+ value = zodSchemas[key].parse(value);
19
+ } catch (error) {
20
+ if (error instanceof ZodError)
21
+ throw new ValidationError(error);
22
+ else
23
+ throw error;
24
+ }
18
25
  fillDefaultValues(schema, key, value);
19
26
  return value;
20
27
  };
@@ -92,6 +99,13 @@ function getFieldZodSchemaBase(zodSchemas, schema, field, shouldBeLazy) {
92
99
  }
93
100
  throw new Error(`Unknown type: ${field.type}`);
94
101
  }
102
+ class ValidationError extends Error {
103
+ constructor(error) {
104
+ super(error.errors[0].message);
105
+ this.message = error.errors[0].message;
106
+ this.issues = error.errors;
107
+ }
108
+ }
95
109
 
96
110
  const operatorFns = {
97
111
  eq: (x, w) => x === w,
@@ -68,8 +68,9 @@ function verifyEventSourcingSetup(schema, effects) {
68
68
  id: aggId
69
69
  }, ev.change));
70
70
  ev.beforeChange = aggDoc;
71
+ const originalChange = ev.change;
71
72
  ev.change = (0, _radsDb.diff)(newAggDoc, aggDoc);
72
- keepHistory(schema[entityName].keepHistoryFields, newAggDoc, ev);
73
+ keepHistory(schema[entityName].keepHistoryFields, originalChange, ev);
73
74
  aggDoc = newAggDoc;
74
75
  if (!context.options.keepNulls) (0, _radsDb.cleanUndefinedAndNull)(aggDoc);
75
76
  }
@@ -97,11 +98,11 @@ function verifyEventSourcingSetup(schema, effects) {
97
98
  });
98
99
  }
99
100
  }
100
- function keepHistory(keepHistoryFields, newAggDoc, eventChange) {
101
- if (keepHistoryFields && newAggDoc) {
101
+ function keepHistory(keepHistoryFields, originalChange, changeEvent) {
102
+ if (keepHistoryFields && originalChange) {
102
103
  keepHistoryFields.forEach(prop => {
103
- if (!eventChange.change[prop]) {
104
- eventChange.change[prop] = newAggDoc[prop];
104
+ if (!changeEvent.change[prop] && originalChange[prop] !== void 0) {
105
+ changeEvent.change[prop] = originalChange[prop];
105
106
  }
106
107
  });
107
108
  }
@@ -62,8 +62,9 @@ function verifyEventSourcingSetup(schema, effects) {
62
62
  for (const ev of events) {
63
63
  const newAggDoc = context.validators[entityName](merge(aggDoc || { id: aggId }, ev.change));
64
64
  ev.beforeChange = aggDoc;
65
+ const originalChange = ev.change;
65
66
  ev.change = diff(newAggDoc, aggDoc);
66
- keepHistory(schema[entityName].keepHistoryFields, newAggDoc, ev);
67
+ keepHistory(schema[entityName].keepHistoryFields, originalChange, ev);
67
68
  aggDoc = newAggDoc;
68
69
  if (!context.options.keepNulls)
69
70
  cleanUndefinedAndNull(aggDoc);
@@ -85,11 +86,11 @@ function verifyEventSourcingSetup(schema, effects) {
85
86
  });
86
87
  }
87
88
  }
88
- function keepHistory(keepHistoryFields, newAggDoc, eventChange) {
89
- if (keepHistoryFields && newAggDoc) {
89
+ function keepHistory(keepHistoryFields, originalChange, changeEvent) {
90
+ if (keepHistoryFields && originalChange) {
90
91
  keepHistoryFields.forEach((prop) => {
91
- if (!eventChange.change[prop]) {
92
- eventChange.change[prop] = newAggDoc[prop];
92
+ if (!changeEvent.change[prop] && originalChange[prop] !== void 0) {
93
+ changeEvent.change[prop] = originalChange[prop];
93
94
  }
94
95
  });
95
96
  }
@@ -52,7 +52,9 @@ async function getSchema(normalizedOptions) {
52
52
  } = normalizedOptions;
53
53
  let schema;
54
54
  if (entitiesDir) {
55
- if (!_nodeFs.default.existsSync(entitiesDir)) await _promises.default.mkdir(entitiesDir);
55
+ if (!_nodeFs.default.existsSync(entitiesDir)) await _promises.default.mkdir(entitiesDir, {
56
+ recursive: true
57
+ });
56
58
  const response = await _promises.default.readdir(entitiesDir, {
57
59
  withFileTypes: true
58
60
  });
@@ -49,7 +49,7 @@ export async function getSchema(normalizedOptions) {
49
49
  let schema;
50
50
  if (entitiesDir) {
51
51
  if (!fs2.existsSync(entitiesDir))
52
- await fs.mkdir(entitiesDir);
52
+ await fs.mkdir(entitiesDir, { recursive: true });
53
53
  const response = await fs.readdir(entitiesDir, { withFileTypes: true });
54
54
  const entities = {};
55
55
  for (const file of response) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rads-db",
3
- "version": "3.0.33",
3
+ "version": "3.0.36",
4
4
  "files": [
5
5
  "dist",
6
6
  "drivers",
@@ -46,9 +46,9 @@
46
46
  "author": "",
47
47
  "license": "ISC",
48
48
  "peerDependencies": {
49
- "@supabase/supabase-js": ">=2",
50
49
  "@azure/cosmos": ">=3",
51
50
  "@azure/storage-blob": ">=12",
51
+ "@supabase/supabase-js": ">=2",
52
52
  "dexie": ">=3",
53
53
  "h3": ">=1.6.0",
54
54
  "typescript": ">=5.0.0"
@@ -72,8 +72,8 @@
72
72
  },
73
73
  "packageManager": "pnpm@8.6.1",
74
74
  "devDependencies": {
75
- "@supabase/supabase-js": "^2.39.0",
76
75
  "@azure/cosmos": "^3.17.3",
76
+ "@supabase/supabase-js": "^2.39.0",
77
77
  "@types/eslint": "^8.44.8",
78
78
  "@types/klaw": "^3.0.3",
79
79
  "@types/lodash": "4.14.191",
@@ -87,6 +87,7 @@
87
87
  "rads-db": "link:.",
88
88
  "simple-git-hooks": "^2.8.1",
89
89
  "symlink-dir": "^5.1.1",
90
+ "ts-node": "^10.9.2",
90
91
  "tsup": "^6.7.0",
91
92
  "unbuild": "^1.2.1",
92
93
  "vite": "^4.0.0",
@@ -105,7 +106,7 @@
105
106
  "scripts": {
106
107
  "test": "vitest --run && vitest typecheck --run",
107
108
  "link-rads-db": "symlink-dir ./test/_rads-db ./node_modules/_rads-db",
108
- "generate-test-schema": "rads-db -d './test/entities'",
109
+ "generate-test-schema": "ts-node ./src/integrations/cli -d './test/entities'",
109
110
  "dev": "pnpm install && pnpm link-rads-db && pnpm build && pnpm generate-test-schema && vitest --ui --test-timeout 300000",
110
111
  "lint": "cross-env NODE_ENV=production eslint src/**/*.* test/**/*.*",
111
112
  "dev-typecheck": "pnpm install && pnpm build && pnpm generate-test-schema && pnpm link-rads-db && vitest typecheck --ui",