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
@@ -1,7 +1,7 @@
1
1
  export { MigrationConfig, StatusOutput, VerbosityLevel, createProgressBar, createSpinner, formatChangeSummary, formatDuration, formatStatusJson, getMigrationsDirectory, getSchemaDirectory, getVerbosity, loadConfig, logBox, logDebug, logError, logInfo, logKeyValue, logList, logSection, logStep, logSuccess, logTable, logTimed, logTimestamp, logWarning, setVerbosity, withProgress } from './utils/index.cjs';
2
2
  import 'ora';
3
- import '../types-D-Fsdn_O.cjs';
4
- import '../fields-RVj26U-O.cjs';
3
+ import '../types-d0yBwHoN.cjs';
4
+ import '../fields-DBBm06VU.cjs';
5
5
  import 'zod';
6
6
  import '../permissions-ZHafVSIx.cjs';
7
7
 
@@ -1,7 +1,7 @@
1
1
  export { MigrationConfig, StatusOutput, VerbosityLevel, createProgressBar, createSpinner, formatChangeSummary, formatDuration, formatStatusJson, getMigrationsDirectory, getSchemaDirectory, getVerbosity, loadConfig, logBox, logDebug, logError, logInfo, logKeyValue, logList, logSection, logStep, logSuccess, logTable, logTimed, logTimestamp, logWarning, setVerbosity, withProgress } from './utils/index.js';
2
2
  import 'ora';
3
- import '../types-CUVzgZ9k.js';
4
- import '../fields-RVj26U-O.js';
3
+ import '../types-BWhwQxG-.js';
4
+ import '../fields-DBBm06VU.js';
5
5
  import 'zod';
6
6
  import '../permissions-ZHafVSIx.js';
7
7
 
package/dist/cli/index.js CHANGED
@@ -1935,10 +1935,19 @@ function aggregateChanges(currentSchema, previousSnapshot, config) {
1935
1935
  collectionsToModify.push(modification);
1936
1936
  }
1937
1937
  }
1938
+ const existingCollectionIds = /* @__PURE__ */ new Map();
1939
+ if (previousSnapshot) {
1940
+ for (const [name, collection] of previousSnapshot.collections) {
1941
+ if (collection.id) {
1942
+ existingCollectionIds.set(name, collection.id);
1943
+ }
1944
+ }
1945
+ }
1938
1946
  return {
1939
1947
  collectionsToCreate: collectionsWithIds,
1940
1948
  collectionsToDelete: filteredCollectionsToDelete,
1941
- collectionsToModify
1949
+ collectionsToModify,
1950
+ existingCollectionIds
1942
1951
  };
1943
1952
  }
