pocketbase-zod-schema 0.3.0 → 0.3.2

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 (66) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/cli/index.cjs +175 -50
  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 +175 -50
  7. package/dist/cli/index.js.map +1 -1
  8. package/dist/cli/migrate.cjs +175 -50
  9. package/dist/cli/migrate.cjs.map +1 -1
  10. package/dist/cli/migrate.js +175 -50
  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-UcOPu1OQ.d.cts → fields-DBBm06VU.d.cts} +35 -7
  15. package/dist/{fields-UcOPu1OQ.d.ts → fields-DBBm06VU.d.ts} +35 -7
  16. package/dist/index.cjs +264 -84
  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 +264 -84
  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 +77 -26
  27. package/dist/migration/diff.cjs.map +1 -1
  28. package/dist/migration/diff.d.cts +4 -4
  29. package/dist/migration/diff.d.ts +4 -4
  30. package/dist/migration/diff.js +77 -26
  31. package/dist/migration/diff.js.map +1 -1
  32. package/dist/migration/generator.cjs +81 -34
  33. package/dist/migration/generator.cjs.map +1 -1
  34. package/dist/migration/generator.d.cts +2 -2
  35. package/dist/migration/generator.d.ts +2 -2
  36. package/dist/migration/generator.js +81 -34
  37. package/dist/migration/generator.js.map +1 -1
  38. package/dist/migration/index.cjs +209 -78
  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 +209 -78
  43. package/dist/migration/index.js.map +1 -1
  44. package/dist/migration/snapshot.cjs +51 -18
  45. package/dist/migration/snapshot.cjs.map +1 -1
  46. package/dist/migration/snapshot.d.cts +2 -2
  47. package/dist/migration/snapshot.d.ts +2 -2
  48. package/dist/migration/snapshot.js +51 -18
  49. package/dist/migration/snapshot.js.map +1 -1
  50. package/dist/migration/utils/index.cjs +5 -3
  51. package/dist/migration/utils/index.cjs.map +1 -1
  52. package/dist/migration/utils/index.d.cts +4 -4
  53. package/dist/migration/utils/index.d.ts +4 -4
  54. package/dist/migration/utils/index.js +5 -3
  55. package/dist/migration/utils/index.js.map +1 -1
  56. package/dist/schema.cjs +55 -6
  57. package/dist/schema.cjs.map +1 -1
  58. package/dist/schema.d.cts +1 -1
  59. package/dist/schema.d.ts +1 -1
  60. package/dist/schema.js +55 -6
  61. package/dist/schema.js.map +1 -1
  62. package/dist/{type-mapper-DrQmtznD.d.cts → type-mapper-DsGgZwUo.d.cts} +1 -1
  63. package/dist/{type-mapper-n231Fspm.d.ts → type-mapper-Dvh4QTM-.d.ts} +1 -1
  64. package/dist/{types-YoBjsa-A.d.cts → types-CVxPCgWX.d.cts} +1 -1
  65. package/dist/{types-Ds3NQvny.d.ts → types-Dfp-NP2D.d.ts} +1 -1
  66. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## [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)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * update field removal method ([f673f5c](https://github.com/dastron/pocketbase-zod-schema/commit/f673f5c06e972ab4f556666469931c3729614bd4))
