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
@@ -1319,6 +1319,16 @@ function getFieldTypeInfo(zodType, fieldName) {
1319
1319
  }
1320
1320
 
1321
1321
  // src/migration/analyzer.ts
1322
+ var tsxLoaderRegistered = false;
1323
+ async function ensureTsxLoader() {
1324
+ if (tsxLoaderRegistered) return;
1325
+ try {
1326
+ await import('tsx/esm');
1327
+ tsxLoaderRegistered = true;
1328
+ } catch {
1329
+ tsxLoaderRegistered = false;
1330
+ }
1331
+ }
1322
1332
  var DEFAULT_CONFIG = {
1323
1333
  workspaceRoot: process.cwd(),
1324
1334
  excludePatterns: [
@@ -1416,24 +1426,37 @@ async function importSchemaModule(filePath, config) {
1416
1426
  } else {
1417
1427
  resolvedPath = jsPath;
1418
1428
  }
1429
+ if (resolvedPath.endsWith(".ts")) {
1430
+ await ensureTsxLoader();
1431
+ if (!tsxLoaderRegistered) {
1432
+ throw new SchemaParsingError(
1433
+ `Failed to import TypeScript schema file. The 'tsx' package is required to load TypeScript files.
1434
+ Please install tsx: npm install tsx (or yarn add tsx, or pnpm add tsx)
1435
+ Alternatively, compile your schema files to JavaScript first.`,
1436
+ filePath
1437
+ );
1438
+ }
1439
+ }
1419
1440
  const fileUrl = new URL(`file://${path.resolve(resolvedPath)}`);
1420
1441
  const module = await import(fileUrl.href);
1421
1442
  return module;
1422
1443
  } catch (error) {
1423
1444
  const tsPath = `${filePath}.ts`;
1424
1445
  const isTypeScriptFile = fs3.existsSync(tsPath);
1446
+ if (isTypeScriptFile && error instanceof SchemaParsingError) {
1447
+ throw error;
1448
+ }
1425
1449
  if (isTypeScriptFile) {
1426
1450
  throw new SchemaParsingError(
1427
- `Failed to import TypeScript schema file. Node.js cannot import TypeScript files directly.
1428
- Please either:
1429
- 1. Compile your schema files to JavaScript first, or
1430
- 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")`,
1451
+ `Failed to import TypeScript schema file. The 'tsx' package is required to load TypeScript files.
1452
+ Please install tsx: npm install tsx (or yarn add tsx, or pnpm add tsx)
1453
+ Alternatively, compile your schema files to JavaScript first.`,
1431
1454
  filePath,
1432
1455
  error
1433
1456
  );
1434
1457
  }
1435
1458
  throw new SchemaParsingError(
1436
- `Failed to import schema module. Make sure the schema files are compiled to JavaScript.`,
1459
+ `Failed to import schema module. Make sure the schema files exist and are valid.`,
1437
1460
  filePath,
1438
1461
  error
1439
1462
  );
@@ -1443,6 +1466,19 @@ function getCollectionNameFromFile(filePath) {
1443
1466
  const filename = path.basename(filePath).replace(/\.(ts|js)$/, "");
1444
1467
  return toCollectionName(filename);
1445
1468
  }
1469
+ function extractCollectionNameFromSchema(zodSchema) {
1470
+ if (!zodSchema.description) {
1471
+ return null;
1472
+ }
1473
+ try {
1474
+ const metadata = JSON.parse(zodSchema.description);
1475
+ if (metadata.collectionName && typeof metadata.collectionName === "string") {
1476
+ return metadata.collectionName;
1477
+ }
1478
+ } catch {
1479
+ }
1480
+ return null;
1481
+ }
1446
1482
  function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema"]) {
1447
1483
  const result = {};
1448
1484
  for (const [key, value] of Object.entries(module)) {
@@ -1517,11 +1553,8 @@ function buildFieldDefinition(fieldName, zodType) {
1517
1553
  // Default to false, can be configured later
1518
1554
  };
1519
1555
  if (fieldDef.options) {
1520
- const { min, max, pattern, ...relationSafeOptions } = fieldDef.options;
1521
- console.log("min", min);
1522
- console.log("max", max);
1523
- console.log("pattern", pattern);
1524
- fieldDef.options = Object.keys(relationSafeOptions).length > 0 ? relationSafeOptions : void 0;
1556
+ const { min: _min, max: _max, pattern: _pattern, ...relationSafeOptions } = fieldDef.options;
1557
+ fieldDef.options = Object.keys(relationSafeOptions).length ? relationSafeOptions : void 0;
1525
1558
  }
1526
1559
  }
1527
1560
  return fieldDef;
@@ -1618,7 +1651,8 @@ async function buildSchemaDefinition(config) {
1618
1651
  console.warn(`No valid schema found in ${filePath}, skipping...`);
1619
1652
  continue;
1620
1653
  }
1621
- const collectionName = getCollectionNameFromFile(filePath);
1654
+ const collectionNameFromSchema = extractCollectionNameFromSchema(zodSchema);
1655
+ const collectionName = collectionNameFromSchema ?? getCollectionNameFromFile(filePath);
1622
1656
  const collectionSchema = convertZodSchemaToCollectionSchema(collectionName, zodSchema);
1623
1657
  collections.set(collectionName, collectionSchema);
1624
1658
  } catch (error) {