pocketbase-zod-schema 0.1.4 → 0.2.1

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 (45) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +233 -98
  3. package/dist/cli/index.cjs +45 -11
  4. package/dist/cli/index.cjs.map +1 -1
  5. package/dist/cli/index.js +45 -11
  6. package/dist/cli/index.js.map +1 -1
  7. package/dist/cli/migrate.cjs +45 -11
  8. package/dist/cli/migrate.cjs.map +1 -1
  9. package/dist/cli/migrate.js +45 -11
  10. package/dist/cli/migrate.js.map +1 -1
  11. package/dist/index.cjs +86 -27
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.d.cts +2 -2
  14. package/dist/index.d.ts +2 -2
  15. package/dist/index.js +81 -26
  16. package/dist/index.js.map +1 -1
  17. package/dist/migration/analyzer.cjs +46 -11
  18. package/dist/migration/analyzer.cjs.map +1 -1
  19. package/dist/migration/analyzer.d.cts +11 -1
  20. package/dist/migration/analyzer.d.ts +11 -1
  21. package/dist/migration/analyzer.js +46 -12
  22. package/dist/migration/analyzer.js.map +1 -1
  23. package/dist/migration/index.cjs +45 -11
  24. package/dist/migration/index.cjs.map +1 -1
  25. package/dist/migration/index.js +45 -11
  26. package/dist/migration/index.js.map +1 -1
  27. package/dist/migration/snapshot.cjs.map +1 -1
  28. package/dist/migration/snapshot.js.map +1 -1
  29. package/dist/mutator.cjs +20 -21
  30. package/dist/mutator.cjs.map +1 -1
  31. package/dist/mutator.d.cts +2 -2
  32. package/dist/mutator.d.ts +2 -2
  33. package/dist/mutator.js +20 -21
  34. package/dist/mutator.js.map +1 -1
  35. package/dist/schema.cjs +41 -16
  36. package/dist/schema.cjs.map +1 -1
  37. package/dist/schema.d.cts +98 -8
  38. package/dist/schema.d.ts +98 -8
  39. package/dist/schema.js +36 -15
  40. package/dist/schema.js.map +1 -1
  41. package/dist/types.d.cts +1 -1
  42. package/dist/types.d.ts +1 -1
  43. package/dist/{user-_AM523hb.d.cts → user-DTJQIj4K.d.cts} +31 -5
  44. package/dist/{user-_AM523hb.d.ts → user-DTJQIj4K.d.ts} +31 -5
  45. package/package.json +2 -1
