zodvex 0.2.0 → 0.2.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 CHANGED
@@ -1,37 +1,22 @@
1
1
  'use strict';
2
2
 
3
+ var customFunctions = require('convex-helpers/server/customFunctions');
3
4
  var zod = require('zod');
4
5
  var values = require('convex/values');
5
- var customFunctions = require('convex-helpers/server/customFunctions');
6
6
  var server = require('convex-helpers/server');
7
7
 
8
- var __defProp = Object.defineProperty;
9
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
10
- var __getOwnPropNames = Object.getOwnPropertyNames;
11
- var __hasOwnProp = Object.prototype.hasOwnProperty;
12
- var __esm = (fn, res) => function __init() {
13
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
14
- };
15
- var __export = (target, all) => {
16
- for (var name in all)
17
- __defProp(target, name, { get: all[name], enumerable: true });
18
- };
19
- var __copyProps = (to, from, except, desc) => {
20
- if (from && typeof from === "object" || typeof from === "function") {
21
- for (let key of __getOwnPropNames(from))
22
- if (!__hasOwnProp.call(to, key) && key !== except)
23
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
- }
25
- return to;
8
+ var metadata = /* @__PURE__ */ new WeakMap();
9
+ var registryHelpers = {
10
+ getMetadata: (type) => metadata.get(type),
11
+ setMetadata: (type, meta) => metadata.set(type, meta)
26
12
  };
27
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
13
  function zid(tableName) {
29
14
  const baseSchema = zod.z.string().refine((val) => typeof val === "string" && val.length > 0, {
30
15
  message: `Invalid ID for table "${tableName}"`
31
16
  }).transform((val) => {
32
17
  return val;
33
18
  }).brand(`ConvexId_${tableName}`).describe(`convexId:${tableName}`);
34
- exports.registryHelpers.setMetadata(baseSchema, {
19
+ registryHelpers.setMetadata(baseSchema, {
35
20
  isConvexId: true,
36
21
  tableName
37
22
  });
@@ -39,22 +24,29 @@ function zid(tableName) {
39
24
  branded._tableName = tableName;
40
25
  return branded;
41
26
  }
42
- var metadata; exports.registryHelpers = void 0;
43
- var init_ids = __esm({
44
- "src/ids.ts"() {
45
- metadata = /* @__PURE__ */ new WeakMap();
46
- exports.registryHelpers = {
47
- getMetadata: (type) => metadata.get(type),
48
- setMetadata: (type, meta) => metadata.set(type, meta)
49
- };
50
- }
51
- });
27
+ var baseCodecs = [];
52
28
  function registerBaseCodec(codec) {
53
29
  baseCodecs.unshift(codec);
54
30
  }
55
31
  function findBaseCodec(schema) {
56
32
  return baseCodecs.find((codec) => codec.check(schema));
57
33
  }
34
+ registerBaseCodec({
35
+ check: (schema) => schema instanceof zod.z.ZodDate,
36
+ toValidator: () => values.v.float64(),
37
+ fromConvex: (value) => {
38
+ if (typeof value === "number") {
39
+ return new Date(value);
40
+ }
41
+ return value;
42
+ },
43
+ toConvex: (value) => {
44
+ if (value instanceof Date) {
45
+ return value.getTime();
46
+ }
47
+ return value;
48
+ }
49
+ });
58
50
  function asZodType(schema) {
59
51
  return schema;
60
52
  }
@@ -65,28 +57,6 @@ function isDateSchema(schema) {
65
57
  }
66
58
  return false;
67
59
  }
68
- var baseCodecs;
69
- var init_registry = __esm({
70
- "src/registry.ts"() {
71
- baseCodecs = [];
72
- registerBaseCodec({
73
- check: (schema) => schema instanceof zod.z.ZodDate,
74
- toValidator: () => values.v.float64(),
75
- fromConvex: (value) => {
76
- if (typeof value === "number") {
77
- return new Date(value);
78
- }
79
- return value;
80
- },
81
- toConvex: (value) => {
82
- if (value instanceof Date) {
83
- return value.getTime();
84
- }
85
- return value;
86
- }
87
- });
88
- }
89
- });
90
60
  function convertEnumType(actualValidator) {
91
61
  const options = actualValidator.options;
92
62
  if (options && Array.isArray(options) && options.length > 0) {
@@ -108,10 +78,6 @@ function convertEnumType(actualValidator) {
108
78
  return values.v.any();
109
79
  }
110
80
  }
111
- var init_enum = __esm({
112
- "src/mapping/handlers/enum.ts"() {
113
- }
114
- });
115
81
  function convertNullableType(actualValidator, visited, zodToConvexInternal2) {
116
82
  const innerSchema = actualValidator.unwrap();
117
83
  if (innerSchema && innerSchema instanceof zod.z.ZodType) {
@@ -137,10 +103,6 @@ function convertNullableType(actualValidator, visited, zodToConvexInternal2) {
137
103
  };
138
104
  }
139
105
  }
140
- var init_nullable = __esm({
141
- "src/mapping/handlers/nullable.ts"() {
142
- }
143
- });
144
106
  function convertRecordType(actualValidator, visited, zodToConvexInternal2) {
145
107
  let valueType = actualValidator._def?.valueType;
146
108
  if (!valueType) {
@@ -179,10 +141,6 @@ function convertRecordType(actualValidator, visited, zodToConvexInternal2) {
179
141
  return values.v.record(values.v.string(), values.v.any());
180
142
  }
181
143
  }
182
- var init_record = __esm({
183
- "src/mapping/handlers/record.ts"() {
184
- }
185
- });
186
144
  function convertDiscriminatedUnionType(actualValidator, visited, zodToConvexInternal2) {
187
145
  const options = actualValidator.def?.options || actualValidator.def?.optionsMap?.values();
188
146
  if (options) {
@@ -226,22 +184,8 @@ function convertUnionType(actualValidator, visited, zodToConvexInternal2) {
226
184
  return values.v.any();
227
185
  }
228
186
  }
229
- var init_union = __esm({
230
- "src/mapping/handlers/union.ts"() {
231
- }
232
- });
233
-
234
- // src/mapping/handlers/index.ts
235
- var init_handlers = __esm({
236
- "src/mapping/handlers/index.ts"() {
237
- init_enum();
238
- init_nullable();
239
- init_record();
240
- init_union();
241
- }
242
- });
243
187
  function isZid(schema) {
244
- const metadata2 = exports.registryHelpers.getMetadata(schema);
188
+ const metadata2 = registryHelpers.getMetadata(schema);
245
189
  return metadata2?.isConvexId === true && metadata2?.tableName && typeof metadata2.tableName === "string";
246
190
  }
247
191
  function makeUnion(members) {
@@ -259,12 +203,12 @@ function getObjectShape(obj) {
259
203
  }
260
204
  return {};
261
205
  }
262
- var init_utils = __esm({
263
- "src/mapping/utils.ts"() {
264
- init_ids();
265
- }
266
- });
206
+
207
+ // src/mapping/core.ts
267
208
  function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set()) {
209
+ if (!zodValidator) {
210
+ return values.v.any();
211
+ }
268
212
  if (visited.has(zodValidator)) {
269
213
  return values.v.any();
270
214
  }
@@ -289,7 +233,7 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
289
233
  }
290
234
  let convexValidator;
291
235
  if (isZid(actualValidator)) {
292
- const metadata2 = exports.registryHelpers.getMetadata(actualValidator);
236
+ const metadata2 = registryHelpers.getMetadata(actualValidator);
293
237
  const tableName = metadata2?.tableName || "unknown";
294
238
  convexValidator = values.v.id(tableName);
295
239
  } else {
@@ -395,7 +339,7 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
395
339
  if (codec) {
396
340
  convexValidator = codec.toValidator(actualValidator);
397
341
  } else {
398
- const metadata2 = exports.registryHelpers.getMetadata(actualValidator);
342
+ const metadata2 = registryHelpers.getMetadata(actualValidator);
399
343
  if (metadata2?.brand && metadata2?.originalSchema) {
400
344
  convexValidator = zodToConvexInternal(metadata2.originalSchema, visited);
401
345
  } else {
@@ -470,6 +414,13 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
470
414
  convexValidator = values.v.any();
471
415
  break;
472
416
  default:
417
+ if (process.env.NODE_ENV !== "production") {
418
+ console.warn(
419
+ `[zodvex] Unrecognized Zod type "${defType}" encountered. Falling back to v.any().`,
420
+ "Schema:",
421
+ actualValidator
422
+ );
423
+ }
473
424
  convexValidator = values.v.any();
474
425
  break;
475
426
  }
@@ -494,33 +445,8 @@ function zodToConvexFields(zod$1) {
494
445
  }
495
446
  return result;
496
447
  }
497
- var init_core = __esm({
498
- "src/mapping/core.ts"() {
499
- init_ids();
500
- init_registry();
501
- init_handlers();
502
- init_utils();
503
- }
504
- });
505
-
506
- // src/mapping/index.ts
507
- var mapping_exports = {};
508
- __export(mapping_exports, {
509
- getObjectShape: () => getObjectShape,
510
- makeUnion: () => makeUnion,
511
- zodToConvex: () => zodToConvex,
512
- zodToConvexFields: () => zodToConvexFields
513
- });
514
- var init_mapping = __esm({
515
- "src/mapping/index.ts"() {
516
- init_core();
517
- init_utils();
518
- }
519
- });
520
448
 
521
449
  // src/codec.ts
522
- init_mapping();
523
- init_registry();
524
450
  function asZodType2(schema) {
525
451
  return schema;
526
452
  }
@@ -667,7 +593,6 @@ function fromConvexJS(value, schema) {
667
593
  }
668
594
  return value;
669
595
  }
670
- init_mapping();
671
596
  function pick(obj, keys) {
672
597
  const result = {};
673
598
  for (const key of keys) {
@@ -728,8 +653,7 @@ function toKeys(mask) {
728
653
  }
729
654
  function pickShape(schemaOrShape, mask) {
730
655
  const keys = toKeys(mask);
731
- const { getObjectShape: getObjectShape3 } = (init_mapping(), __toCommonJS(mapping_exports));
732
- const shape = schemaOrShape instanceof zod.z.ZodObject ? getObjectShape3(schemaOrShape) : schemaOrShape || {};
656
+ const shape = schemaOrShape instanceof zod.z.ZodObject ? getObjectShape(schemaOrShape) : schemaOrShape || {};
733
657
  const out = {};
734
658
  for (const k of keys) {
735
659
  if (k in shape) out[k] = shape[k];
@@ -740,8 +664,7 @@ function safePick(schema, mask) {
740
664
  return zod.z.object(pickShape(schema, mask));
741
665
  }
742
666
  function safeOmit(schema, mask) {
743
- const { getObjectShape: getObjectShape3 } = (init_mapping(), __toCommonJS(mapping_exports));
744
- const shape = getObjectShape3(schema);
667
+ const shape = getObjectShape(schema);
745
668
  const omit = new Set(toKeys(mask));
746
669
  const keep = Object.keys(shape).filter((k) => !omit.has(k));
747
670
  const picked = pickShape(schema, keep);
@@ -789,10 +712,9 @@ function customFnBuilder(builder, customization) {
789
712
  handleZodValidationError(parsed.error, "args");
790
713
  }
791
714
  const finalCtx = { ...ctx, ...added?.ctx ?? {} };
792
- const finalArgs = {
793
- ...parsed.data,
794
- ...added?.args ?? {}
795
- };
715
+ const baseArgs = parsed.data;
716
+ const addedArgs = added?.args ?? {};
717
+ const finalArgs = { ...baseArgs, ...addedArgs };
796
718
  const ret = await handler(finalCtx, finalArgs);
797
719
  if (returns && !fn.skipConvexValidation) {
798
720
  let validated;
@@ -850,24 +772,6 @@ function customFnBuilder(builder, customization) {
850
772
  function zCustomQuery(query, customization) {
851
773
  return customFnBuilder(query, customization);
852
774
  }
853
- function zStrictQuery(query, customization) {
854
- return customFnBuilder(
855
- query,
856
- customization
857
- );
858
- }
859
- function zStrictMutation(mutation, customization) {
860
- return customFnBuilder(
861
- mutation,
862
- customization
863
- );
864
- }
865
- function zStrictAction(action, customization) {
866
- return customFnBuilder(
867
- action,
868
- customization
869
- );
870
- }
871
775
  function zCustomMutation(mutation, customization) {
872
776
  return customFnBuilder(
873
777
  mutation,
@@ -880,66 +784,28 @@ function zCustomAction(action, customization) {
880
784
  customization
881
785
  );
882
786
  }
883
-
884
- // src/index.ts
885
- init_ids();
886
- init_mapping();
887
- init_registry();
888
-
889
- // src/tables.ts
890
- init_ids();
891
- init_mapping();
892
- function zodDoc(tableName, schema) {
893
- return schema.extend({
894
- _id: zid(tableName),
895
- _creationTime: zod.z.number()
896
- });
897
- }
898
- function zodDocOrNull(tableName, schema) {
899
- return zod.z.union([zodDoc(tableName, schema), zod.z.null()]);
900
- }
901
- function zodTable(name, shape) {
902
- const convexFields = zodToConvexFields(shape);
903
- const table = server.Table(name, convexFields);
904
- return Object.assign(table, {
905
- shape,
906
- zDoc: zodDoc(name, zod.z.object(shape))
907
- });
908
- }
909
- function zodTableWithDocs(name, schema) {
910
- const convexFields = zodToConvexFields(schema.shape);
911
- const shape = getObjectShape(schema);
912
- const mapped = {};
913
- for (const [k, field] of Object.entries(shape)) {
914
- mapped[k] = mapDateFieldToNumber(field);
915
- }
916
- const docSchema = zod.z.object({
917
- ...mapped,
918
- _id: zid(name),
919
- _creationTime: zod.z.number()
920
- });
921
- const docArray = zod.z.array(docSchema);
922
- const base = server.Table(name, convexFields);
923
- return { ...base, schema, docSchema, docArray };
924
- }
925
- init_mapping();
926
787
  var customCheckCache = /* @__PURE__ */ new WeakMap();
927
- function containsCustom(schema) {
788
+ function containsCustom(schema, maxDepth = 50, currentDepth = 0) {
928
789
  const cached = customCheckCache.get(schema);
929
790
  if (cached !== void 0) {
930
791
  return cached;
931
792
  }
793
+ if (currentDepth > maxDepth) {
794
+ return false;
795
+ }
932
796
  let result = false;
933
797
  if (schema._def?.typeName === "ZodCustom") {
934
798
  result = true;
935
799
  } else if (schema instanceof zod.z.ZodUnion) {
936
- result = schema.options.some(containsCustom);
800
+ result = schema.options.some(
801
+ (opt) => containsCustom(opt, maxDepth, currentDepth + 1)
802
+ );
937
803
  } else if (schema instanceof zod.z.ZodOptional) {
938
- result = containsCustom(schema.unwrap());
804
+ result = containsCustom(schema.unwrap(), maxDepth, currentDepth + 1);
939
805
  } else if (schema instanceof zod.z.ZodNullable) {
940
- result = containsCustom(schema.unwrap());
806
+ result = containsCustom(schema.unwrap(), maxDepth, currentDepth + 1);
941
807
  } else if (schema instanceof zod.z.ZodDefault) {
942
- result = containsCustom(schema.removeDefault());
808
+ result = containsCustom(schema.removeDefault(), maxDepth, currentDepth + 1);
943
809
  }
944
810
  customCheckCache.set(schema, result);
945
811
  return result;
@@ -983,9 +849,6 @@ function zQuery(query, input, handler, options) {
983
849
  }
984
850
  });
985
851
  }
986
- function zInternalQuery(internalQuery, input, handler, options) {
987
- return zQuery(internalQuery, input, handler, options);
988
- }
989
852
  function zMutation(mutation, input, handler, options) {
990
853
  let zodSchema;
991
854
  let args;
@@ -1025,9 +888,6 @@ function zMutation(mutation, input, handler, options) {
1025
888
  }
1026
889
  });
1027
890
  }
1028
- function zInternalMutation(internalMutation, input, handler, options) {
1029
- return zMutation(internalMutation, input, handler, options);
1030
- }
1031
891
  function zAction(action, input, handler, options) {
1032
892
  let zodSchema;
1033
893
  let args;
@@ -1067,11 +927,74 @@ function zAction(action, input, handler, options) {
1067
927
  }
1068
928
  });
1069
929
  }
1070
- function zInternalAction(internalAction, input, handler, options) {
1071
- return zAction(internalAction, input, handler, options);
930
+
931
+ // src/builders.ts
932
+ function zQueryBuilder(builder) {
933
+ return (config) => {
934
+ return zQuery(builder, config.args ?? {}, config.handler, {
935
+ returns: config.returns
936
+ });
937
+ };
938
+ }
939
+ function zMutationBuilder(builder) {
940
+ return (config) => {
941
+ return zMutation(builder, config.args ?? {}, config.handler, {
942
+ returns: config.returns
943
+ });
944
+ };
945
+ }
946
+ function zActionBuilder(builder) {
947
+ return (config) => {
948
+ return zAction(builder, config.args ?? {}, config.handler, {
949
+ returns: config.returns
950
+ });
951
+ };
952
+ }
953
+ function zCustomQueryBuilder(query, customization) {
954
+ return customFnBuilder(
955
+ query,
956
+ customization
957
+ );
958
+ }
959
+ function zCustomMutationBuilder(mutation, customization) {
960
+ return customFnBuilder(
961
+ mutation,
962
+ customization
963
+ );
964
+ }
965
+ function zCustomActionBuilder(action, customization) {
966
+ return customFnBuilder(
967
+ action,
968
+ customization
969
+ );
970
+ }
971
+ function zodDoc(tableName, schema) {
972
+ return schema.extend({
973
+ _id: zid(tableName),
974
+ _creationTime: zod.z.number()
975
+ });
976
+ }
977
+ function zodDocOrNull(tableName, schema) {
978
+ return zod.z.union([zodDoc(tableName, schema), zod.z.null()]);
979
+ }
980
+ function zodTable(name, shape) {
981
+ const convexFields = zodToConvexFields(shape);
982
+ const table = server.Table(name, convexFields);
983
+ const zDoc = zodDoc(name, zod.z.object(shape));
984
+ const docArray = zod.z.array(zDoc);
985
+ return Object.assign(table, {
986
+ shape,
987
+ zDoc,
988
+ docArray
989
+ });
1072
990
  }
1073
991
 
992
+ Object.defineProperty(exports, "customCtx", {
993
+ enumerable: true,
994
+ get: function () { return customFunctions.customCtx; }
995
+ });
1074
996
  exports.convexCodec = convexCodec;
997
+ exports.customFnBuilder = customFnBuilder;
1075
998
  exports.findBaseCodec = findBaseCodec;
1076
999
  exports.formatZodIssues = formatZodIssues;
1077
1000
  exports.fromConvexJS = fromConvexJS;
@@ -1083,28 +1006,25 @@ exports.mapDateFieldToNumber = mapDateFieldToNumber;
1083
1006
  exports.pick = pick;
1084
1007
  exports.pickShape = pickShape;
1085
1008
  exports.registerBaseCodec = registerBaseCodec;
1009
+ exports.registryHelpers = registryHelpers;
1086
1010
  exports.returnsAs = returnsAs;
1087
1011
  exports.safeOmit = safeOmit;
1088
1012
  exports.safePick = safePick;
1089
1013
  exports.toConvexJS = toConvexJS;
1090
- exports.zAction = zAction;
1014
+ exports.zActionBuilder = zActionBuilder;
1091
1015
  exports.zCustomAction = zCustomAction;
1016
+ exports.zCustomActionBuilder = zCustomActionBuilder;
1092
1017
  exports.zCustomMutation = zCustomMutation;
1018
+ exports.zCustomMutationBuilder = zCustomMutationBuilder;
1093
1019
  exports.zCustomQuery = zCustomQuery;
1094
- exports.zInternalAction = zInternalAction;
1095
- exports.zInternalMutation = zInternalMutation;
1096
- exports.zInternalQuery = zInternalQuery;
1097
- exports.zMutation = zMutation;
1020
+ exports.zCustomQueryBuilder = zCustomQueryBuilder;
1021
+ exports.zMutationBuilder = zMutationBuilder;
1098
1022
  exports.zPaginated = zPaginated;
1099
- exports.zQuery = zQuery;
1100
- exports.zStrictAction = zStrictAction;
1101
- exports.zStrictMutation = zStrictMutation;
1102
- exports.zStrictQuery = zStrictQuery;
1023
+ exports.zQueryBuilder = zQueryBuilder;
1103
1024
  exports.zid = zid;
1104
1025
  exports.zodDoc = zodDoc;
1105
1026
  exports.zodDocOrNull = zodDocOrNull;
1106
1027
  exports.zodTable = zodTable;
1107
- exports.zodTableWithDocs = zodTableWithDocs;
1108
1028
  exports.zodToConvex = zodToConvex;
1109
1029
  exports.zodToConvexFields = zodToConvexFields;
1110
1030
  //# sourceMappingURL=index.js.map