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
@@ -1524,17 +1524,19 @@ var CollectionIdRegistry = class {
1524
1524
  }
1525
1525
  /**
1526
1526
  * Generates a unique collection ID for a given collection name
1527
- * Special case: Returns constant "_pb_users_auth_" for users collection
1528
1527
  * Retries up to 10 times if collision occurs (extremely rare)
1528
+ * Special case: returns "_pb_users_auth_" for users collection
1529
1529
  *
1530
- * @param collectionName - The name of the collection
1530
+ * @param collectionName - The name of the collection (optional)
1531
1531
  * @returns A unique collection ID
1532
1532
  * @throws Error if unable to generate unique ID after max attempts
1533
1533
  */
1534
1534
  generate(collectionName) {
1535
1535
  if (collectionName && collectionName.toLowerCase() === "users") {
1536
1536
  const usersId = "_pb_users_auth_";
1537
- this.register(usersId);
1537
+ if (!this.has(usersId)) {
1538
+ this.register(usersId);
1539
+ }
1538
1540
  return usersId;
1539
1541
  }
1540
1542
  const maxAttempts = 10;
@@ -1707,18 +1709,49 @@ function compareFieldConstraints(currentField, previousField) {
1707
1709
  }
1708
1710
  return changes;
1709
1711
  }