@@ -61,6 +61,7 @@ interface SchemaAnalyzerConfig {
61
61
  declare function discoverSchemaFiles(config: SchemaAnalyzerConfig | string): string[];
62
62
  /**
63
63
  * Dynamically imports a schema module
64
+ * Supports both JavaScript and TypeScript files using tsx
64
65
  *
65
66
  * @param filePath - Path to the schema file (without extension)
66
67
  * @param config - Optional configuration for path transformation
@@ -75,6 +76,15 @@ declare function importSchemaModule(filePath: string, config?: SchemaAnalyzerCon
75
76
  * @returns The collection name
76
77
  */
77
78
  declare function getCollectionNameFromFile(filePath: string): string;
79
+ /**
80
+ * Extracts the collection name from a Zod schema's metadata
81
+ * Checks if the schema was created with defineCollection() which stores
82
+ * the collection name in the schema description
83
+ *
84
+ * @param zodSchema - The Zod schema to extract collection name from
85
+ * @returns The collection name if found in metadata, null otherwise
86
+ */
87
+ declare function extractCollectionNameFromSchema(zodSchema: z.ZodTypeAny): string | null;
78
88
  /**
79
89
  * Extracts Zod schema definitions from a module
80
90
  * Looks for schemas ending with configured patterns (default: "Schema" or "InputSchema")
@@ -183,4 +193,4 @@ declare class SchemaAnalyzer {
183
193
  convertZodSchemaToCollectionSchema(name: string, schema: z.ZodObject<any>): CollectionSchema;
184
194
  }
185
195
 
186
- export { SchemaAnalyzer, type SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection };
196
+ export { SchemaAnalyzer, type SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection };
@@ -61,6 +61,7 @@ interface SchemaAnalyzerConfig {
61
61
  declare function discoverSchemaFiles(config: SchemaAnalyzerConfig | string): string[];
62
62
  /**
63
63
  * Dynamically imports a schema module
64
+ * Supports both JavaScript and TypeScript files using tsx
64
65
  *
65
66
  * @param filePath - Path to the schema file (without extension)
66
67
  * @param config - Optional configuration for path transformation
@@ -75,6 +76,15 @@ declare function importSchemaModule(filePath: string, config?: SchemaAnalyzerCon
75
76
  * @returns The collection name
76
77
  */
77
78
  declare function getCollectionNameFromFile(filePath: string): string;
79
+ /**
80
+ * Extracts the collection name from a Zod schema's metadata
81
+ * Checks if the schema was created with defineCollection() which stores
82
+ * the collection name in the schema description
83
+ *
84
+ * @param zodSchema - The Zod schema to extract collection name from
85
+ * @returns The collection name if found in metadata, null otherwise
86
+ */
87
+ declare function extractCollectionNameFromSchema(zodSchema: z.ZodTypeAny): string | null;
78
88
  /**
79
89
  * Extracts Zod schema definitions from a module
80
90
  * Looks for schemas ending with configured patterns (default: "Schema" or "InputSchema")
@@ -183,4 +193,4 @@ declare class SchemaAnalyzer {
183
193
  convertZodSchemaToCollectionSchema(name: string, schema: z.ZodObject<any>): CollectionSchema;
184
194
  }
185
195
 
186
- export { SchemaAnalyzer, type SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection };
196
+ export { SchemaAnalyzer, type SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection };
@@ -957,6 +957,16 @@ function isFieldRequired(zodType) {
957
957
  }
958
958
 
959
959
  // src/migration/analyzer.ts
960
+ var tsxLoaderRegistered = false;
961
+ async function ensureTsxLoader() {
962
+ if (tsxLoaderRegistered) return;
963
+ try {
964
+ await import('tsx/esm');
965
+ tsxLoaderRegistered = true;
966
+ } catch {
967
+ tsxLoaderRegistered = false;
968
+ }
969
+ }
960
970
  var DEFAULT_CONFIG = {
961
971
  workspaceRoot: process.cwd(),
962
972
  excludePatterns: [
@@ -1054,24 +1064,37 @@ async function importSchemaModule(filePath, config) {
1054
1064
  } else {
1055
1065
  resolvedPath = jsPath;
1056
1066
  }
1067
+ if (resolvedPath.endsWith(".ts")) {
1068
+ await ensureTsxLoader();
1069
+ if (!tsxLoaderRegistered) {
1070
+ throw new SchemaParsingError(
1071
+ `Failed to import TypeScript schema file. The 'tsx' package is required to load TypeScript files.
1072
+ Please install tsx: npm install tsx (or yarn add tsx, or pnpm add tsx)
1073
+ Alternatively, compile your schema files to JavaScript first.`,
1074
+ filePath
1075
+ );
1076
+ }
1077
+ }
1057
1078
  const fileUrl = new URL(`file://${path.resolve(resolvedPath)}`);
1058
1079
  const module = await import(fileUrl.href);
1059
1080
  return module;
1060
1081
  } catch (error) {
1061
1082
  const tsPath = `${filePath}.ts`;
1062
1083
  const isTypeScriptFile = fs.existsSync(tsPath);
1084
+ if (isTypeScriptFile && error instanceof SchemaParsingError) {
1085
+ throw error;
1086
+ }
1063
1087
  if (isTypeScriptFile) {
1064
1088
  throw new SchemaParsingError(
1065
- `Failed to import TypeScript schema file. Node.js cannot import TypeScript files directly.
1066
- Please either:
1067
- 1. Compile your schema files to JavaScript first, or
1068
- 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")`,
1089
+ `Failed to import TypeScript schema file. The 'tsx' package is required to load TypeScript files.
1090
+ Please install tsx: npm install tsx (or yarn add tsx, or pnpm add tsx)
1091
+ Alternatively, compile your schema files to JavaScript first.`,
1069
1092
  filePath,
1070
1093
  error
1071
1094
  );
1072
1095
  }
1073
1096
  throw new SchemaParsingError(
1074
- `Failed to import schema module. Make sure the schema files are compiled to JavaScript.`,
1097
+ `Failed to import schema module. Make sure the schema files exist and are valid.`,
1075
1098
  filePath,
1076
1099
  error
1077
1100
  );
@@ -1081,6 +1104,19 @@ function getCollectionNameFromFile(filePath) {
1081
1104
  const filename = path.basename(filePath).replace(/\.(ts|js)$/, "");
1082
1105
  return toCollectionName(filename);
1083
1106
  }
1107
+ function extractCollectionNameFromSchema(zodSchema) {
1108
+ if (!zodSchema.description) {
1109
+ return null;
1110
+ }
1111
+ try {
1112
+ const metadata = JSON.parse(zodSchema.description);
1113
+ if (metadata.collectionName && typeof metadata.collectionName === "string") {
1114
+ return metadata.collectionName;
1115
+ }
1116
+ } catch {
1117
+ }
1118
+ return null;
1119
+ }
1084
1120
  function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema"]) {
1085
1121
  const result = {};
1086
1122
  for (const [key, value] of Object.entries(module)) {
@@ -1155,11 +1191,8 @@ function buildFieldDefinition(fieldName, zodType) {
1155
1191
  // Default to false, can be configured later
1156
1192
  };
1157
1193
  if (fieldDef.options) {
1158
- const { min, max, pattern, ...relationSafeOptions } = fieldDef.options;
1159
- console.log("min", min);
1160
- console.log("max", max);
1161
- console.log("pattern", pattern);
1162
- fieldDef.options = Object.keys(relationSafeOptions).length > 0 ? relationSafeOptions : void 0;
1194
+ const { min: _min, max: _max, pattern: _pattern, ...relationSafeOptions } = fieldDef.options;
1195
+ fieldDef.options = Object.keys(relationSafeOptions).length ? relationSafeOptions : void 0;
1163
1196
  }
1164
1197
  }
1165
1198
  return fieldDef;
@@ -1256,7 +1289,8 @@ async function buildSchemaDefinition(config) {
1256
1289
  console.warn(`No valid schema found in ${filePath}, skipping...`);
1257
1290
  continue;
1258
1291
  }
1259
- const collectionName = getCollectionNameFromFile(filePath);
1292
+ const collectionNameFromSchema = extractCollectionNameFromSchema(zodSchema);
1293
+ const collectionName = collectionNameFromSchema ?? getCollectionNameFromFile(filePath);
1260
1294
  const collectionSchema = convertZodSchemaToCollectionSchema(collectionName, zodSchema);
1261
1295
  collections.set(collectionName, collectionSchema);
1262
1296
  } catch (error) {
@@ -1300,6 +1334,6 @@ var SchemaAnalyzer = class {
1300
1334
  }
1301
1335
  };
1302
1336
 
1303
- export { SchemaAnalyzer, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection };
1337
+ export { SchemaAnalyzer, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection };
1304
1338
  //# sourceMappingURL=analyzer.js.map
1305
1339
  //# sourceMappingURL=analyzer.js.map