pocketbase-zod-schema 0.1.3 → 0.2.0

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 (57) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +233 -98
  3. package/dist/cli/index.cjs +449 -108
  4. package/dist/cli/index.cjs.map +1 -1
  5. package/dist/cli/index.js +447 -106
  6. package/dist/cli/index.js.map +1 -1
  7. package/dist/cli/migrate.cjs +452 -111
  8. package/dist/cli/migrate.cjs.map +1 -1
  9. package/dist/cli/migrate.js +447 -106
  10. package/dist/cli/migrate.js.map +1 -1
  11. package/dist/index.cjs +593 -175
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.d.cts +4 -4
  14. package/dist/index.d.ts +4 -4
  15. package/dist/index.js +583 -172
  16. package/dist/index.js.map +1 -1
  17. package/dist/migration/analyzer.cjs +44 -6
  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 +44 -7
  22. package/dist/migration/analyzer.js.map +1 -1
  23. package/dist/migration/diff.cjs +21 -3
  24. package/dist/migration/diff.cjs.map +1 -1
  25. package/dist/migration/diff.js +21 -3
  26. package/dist/migration/diff.js.map +1 -1
  27. package/dist/migration/index.cjs +500 -129
  28. package/dist/migration/index.cjs.map +1 -1
  29. package/dist/migration/index.d.cts +1 -1
  30. package/dist/migration/index.d.ts +1 -1
  31. package/dist/migration/index.js +499 -129
  32. package/dist/migration/index.js.map +1 -1
  33. package/dist/migration/snapshot.cjs +432 -118
  34. package/dist/migration/snapshot.cjs.map +1 -1
  35. package/dist/migration/snapshot.d.cts +34 -12
  36. package/dist/migration/snapshot.d.ts +34 -12
  37. package/dist/migration/snapshot.js +430 -117
  38. package/dist/migration/snapshot.js.map +1 -1
  39. package/dist/mutator.cjs +20 -21
  40. package/dist/mutator.cjs.map +1 -1
  41. package/dist/mutator.d.cts +4 -4
  42. package/dist/mutator.d.ts +4 -4
  43. package/dist/mutator.js +20 -21
  44. package/dist/mutator.js.map +1 -1
  45. package/dist/schema.cjs +69 -10
  46. package/dist/schema.cjs.map +1 -1
  47. package/dist/schema.d.cts +98 -8
  48. package/dist/schema.d.ts +98 -8
  49. package/dist/schema.js +62 -9
  50. package/dist/schema.js.map +1 -1
  51. package/dist/types.d.cts +5 -2
  52. package/dist/types.d.ts +5 -2
  53. package/dist/user-DTJQIj4K.d.cts +149 -0
  54. package/dist/user-DTJQIj4K.d.ts +149 -0
  55. package/package.json +3 -3
  56. package/dist/user-C39DQ40N.d.cts +0 -53
  57. package/dist/user-C39DQ40N.d.ts +0 -53
@@ -980,6 +980,16 @@ function isFieldRequired(zodType) {
980
980
  }
981
981
 
982
982
  // src/migration/analyzer.ts
