pocketbase-zod-schema 0.3.0 → 0.3.1

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 (64) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/cli/index.cjs +167 -46
  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 +167 -46
  7. package/dist/cli/index.js.map +1 -1
  8. package/dist/cli/migrate.cjs +167 -46
  9. package/dist/cli/migrate.cjs.map +1 -1
  10. package/dist/cli/migrate.js +167 -46
  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-RVj26U-O.d.cts} +1 -0
  15. package/dist/{fields-UcOPu1OQ.d.ts → fields-RVj26U-O.d.ts} +1 -0
  16. package/dist/index.cjs +167 -46
  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 -46
  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 +39 -2
  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 +39 -2
  37. package/dist/migration/generator.js.map +1 -1
  38. package/dist/migration/index.cjs +167 -46
  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 +167 -46
  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.map +1 -1
  57. package/dist/schema.d.cts +1 -1
  58. package/dist/schema.d.ts +1 -1
  59. package/dist/schema.js.map +1 -1
  60. package/dist/{type-mapper-n231Fspm.d.ts → type-mapper-CZzVeDj7.d.ts} +1 -1
  61. package/dist/{type-mapper-DrQmtznD.d.cts → type-mapper-DaBe-1ph.d.cts} +1 -1
  62. package/dist/{types-Ds3NQvny.d.ts → types-CUVzgZ9k.d.ts} +1 -1
  63. package/dist/{types-YoBjsa-A.d.cts → types-D-Fsdn_O.d.cts} +1 -1
  64. 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-YoBjsa-A.cjs';
4
- import '../fields-UcOPu1OQ.cjs';
3
+ import '../types-D-Fsdn_O.cjs';
4
+ import '../fields-RVj26U-O.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-Ds3NQvny.js';
4
- import '../fields-UcOPu1OQ.js';
3
+ import '../types-CUVzgZ9k.js';
4
+ import '../fields-RVj26U-O.js';
5
5
  import 'zod';
6
6
  import '../permissions-ZHafVSIx.js';
7
7
 
package/dist/cli/index.js CHANGED
@@ -1492,17 +1492,19 @@ var CollectionIdRegistry = class {
1492
1492
  }
1493
1493
  /**
1494
1494
  * Generates a unique collection ID for a given collection name
1495
- * Special case: Returns constant "_pb_users_auth_" for users collection
1496
1495
  * Retries up to 10 times if collision occurs (extremely rare)
1496
+ * Special case: returns "_pb_users_auth_" for users collection
1497
1497
  *
1498
- * @param collectionName - The name of the collection
1498
+ * @param collectionName - The name of the collection (optional)
1499
1499
  * @returns A unique collection ID
1500
1500
  * @throws Error if unable to generate unique ID after max attempts
1501
1501
  */
1502
1502
  generate(collectionName) {
1503
1503
  if (collectionName && collectionName.toLowerCase() === "users") {
1504
1504
  const usersId = "_pb_users_auth_";
1505
- this.register(usersId);
1505
+ if (!this.has(usersId)) {
1506
+ this.register(usersId);
1507
+ }
1506
1508
  return usersId;
1507
1509
  }
1508
1510
  const maxAttempts = 10;
@@ -1675,18 +1677,49 @@ function compareFieldConstraints(currentField, previousField) {
1675
1677
  }
1676
1678
  return changes;
1677
1679
  }
