pocketbase-zod-schema 0.3.1 → 0.3.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 (60) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cli/index.cjs +64 -38
  3. package/dist/cli/index.cjs.map +1 -1
  4. package/dist/cli/index.d.cts +2 -2
  5. package/dist/cli/index.d.ts +2 -2
  6. package/dist/cli/index.js +64 -38
  7. package/dist/cli/index.js.map +1 -1
  8. package/dist/cli/migrate.cjs +64 -38
  9. package/dist/cli/migrate.cjs.map +1 -1
  10. package/dist/cli/migrate.js +64 -38
  11. package/dist/cli/migrate.js.map +1 -1
  12. package/dist/cli/utils/index.d.cts +2 -2
  13. package/dist/cli/utils/index.d.ts +2 -2
  14. package/dist/{fields-RVj26U-O.d.cts → fields-DBBm06VU.d.cts} +34 -7
  15. package/dist/{fields-RVj26U-O.d.ts → fields-DBBm06VU.d.ts} +34 -7
  16. package/dist/index.cjs +167 -75
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.cts +3 -3
  19. package/dist/index.d.ts +3 -3
  20. package/dist/index.js +167 -75
  21. package/dist/index.js.map +1 -1
  22. package/dist/migration/analyzer.cjs.map +1 -1
  23. package/dist/migration/analyzer.d.cts +2 -2
  24. package/dist/migration/analyzer.d.ts +2 -2
  25. package/dist/migration/analyzer.js.map +1 -1
  26. package/dist/migration/diff.cjs +10 -1
  27. package/dist/migration/diff.cjs.map +1 -1
  28. package/dist/migration/diff.d.cts +2 -2
  29. package/dist/migration/diff.d.ts +2 -2
  30. package/dist/migration/diff.js +10 -1
  31. package/dist/migration/diff.js.map +1 -1
  32. package/dist/migration/generator.cjs +102 -68
  33. package/dist/migration/generator.cjs.map +1 -1
  34. package/dist/migration/generator.d.cts +3 -3
  35. package/dist/migration/generator.d.ts +3 -3
  36. package/dist/migration/generator.js +102 -68
  37. package/dist/migration/generator.js.map +1 -1
  38. package/dist/migration/index.cjs +112 -69
  39. package/dist/migration/index.cjs.map +1 -1
  40. package/dist/migration/index.d.cts +3 -3
  41. package/dist/migration/index.d.ts +3 -3
  42. package/dist/migration/index.js +112 -69
  43. package/dist/migration/index.js.map +1 -1
  44. package/dist/migration/snapshot.d.cts +2 -2
  45. package/dist/migration/snapshot.d.ts +2 -2
  46. package/dist/migration/utils/index.cjs.map +1 -1
  47. package/dist/migration/utils/index.d.cts +2 -2
  48. package/dist/migration/utils/index.d.ts +2 -2
  49. package/dist/migration/utils/index.js.map +1 -1
  50. package/dist/schema.cjs +55 -6
  51. package/dist/schema.cjs.map +1 -1
  52. package/dist/schema.d.cts +1 -1
  53. package/dist/schema.d.ts +1 -1
  54. package/dist/schema.js +55 -6
  55. package/dist/schema.js.map +1 -1
  56. package/dist/{type-mapper-DaBe-1ph.d.cts → type-mapper-DsGgZwUo.d.cts} +1 -1
  57. package/dist/{type-mapper-CZzVeDj7.d.ts → type-mapper-Dvh4QTM-.d.ts} +1 -1
  58. package/dist/{types-CUVzgZ9k.d.ts → types-BWhwQxG-.d.ts} +6 -1
  59. package/dist/{types-D-Fsdn_O.d.cts → types-d0yBwHoN.d.cts} +6 -1
  60. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.3.3](https://github.com/dastron/pocketbase-zod-schema/compare/pocketbase-zod-schema-v0.3.2...pocketbase-zod-schema-v0.3.3) (2026-01-06)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * looping migrations ([faf75ca](https://github.com/dastron/pocketbase-zod-schema/commit/faf75cada22149b262065f078fdffa8f5a9fdacd))
9
+
10
+ ## [0.3.2](https://github.com/dastron/pocketbase-zod-schema/compare/pocketbase-zod-schema-v0.3.1...pocketbase-zod-schema-v0.3.2) (2026-01-06)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * update field removal method ([f673f5c](https://github.com/dastron/pocketbase-zod-schema/commit/f673f5c06e972ab4f556666469931c3729614bd4))
16
+
3
17
  ## [0.3.1](https://github.com/dastron/pocketbase-zod-schema/compare/pocketbase-zod-schema-v0.3.0...pocketbase-zod-schema-v0.3.1) (2026-01-05)
4
18
 
5
19
 
@@ -1962,10 +1962,19 @@ function aggregateChanges(currentSchema, previousSnapshot, config) {
1962
1962
  collectionsToModify.push(modification);
1963
1963
  }
1964
1964
  }
