pocketbase-zod-schema 0.1.2 → 0.1.3

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.
Files changed (63) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +329 -99
  3. package/dist/cli/index.cjs +176 -55
  4. package/dist/cli/index.cjs.map +1 -1
  5. package/dist/cli/index.js +176 -55
  6. package/dist/cli/index.js.map +1 -1
  7. package/dist/cli/migrate.cjs +196 -58
  8. package/dist/cli/migrate.cjs.map +1 -1
  9. package/dist/cli/migrate.js +194 -57
  10. package/dist/cli/migrate.js.map +1 -1
  11. package/dist/cli/utils/index.cjs +1 -1
  12. package/dist/cli/utils/index.cjs.map +1 -1
  13. package/dist/cli/utils/index.js +1 -1
  14. package/dist/cli/utils/index.js.map +1 -1
  15. package/dist/index.cjs +197 -96
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.cts +3 -3
  18. package/dist/index.d.ts +3 -3
  19. package/dist/index.js +197 -95
  20. package/dist/index.js.map +1 -1
  21. package/dist/migration/analyzer.cjs +101 -28
  22. package/dist/migration/analyzer.cjs.map +1 -1
  23. package/dist/migration/analyzer.js +101 -28
  24. package/dist/migration/analyzer.js.map +1 -1
  25. package/dist/migration/generator.cjs +60 -25
  26. package/dist/migration/generator.cjs.map +1 -1
  27. package/dist/migration/generator.d.cts +9 -5
  28. package/dist/migration/generator.d.ts +9 -5
  29. package/dist/migration/generator.js +60 -25
  30. package/dist/migration/generator.js.map +1 -1
  31. package/dist/migration/index.cjs +162 -53
  32. package/dist/migration/index.cjs.map +1 -1
  33. package/dist/migration/index.js +162 -53
  34. package/dist/migration/index.js.map +1 -1
  35. package/dist/migration/snapshot.cjs +1 -0
  36. package/dist/migration/snapshot.cjs.map +1 -1
  37. package/dist/migration/snapshot.js +1 -0
  38. package/dist/migration/snapshot.js.map +1 -1
  39. package/dist/migration/utils/index.cjs +19 -17
  40. package/dist/migration/utils/index.cjs.map +1 -1
  41. package/dist/migration/utils/index.d.cts +3 -1
  42. package/dist/migration/utils/index.d.ts +3 -1
  43. package/dist/migration/utils/index.js +19 -17
  44. package/dist/migration/utils/index.js.map +1 -1
  45. package/dist/mutator.cjs +9 -11
  46. package/dist/mutator.cjs.map +1 -1
  47. package/dist/mutator.d.cts +5 -9
  48. package/dist/mutator.d.ts +5 -9
  49. package/dist/mutator.js +9 -11
  50. package/dist/mutator.js.map +1 -1
  51. package/dist/schema.cjs +50 -53
  52. package/dist/schema.cjs.map +1 -1
  53. package/dist/schema.d.cts +94 -12
  54. package/dist/schema.d.ts +94 -12
  55. package/dist/schema.js +50 -52
  56. package/dist/schema.js.map +1 -1
  57. package/dist/types.d.cts +2 -5
  58. package/dist/types.d.ts +2 -5
  59. package/dist/user-C39DQ40N.d.cts +53 -0
  60. package/dist/user-C39DQ40N.d.ts +53 -0
  61. package/package.json +2 -3
  62. package/dist/user-jS1aYoeD.d.cts +0 -123
  63. package/dist/user-jS1aYoeD.d.ts +0 -123
@@ -26,6 +26,35 @@ var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
26
26
  var path__namespace = /*#__PURE__*/_interopNamespace(path);
27
27
 
28
28
  // src/migration/analyzer.ts
