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
@@ -1342,6 +1342,16 @@ function getFieldTypeInfo(zodType, fieldName) {
1342
1342
  }
1343
1343
 
1344
1344
  // src/migration/analyzer.ts
1345
+ var tsxLoaderRegistered = false;
1346
+ async function ensureTsxLoader() {
1347
+ if (tsxLoaderRegistered) return;
1348
+ try {
1349
+ await import('tsx/esm');
1350
+ tsxLoaderRegistered = true;
1351
+ } catch {
1352
+ tsxLoaderRegistered = false;
1353
+ }
1354
+ }
1345
1355
  var DEFAULT_CONFIG = {
1346
1356
  workspaceRoot: process.cwd(),
1347
1357
  excludePatterns: [
@@ -1439,24 +1449,37 @@ async function importSchemaModule(filePath, config) {
1439
1449
  } else {
1440
1450
  resolvedPath = jsPath;
1441
1451
  }
1452
+ if (resolvedPath.endsWith(".ts")) {
1453
+ await ensureTsxLoader();
1454
+ if (!tsxLoaderRegistered) {
1455
+ throw new SchemaParsingError(
1456
+ `Failed to import TypeScript schema file. The 'tsx' package is required to load TypeScript files.
1457
+ Please install tsx: npm install tsx (or yarn add tsx, or pnpm add tsx)
1458
+ Alternatively, compile your schema files to JavaScript first.`,
1459
+ filePath
1460
+ );
1461
+ }
1462
+ }
1442
1463
  const fileUrl = new URL(`file://${path__namespace.resolve(resolvedPath)}`);
1443
1464
  const module = await import(fileUrl.href);
1444
1465
  return module;
1445
1466
  } catch (error) {
1446
1467
  const tsPath = `${filePath}.ts`;
1447
1468
  const isTypeScriptFile = fs3__namespace.existsSync(tsPath);
1469
+ if (isTypeScriptFile && error instanceof SchemaParsingError) {
1470
+ throw error;
1471
+ }
1448
1472
  if (isTypeScriptFile) {
1449
1473
  throw new SchemaParsingError(
1450
- `Failed to import TypeScript schema file. Node.js cannot import TypeScript files directly.
1451
- Please either:
1452
- 1. Compile your schema files to JavaScript first, or
1453
- 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")`,
1474
+ `Failed to import TypeScript schema file. The 'tsx' package is required to load TypeScript files.
1475
+ Please install tsx: npm install tsx (or yarn add tsx, or pnpm add tsx)
1476
+ Alternatively, compile your schema files to JavaScript first.`,
1454
1477
  filePath,
1455
1478
  error
1456
1479
  );
1457
1480
  }
1458
1481
  throw new SchemaParsingError(
1459
- `Failed to import schema module. Make sure the schema files are compiled to JavaScript.`,
1482
+ `Failed to import schema module. Make sure the schema files exist and are valid.`,
1460
1483
  filePath,
1461
1484
  error
1462
1485
  );
@@ -1466,6 +1489,19 @@ function getCollectionNameFromFile(filePath) {
1466
1489
  const filename = path__namespace.basename(filePath).replace(/\.(ts|js)$/, "");
1467
1490
  return toCollectionName(filename);
1468
1491
  }
1492
+ function extractCollectionNameFromSchema(zodSchema) {
1493
+ if (!zodSchema.description) {
1494
+ return null;
1495
+ }
1496
+ try {
1497
+ const metadata = JSON.parse(zodSchema.description);
1498
+ if (metadata.collectionName && typeof metadata.collectionName === "string") {
1499
+ return metadata.collectionName;
1500
+ }
1501
+ } catch {
1502
+ }
1503
+ return null;
1504
+ }
1469
1505
  function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema"]) {
1470
1506
  const result = {};
1471
1507
  for (const [key, value] of Object.entries(module)) {
@@ -1540,11 +1576,8 @@ function buildFieldDefinition(fieldName, zodType) {
1540
1576
  // Default to false, can be configured later
1541
1577
  };
1542
1578
  if (fieldDef.options) {
1543
- const { min, max, pattern, ...relationSafeOptions } = fieldDef.options;
1544
- console.log("min", min);
1545
- console.log("max", max);
1546
- console.log("pattern", pattern);
1547
- fieldDef.options = Object.keys(relationSafeOptions).length > 0 ? relationSafeOptions : void 0;
1579
+ const { min: _min, max: _max, pattern: _pattern, ...relationSafeOptions } = fieldDef.options;
1580
+ fieldDef.options = Object.keys(relationSafeOptions).length ? relationSafeOptions : void 0;
1548
1581
  }
1549
1582
  }
1550
1583
  return fieldDef;
@@ -1641,7 +1674,8 @@ async function buildSchemaDefinition(config) {
1641
1674
  console.warn(`No valid schema found in ${filePath}, skipping...`);
1642
1675
  continue;
1643
1676
  }
1644
- const collectionName = getCollectionNameFromFile(filePath);
1677
+ const collectionNameFromSchema = extractCollectionNameFromSchema(zodSchema);
1678
+ const collectionName = collectionNameFromSchema ?? getCollectionNameFromFile(filePath);
1645
1679
  const collectionSchema = convertZodSchemaToCollectionSchema(collectionName, zodSchema);
1646
1680
  collections.set(collectionName, collectionSchema);
1647
1681
  } catch (error) {