1965
+ const existingCollectionIds = /* @__PURE__ */ new Map();
1966
+ if (previousSnapshot) {
1967
+ for (const [name, collection] of previousSnapshot.collections) {
1968
+ if (collection.id) {
1969
+ existingCollectionIds.set(name, collection.id);
1970
+ }
1971
+ }
1972
+ }
1965
1973
  return {
1966
1974
  collectionsToCreate: collectionsWithIds,
1967
1975
  collectionsToDelete: filteredCollectionsToDelete,
1968
- collectionsToModify
1976
+ collectionsToModify,
1977
+ existingCollectionIds
1969
1978
  };
1970
1979
  }
1971
1980
  function categorizeChangesBySeverity(diff, _config) {
@@ -2168,7 +2177,7 @@ function formatValue(value) {
2168
2177
  return JSON.stringify(value).replace(/","/g, '", "');
2169
2178
  }
2170
2179
  if (typeof value === "object") {
2171
- const entries = Object.entries(value).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
2180
+ const entries = Object.entries(value).filter(([_k, v]) => v !== void 0).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
2172
2181
  return `{ ${entries} }`;
2173
2182
  }
2174
2183
  return String(value);
@@ -2431,7 +2440,7 @@ function generateFieldAddition(collectionName, field, varName, isLast = false, c
2431
2440
  lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
2432
2441
  return lines.join("\n");
2433
2442
  }
2434
- function generateFieldModification(collectionName, modification, varName, isLast = false) {
2443
+ function generateFieldModification(collectionName, modification, varName, isLast = false, collectionIdMap) {
2435
2444
  const lines = [];
2436
2445
  const collectionVar = varName || `collection_${collectionName}_${modification.fieldName}`;
2437
2446
  const fieldVar = `${collectionVar}_field`;
@@ -2446,7 +2455,14 @@ function generateFieldModification(collectionName, modification, varName, isLast
2446
2455
  const relationKey = change.property.replace("relation.", "");
2447
2456
  if (relationKey === "collection") {
2448
2457
  const isUsersCollection = String(change.newValue).toLowerCase() === "users";
2449
- const collectionIdValue = isUsersCollection ? '"_pb_users_auth_"' : `app.findCollectionByNameOrId("${change.newValue}").id`;
2458
+ let collectionIdValue;
2459
+ if (isUsersCollection) {
2460
+ collectionIdValue = '"_pb_users_auth_"';
2461
+ } else if (collectionIdMap && collectionIdMap.has(String(change.newValue))) {
2462
+ collectionIdValue = `"${collectionIdMap.get(String(change.newValue))}"`;
2463
+ } else {
2464
+ collectionIdValue = `app.findCollectionByNameOrId("${change.newValue}").id`;
2465
+ }
2450
2466
  lines.push(` ${fieldVar}.collectionId = ${collectionIdValue};`);
2451
2467
  } else {
2452
2468
  lines.push(` ${fieldVar}.${relationKey} = ${formatValue(change.newValue)};`);
@@ -2462,11 +2478,9 @@ function generateFieldModification(collectionName, modification, varName, isLast
2462
2478
  function generateFieldDeletion(collectionName, fieldName, varName, isLast = false) {
2463
2479
  const lines = [];
2464
2480
  const collectionVar = varName || `collection_${collectionName}_${fieldName}`;
2465
- const fieldVar = `${collectionVar}_field`;
2466
2481
  lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
2467
- lines.push(` const ${fieldVar} = ${collectionVar}.fields.getByName("${fieldName}");`);
2468
2482
  lines.push(``);
2469
- lines.push(` ${collectionVar}.fields.remove(${fieldVar}.id);`);
2483
+ lines.push(` ${collectionVar}.fields.removeByName("${fieldName}");`);
2470
2484
  lines.push(``);
2471
2485
  lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
2472
2486
  return lines.join("\n");
@@ -2524,9 +2538,10 @@ function generateOperationUpMigration(operation, collectionIdMap) {
2524
2538
  const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection?.name ?? modification.collection;
2525
2539
  let operationCount = 0;
2526
2540
  const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + modification.rulesToUpdate.length + modification.permissionsToUpdate.length;
2527
- for (const field of modification.fieldsToAdd) {
2541
+ for (let i = 0; i < modification.fieldsToAdd.length; i++) {
2542
+ const field = modification.fieldsToAdd[i];
2528
2543
  operationCount++;
2529
- const varName = `collection_${collectionName}_add_${field.name}`;
2544
+ const varName = `collection_${collectionName}_add_${field.name}_${i}`;
2530
2545
  const isLast = operationCount === totalOperations;
2531
2546
  lines.push(generateFieldAddition(collectionName, field, varName, isLast, collectionIdMap));
2532
2547
  if (!isLast) lines.push("");
@@ -2535,7 +2550,7 @@ function generateOperationUpMigration(operation, collectionIdMap) {
2535
2550
  operationCount++;
2536
2551
  const varName = `collection_${collectionName}_modify_${fieldMod.fieldName}`;
2537
2552
  const isLast = operationCount === totalOperations;
2538
- lines.push(generateFieldModification(collectionName, fieldMod, varName, isLast));
2553
+ lines.push(generateFieldModification(collectionName, fieldMod, varName, isLast, collectionIdMap));
2539
2554
  if (!isLast) lines.push("");
2540
2555
  }
2541
2556
  for (const field of modification.fieldsToRemove) {
@@ -2584,20 +2599,23 @@ function generateOperationUpMigration(operation, collectionIdMap) {
2584
2599
  lines.push(generateCollectionDeletion(collectionName, varName, true));
2585
2600
  }
2586
2601
  let code = lines.join("\n");
2587
- const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2588
- const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2589
- const saveMatches = [...code.matchAll(savePattern)];
2590
- const deleteMatches = [...code.matchAll(deletePattern)];
2591
- const allMatches = [
2592
- ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2593
- ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2594
- ].sort((a, b) => b.index - a.index);
2595
- if (allMatches.length > 0) {
2596
- const lastMatch = allMatches[0];
2597
- if (lastMatch.type === "save") {
2598
- code = code.substring(0, lastMatch.match.index) + lastMatch.match[1] + "return app.save(" + lastMatch.match[2] + ");" + code.substring(lastMatch.match.index + lastMatch.match[0].length);
2599
- } else {
2600
- code = code.substring(0, lastMatch.match.index) + lastMatch.match[1] + "return app.delete(" + lastMatch.match[2] + ");" + code.substring(lastMatch.match.index + lastMatch.match[0].length);
2602
+ const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
2603
+ if (!hasReturnStatement) {
2604
+ const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2605
+ const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2606
+ const saveMatches = [...code.matchAll(savePattern)];
2607
+ const deleteMatches = [...code.matchAll(deletePattern)];
2608
+ const allMatches = [
2609
+ ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2610
+ ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2611
+ ].sort((a, b) => b.index - a.index);
2612
+ if (allMatches.length > 0) {
2613
+ const lastMatch = allMatches[0];
2614
+ if (lastMatch.type === "save") {
2615
+ code = code.substring(0, lastMatch.match.index) + lastMatch.match[1] + "return app.save(" + lastMatch.match[2] + ");" + code.substring(lastMatch.match.index + lastMatch.match[0].length);
2616
+ } else {
2617
+ code = code.substring(0, lastMatch.match.index) + lastMatch.match[1] + "return app.delete(" + lastMatch.match[2] + ");" + code.substring(lastMatch.match.index + lastMatch.match[0].length);
2618
+ }
2601
2619
  }
2602
2620
  }
2603
2621
  return code;
@@ -2686,20 +2704,23 @@ function generateOperationDownMigration(operation, collectionIdMap) {
2686
2704
  }
2687
2705
  }
2688
2706
  let code = lines.join("\n");
2689
- const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2690
- const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2691
- const saveMatches = [...code.matchAll(savePattern)];
2692
- const deleteMatches = [...code.matchAll(deletePattern)];
2693
- const allMatches = [
2694
- ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2695
- ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2696
- ].sort((a, b) => b.index - a.index);
2697
- if (allMatches.length > 0) {
2698
- const lastMatch = allMatches[0];
2699
- if (lastMatch.type === "save") {
2700
- code = code.substring(0, lastMatch.match.index) + lastMatch.match[1] + "return app.save(" + lastMatch.match[2] + ");" + code.substring(lastMatch.match.index + lastMatch.match[0].length);
2701
- } else {
2702
- code = code.substring(0, lastMatch.match.index) + lastMatch.match[1] + "return app.delete(" + lastMatch.match[2] + ");" + code.substring(lastMatch.match.index + lastMatch.match[0].length);
2707
+ const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
2708
+ if (!hasReturnStatement) {
2709
+ const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2710
+ const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2711
+ const saveMatches = [...code.matchAll(savePattern)];
2712
+ const deleteMatches = [...code.matchAll(deletePattern)];
2713
+ const allMatches = [
2714
+ ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2715
+ ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2716
+ ].sort((a, b) => b.index - a.index);
2717
+ if (allMatches.length > 0) {
2718
+ const lastMatch = allMatches[0];
2719
+ if (lastMatch.type === "save") {
2720
+ code = code.substring(0, lastMatch.match.index) + lastMatch.match[1] + "return app.save(" + lastMatch.match[2] + ");" + code.substring(lastMatch.match.index + lastMatch.match[0].length);
2721
+ } else {
2722
+ code = code.substring(0, lastMatch.match.index) + lastMatch.match[1] + "return app.delete(" + lastMatch.match[2] + ");" + code.substring(lastMatch.match.index + lastMatch.match[0].length);
2723
+ }
2703
2724
  }
2704
2725
  }
2705
2726
  return code;
@@ -2723,6 +2744,11 @@ function generate(diff, config) {
2723
2744
  collectionIdMap.set(collection.name, collection.id);
2724
2745
  }
2725
2746
  }
2747
+ if (diff.existingCollectionIds) {
2748
+ for (const [name, id] of diff.existingCollectionIds) {
2749
+ collectionIdMap.set(name, id);
2750
+ }
2751
+ }
2726
2752
  const baseTimestamp = generateTimestamp(normalizedConfig);
2727
2753
  const operations = splitDiffByCollection(diff, baseTimestamp);
2728
2754
  const filePaths = [];