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
@@ -1939,10 +1939,19 @@ function aggregateChanges(currentSchema, previousSnapshot, config) {
1939
1939
  collectionsToModify.push(modification);
1940
1940
  }
1941
1941
  }
1942
+ const existingCollectionIds = /* @__PURE__ */ new Map();
1943
+ if (previousSnapshot) {
1944
+ for (const [name, collection] of previousSnapshot.collections) {
1945
+ if (collection.id) {
1946
+ existingCollectionIds.set(name, collection.id);
1947
+ }
1948
+ }
1949
+ }
1942
1950
  return {
1943
1951
  collectionsToCreate: collectionsWithIds,
1944
1952
  collectionsToDelete: filteredCollectionsToDelete,
1945
- collectionsToModify
1953
+ collectionsToModify,
1954
+ existingCollectionIds
1946
1955
  };
1947
1956
  }
1948
1957
  function categorizeChangesBySeverity(diff, _config) {
@@ -2145,7 +2154,7 @@ function formatValue(value) {
2145
2154
  return JSON.stringify(value).replace(/","/g, '", "');
2146
2155
  }
2147
2156
  if (typeof value === "object") {
2148
- const entries = Object.entries(value).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
2157
+ const entries = Object.entries(value).filter(([_k, v]) => v !== void 0).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
2149
2158
  return `{ ${entries} }`;
2150
2159
  }
2151
2160
  return String(value);
@@ -2408,7 +2417,7 @@ function generateFieldAddition(collectionName, field, varName, isLast = false, c
2408
2417
  lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
2409
2418
  return lines.join("\n");
2410
2419
  }
2411
- function generateFieldModification(collectionName, modification, varName, isLast = false) {
2420
+ function generateFieldModification(collectionName, modification, varName, isLast = false, collectionIdMap) {
2412
2421
  const lines = [];
2413
2422
  const collectionVar = varName || `collection_${collectionName}_${modification.fieldName}`;
2414
2423
  const fieldVar = `${collectionVar}_field`;
@@ -2423,7 +2432,14 @@ function generateFieldModification(collectionName, modification, varName, isLast
2423
2432
  const relationKey = change.property.replace("relation.", "");
2424
2433
  if (relationKey === "collection") {
2425
2434
  const isUsersCollection = String(change.newValue).toLowerCase() === "users";
2426
- const collectionIdValue = isUsersCollection ? '"_pb_users_auth_"' : `app.findCollectionByNameOrId("${change.newValue}").id`;
2435
+ let collectionIdValue;
2436
+ if (isUsersCollection) {
2437
+ collectionIdValue = '"_pb_users_auth_"';
2438
+ } else if (collectionIdMap && collectionIdMap.has(String(change.newValue))) {
2439
+ collectionIdValue = `"${collectionIdMap.get(String(change.newValue))}"`;
2440
+ } else {
2441
+ collectionIdValue = `app.findCollectionByNameOrId("${change.newValue}").id`;
2442
+ }
2427
2443
  lines.push(` ${fieldVar}.collectionId = ${collectionIdValue};`);
2428
2444
  } else {
2429
2445
  lines.push(` ${fieldVar}.${relationKey} = ${formatValue(change.newValue)};`);
@@ -2439,11 +2455,9 @@ function generateFieldModification(collectionName, modification, varName, isLast
2439
2455
  function generateFieldDeletion(collectionName, fieldName, varName, isLast = false) {
2440
2456
  const lines = [];
2441
2457
  const collectionVar = varName || `collection_${collectionName}_${fieldName}`;
2442
- const fieldVar = `${collectionVar}_field`;
2443
2458
  lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
2444
- lines.push(` const ${fieldVar} = ${collectionVar}.fields.getByName("${fieldName}");`);
2445
2459
  lines.push(``);
2446
- lines.push(` ${collectionVar}.fields.remove(${fieldVar}.id);`);
2460
+ lines.push(` ${collectionVar}.fields.removeByName("${fieldName}");`);
2447
2461
  lines.push(``);
2448
2462
  lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
2449
2463
  return lines.join("\n");
@@ -2501,9 +2515,10 @@ function generateOperationUpMigration(operation, collectionIdMap) {
2501
2515
  const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection?.name ?? modification.collection;
2502
2516
  let operationCount = 0;
2503
2517
  const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + modification.rulesToUpdate.length + modification.permissionsToUpdate.length;
2504
- for (const field of modification.fieldsToAdd) {
2518
+ for (let i = 0; i < modification.fieldsToAdd.length; i++) {
2519
+ const field = modification.fieldsToAdd[i];
2505
2520
  operationCount++;
2506
- const varName = `collection_${collectionName}_add_${field.name}`;
2521
+ const varName = `collection_${collectionName}_add_${field.name}_${i}`;
2507
2522
  const isLast = operationCount === totalOperations;
2508
2523
  lines.push(generateFieldAddition(collectionName, field, varName, isLast, collectionIdMap));
2509
2524
  if (!isLast) lines.push("");
@@ -2512,7 +2527,7 @@ function generateOperationUpMigration(operation, collectionIdMap) {
2512
2527
  operationCount++;
2513
2528
  const varName = `collection_${collectionName}_modify_${fieldMod.fieldName}`;
2514
2529
  const isLast = operationCount === totalOperations;
2515
- lines.push(generateFieldModification(collectionName, fieldMod, varName, isLast));
2530
+ lines.push(generateFieldModification(collectionName, fieldMod, varName, isLast, collectionIdMap));
2516
2531
  if (!isLast) lines.push("");
2517
2532
  }
2518
2533
  for (const field of modification.fieldsToRemove) {
@@ -2561,20 +2576,23 @@ function generateOperationUpMigration(operation, collectionIdMap) {
2561
2576
  lines.push(generateCollectionDeletion(collectionName, varName, true));
2562
2577
  }
2563
2578
  let code = lines.join("\n");
2564
- const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2565
- const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2566
- const saveMatches = [...code.matchAll(savePattern)];
2567
- const deleteMatches = [...code.matchAll(deletePattern)];
2568
- const allMatches = [
2569
- ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2570
- ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2571
- ].sort((a, b) => b.index - a.index);
2572
- if (allMatches.length > 0) {
2573
- const lastMatch = allMatches[0];
2574
- if (lastMatch.type === "save") {
2575
- 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);
2576
- } else {
2577
- 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);
2579
+ const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
2580
+ if (!hasReturnStatement) {
2581
+ const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2582
+ const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2583
+ const saveMatches = [...code.matchAll(savePattern)];
2584
+ const deleteMatches = [...code.matchAll(deletePattern)];
2585
+ const allMatches = [
2586
+ ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2587
+ ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2588
+ ].sort((a, b) => b.index - a.index);
2589
+ if (allMatches.length > 0) {
2590
+ const lastMatch = allMatches[0];
2591
+ if (lastMatch.type === "save") {
2592
+ 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);
2593
+ } else {
2594
+ 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);
2595
+ }
2578
2596
  }
2579
2597
  }
2580
2598
  return code;
@@ -2663,20 +2681,23 @@ function generateOperationDownMigration(operation, collectionIdMap) {
2663
2681
  }
2664
2682
  }
2665
2683
  let code = lines.join("\n");
2666
- const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2667
- const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2668
- const saveMatches = [...code.matchAll(savePattern)];
2669
- const deleteMatches = [...code.matchAll(deletePattern)];
2670
- const allMatches = [
2671
- ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2672
- ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2673
- ].sort((a, b) => b.index - a.index);
2674
- if (allMatches.length > 0) {
2675
- const lastMatch = allMatches[0];
2676
- if (lastMatch.type === "save") {
2677
- 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);
2678
- } else {
2679
- 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);
2684
+ const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
2685
+ if (!hasReturnStatement) {
2686
+ const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2687
+ const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2688
+ const saveMatches = [...code.matchAll(savePattern)];
2689
+ const deleteMatches = [...code.matchAll(deletePattern)];
2690
+ const allMatches = [
2691
+ ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2692
+ ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2693
+ ].sort((a, b) => b.index - a.index);
2694
+ if (allMatches.length > 0) {
2695
+ const lastMatch = allMatches[0];
2696
+ if (lastMatch.type === "save") {
2697
+ 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);
2698
+ } else {
2699
+ 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);
2700
+ }
2680
2701
  }
2681
2702
  }
2682
2703
  return code;
@@ -2700,6 +2721,11 @@ function generate(diff, config) {
2700
2721
  collectionIdMap.set(collection.name, collection.id);
2701
2722
  }
2702
2723
  }
2724
+ if (diff.existingCollectionIds) {
2725
+ for (const [name, id] of diff.existingCollectionIds) {
2726
+ collectionIdMap.set(name, id);
2727
+ }
2728
+ }
2703
2729
  const baseTimestamp = generateTimestamp(normalizedConfig);
2704
2730
  const operations = splitDiffByCollection(diff, baseTimestamp);
2705
2731
  const filePaths = [];