9
+
10
+ ## [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)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * add collection id ([2ee0808](https://github.com/dastron/pocketbase-zod-schema/commit/2ee08082f5a27b76a277e37427ba16011046a1cc))
16
+ * Add Collection Id on Create ([c911b94](https://github.com/dastron/pocketbase-zod-schema/commit/c911b946b34317387bce06a70ad14f3718658a11))
17
+
3
18
  ## [0.3.0](https://github.com/dastron/pocketbase-zod-schema/compare/pocketbase-zod-schema-v0.2.5...pocketbase-zod-schema-v0.3.0) (2026-01-04)
4
19
 
5
20
 
@@ -1519,17 +1519,19 @@ var CollectionIdRegistry = class {
1519
1519
  }
1520
1520
  /**
1521
1521
  * Generates a unique collection ID for a given collection name
1522
- * Special case: Returns constant "_pb_users_auth_" for users collection
1523
1522
  * Retries up to 10 times if collision occurs (extremely rare)
1523
+ * Special case: returns "_pb_users_auth_" for users collection
1524
1524
  *
1525
- * @param collectionName - The name of the collection
1525
+ * @param collectionName - The name of the collection (optional)
1526
1526
  * @returns A unique collection ID
1527
1527
  * @throws Error if unable to generate unique ID after max attempts
1528
1528
  */
1529
1529
  generate(collectionName) {
1530
1530
  if (collectionName && collectionName.toLowerCase() === "users") {
1531
1531
  const usersId = "_pb_users_auth_";
1532
- this.register(usersId);
1532
+ if (!this.has(usersId)) {
1533
+ this.register(usersId);
1534
+ }
1533
1535
  return usersId;
1534
1536
  }
1535
1537
  const maxAttempts = 10;
@@ -1702,18 +1704,49 @@ function compareFieldConstraints(currentField, previousField) {
1702
1704
  }
1703
1705
  return changes;
1704
1706
  }
1707
+ function normalizeOptionValue(key, value, fieldType) {
1708
+ if (key === "maxSelect" && value === 1 && (fieldType === "select" || fieldType === "file")) {
1709
+ return void 0;
1710
+ }
1711
+ if (key === "maxSize" && value === 0 && fieldType === "file") {
1712
+ return void 0;
1713
+ }
1714
+ if (fieldType === "file") {
1715
+ if (key === "mimeTypes" && Array.isArray(value) && value.length === 0) {
1716
+ return void 0;
1717
+ }
1718
+ if (key === "thumbs" && Array.isArray(value) && value.length === 0) {
1719
+ return void 0;
1720
+ }
1721
+ if (key === "protected" && value === false) {
1722
+ return void 0;
1723
+ }
1724
+ }
1725
+ if (fieldType === "autodate") {
1726
+ if (key === "onCreate" && value === true) {
1727
+ return void 0;
1728
+ }
1729
+ if (key === "onUpdate" && value === false) {
1730
+ return void 0;
1731
+ }
1732
+ }
1733
+ return value;
1734
+ }
1705
1735
  function compareFieldOptions(currentField, previousField) {
1706
1736
  const changes = [];
1707
1737
  const currentOptions = currentField.options || {};
1708
1738
  const previousOptions = previousField.options || {};
1709
1739
  const allKeys = /* @__PURE__ */ new Set([...Object.keys(currentOptions), ...Object.keys(previousOptions)]);
1740
+ const fieldType = currentField.type;
1710
1741
  for (const key of allKeys) {
1711
1742
  const currentValue = currentOptions[key];
1712
1743
  const previousValue = previousOptions[key];
1713
- if (currentValue === void 0 && previousValue === void 0) {
1744
+ const normalizedCurrent = normalizeOptionValue(key, currentValue, fieldType);
1745
+ const normalizedPrevious = normalizeOptionValue(key, previousValue, fieldType);
1746
+ if (normalizedCurrent === void 0 && normalizedPrevious === void 0) {
1714
1747
  continue;
1715
1748
  }
1716
- if (!areValuesEqual(currentValue, previousValue)) {
1749
+ if (!areValuesEqual(normalizedCurrent, normalizedPrevious)) {
1717
1750
  changes.push({
1718
1751
  property: `options.${key}`,
1719
1752
  oldValue: previousValue,
@@ -1723,7 +1756,7 @@ function compareFieldOptions(currentField, previousField) {
1723
1756
  }
1724
1757
  return changes;
1725
1758
  }
1726
- function compareRelationConfigurations(currentField, previousField) {
1759
+ function compareRelationConfigurations(currentField, previousField, collectionIdToName) {
1727
1760
  const changes = [];
1728
1761
  const currentRelation = currentField.relation;
1729
1762
  const previousRelation = previousField.relation;
@@ -1735,8 +1768,8 @@ function compareRelationConfigurations(currentField, previousField) {
1735
1768
  }
1736
1769
  const normalizeCollection = (collection) => {
1737
1770
  if (!collection) return collection;
1738
- if (collection === "_pb_users_auth_") {
1739
- return "Users";
1771
+ if (collectionIdToName && collectionIdToName.has(collection)) {
1772
+ return collectionIdToName.get(collection);
1740
1773
  }
1741
1774
  const nameMatch = collection.match(/app\.findCollectionByNameOrId\s*\(\s*["']([^"']+)["']\s*\)/);
1742
1775
  if (nameMatch) {
@@ -1746,13 +1779,11 @@ function compareRelationConfigurations(currentField, previousField) {
1746
1779
  };
1747
1780
  const normalizedCurrent = normalizeCollection(currentRelation.collection);
1748
1781
  const normalizedPrevious = normalizeCollection(previousRelation.collection);
1749
- if (normalizedCurrent !== normalizedPrevious) {
1782
+ if (normalizedCurrent.toLowerCase() !== normalizedPrevious.toLowerCase()) {
1750
1783
  changes.push({
1751
1784
  property: "relation.collection",
1752
- oldValue: normalizedPrevious,
1753
- // Use normalized value for clarity
1754
- newValue: normalizedCurrent
1755
- // Use normalized value for clarity
1785
+ oldValue: previousRelation.collection,
1786
+ newValue: currentRelation.collection
1756
1787
  });
1757
1788
  }
1758
1789
  if (currentRelation.cascadeDelete !== previousRelation.cascadeDelete) {
@@ -1762,14 +1793,20 @@ function compareRelationConfigurations(currentField, previousField) {
1762
1793
  newValue: currentRelation.cascadeDelete
1763
1794
  });
1764
1795
  }
1765
- if (currentRelation.maxSelect !== previousRelation.maxSelect) {
1796
+ const normalizeMax = (val) => val === 1 ? null : val;
1797
+ const currentMax = normalizeMax(currentRelation.maxSelect);
1798
+ const previousMax = normalizeMax(previousRelation.maxSelect);
1799
+ if (currentMax != previousMax) {
1766
1800
  changes.push({
1767
1801
  property: "relation.maxSelect",
1768
1802
  oldValue: previousRelation.maxSelect,
1769
1803
  newValue: currentRelation.maxSelect
1770
1804
  });
1771
1805
  }
1772
- if (currentRelation.minSelect !== previousRelation.minSelect) {
1806
+ const normalizeMin = (val) => val === 0 ? null : val;
1807
+ const currentMin = normalizeMin(currentRelation.minSelect);
1808
+ const previousMin = normalizeMin(previousRelation.minSelect);
1809
+ if (currentMin != previousMin) {
1773
1810
  changes.push({
1774
1811
  property: "relation.minSelect",
1775
1812
  oldValue: previousRelation.minSelect,
@@ -1778,7 +1815,7 @@ function compareRelationConfigurations(currentField, previousField) {
1778
1815
  }
1779
1816
  return changes;
1780
1817
  }
1781
- function detectFieldChanges(currentField, previousField) {
1818
+ function detectFieldChanges(currentField, previousField, collectionIdToName) {
1782
1819
  const changes = [];
1783
1820
  const typeChange = compareFieldTypes(currentField, previousField);
1784
1821
  if (typeChange) {
@@ -1787,7 +1824,7 @@ function detectFieldChanges(currentField, previousField) {
1787
1824
  changes.push(...compareFieldConstraints(currentField, previousField));
1788
1825
  changes.push(...compareFieldOptions(currentField, previousField));
1789
1826
  if (currentField.type === "relation" && previousField.type === "relation") {
1790
- changes.push(...compareRelationConfigurations(currentField, previousField));
1827
+ changes.push(...compareRelationConfigurations(currentField, previousField, collectionIdToName));
1791
1828
  }
1792
1829
  return changes;
1793
1830
  }
@@ -1798,7 +1835,7 @@ function compareIndexes(currentIndexes = [], previousIndexes = []) {
1798
1835
  const indexesToRemove = previousIndexes.filter((idx) => !currentSet.has(idx));
1799
1836
  return { indexesToAdd, indexesToRemove };
1800
1837
  }
1801
- function compareRules(currentRules, previousRules) {
1838
+ function compareRules(currentRules, previousRules, currentPermissions, previousPermissions) {
1802
1839
  const updates = [];
1803
1840
  const ruleTypes = [
1804
1841
  "listRule",
@@ -1809,8 +1846,8 @@ function compareRules(currentRules, previousRules) {
1809
1846
  "manageRule"
1810
1847
  ];
1811
1848
  for (const ruleType of ruleTypes) {
1812
- const currentValue = currentRules?.[ruleType] ?? null;
1813
- const previousValue = previousRules?.[ruleType] ?? null;
1849
+ const currentValue = currentRules?.[ruleType] ?? currentPermissions?.[ruleType] ?? null;
1850
+ const previousValue = previousRules?.[ruleType] ?? previousPermissions?.[ruleType] ?? null;
1814
1851
  if (currentValue !== previousValue) {
1815
1852
  updates.push({
1816
1853
  ruleType,
@@ -1837,7 +1874,7 @@ function comparePermissions(currentPermissions, previousPermissions) {
1837
1874
  }
1838
1875
  return changes;
1839
1876
  }
1840
- function compareCollectionFields(currentCollection, previousCollection, config) {
1877
+ function compareCollectionFields(currentCollection, previousCollection, config, collectionIdToName) {
1841
1878
  let fieldsToAdd = findNewFields(currentCollection.fields, previousCollection.fields);
1842
1879
  const fieldsToRemove = findRemovedFields(currentCollection.fields, previousCollection.fields);
1843
1880
  const fieldsToModify = [];
@@ -1847,7 +1884,7 @@ function compareCollectionFields(currentCollection, previousCollection, config)
1847
1884
  }
1848
1885
  const matchedFields = matchFieldsByName(currentCollection.fields, previousCollection.fields);
1849
1886
  for (const [currentField, previousField] of matchedFields) {
1850
- const changes = detectFieldChanges(currentField, previousField);
1887
+ const changes = detectFieldChanges(currentField, previousField, collectionIdToName);
1851
1888
  if (changes.length > 0) {
1852
1889
  fieldsToModify.push({
1853
1890
  fieldName: currentField.name,
@@ -1859,14 +1896,20 @@ function compareCollectionFields(currentCollection, previousCollection, config)
1859
1896
  }
1860
1897
  return { fieldsToAdd, fieldsToRemove, fieldsToModify };
1861
1898
  }
1862
- function buildCollectionModification(currentCollection, previousCollection, config) {
1899
+ function buildCollectionModification(currentCollection, previousCollection, config, collectionIdToName) {
1863
1900
  const { fieldsToAdd, fieldsToRemove, fieldsToModify } = compareCollectionFields(
1864
1901
  currentCollection,
1865
1902
  previousCollection,
1866
- config
1903
+ config,
1904
+ collectionIdToName
1867
1905
  );
1868
1906
  const { indexesToAdd, indexesToRemove } = compareIndexes(currentCollection.indexes, previousCollection.indexes);
1869
- const rulesToUpdate = compareRules(currentCollection.rules, previousCollection.rules);
1907
+ const rulesToUpdate = compareRules(
1908
+ currentCollection.rules,
1909
+ previousCollection.rules,
1910
+ currentCollection.permissions,
1911
+ previousCollection.permissions
1912
+ );
1870
1913
  const permissionsToUpdate = comparePermissions(currentCollection.permissions, previousCollection.permissions);
1871
1914
  return {
1872
1915
  collection: currentCollection.name,
@@ -1883,6 +1926,14 @@ function hasChanges(modification) {
1883
1926
  return modification.fieldsToAdd.length > 0 || modification.fieldsToRemove.length > 0 || modification.fieldsToModify.length > 0 || modification.indexesToAdd.length > 0 || modification.indexesToRemove.length > 0 || modification.rulesToUpdate.length > 0 || modification.permissionsToUpdate.length > 0;
1884
1927
  }
1885
1928
  function aggregateChanges(currentSchema, previousSnapshot, config) {
1929
+ const collectionIdToName = /* @__PURE__ */ new Map();
1930
+ if (previousSnapshot) {
1931
+ for (const [name, collection] of previousSnapshot.collections) {
1932
+ if (collection.id) {
1933
+ collectionIdToName.set(collection.id, name);
1934
+ }
1935
+ }
1936
+ }
1886
1937
  const collectionsToCreate = findNewCollections(currentSchema, previousSnapshot);
1887
1938
  const collectionsToDelete = findRemovedCollections(currentSchema, previousSnapshot);
1888
1939
  const filteredCollectionsToCreate = collectionsToCreate.filter(
@@ -1906,7 +1957,7 @@ function aggregateChanges(currentSchema, previousSnapshot, config) {
1906
1957
  const collectionsToModify = [];
1907
1958
  const matchedCollections = matchCollectionsByName(currentSchema, previousSnapshot);
1908
1959
  for (const [currentCollection, previousCollection] of matchedCollections) {
1909
- const modification = buildCollectionModification(currentCollection, previousCollection, config);
1960
+ const modification = buildCollectionModification(currentCollection, previousCollection, config, collectionIdToName);
1910
1961
  if (hasChanges(modification)) {
1911
1962
  collectionsToModify.push(modification);
1912
1963
  }
@@ -2117,7 +2168,7 @@ function formatValue(value) {
2117
2168
  return JSON.stringify(value).replace(/","/g, '", "');
2118
2169
  }
2119
2170
  if (typeof value === "object") {
2120
- const entries = Object.entries(value).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
2171
+ const entries = Object.entries(value).filter(([_k, v]) => v !== void 0).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
2121
2172
  return `{ ${entries} }`;
2122
2173
  }
2123
2174
  return String(value);
@@ -2286,6 +2337,9 @@ function getSystemFields() {
2286
2337
  function generateCollectionCreation(collection, varName = "collection", isLast = false, collectionIdMap) {
2287
2338
  const lines = [];
2288
2339
  lines.push(` const ${varName} = new Collection({`);
2340
+ if (collection.id) {
2341
+ lines.push(` id: ${formatValue(collection.id)},`);
2342
+ }
2289
2343
  lines.push(` name: "${collection.name}",`);
2290
2344
  lines.push(` type: "${collection.type}",`);
2291
2345
  const permissionsCode = generateCollectionPermissions(collection.permissions);
@@ -2408,11 +2462,9 @@ function generateFieldModification(collectionName, modification, varName, isLast
2408
2462
  function generateFieldDeletion(collectionName, fieldName, varName, isLast = false) {
2409
2463
  const lines = [];
2410
2464
  const collectionVar = varName || `collection_${collectionName}_${fieldName}`;
2411
- const fieldVar = `${collectionVar}_field`;
2412
2465
  lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
2413
- lines.push(` const ${fieldVar} = ${collectionVar}.fields.getByName("${fieldName}");`);
2414
2466
  lines.push(``);
2415
- lines.push(` ${collectionVar}.fields.remove(${fieldVar}.id);`);
2467
+ lines.push(` ${collectionVar}.fields.removeByName("${fieldName}");`);
2416
2468
  lines.push(``);
2417
2469
  lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
2418
2470
  return lines.join("\n");
@@ -2529,7 +2581,27 @@ function generateOperationUpMigration(operation, collectionIdMap) {
2529
2581
  const varName = `collection_${collectionName}`;
2530
2582
  lines.push(generateCollectionDeletion(collectionName, varName, true));
2531
2583
  }
2532
- return lines.join("\n");
2584
+ let code = lines.join("\n");
2585
+ const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
2586
+ if (!hasReturnStatement) {
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);
2601
+ }
2602
+ }
2603
+ }
2604
+ return code;
2533
2605
  }
2534
2606
  function generateOperationDownMigration(operation, collectionIdMap) {
2535
2607
  const lines = [];
@@ -2614,7 +2686,27 @@ function generateOperationDownMigration(operation, collectionIdMap) {
2614
2686
  lines.push(generateCollectionCreation(collection, varName, true, collectionIdMap));
2615
2687
  }
2616
2688
  }
2617
- return lines.join("\n");
2689
+ let code = lines.join("\n");
2690
+ const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
2691
+ if (!hasReturnStatement) {
2692
+ const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2693
+ const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2694
+ const saveMatches = [...code.matchAll(savePattern)];
2695
+ const deleteMatches = [...code.matchAll(deletePattern)];
2696
+ const allMatches = [
2697
+ ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2698
+ ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2699
+ ].sort((a, b) => b.index - a.index);
2700
+ if (allMatches.length > 0) {
2701
+ const lastMatch = allMatches[0];
2702
+ if (lastMatch.type === "save") {
2703
+ 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);
2704
+ } else {
2705
+ 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);
2706
+ }
2707
+ }
2708
+ }
2709
+ return code;
2618
2710
  }
2619
2711
  function generate(diff, config) {
2620
2712
  const normalizedConfig = typeof config === "string" ? { migrationDir: config } : config;
@@ -2663,7 +2755,7 @@ function generate(diff, config) {
2663
2755
  var SNAPSHOT_VERSION = "1.0.0";
2664
2756
  function resolveCollectionIdToName(collectionId) {
2665
2757
  if (collectionId === "_pb_users_auth_") {
2666
- return "Users";
2758
+ return "users";
2667
2759
  }
2668
2760
  const nameMatch = collectionId.match(/app\.findCollectionByNameOrId\s*\(\s*["']([^"']+)["']\s*\)/);
2669
2761
  if (nameMatch) {
@@ -2671,6 +2763,39 @@ function resolveCollectionIdToName(collectionId) {
2671
2763
  }
2672
2764
  return collectionId;
2673
2765
  }
2766
+ function extractFieldOptions2(pbField) {
2767
+ const options = {};
2768
+ if (pbField.options && typeof pbField.options === "object") {
2769
+ Object.assign(options, pbField.options);
2770
+ }
2771
+ const directOptionKeys = [
2772
+ "min",
2773
+ "max",
2774
+ "pattern",
2775
+ "noDecimal",
2776
+ // text/number fields
2777
+ "values",
2778
+ "maxSelect",
2779
+ // select fields
2780
+ "mimeTypes",
2781
+ "maxSize",
2782
+ "thumbs",
2783
+ "protected",
2784
+ // file fields
2785
+ "onCreate",
2786
+ "onUpdate",
2787
+ // autodate fields
2788
+ "exceptDomains",
2789
+ "onlyDomains"
2790
+ // email/url fields
2791
+ ];
2792
+ for (const key of directOptionKeys) {
2793
+ if (pbField[key] !== void 0) {
2794
+ options[key] = pbField[key];
2795
+ }
2796
+ }
2797
+ return options;
2798
+ }
2674
2799
  function convertPocketBaseCollection(pbCollection) {
2675
2800
  const fields = [];
2676
2801
  const systemFieldNames = ["id", "created", "updated", "collectionId", "collectionName", "expand"];
@@ -2688,23 +2813,19 @@ function convertPocketBaseCollection(pbCollection) {
2688
2813
  type: pbField.type,
2689
2814
  required: pbField.required || false
2690
2815
  };
2691
- field.options = pbField.options ? { ...pbField.options } : {};
2692
- if (pbField.type === "select") {
2693
- if (pbField.values && Array.isArray(pbField.values)) {
2694
- field.options.values = pbField.values;
2695
- } else if (pbField.options?.values && Array.isArray(pbField.options.values)) {
2696
- field.options.values = pbField.options.values;
2697
- }
2698
- }
2816
+ field.options = extractFieldOptions2(pbField);
2699
2817
  if (pbField.type === "relation") {
2700
2818
  const collectionId = pbField.collectionId || pbField.options?.collectionId || "";
2701
- const collectionName = resolveCollectionIdToName(collectionId);
2819
+ const collectionName = resolveCollectionIdToName(collectionId || "");
2702
2820
  field.relation = {
2703
2821
  collection: collectionName,
2704
2822
  cascadeDelete: pbField.cascadeDelete ?? pbField.options?.cascadeDelete ?? false,
2705
2823
  maxSelect: pbField.maxSelect ?? pbField.options?.maxSelect,
2706
2824
  minSelect: pbField.minSelect ?? pbField.options?.minSelect
2707
2825
  };
2826
+ delete field.options.maxSelect;
2827
+ delete field.options.minSelect;
2828
+ delete field.options.cascadeDelete;
2708
2829
  }
2709
2830
  const hasOnlyValues = Object.keys(field.options).length === 1 && field.options.values !== void 0;
2710
2831
  if (Object.keys(field.options).length === 0) {
@@ -2718,17 +2839,21 @@ function convertPocketBaseCollection(pbCollection) {
2718
2839
  type: pbCollection.type || "base",
2719
2840
  fields
2720
2841
  };
2842
+ if (pbCollection.id) {
2843
+ schema.id = pbCollection.id;
2844
+ }
2721
2845
  if (pbCollection.indexes && Array.isArray(pbCollection.indexes)) {
2722
2846
  schema.indexes = pbCollection.indexes;
2723
2847
  }
2724
- const rules = {};
2725
- if (pbCollection.listRule !== void 0) rules.listRule = pbCollection.listRule;
2726
- if (pbCollection.viewRule !== void 0) rules.viewRule = pbCollection.viewRule;
2727
- if (pbCollection.createRule !== void 0) rules.createRule = pbCollection.createRule;
2728
- if (pbCollection.updateRule !== void 0) rules.updateRule = pbCollection.updateRule;
2729
- if (pbCollection.deleteRule !== void 0) rules.deleteRule = pbCollection.deleteRule;
2730
- if (pbCollection.manageRule !== void 0) rules.manageRule = pbCollection.manageRule;
2731
- if (Object.keys(rules).length > 0) {
2848
+ const hasAnyRule = pbCollection.listRule !== void 0 || pbCollection.viewRule !== void 0 || pbCollection.createRule !== void 0 || pbCollection.updateRule !== void 0 || pbCollection.deleteRule !== void 0 || pbCollection.manageRule !== void 0;
2849
+ if (hasAnyRule) {
2850
+ const rules = {};
2851
+ if (pbCollection.listRule !== void 0) rules.listRule = pbCollection.listRule;
2852
+ if (pbCollection.viewRule !== void 0) rules.viewRule = pbCollection.viewRule;
2853
+ if (pbCollection.createRule !== void 0) rules.createRule = pbCollection.createRule;
2854
+ if (pbCollection.updateRule !== void 0) rules.updateRule = pbCollection.updateRule;
2855
+ if (pbCollection.deleteRule !== void 0) rules.deleteRule = pbCollection.deleteRule;
2856
+ if (pbCollection.manageRule !== void 0) rules.manageRule = pbCollection.manageRule;
2732
2857
  schema.rules = rules;
2733
2858
  schema.permissions = { ...rules };
2734
2859
  }