983
+ var tsxLoaderRegistered = false;
984
+ async function ensureTsxLoader() {
985
+ if (tsxLoaderRegistered) return;
986
+ try {
987
+ await import('tsx/esm');
988
+ tsxLoaderRegistered = true;
989
+ } catch {
990
+ tsxLoaderRegistered = false;
991
+ }
992
+ }
983
993
  var DEFAULT_CONFIG = {
984
994
  workspaceRoot: process.cwd(),
985
995
  excludePatterns: [
@@ -1077,24 +1087,37 @@ async function importSchemaModule(filePath, config) {
1077
1087
  } else {
1078
1088
  resolvedPath = jsPath;
1079
1089
  }
1090
+ if (resolvedPath.endsWith(".ts")) {
1091
+ await ensureTsxLoader();
1092
+ if (!tsxLoaderRegistered) {
1093
+ throw new SchemaParsingError(
1094
+ `Failed to import TypeScript schema file. The 'tsx' package is required to load TypeScript files.
1095
+ Please install tsx: npm install tsx (or yarn add tsx, or pnpm add tsx)
1096
+ Alternatively, compile your schema files to JavaScript first.`,
1097
+ filePath
1098
+ );
1099
+ }
1100
+ }
1080
1101
  const fileUrl = new URL(`file://${path__namespace.resolve(resolvedPath)}`);
1081
1102
  const module = await import(fileUrl.href);
1082
1103
  return module;
1083
1104
  } catch (error) {
1084
1105
  const tsPath = `${filePath}.ts`;
1085
1106
  const isTypeScriptFile = fs__namespace.existsSync(tsPath);
1107
+ if (isTypeScriptFile && error instanceof SchemaParsingError) {
1108
+ throw error;
1109
+ }
1086
1110
  if (isTypeScriptFile) {
1087
1111
  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")`,
1112
+ `Failed to import TypeScript schema file. The 'tsx' package is required to load TypeScript files.
1113
+ Please install tsx: npm install tsx (or yarn add tsx, or pnpm add tsx)
1114
+ Alternatively, compile your schema files to JavaScript first.`,
1092
1115
  filePath,
1093
1116
  error
1094
1117
  );
1095
1118
  }
1096
1119
  throw new SchemaParsingError(
1097
- `Failed to import schema module. Make sure the schema files are compiled to JavaScript.`,
1120
+ `Failed to import schema module. Make sure the schema files exist and are valid.`,
1098
1121
  filePath,
1099
1122
  error
1100
1123
  );
@@ -1104,6 +1127,19 @@ function getCollectionNameFromFile(filePath) {
1104
1127
  const filename = path__namespace.basename(filePath).replace(/\.(ts|js)$/, "");
1105
1128
  return toCollectionName(filename);
1106
1129
  }
1130
+ function extractCollectionNameFromSchema(zodSchema) {
1131
+ if (!zodSchema.description) {
1132
+ return null;
1133
+ }
1134
+ try {
1135
+ const metadata = JSON.parse(zodSchema.description);
1136
+ if (metadata.collectionName && typeof metadata.collectionName === "string") {
1137
+ return metadata.collectionName;
1138
+ }
1139
+ } catch {
1140
+ }
1141
+ return null;
1142
+ }
1107
1143
  function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema"]) {
1108
1144
  const result = {};
1109
1145
  for (const [key, value] of Object.entries(module)) {
@@ -1279,7 +1315,8 @@ async function buildSchemaDefinition(config) {
1279
1315
  console.warn(`No valid schema found in ${filePath}, skipping...`);
1280
1316
  continue;
1281
1317
  }
1282
- const collectionName = getCollectionNameFromFile(filePath);
1318
+ const collectionNameFromSchema = extractCollectionNameFromSchema(zodSchema);
1319
+ const collectionName = collectionNameFromSchema ?? getCollectionNameFromFile(filePath);
1283
1320
  const collectionSchema = convertZodSchemaToCollectionSchema(collectionName, zodSchema);
1284
1321
  collections.set(collectionName, collectionSchema);
1285
1322
  } catch (error) {
@@ -1328,6 +1365,7 @@ exports.buildFieldDefinition = buildFieldDefinition;
1328
1365
  exports.buildSchemaDefinition = buildSchemaDefinition;
1329
1366
  exports.convertZodSchemaToCollectionSchema = convertZodSchemaToCollectionSchema;
1330
1367
  exports.discoverSchemaFiles = discoverSchemaFiles;
1368
+ exports.extractCollectionNameFromSchema = extractCollectionNameFromSchema;
1331
1369
  exports.extractFieldDefinitions = extractFieldDefinitions;
1332
1370
  exports.extractIndexes = extractIndexes;
1333
1371
  exports.extractSchemaDefinitions = extractSchemaDefinitions;