29
+ ({
30
+ id: zod.z.string().describe("unique id"),
31
+ collectionId: zod.z.string().describe("collection id"),
32
+ collectionName: zod.z.string().describe("collection name"),
33
+ expand: zod.z.record(zod.z.any()).describe("expandable fields")
34
+ });
35
+ ({
36
+ created: zod.z.string().describe("creation timestamp"),
37
+ updated: zod.z.string().describe("last update timestamp")
38
+ });
39
+ ({
40
+ thumbnailURL: zod.z.string().optional(),
41
+ imageFiles: zod.z.array(zod.z.string())
42
+ });
43
+ ({
44
+ imageFiles: zod.z.array(zod.z.instanceof(File))
45
+ });
46
+ var RELATION_METADATA_KEY = "__pocketbase_relation__";
47
+ function extractRelationMetadata(description) {
48
+ if (!description) return null;
49
+ try {
50
+ const parsed = JSON.parse(description);
51
+ if (parsed[RELATION_METADATA_KEY]) {
52
+ return parsed[RELATION_METADATA_KEY];
53
+ }
54
+ } catch {
55
+ }
56
+ return null;
57
+ }
29
58
 
30
59
  // src/migration/errors.ts
31
60
  var MigrationError = class _MigrationError extends Error {
@@ -100,7 +129,7 @@ Cause: ${this.originalError.message}`);
100
129
  }
101
130
  };
102
131
 
103
- // src/schema/permission-templates.ts
132
+ // src/utils/permission-templates.ts
104
133
  var PermissionTemplates = {
105
134
  /**
106
135
  * Public access - anyone can perform all operations
@@ -759,26 +788,28 @@ function getMaxSelect(fieldName, zodType) {
759
788
  return 1;
760
789
  }
761
790
  function getMinSelect(fieldName, zodType) {
762
- if (!isMultipleRelationField(fieldName, zodType)) {
763
- return void 0;
764
- }
765
- let unwrappedType = zodType;
766
- if (zodType instanceof zod.z.ZodOptional) {
767
- unwrappedType = zodType._def.innerType;
768
- }
769
- if (unwrappedType instanceof zod.z.ZodNullable) {
770
- unwrappedType = unwrappedType._def.innerType;
771
- }
772
- if (unwrappedType instanceof zod.z.ZodDefault) {
773
- unwrappedType = unwrappedType._def.innerType;
791
+ if (isSingleRelationField(fieldName, zodType)) {
792
+ return 0;
774
793
  }
775
- if (unwrappedType instanceof zod.z.ZodArray) {
776
- const arrayDef = unwrappedType._def;
777
- if (arrayDef.minLength) {
778
- return arrayDef.minLength.value;
794
+ if (isMultipleRelationField(fieldName, zodType)) {
795
+ let unwrappedType = zodType;
796
+ if (zodType instanceof zod.z.ZodOptional) {
797
+ unwrappedType = zodType._def.innerType;
798
+ }
799
+ if (unwrappedType instanceof zod.z.ZodNullable) {
800
+ unwrappedType = unwrappedType._def.innerType;
801
+ }
802
+ if (unwrappedType instanceof zod.z.ZodDefault) {
803
+ unwrappedType = unwrappedType._def.innerType;
804
+ }
805
+ if (unwrappedType instanceof zod.z.ZodArray) {
806
+ const arrayDef = unwrappedType._def;
807
+ if (arrayDef.minLength) {
808
+ return arrayDef.minLength.value;
809
+ }
779
810
  }
780
811
  }
781
- return void 0;
812
+ return 0;
782
813
  }
783
814
  function mapZodStringType(zodType) {
784
815
  const checks = zodType._def.checks || [];
@@ -1036,13 +1067,32 @@ async function importSchemaModule(filePath, config) {
1036
1067
  if (config?.pathTransformer) {
1037
1068
  importPath = config.pathTransformer(filePath);
1038
1069
  }
1039
- if (!importPath.endsWith(".js")) {
1040
- importPath = `${importPath}.js`;
1070
+ let resolvedPath = null;
1071
+ const jsPath = `${importPath}.js`;
1072
+ const tsPath = `${importPath}.ts`;
1073
+ if (fs__namespace.existsSync(jsPath)) {
1074
+ resolvedPath = jsPath;
1075
+ } else if (fs__namespace.existsSync(tsPath)) {
1076
+ resolvedPath = tsPath;
1077
+ } else {
1078
+ resolvedPath = jsPath;
1041
1079
  }
1042
- const fileUrl = new URL(`file://${path__namespace.resolve(importPath)}`);
1080
+ const fileUrl = new URL(`file://${path__namespace.resolve(resolvedPath)}`);
1043
1081
  const module = await import(fileUrl.href);
1044
1082
  return module;
1045
1083
  } catch (error) {
1084
+ const tsPath = `${filePath}.ts`;
1085
+ const isTypeScriptFile = fs__namespace.existsSync(tsPath);
1086
+ if (isTypeScriptFile) {
1087
+ throw new SchemaParsingError(
1088
+ `Failed to import TypeScript schema file. Node.js cannot import TypeScript files directly.
1089
+ Please either:
1090
+ 1. Compile your schema files to JavaScript first, or
1091
+ 2. Use tsx to run the migration tool (e.g., "npx tsx package/dist/cli/migrate.js status" or "tsx package/dist/cli/migrate.js status")`,
1092
+ filePath,
1093
+ error
1094
+ );
1095
+ }
1046
1096
  throw new SchemaParsingError(
1047
1097
  `Failed to import schema module. Make sure the schema files are compiled to JavaScript.`,
1048
1098
  filePath,
@@ -1105,7 +1155,17 @@ function buildFieldDefinition(fieldName, zodType) {
1105
1155
  required,
1106
1156
  options
1107
1157
  };
1108
- if (isRelationField(fieldName, zodType)) {
1158
+ const relationMetadata = extractRelationMetadata(zodType.description);
1159
+ if (relationMetadata) {
1160
+ fieldDef.type = "relation";
1161
+ fieldDef.relation = {
1162
+ collection: relationMetadata.collection,
1163
+ maxSelect: relationMetadata.maxSelect,
1164
+ minSelect: relationMetadata.minSelect,
1165
+ cascadeDelete: relationMetadata.cascadeDelete
1166
+ };
1167
+ fieldDef.options = void 0;
1168
+ } else if (isRelationField(fieldName, zodType)) {
1109
1169
  fieldDef.type = "relation";
1110
1170
  const targetCollection = resolveTargetCollection(fieldName);
1111
1171
  const maxSelect = getMaxSelect(fieldName, zodType);
@@ -1117,6 +1177,13 @@ function buildFieldDefinition(fieldName, zodType) {
1117
1177
  cascadeDelete: false
1118
1178
  // Default to false, can be configured later
1119
1179
  };
1180
+ if (fieldDef.options) {
1181
+ const { min, max, pattern, ...relationSafeOptions } = fieldDef.options;
1182
+ console.log("min", min);
1183
+ console.log("max", max);
1184
+ console.log("pattern", pattern);
1185
+ fieldDef.options = Object.keys(relationSafeOptions).length > 0 ? relationSafeOptions : void 0;
1186
+ }
1120
1187
  }
1121
1188
  return fieldDef;
1122
1189
  }
@@ -1169,11 +1236,12 @@ function convertZodSchemaToCollectionSchema(collectionName, zodSchema) {
1169
1236
  fields,
1170
1237
  indexes,
1171
1238
  rules: {
1172
- listRule: null,
1173
- viewRule: null,
1174
- createRule: null,
1175
- updateRule: null,
1176
- deleteRule: null
1239
+ listRule: permissions?.listRule ?? null,
1240
+ viewRule: permissions?.viewRule ?? null,
1241
+ createRule: permissions?.createRule ?? null,
1242
+ updateRule: permissions?.updateRule ?? null,
1243
+ deleteRule: permissions?.deleteRule ?? null,
1244
+ manageRule: permissions?.manageRule ?? null
1177
1245
  },
1178
1246
  permissions
1179
1247
  };
@@ -1197,7 +1265,12 @@ async function buildSchemaDefinition(config) {
1197
1265
  if (normalizedConfig.pathTransformer) {
1198
1266
  importPath = normalizedConfig.pathTransformer(filePath);
1199
1267
  } else if (mergedConfig.useCompiledFiles) {
1200
- importPath = filePath.replace(/\/src\//, "/dist/");
1268
+ const distPath = filePath.replace(/\/src\//, "/dist/");
1269
+ if (fs__namespace.existsSync(`${distPath}.js`) || fs__namespace.existsSync(`${distPath}.mjs`)) {
1270
+ importPath = distPath;
1271
+ } else {
1272
+ importPath = filePath;
1273
+ }
1201
1274
  }
1202
1275
  const module = await importSchemaModule(importPath, normalizedConfig);
1203
1276
  const schemas = extractSchemaDefinitions(module, mergedConfig.schemaPatterns);