1712
+ function normalizeOptionValue(key, value, fieldType) {
1713
+ if (key === "maxSelect" && value === 1 && (fieldType === "select" || fieldType === "file")) {
1714
+ return void 0;
1715
+ }
1716
+ if (key === "maxSize" && value === 0 && fieldType === "file") {
1717
+ return void 0;
1718
+ }
1719
+ if (fieldType === "file") {
1720
+ if (key === "mimeTypes" && Array.isArray(value) && value.length === 0) {
1721
+ return void 0;
1722
+ }
1723
+ if (key === "thumbs" && Array.isArray(value) && value.length === 0) {
1724
+ return void 0;
1725
+ }
1726
+ if (key === "protected" && value === false) {
1727
+ return void 0;
1728
+ }
1729
+ }
1730
+ if (fieldType === "autodate") {
1731
+ if (key === "onCreate" && value === true) {
1732
+ return void 0;
1733
+ }
1734
+ if (key === "onUpdate" && value === false) {
1735
+ return void 0;
1736
+ }
1737
+ }
1738
+ return value;
1739
+ }
1710
1740
  function compareFieldOptions(currentField, previousField) {
1711
1741
  const changes = [];
1712
1742
  const currentOptions = currentField.options || {};
1713
1743
  const previousOptions = previousField.options || {};
1714
1744
  const allKeys = /* @__PURE__ */ new Set([...Object.keys(currentOptions), ...Object.keys(previousOptions)]);
1745
+ const fieldType = currentField.type;
1715
1746
  for (const key of allKeys) {
1716
1747
  const currentValue = currentOptions[key];
1717
1748
  const previousValue = previousOptions[key];
1718
- if (currentValue === void 0 && previousValue === void 0) {
1749
+ const normalizedCurrent = normalizeOptionValue(key, currentValue, fieldType);
1750
+ const normalizedPrevious = normalizeOptionValue(key, previousValue, fieldType);
1751
+ if (normalizedCurrent === void 0 && normalizedPrevious === void 0) {
1719
1752
  continue;
1720
1753
  }
1721
- if (!areValuesEqual(currentValue, previousValue)) {
1754
+ if (!areValuesEqual(normalizedCurrent, normalizedPrevious)) {
1722
1755
  changes.push({
1723
1756
  property: `options.${key}`,
1724
1757
  oldValue: previousValue,
@@ -1728,7 +1761,7 @@ function compareFieldOptions(currentField, previousField) {
1728
1761
  }
1729
1762
  return changes;
1730
1763
  }
1731
- function compareRelationConfigurations(currentField, previousField) {
1764
+ function compareRelationConfigurations(currentField, previousField, collectionIdToName) {
1732
1765
  const changes = [];
1733
1766
  const currentRelation = currentField.relation;
1734
1767
  const previousRelation = previousField.relation;
@@ -1740,8 +1773,8 @@ function compareRelationConfigurations(currentField, previousField) {
1740
1773
  }
1741
1774
  const normalizeCollection = (collection) => {
1742
1775
  if (!collection) return collection;
1743
- if (collection === "_pb_users_auth_") {
1744
- return "Users";
1776
+ if (collectionIdToName && collectionIdToName.has(collection)) {
1777
+ return collectionIdToName.get(collection);
1745
1778
  }
1746
1779
  const nameMatch = collection.match(/app\.findCollectionByNameOrId\s*\(\s*["']([^"']+)["']\s*\)/);
1747
1780
  if (nameMatch) {
@@ -1751,13 +1784,11 @@ function compareRelationConfigurations(currentField, previousField) {
1751
1784
  };
1752
1785
  const normalizedCurrent = normalizeCollection(currentRelation.collection);
1753
1786
  const normalizedPrevious = normalizeCollection(previousRelation.collection);
1754
- if (normalizedCurrent !== normalizedPrevious) {
1787
+ if (normalizedCurrent.toLowerCase() !== normalizedPrevious.toLowerCase()) {
1755
1788
  changes.push({
1756
1789
  property: "relation.collection",
1757
- oldValue: normalizedPrevious,
1758
- // Use normalized value for clarity
1759
- newValue: normalizedCurrent
1760
- // Use normalized value for clarity
1790
+ oldValue: previousRelation.collection,
1791
+ newValue: currentRelation.collection
1761
1792
  });
1762
1793
  }
1763
1794
  if (currentRelation.cascadeDelete !== previousRelation.cascadeDelete) {
@@ -1767,14 +1798,20 @@ function compareRelationConfigurations(currentField, previousField) {
1767
1798
  newValue: currentRelation.cascadeDelete
1768
1799
  });
1769
1800
  }
1770
- if (currentRelation.maxSelect !== previousRelation.maxSelect) {
1801
+ const normalizeMax = (val) => val === 1 ? null : val;
1802
+ const currentMax = normalizeMax(currentRelation.maxSelect);
1803
+ const previousMax = normalizeMax(previousRelation.maxSelect);
1804
+ if (currentMax != previousMax) {
1771
1805
  changes.push({
1772
1806
  property: "relation.maxSelect",
1773
1807
  oldValue: previousRelation.maxSelect,
1774
1808
  newValue: currentRelation.maxSelect
1775
1809
  });
1776
1810
  }
1777
- if (currentRelation.minSelect !== previousRelation.minSelect) {
1811
+ const normalizeMin = (val) => val === 0 ? null : val;
1812
+ const currentMin = normalizeMin(currentRelation.minSelect);
1813
+ const previousMin = normalizeMin(previousRelation.minSelect);
1814
+ if (currentMin != previousMin) {
1778
1815
  changes.push({
1779
1816
  property: "relation.minSelect",
1780
1817
  oldValue: previousRelation.minSelect,
@@ -1783,7 +1820,7 @@ function compareRelationConfigurations(currentField, previousField) {
1783
1820
  }
1784
1821
  return changes;
1785
1822
  }
1786
- function detectFieldChanges(currentField, previousField) {
1823
+ function detectFieldChanges(currentField, previousField, collectionIdToName) {
1787
1824
  const changes = [];
1788
1825
  const typeChange = compareFieldTypes(currentField, previousField);
1789
1826
  if (typeChange) {
@@ -1792,7 +1829,7 @@ function detectFieldChanges(currentField, previousField) {
1792
1829
  changes.push(...compareFieldConstraints(currentField, previousField));
1793
1830
  changes.push(...compareFieldOptions(currentField, previousField));
1794
1831
  if (currentField.type === "relation" && previousField.type === "relation") {
1795
- changes.push(...compareRelationConfigurations(currentField, previousField));
1832
+ changes.push(...compareRelationConfigurations(currentField, previousField, collectionIdToName));
1796
1833
  }
1797
1834
  return changes;
1798
1835
  }
@@ -1803,7 +1840,7 @@ function compareIndexes(currentIndexes = [], previousIndexes = []) {
1803
1840
  const indexesToRemove = previousIndexes.filter((idx) => !currentSet.has(idx));
1804
1841
  return { indexesToAdd, indexesToRemove };
1805
1842
  }
1806
- function compareRules(currentRules, previousRules) {
1843
+ function compareRules(currentRules, previousRules, currentPermissions, previousPermissions) {
1807
1844
  const updates = [];
1808
1845
  const ruleTypes = [
1809
1846
  "listRule",
@@ -1814,8 +1851,8 @@ function compareRules(currentRules, previousRules) {
1814
1851
  "manageRule"
1815
1852
  ];
1816
1853
  for (const ruleType of ruleTypes) {
1817
- const currentValue = currentRules?.[ruleType] ?? null;
1818
- const previousValue = previousRules?.[ruleType] ?? null;
1854
+ const currentValue = currentRules?.[ruleType] ?? currentPermissions?.[ruleType] ?? null;
1855
+ const previousValue = previousRules?.[ruleType] ?? previousPermissions?.[ruleType] ?? null;
1819
1856
  if (currentValue !== previousValue) {
1820
1857
  updates.push({
1821
1858
  ruleType,
@@ -1842,7 +1879,7 @@ function comparePermissions(currentPermissions, previousPermissions) {
1842
1879
  }
1843
1880
  return changes;
1844
1881
  }
1845
- function compareCollectionFields(currentCollection, previousCollection, config) {
1882
+ function compareCollectionFields(currentCollection, previousCollection, config, collectionIdToName) {
1846
1883
  let fieldsToAdd = findNewFields(currentCollection.fields, previousCollection.fields);
1847
1884
  const fieldsToRemove = findRemovedFields(currentCollection.fields, previousCollection.fields);
1848
1885
  const fieldsToModify = [];
@@ -1852,7 +1889,7 @@ function compareCollectionFields(currentCollection, previousCollection, config)
1852
1889
  }
1853
1890
  const matchedFields = matchFieldsByName(currentCollection.fields, previousCollection.fields);
1854
1891
  for (const [currentField, previousField] of matchedFields) {
1855
- const changes = detectFieldChanges(currentField, previousField);
1892
+ const changes = detectFieldChanges(currentField, previousField, collectionIdToName);
1856
1893
  if (changes.length > 0) {
1857
1894
  fieldsToModify.push({
1858
1895
  fieldName: currentField.name,
@@ -1864,14 +1901,20 @@ function compareCollectionFields(currentCollection, previousCollection, config)
1864
1901
  }
1865
1902
  return { fieldsToAdd, fieldsToRemove, fieldsToModify };
1866
1903
  }
1867
- function buildCollectionModification(currentCollection, previousCollection, config) {
1904
+ function buildCollectionModification(currentCollection, previousCollection, config, collectionIdToName) {
1868
1905
  const { fieldsToAdd, fieldsToRemove, fieldsToModify } = compareCollectionFields(
1869
1906
  currentCollection,
1870
1907
  previousCollection,
1871
- config
1908
+ config,
1909
+ collectionIdToName
1872
1910
  );
1873
1911
  const { indexesToAdd, indexesToRemove } = compareIndexes(currentCollection.indexes, previousCollection.indexes);
1874
- const rulesToUpdate = compareRules(currentCollection.rules, previousCollection.rules);
1912
+ const rulesToUpdate = compareRules(
1913
+ currentCollection.rules,
1914
+ previousCollection.rules,
1915
+ currentCollection.permissions,
1916
+ previousCollection.permissions
1917
+ );
1875
1918
  const permissionsToUpdate = comparePermissions(currentCollection.permissions, previousCollection.permissions);
1876
1919
  return {
1877
1920
  collection: currentCollection.name,
@@ -1888,6 +1931,14 @@ function hasChanges(modification) {
1888
1931
  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;
1889
1932
  }
1890
1933
  function aggregateChanges(currentSchema, previousSnapshot, config) {
1934
+ const collectionIdToName = /* @__PURE__ */ new Map();
1935
+ if (previousSnapshot) {
1936
+ for (const [name, collection] of previousSnapshot.collections) {
1937
+ if (collection.id) {
1938
+ collectionIdToName.set(collection.id, name);
1939
+ }
1940
+ }
1941
+ }
1891
1942
  const collectionsToCreate = findNewCollections(currentSchema, previousSnapshot);
1892
1943
  const collectionsToDelete = findRemovedCollections(currentSchema, previousSnapshot);
1893
1944
  const filteredCollectionsToCreate = collectionsToCreate.filter(
@@ -1911,7 +1962,7 @@ function aggregateChanges(currentSchema, previousSnapshot, config) {
1911
1962
  const collectionsToModify = [];
1912
1963
  const matchedCollections = matchCollectionsByName(currentSchema, previousSnapshot);
1913
1964
  for (const [currentCollection, previousCollection] of matchedCollections) {
1914
- const modification = buildCollectionModification(currentCollection, previousCollection, config);
1965
+ const modification = buildCollectionModification(currentCollection, previousCollection, config, collectionIdToName);
1915
1966
  if (hasChanges(modification)) {
1916
1967
  collectionsToModify.push(modification);
1917
1968
  }
@@ -2122,7 +2173,7 @@ function formatValue(value) {
2122
2173
  return JSON.stringify(value).replace(/","/g, '", "');
2123
2174
  }
2124
2175
  if (typeof value === "object") {
2125
- const entries = Object.entries(value).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
2176
+ const entries = Object.entries(value).filter(([_k, v]) => v !== void 0).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
2126
2177
  return `{ ${entries} }`;
2127
2178
  }
2128
2179
  return String(value);
@@ -2291,6 +2342,9 @@ function getSystemFields() {
2291
2342
  function generateCollectionCreation(collection, varName = "collection", isLast = false, collectionIdMap) {
2292
2343
  const lines = [];
2293
2344
  lines.push(` const ${varName} = new Collection({`);
2345
+ if (collection.id) {
2346
+ lines.push(` id: ${formatValue(collection.id)},`);
2347
+ }
2294
2348
  lines.push(` name: "${collection.name}",`);
2295
2349
  lines.push(` type: "${collection.type}",`);
2296
2350
  const permissionsCode = generateCollectionPermissions(collection.permissions);
@@ -2413,11 +2467,9 @@ function generateFieldModification(collectionName, modification, varName, isLast
2413
2467
  function generateFieldDeletion(collectionName, fieldName, varName, isLast = false) {
2414
2468
  const lines = [];
2415
2469
  const collectionVar = varName || `collection_${collectionName}_${fieldName}`;
2416
- const fieldVar = `${collectionVar}_field`;
2417
2470
  lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
2418
- lines.push(` const ${fieldVar} = ${collectionVar}.fields.getByName("${fieldName}");`);
2419
2471
  lines.push(``);
2420
- lines.push(` ${collectionVar}.fields.remove(${fieldVar}.id);`);
2472
+ lines.push(` ${collectionVar}.fields.removeByName("${fieldName}");`);
2421
2473
  lines.push(``);
2422
2474
  lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
2423
2475
  return lines.join("\n");
@@ -2534,7 +2586,27 @@ function generateOperationUpMigration(operation, collectionIdMap) {
2534
2586
  const varName = `collection_${collectionName}`;
2535
2587
  lines.push(generateCollectionDeletion(collectionName, varName, true));
2536
2588
  }
2537
- return lines.join("\n");
2589
+ let code = lines.join("\n");
2590
+ const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
2591
+ if (!hasReturnStatement) {
2592
+ const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2593
+ const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2594
+ const saveMatches = [...code.matchAll(savePattern)];
2595
+ const deleteMatches = [...code.matchAll(deletePattern)];
2596
+ const allMatches = [
2597
+ ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2598
+ ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2599
+ ].sort((a, b) => b.index - a.index);
2600
+ if (allMatches.length > 0) {
2601
+ const lastMatch = allMatches[0];
2602
+ if (lastMatch.type === "save") {
2603
+ 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);
2604
+ } else {
2605
+ 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);
2606
+ }
2607
+ }
2608
+ }
2609
+ return code;
2538
2610
  }
2539
2611
  function generateOperationDownMigration(operation, collectionIdMap) {
2540
2612
  const lines = [];
@@ -2619,7 +2691,27 @@ function generateOperationDownMigration(operation, collectionIdMap) {
2619
2691
  lines.push(generateCollectionCreation(collection, varName, true, collectionIdMap));
2620
2692
  }
2621
2693
  }
2622
- return lines.join("\n");
2694
+ let code = lines.join("\n");
2695
+ const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
2696
+ if (!hasReturnStatement) {
2697
+ const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
2698
+ const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
2699
+ const saveMatches = [...code.matchAll(savePattern)];
2700
+ const deleteMatches = [...code.matchAll(deletePattern)];
2701
+ const allMatches = [
2702
+ ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
2703
+ ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
2704
+ ].sort((a, b) => b.index - a.index);
2705
+ if (allMatches.length > 0) {
2706
+ const lastMatch = allMatches[0];
2707
+ if (lastMatch.type === "save") {
2708
+ 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);
2709
+ } else {
2710
+ 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);
2711
+ }
2712
+ }
2713
+ }
2714
+ return code;
2623
2715
  }
2624
2716
  function generate(diff, config) {
2625
2717
  const normalizedConfig = typeof config === "string" ? { migrationDir: config } : config;
@@ -2668,7 +2760,7 @@ function generate(diff, config) {
2668
2760
  var SNAPSHOT_VERSION = "1.0.0";
2669
2761
  function resolveCollectionIdToName(collectionId) {
2670
2762
  if (collectionId === "_pb_users_auth_") {
2671
- return "Users";
2763
+ return "users";
2672
2764
  }
2673
2765
  const nameMatch = collectionId.match(/app\.findCollectionByNameOrId\s*\(\s*["']([^"']+)["']\s*\)/);
2674
2766
  if (nameMatch) {
@@ -2676,6 +2768,39 @@ function resolveCollectionIdToName(collectionId) {
2676
2768
  }
2677
2769
  return collectionId;
2678
2770
  }
2771
+ function extractFieldOptions2(pbField) {
2772
+ const options = {};
2773
+ if (pbField.options && typeof pbField.options === "object") {
2774
+ Object.assign(options, pbField.options);
2775
+ }
2776
+ const directOptionKeys = [
2777
+ "min",
2778
+ "max",
2779
+ "pattern",
2780
+ "noDecimal",
2781
+ // text/number fields
2782
+ "values",
2783
+ "maxSelect",
2784
+ // select fields
2785
+ "mimeTypes",
2786
+ "maxSize",
2787
+ "thumbs",
2788
+ "protected",
2789
+ // file fields
2790
+ "onCreate",
2791
+ "onUpdate",
2792
+ // autodate fields
2793
+ "exceptDomains",
2794
+ "onlyDomains"
2795
+ // email/url fields
2796
+ ];
2797
+ for (const key of directOptionKeys) {
2798
+ if (pbField[key] !== void 0) {
2799
+ options[key] = pbField[key];
2800
+ }
2801
+ }
2802
+ return options;
2803
+ }
2679
2804
  function convertPocketBaseCollection(pbCollection) {
2680
2805
  const fields = [];
2681
2806
  const systemFieldNames = ["id", "created", "updated", "collectionId", "collectionName", "expand"];
@@ -2693,23 +2818,19 @@ function convertPocketBaseCollection(pbCollection) {
2693
2818
  type: pbField.type,
2694
2819
  required: pbField.required || false
2695
2820
  };
2696
- field.options = pbField.options ? { ...pbField.options } : {};
2697
- if (pbField.type === "select") {
2698
- if (pbField.values && Array.isArray(pbField.values)) {
2699
- field.options.values = pbField.values;
2700
- } else if (pbField.options?.values && Array.isArray(pbField.options.values)) {
2701
- field.options.values = pbField.options.values;
2702
- }
2703
- }
2821
+ field.options = extractFieldOptions2(pbField);
2704
2822
  if (pbField.type === "relation") {
2705
2823
  const collectionId = pbField.collectionId || pbField.options?.collectionId || "";
2706
- const collectionName = resolveCollectionIdToName(collectionId);
2824
+ const collectionName = resolveCollectionIdToName(collectionId || "");
2707
2825
  field.relation = {
2708
2826
  collection: collectionName,
2709
2827
  cascadeDelete: pbField.cascadeDelete ?? pbField.options?.cascadeDelete ?? false,
2710
2828
  maxSelect: pbField.maxSelect ?? pbField.options?.maxSelect,
2711
2829
  minSelect: pbField.minSelect ?? pbField.options?.minSelect
2712
2830
  };
2831
+ delete field.options.maxSelect;
2832
+ delete field.options.minSelect;
2833
+ delete field.options.cascadeDelete;
2713
2834
  }
2714
2835
  const hasOnlyValues = Object.keys(field.options).length === 1 && field.options.values !== void 0;
2715
2836
  if (Object.keys(field.options).length === 0) {
@@ -2723,17 +2844,21 @@ function convertPocketBaseCollection(pbCollection) {
2723
2844
  type: pbCollection.type || "base",
2724
2845
  fields
2725
2846
  };
2847
+ if (pbCollection.id) {
2848
+ schema.id = pbCollection.id;
2849
+ }
2726
2850
  if (pbCollection.indexes && Array.isArray(pbCollection.indexes)) {
2727
2851
  schema.indexes = pbCollection.indexes;
2728
2852
  }
2729
- const rules = {};
2730
- if (pbCollection.listRule !== void 0) rules.listRule = pbCollection.listRule;
2731
- if (pbCollection.viewRule !== void 0) rules.viewRule = pbCollection.viewRule;
2732
- if (pbCollection.createRule !== void 0) rules.createRule = pbCollection.createRule;
2733
- if (pbCollection.updateRule !== void 0) rules.updateRule = pbCollection.updateRule;
2734
- if (pbCollection.deleteRule !== void 0) rules.deleteRule = pbCollection.deleteRule;
2735
- if (pbCollection.manageRule !== void 0) rules.manageRule = pbCollection.manageRule;
2736
- if (Object.keys(rules).length > 0) {
2853
+ 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;
2854
+ if (hasAnyRule) {
2855
+ const rules = {};
2856
+ if (pbCollection.listRule !== void 0) rules.listRule = pbCollection.listRule;
2857
+ if (pbCollection.viewRule !== void 0) rules.viewRule = pbCollection.viewRule;
2858
+ if (pbCollection.createRule !== void 0) rules.createRule = pbCollection.createRule;
2859
+ if (pbCollection.updateRule !== void 0) rules.updateRule = pbCollection.updateRule;
2860
+ if (pbCollection.deleteRule !== void 0) rules.deleteRule = pbCollection.deleteRule;
2861
+ if (pbCollection.manageRule !== void 0) rules.manageRule = pbCollection.manageRule;
2737
2862
  schema.rules = rules;
2738
2863
  schema.permissions = { ...rules };
2739
2864
  }