1680
+ function normalizeOptionValue(key, value, fieldType) {
1681
+ if (key === "maxSelect" && value === 1 && (fieldType === "select" || fieldType === "file")) {
1682
+ return void 0;
1683
+ }
1684
+ if (key === "maxSize" && value === 0 && fieldType === "file") {
1685
+ return void 0;
1686
+ }
1687
+ if (fieldType === "file") {
1688
+ if (key === "mimeTypes" && Array.isArray(value) && value.length === 0) {
1689
+ return void 0;
1690
+ }
1691
+ if (key === "thumbs" && Array.isArray(value) && value.length === 0) {
1692
+ return void 0;
1693
+ }
1694
+ if (key === "protected" && value === false) {
1695
+ return void 0;
1696
+ }
1697
+ }
1698
+ if (fieldType === "autodate") {
1699
+ if (key === "onCreate" && value === true) {
1700
+ return void 0;
1701
+ }
1702
+ if (key === "onUpdate" && value === false) {
1703
+ return void 0;
1704
+ }
1705
+ }
1706
+ return value;
1707
+ }
1678
1708
  function compareFieldOptions(currentField, previousField) {
1679
1709
  const changes = [];
1680
1710
  const currentOptions = currentField.options || {};
1681
1711
  const previousOptions = previousField.options || {};
1682
1712
  const allKeys = /* @__PURE__ */ new Set([...Object.keys(currentOptions), ...Object.keys(previousOptions)]);
1713
+ const fieldType = currentField.type;
1683
1714
  for (const key of allKeys) {
1684
1715
  const currentValue = currentOptions[key];
1685
1716
  const previousValue = previousOptions[key];
1686
- if (currentValue === void 0 && previousValue === void 0) {
1717
+ const normalizedCurrent = normalizeOptionValue(key, currentValue, fieldType);
1718
+ const normalizedPrevious = normalizeOptionValue(key, previousValue, fieldType);
1719
+ if (normalizedCurrent === void 0 && normalizedPrevious === void 0) {
1687
1720
  continue;
1688
1721
  }
1689
- if (!areValuesEqual(currentValue, previousValue)) {
1722
+ if (!areValuesEqual(normalizedCurrent, normalizedPrevious)) {
1690
1723
  changes.push({
1691
1724
  property: `options.${key}`,
1692
1725
  oldValue: previousValue,
@@ -1696,7 +1729,7 @@ function compareFieldOptions(currentField, previousField) {
1696
1729
  }
1697
1730
  return changes;
1698
1731
  }
1699
- function compareRelationConfigurations(currentField, previousField) {
1732
+ function compareRelationConfigurations(currentField, previousField, collectionIdToName) {
1700
1733
  const changes = [];
1701
1734
  const currentRelation = currentField.relation;
1702
1735
  const previousRelation = previousField.relation;
@@ -1708,8 +1741,8 @@ function compareRelationConfigurations(currentField, previousField) {
1708
1741
  }
1709
1742
  const normalizeCollection = (collection) => {
1710
1743
  if (!collection) return collection;
1711
- if (collection === "_pb_users_auth_") {
1712
- return "Users";
1744
+ if (collectionIdToName && collectionIdToName.has(collection)) {
1745
+ return collectionIdToName.get(collection);
1713
1746
  }
1714
1747
  const nameMatch = collection.match(/app\.findCollectionByNameOrId\s*\(\s*["']([^"']+)["']\s*\)/);
1715
1748
  if (nameMatch) {
@@ -1719,13 +1752,11 @@ function compareRelationConfigurations(currentField, previousField) {
1719
1752
  };
1720
1753
  const normalizedCurrent = normalizeCollection(currentRelation.collection);
1721
1754
  const normalizedPrevious = normalizeCollection(previousRelation.collection);
1722
- if (normalizedCurrent !== normalizedPrevious) {
1755
+ if (normalizedCurrent.toLowerCase() !== normalizedPrevious.toLowerCase()) {
1723
1756
  changes.push({
1724
1757
  property: "relation.collection",
1725
- oldValue: normalizedPrevious,
1726
- // Use normalized value for clarity
1727
- newValue: normalizedCurrent
1728
- // Use normalized value for clarity
1758
+ oldValue: previousRelation.collection,
1759
+ newValue: currentRelation.collection
1729
1760
  });
1730
1761
  }
1731
1762
  if (currentRelation.cascadeDelete !== previousRelation.cascadeDelete) {
@@ -1735,14 +1766,20 @@ function compareRelationConfigurations(currentField, previousField) {
1735
1766
  newValue: currentRelation.cascadeDelete
1736
1767
  });
1737
1768
  }
1738
- if (currentRelation.maxSelect !== previousRelation.maxSelect) {
1769
+ const normalizeMax = (val) => val === 1 ? null : val;
1770
+ const currentMax = normalizeMax(currentRelation.maxSelect);
1771
+ const previousMax = normalizeMax(previousRelation.maxSelect);
1772
+ if (currentMax != previousMax) {
1739
1773
  changes.push({
1740
1774
  property: "relation.maxSelect",
1741
1775
  oldValue: previousRelation.maxSelect,
1742
1776
  newValue: currentRelation.maxSelect
1743
1777
  });
1744
1778
  }
1745
- if (currentRelation.minSelect !== previousRelation.minSelect) {
1779
+ const normalizeMin = (val) => val === 0 ? null : val;
1780
+ const currentMin = normalizeMin(currentRelation.minSelect);
1781
+ const previousMin = normalizeMin(previousRelation.minSelect);
1782
+ if (currentMin != previousMin) {
1746
1783
  changes.push({
1747
1784
  property: "relation.minSelect",
1748
1785
  oldValue: previousRelation.minSelect,
@@ -1751,7 +1788,7 @@ function compareRelationConfigurations(currentField, previousField) {
1751
1788
  }
1752
1789
  return changes;
1753
1790
  }
1754
- function detectFieldChanges(currentField, previousField) {
1791
+ function detectFieldChanges(currentField, previousField, collectionIdToName) {
1755
1792
  const changes = [];
1756
1793
  const typeChange = compareFieldTypes(currentField, previousField);
1757
1794
  if (typeChange) {
@@ -1760,7 +1797,7 @@ function detectFieldChanges(currentField, previousField) {
1760
1797
  changes.push(...compareFieldConstraints(currentField, previousField));
1761
1798
  changes.push(...compareFieldOptions(currentField, previousField));
1762
1799
  if (currentField.type === "relation" && previousField.type === "relation") {
1763
- changes.push(...compareRelationConfigurations(currentField, previousField));
1800
+ changes.push(...compareRelationConfigurations(currentField, previousField, collectionIdToName));
1764
1801
  }
1765
1802
  return changes;
1766
1803
  }
@@ -1771,7 +1808,7 @@ function compareIndexes(currentIndexes = [], previousIndexes = []) {
1771
1808
  const indexesToRemove = previousIndexes.filter((idx) => !currentSet.has(idx));
1772
1809
  return { indexesToAdd, indexesToRemove };
1773
1810
  }
1774
- function compareRules(currentRules, previousRules) {
1811
+ function compareRules(currentRules, previousRules, currentPermissions, previousPermissions) {
1775
1812
  const updates = [];
1776
1813
  const ruleTypes = [
1777
1814
  "listRule",
@@ -1782,8 +1819,8 @@ function compareRules(currentRules, previousRules) {
1782
1819
  "manageRule"
1783
1820
  ];
1784
1821
  for (const ruleType of ruleTypes) {
1785
- const currentValue = currentRules?.[ruleType] ?? null;
1786
- const previousValue = previousRules?.[ruleType] ?? null;
1822
+ const currentValue = currentRules?.[ruleType] ?? currentPermissions?.[ruleType] ?? null;
1823
+ const previousValue = previousRules?.[ruleType] ?? previousPermissions?.[ruleType] ?? null;
1787
1824
  if (currentValue !== previousValue) {
1788
1825
  updates.push({
1789
1826
  ruleType,
@@ -1810,7 +1847,7 @@ function comparePermissions(currentPermissions, previousPermissions) {
1810
1847
  }
1811
1848
  return changes;
1812
1849
  }
1813
- function compareCollectionFields(currentCollection, previousCollection, config) {
1850
+ function compareCollectionFields(currentCollection, previousCollection, config, collectionIdToName) {
1814
1851
  let fieldsToAdd = findNewFields(currentCollection.fields, previousCollection.fields);
1815
1852
  const fieldsToRemove = findRemovedFields(currentCollection.fields, previousCollection.fields);
1816
1853
  const fieldsToModify = [];
@@ -1820,7 +1857,7 @@ function compareCollectionFields(currentCollection, previousCollection, config)
1820
1857
  }
1821
1858
  const matchedFields = matchFieldsByName(currentCollection.fields, previousCollection.fields);
1822
1859
  for (const [currentField, previousField] of matchedFields) {
1823
- const changes = detectFieldChanges(currentField, previousField);
1860
+ const changes = detectFieldChanges(currentField, previousField, collectionIdToName);
1824
1861
  if (changes.length > 0) {
1825
1862
  fieldsToModify.push({
1826
1863
  fieldName: currentField.name,
@@ -1832,14 +1869,20 @@ function compareCollectionFields(currentCollection, previousCollection, config)
1832
1869
  }
1833
1870
  return { fieldsToAdd, fieldsToRemove, fieldsToModify };
1834
1871
  }
1835
- function buildCollectionModification(currentCollection, previousCollection, config) {
1872
+ function buildCollectionModification(currentCollection, previousCollection, config, collectionIdToName) {
1836
1873
  const { fieldsToAdd, fieldsToRemove, fieldsToModify } = compareCollectionFields(
1837
1874
  currentCollection,
1838
1875
  previousCollection,
1839
- config
1876
+ config,
1877
+ collectionIdToName
1840
1878
  );
1841
1879
  const { indexesToAdd, indexesToRemove } = compareIndexes(currentCollection.indexes, previousCollection.indexes);
1842
- const rulesToUpdate = compareRules(currentCollection.rules, previousCollection.rules);
1880
+ const rulesToUpdate = compareRules(
1881
+ currentCollection.rules,
1882
+ previousCollection.rules,
1883
+ currentCollection.permissions,
1884
+ previousCollection.permissions
1885
+ );
1843
1886
  const permissionsToUpdate = comparePermissions(currentCollection.permissions, previousCollection.permissions);
1844
1887
  return {
1845
1888
  collection: currentCollection.name,
@@ -1856,6 +1899,14 @@ function hasChanges(modification) {
1856
1899
  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;
1857
1900
  }
1858
1901
  function aggregateChanges(currentSchema, previousSnapshot, config) {
1902
+ const collectionIdToName = /* @__PURE__ */ new Map();
1903
+ if (previousSnapshot) {
1904
+ for (const [name, collection] of previousSnapshot.collections) {
1905
+ if (collection.id) {
1906
+ collectionIdToName.set(collection.id, name);
1907
+ }
1908
+ }
1909
+ }
1859
1910
  const collectionsToCreate = findNewCollections(currentSchema, previousSnapshot);
1860
1911
  const collectionsToDelete = findRemovedCollections(currentSchema, previousSnapshot);
1861
1912
  const filteredCollectionsToCreate = collectionsToCreate.filter(
@@ -1879,7 +1930,7 @@ function aggregateChanges(currentSchema, previousSnapshot, config) {
1879
1930
  const collectionsToModify = [];
1880
1931
  const matchedCollections = matchCollectionsByName(currentSchema, previousSnapshot);
1881
1932
  for (const [currentCollection, previousCollection] of matchedCollections) {
1882
- const modification = buildCollectionModification(currentCollection, previousCollection, config);
1933
+ const modification = buildCollectionModification(currentCollection, previousCollection, config, collectionIdToName);
1883
1934
  if (hasChanges(modification)) {
1884
1935
  collectionsToModify.push(modification);
1885
1936
  }
@@ -2259,6 +2310,9 @@ function getSystemFields() {
2259
2310
  function generateCollectionCreation(collection, varName = "collection", isLast = false, collectionIdMap) {
2260
2311
  const lines = [];
2261
2312
  lines.push(` const ${varName} = new Collection({`);
2313
+ if (collection.id) {
2314
+ lines.push(` id: ${formatValue(collection.id)},`);
2315
+ }
2262
2316
  lines.push(` name: "${collection.name}",`);
2263
2317
  lines.push(` type: "${collection.type}",`);
2264
2318
  const permissionsCode = generateCollectionPermissions(collection.permissions);
@@ -2502,7 +2556,24 @@ function generateOperationUpMigration(operation, collectionIdMap) {
2502
2556
  const varName = `collection_${collectionName}`;
2503
2557
  lines.push(generateCollectionDeletion(collectionName, varName, true));
2504
2558
  }
2505
- return lines.join("\n");
2559
+ 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);
2574
+ }
2575
+ }
2576
+ return code;
2506
2577
  }
2507
2578
  function generateOperationDownMigration(operation, collectionIdMap) {
2508
2579
  const lines = [];
@@ -2587,7 +2658,24 @@ function generateOperationDownMigration(operation, collectionIdMap) {
2587
2658
  lines.push(generateCollectionCreation(collection, varName, true, collectionIdMap));
2588
2659
  }
2589
2660
  }
2590
- return lines.join("\n");
2661
+ 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);
2676
+ }
2677
+ }
2678
+ return code;
2591
2679
  }
2592
2680
  function generate(diff, config) {
2593
2681
  const normalizedConfig = typeof config === "string" ? { migrationDir: config } : config;
@@ -2636,7 +2724,7 @@ function generate(diff, config) {
2636
2724
  var SNAPSHOT_VERSION = "1.0.0";
2637
2725
  function resolveCollectionIdToName(collectionId) {
2638
2726
  if (collectionId === "_pb_users_auth_") {
2639
- return "Users";
2727
+ return "users";
2640
2728
  }
2641
2729
  const nameMatch = collectionId.match(/app\.findCollectionByNameOrId\s*\(\s*["']([^"']+)["']\s*\)/);
2642
2730
  if (nameMatch) {
@@ -2644,6 +2732,39 @@ function resolveCollectionIdToName(collectionId) {
2644
2732
  }
2645
2733
  return collectionId;
2646
2734
  }
2735
+ function extractFieldOptions2(pbField) {
2736
+ const options = {};
2737
+ if (pbField.options && typeof pbField.options === "object") {
2738
+ Object.assign(options, pbField.options);
2739
+ }
2740
+ const directOptionKeys = [
2741
+ "min",
2742
+ "max",
2743
+ "pattern",
2744
+ "noDecimal",
2745
+ // text/number fields
2746
+ "values",
2747
+ "maxSelect",
2748
+ // select fields
2749
+ "mimeTypes",
2750
+ "maxSize",
2751
+ "thumbs",
2752
+ "protected",
2753
+ // file fields
2754
+ "onCreate",
2755
+ "onUpdate",
2756
+ // autodate fields
2757
+ "exceptDomains",
2758
+ "onlyDomains"
2759
+ // email/url fields
2760
+ ];
2761
+ for (const key of directOptionKeys) {
2762
+ if (pbField[key] !== void 0) {
2763
+ options[key] = pbField[key];
2764
+ }
2765
+ }
2766
+ return options;
2767
+ }
2647
2768
  function convertPocketBaseCollection(pbCollection) {
2648
2769
  const fields = [];
2649
2770
  const systemFieldNames = ["id", "created", "updated", "collectionId", "collectionName", "expand"];
@@ -2661,23 +2782,19 @@ function convertPocketBaseCollection(pbCollection) {
2661
2782
  type: pbField.type,
2662
2783
  required: pbField.required || false
2663
2784
  };
2664
- field.options = pbField.options ? { ...pbField.options } : {};
2665
- if (pbField.type === "select") {
2666
- if (pbField.values && Array.isArray(pbField.values)) {
2667
- field.options.values = pbField.values;
2668
- } else if (pbField.options?.values && Array.isArray(pbField.options.values)) {
2669
- field.options.values = pbField.options.values;
2670
- }
2671
- }
2785
+ field.options = extractFieldOptions2(pbField);
2672
2786
  if (pbField.type === "relation") {
2673
2787
  const collectionId = pbField.collectionId || pbField.options?.collectionId || "";
2674
- const collectionName = resolveCollectionIdToName(collectionId);
2788
+ const collectionName = resolveCollectionIdToName(collectionId || "");
2675
2789
  field.relation = {
2676
2790
  collection: collectionName,
2677
2791
  cascadeDelete: pbField.cascadeDelete ?? pbField.options?.cascadeDelete ?? false,
2678
2792
  maxSelect: pbField.maxSelect ?? pbField.options?.maxSelect,
2679
2793
  minSelect: pbField.minSelect ?? pbField.options?.minSelect
2680
2794
  };
2795
+ delete field.options.maxSelect;
2796
+ delete field.options.minSelect;
2797
+ delete field.options.cascadeDelete;
2681
2798
  }
2682
2799
  const hasOnlyValues = Object.keys(field.options).length === 1 && field.options.values !== void 0;
2683
2800
  if (Object.keys(field.options).length === 0) {
@@ -2691,17 +2808,21 @@ function convertPocketBaseCollection(pbCollection) {
2691
2808
  type: pbCollection.type || "base",
2692
2809
  fields
2693
2810
  };
2811
+ if (pbCollection.id) {
2812
+ schema.id = pbCollection.id;
2813
+ }
2694
2814
  if (pbCollection.indexes && Array.isArray(pbCollection.indexes)) {
2695
2815
  schema.indexes = pbCollection.indexes;
2696
2816
  }
2697
- const rules = {};
2698
- if (pbCollection.listRule !== void 0) rules.listRule = pbCollection.listRule;
2699
- if (pbCollection.viewRule !== void 0) rules.viewRule = pbCollection.viewRule;
2700
- if (pbCollection.createRule !== void 0) rules.createRule = pbCollection.createRule;
2701
- if (pbCollection.updateRule !== void 0) rules.updateRule = pbCollection.updateRule;
2702
- if (pbCollection.deleteRule !== void 0) rules.deleteRule = pbCollection.deleteRule;
2703
- if (pbCollection.manageRule !== void 0) rules.manageRule = pbCollection.manageRule;
2704
- if (Object.keys(rules).length > 0) {
2817
+ 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;
2818
+ if (hasAnyRule) {
2819
+ const rules = {};
2820
+ if (pbCollection.listRule !== void 0) rules.listRule = pbCollection.listRule;
2821
+ if (pbCollection.viewRule !== void 0) rules.viewRule = pbCollection.viewRule;
2822
+ if (pbCollection.createRule !== void 0) rules.createRule = pbCollection.createRule;
2823
+ if (pbCollection.updateRule !== void 0) rules.updateRule = pbCollection.updateRule;
2824
+ if (pbCollection.deleteRule !== void 0) rules.deleteRule = pbCollection.deleteRule;
2825
+ if (pbCollection.manageRule !== void 0) rules.manageRule = pbCollection.manageRule;
2705
2826
  schema.rules = rules;
2706
2827
  schema.permissions = { ...rules };
2707
2828
  }