1944
1953
  function categorizeChangesBySeverity(diff, _config) {
@@ -2141,7 +2150,7 @@ function formatValue(value) {
2141
2150
  return JSON.stringify(value).replace(/","/g, '", "');
2142
2151
  }
2143
2152
  if (typeof value === "object") {
2144
- const entries = Object.entries(value).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
2153
+ const entries = Object.entries(value).filter(([_k, v]) => v !== void 0).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
2145
2154
  return `{ ${entries} }`;
2146
2155
  }
2147
2156
  return String(value);
@@ -2404,7 +2413,7 @@ function generateFieldAddition(collectionName, field, varName, isLast = false, c
2404
2413
  lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
2405
2414
  return lines.join("\n");
2406
2415
  }
2407
- function generateFieldModification(collectionName, modification, varName, isLast = false) {
2416
+ function generateFieldModification(collectionName, modification, varName, isLast = false, collectionIdMap) {
2408
2417
  const lines = [];
2409
2418
  const collectionVar = varName || `collection_${collectionName}_${modification.fieldName}`;
2410
2419
  const fieldVar = `${collectionVar}_field`;
@@ -2419,7 +2428,14 @@ function generateFieldModification(collectionName, modification, varName, isLast
2419
2428
  const relationKey = change.property.replace("relation.", "");
2420
2429
  if (relationKey === "collection") {
2421
2430
  const isUsersCollection = String(change.newValue).toLowerCase() === "users";
2422
- const collectionIdValue = isUsersCollection ? '"_pb_users_auth_"' : `app.findCollectionByNameOrId("${change.newValue}").id`;
2431
+ let collectionIdValue;
2432
+ if (isUsersCollection) {
2433
+ collectionIdValue = '"_pb_users_auth_"';
2434
+ } else if (collectionIdMap && collectionIdMap.has(String(change.newValue))) {
2435
+ collectionIdValue = `"${collectionIdMap.get(String(change.newValue))}"`;
2436
+ } else {
2437
+ collectionIdValue = `app.findCollectionByNameOrId("${change.newValue}").id`;
2438
+ }
2423
2439
  lines.push(` ${fieldVar}.collectionId = ${collectionIdValue};`);
2424
2440
  } else {
2425
2441
  lines.push(` ${fieldVar}.${relationKey} = ${formatValue(change.newValue)};`);
@@ -2435,11 +2451,9 @@ function generateFieldModification(collectionName, modification, varName, isLast
2435
2451
  function generateFieldDeletion(collectionName, fieldName, varName, isLast = false) {
2436
2452
  const lines = [];
2437
2453
  const collectionVar = varName || `collection_${collectionName}_${fieldName}`;
2438
- const fieldVar = `${collectionVar}_field`;
2439
2454
  lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
2440
- lines.push(` const ${fieldVar} = ${collectionVar}.fields.getByName("${fieldName}");`);
2441
2455
  lines.push(``);
2442
- lines.push(` ${collectionVar}.fields.remove(${fieldVar}.id);`);
2456
+ lines.push(` ${collectionVar}.fields.removeByName("${fieldName}");`);
2443
2457
  lines.push(``);
2444
2458
  lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
2445
2459
  return lines.join("\n");
@@ -2497,9 +2511,10 @@ function generateOperationUpMigration(operation, collectionIdMap) {
2497
2511
  const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection?.name ?? modification.collection;
2498
2512
  let operationCount = 0;
2499
2513
  const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + modification.rulesToUpdate.length + modification.permissionsToUpdate.length;
2500
- for (const field of modification.fieldsToAdd) {
2514
+ for (let i = 0; i < modification.fieldsToAdd.length; i++) {
2515
+ const field = modification.fieldsToAdd[i];
2501
2516
  operationCount++;
2502
- const varName = `collection_${collectionName}_add_${field.name}`;
2517
+ const varName = `collection_${collectionName}_add_${field.name}_${i}`;
2503
2518
  const isLast = operationCount === totalOperations;
2504
2519
  lines.push(generateFieldAddition(collectionName, field, varName, isLast, collectionIdMap));
2505
2520
  if (!isLast) lines.push("");
@@ -2508,7 +2523,7 @@ function generateOperationUpMigration(operation, collectionIdMap) {
2508
2523
  operationCount++;
2509
2524
  const varName = `collection_${collectionName}_modify_${fieldMod.fieldName}`;
2510
2525
  const isLast = operationCount === totalOperations;
2511
- lines.push(generateFieldModification(collectionName, fieldMod, varName, isLast));
2526
+ lines.push(generateFieldModification(collectionName, fieldMod, varName, isLast, collectionIdMap));
2512
2527
  if (!isLast) lines.push("");
2513
2528
  }
2514
2529
  for (const field of modification.fieldsToRemove) {
@@ -2557,20 +2572,23 @@ function generateOperationUpMigration(operation, collectionIdMap) {
2557
2572
  lines.push(generateCollectionDeletion(collectionName, varName, true));
2558
2573
  }
2559
2574
  let code = lines.join("\n");
2560
- const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2561
- const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2562
- const saveMatches = [...code.matchAll(savePattern)];
2563
- const deleteMatches = [...code.matchAll(deletePattern)];
2564
- const allMatches = [
2565
- ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2566
- ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2567
- ].sort((a, b) => b.index - a.index);
2568
- if (allMatches.length > 0) {
2569
- const lastMatch = allMatches[0];
2570
- if (lastMatch.type === "save") {
2571
- 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);
2572
- } else {
2573
- 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);
2575
+ const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
2576
+ if (!hasReturnStatement) {
2577
+ const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2578
+ const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2579
+ const saveMatches = [...code.matchAll(savePattern)];
2580
+ const deleteMatches = [...code.matchAll(deletePattern)];
2581
+ const allMatches = [
2582
+ ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2583
+ ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2584
+ ].sort((a, b) => b.index - a.index);
2585
+ if (allMatches.length > 0) {
2586
+ const lastMatch = allMatches[0];
2587
+ if (lastMatch.type === "save") {
2588
+ 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);
2589
+ } else {
2590
+ 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);
2591
+ }
2574
2592
  }
2575
2593
  }
2576
2594
  return code;
@@ -2659,20 +2677,23 @@ function generateOperationDownMigration(operation, collectionIdMap) {
2659
2677
  }
2660
2678
  }
2661
2679
  let code = lines.join("\n");
2662
- const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2663
- const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2664
- const saveMatches = [...code.matchAll(savePattern)];
2665
- const deleteMatches = [...code.matchAll(deletePattern)];
2666
- const allMatches = [
2667
- ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2668
- ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2669
- ].sort((a, b) => b.index - a.index);
2670
- if (allMatches.length > 0) {
2671
- const lastMatch = allMatches[0];
2672
- if (lastMatch.type === "save") {
2673
- 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);
2674
- } else {
2675
- 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);
2680
+ const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
2681
+ if (!hasReturnStatement) {
2682
+ const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2683
+ const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2684
+ const saveMatches = [...code.matchAll(savePattern)];
2685
+ const deleteMatches = [...code.matchAll(deletePattern)];
2686
+ const allMatches = [
2687
+ ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2688
+ ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2689
+ ].sort((a, b) => b.index - a.index);
2690
+ if (allMatches.length > 0) {
2691
+ const lastMatch = allMatches[0];
2692
+ if (lastMatch.type === "save") {
2693
+ 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);
2694
+ } else {
2695
+ 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);
2696
+ }
2676
2697
  }
2677
2698
  }
2678
2699
  return code;
@@ -2696,6 +2717,11 @@ function generate(diff, config) {
2696
2717
  collectionIdMap.set(collection.name, collection.id);
2697
2718
  }
2698
2719
  }
2720
+ if (diff.existingCollectionIds) {
2721
+ for (const [name, id] of diff.existingCollectionIds) {
2722
+ collectionIdMap.set(name, id);
2723
+ }
2724
+ }
2699
2725
  const baseTimestamp = generateTimestamp(normalizedConfig);
2700
2726
  const operations = splitDiffByCollection(diff, baseTimestamp);
2701
2727
  const filePaths = [];