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.
- package/CHANGELOG.md +15 -0
- package/dist/cli/index.cjs +175 -50
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.d.cts +2 -2
- package/dist/cli/index.d.ts +2 -2
- package/dist/cli/index.js +175 -50
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/migrate.cjs +175 -50
- package/dist/cli/migrate.cjs.map +1 -1
- package/dist/cli/migrate.js +175 -50
- package/dist/cli/migrate.js.map +1 -1
- package/dist/cli/utils/index.d.cts +2 -2
- package/dist/cli/utils/index.d.ts +2 -2
- package/dist/{fields-UcOPu1OQ.d.cts → fields-DBBm06VU.d.cts} +35 -7
- package/dist/{fields-UcOPu1OQ.d.ts → fields-DBBm06VU.d.ts} +35 -7
- package/dist/index.cjs +264 -84
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +264 -84
- package/dist/index.js.map +1 -1
- package/dist/migration/analyzer.cjs.map +1 -1
- package/dist/migration/analyzer.d.cts +2 -2
- package/dist/migration/analyzer.d.ts +2 -2
- package/dist/migration/analyzer.js.map +1 -1
- package/dist/migration/diff.cjs +77 -26
- package/dist/migration/diff.cjs.map +1 -1
- package/dist/migration/diff.d.cts +4 -4
- package/dist/migration/diff.d.ts +4 -4
- package/dist/migration/diff.js +77 -26
- package/dist/migration/diff.js.map +1 -1
- package/dist/migration/generator.cjs +81 -34
- package/dist/migration/generator.cjs.map +1 -1
- package/dist/migration/generator.d.cts +2 -2
- package/dist/migration/generator.d.ts +2 -2
- package/dist/migration/generator.js +81 -34
- package/dist/migration/generator.js.map +1 -1
- package/dist/migration/index.cjs +209 -78
- package/dist/migration/index.cjs.map +1 -1
- package/dist/migration/index.d.cts +3 -3
- package/dist/migration/index.d.ts +3 -3
- package/dist/migration/index.js +209 -78
- package/dist/migration/index.js.map +1 -1
- package/dist/migration/snapshot.cjs +51 -18
- package/dist/migration/snapshot.cjs.map +1 -1
- package/dist/migration/snapshot.d.cts +2 -2
- package/dist/migration/snapshot.d.ts +2 -2
- package/dist/migration/snapshot.js +51 -18
- package/dist/migration/snapshot.js.map +1 -1
- package/dist/migration/utils/index.cjs +5 -3
- package/dist/migration/utils/index.cjs.map +1 -1
- package/dist/migration/utils/index.d.cts +4 -4
- package/dist/migration/utils/index.d.ts +4 -4
- package/dist/migration/utils/index.js +5 -3
- package/dist/migration/utils/index.js.map +1 -1
- package/dist/schema.cjs +55 -6
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.d.cts +1 -1
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +55 -6
- package/dist/schema.js.map +1 -1
- package/dist/{type-mapper-DrQmtznD.d.cts → type-mapper-DsGgZwUo.d.cts} +1 -1
- package/dist/{type-mapper-n231Fspm.d.ts → type-mapper-Dvh4QTM-.d.ts} +1 -1
- package/dist/{types-YoBjsa-A.d.cts → types-CVxPCgWX.d.cts} +1 -1
- package/dist/{types-Ds3NQvny.d.ts → types-Dfp-NP2D.d.ts} +1 -1
- package/package.json +1 -1
package/dist/cli/migrate.js
CHANGED
|
@@ -1496,17 +1496,19 @@ var CollectionIdRegistry = class {
|
|
|
1496
1496
|
}
|
|
1497
1497
|
/**
|
|
1498
1498
|
* Generates a unique collection ID for a given collection name
|
|
1499
|
-
* Special case: Returns constant "_pb_users_auth_" for users collection
|
|
1500
1499
|
* Retries up to 10 times if collision occurs (extremely rare)
|
|
1500
|
+
* Special case: returns "_pb_users_auth_" for users collection
|
|
1501
1501
|
*
|
|
1502
|
-
* @param collectionName - The name of the collection
|
|
1502
|
+
* @param collectionName - The name of the collection (optional)
|
|
1503
1503
|
* @returns A unique collection ID
|
|
1504
1504
|
* @throws Error if unable to generate unique ID after max attempts
|
|
1505
1505
|
*/
|
|
1506
1506
|
generate(collectionName) {
|
|
1507
1507
|
if (collectionName && collectionName.toLowerCase() === "users") {
|
|
1508
1508
|
const usersId = "_pb_users_auth_";
|
|
1509
|
-
this.
|
|
1509
|
+
if (!this.has(usersId)) {
|
|
1510
|
+
this.register(usersId);
|
|
1511
|
+
}
|
|
1510
1512
|
return usersId;
|
|
1511
1513
|
}
|
|
1512
1514
|
const maxAttempts = 10;
|
|
@@ -1679,18 +1681,49 @@ function compareFieldConstraints(currentField, previousField) {
|
|
|
1679
1681
|
}
|
|
1680
1682
|
return changes;
|
|
1681
1683
|
}
|
|
1684
|
+
function normalizeOptionValue(key, value, fieldType) {
|
|
1685
|
+
if (key === "maxSelect" && value === 1 && (fieldType === "select" || fieldType === "file")) {
|
|
1686
|
+
return void 0;
|
|
1687
|
+
}
|
|
1688
|
+
if (key === "maxSize" && value === 0 && fieldType === "file") {
|
|
1689
|
+
return void 0;
|
|
1690
|
+
}
|
|
1691
|
+
if (fieldType === "file") {
|
|
1692
|
+
if (key === "mimeTypes" && Array.isArray(value) && value.length === 0) {
|
|
1693
|
+
return void 0;
|
|
1694
|
+
}
|
|
1695
|
+
if (key === "thumbs" && Array.isArray(value) && value.length === 0) {
|
|
1696
|
+
return void 0;
|
|
1697
|
+
}
|
|
1698
|
+
if (key === "protected" && value === false) {
|
|
1699
|
+
return void 0;
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
if (fieldType === "autodate") {
|
|
1703
|
+
if (key === "onCreate" && value === true) {
|
|
1704
|
+
return void 0;
|
|
1705
|
+
}
|
|
1706
|
+
if (key === "onUpdate" && value === false) {
|
|
1707
|
+
return void 0;
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
return value;
|
|
1711
|
+
}
|
|
1682
1712
|
function compareFieldOptions(currentField, previousField) {
|
|
1683
1713
|
const changes = [];
|
|
1684
1714
|
const currentOptions = currentField.options || {};
|
|
1685
1715
|
const previousOptions = previousField.options || {};
|
|
1686
1716
|
const allKeys = /* @__PURE__ */ new Set([...Object.keys(currentOptions), ...Object.keys(previousOptions)]);
|
|
1717
|
+
const fieldType = currentField.type;
|
|
1687
1718
|
for (const key of allKeys) {
|
|
1688
1719
|
const currentValue = currentOptions[key];
|
|
1689
1720
|
const previousValue = previousOptions[key];
|
|
1690
|
-
|
|
1721
|
+
const normalizedCurrent = normalizeOptionValue(key, currentValue, fieldType);
|
|
1722
|
+
const normalizedPrevious = normalizeOptionValue(key, previousValue, fieldType);
|
|
1723
|
+
if (normalizedCurrent === void 0 && normalizedPrevious === void 0) {
|
|
1691
1724
|
continue;
|
|
1692
1725
|
}
|
|
1693
|
-
if (!areValuesEqual(
|
|
1726
|
+
if (!areValuesEqual(normalizedCurrent, normalizedPrevious)) {
|
|
1694
1727
|
changes.push({
|
|
1695
1728
|
property: `options.${key}`,
|
|
1696
1729
|
oldValue: previousValue,
|
|
@@ -1700,7 +1733,7 @@ function compareFieldOptions(currentField, previousField) {
|
|
|
1700
1733
|
}
|
|
1701
1734
|
return changes;
|
|
1702
1735
|
}
|
|
1703
|
-
function compareRelationConfigurations(currentField, previousField) {
|
|
1736
|
+
function compareRelationConfigurations(currentField, previousField, collectionIdToName) {
|
|
1704
1737
|
const changes = [];
|
|
1705
1738
|
const currentRelation = currentField.relation;
|
|
1706
1739
|
const previousRelation = previousField.relation;
|
|
@@ -1712,8 +1745,8 @@ function compareRelationConfigurations(currentField, previousField) {
|
|
|
1712
1745
|
}
|
|
1713
1746
|
const normalizeCollection = (collection) => {
|
|
1714
1747
|
if (!collection) return collection;
|
|
1715
|
-
if (
|
|
1716
|
-
return
|
|
1748
|
+
if (collectionIdToName && collectionIdToName.has(collection)) {
|
|
1749
|
+
return collectionIdToName.get(collection);
|
|
1717
1750
|
}
|
|
1718
1751
|
const nameMatch = collection.match(/app\.findCollectionByNameOrId\s*\(\s*["']([^"']+)["']\s*\)/);
|
|
1719
1752
|
if (nameMatch) {
|
|
@@ -1723,13 +1756,11 @@ function compareRelationConfigurations(currentField, previousField) {
|
|
|
1723
1756
|
};
|
|
1724
1757
|
const normalizedCurrent = normalizeCollection(currentRelation.collection);
|
|
1725
1758
|
const normalizedPrevious = normalizeCollection(previousRelation.collection);
|
|
1726
|
-
if (normalizedCurrent !== normalizedPrevious) {
|
|
1759
|
+
if (normalizedCurrent.toLowerCase() !== normalizedPrevious.toLowerCase()) {
|
|
1727
1760
|
changes.push({
|
|
1728
1761
|
property: "relation.collection",
|
|
1729
|
-
oldValue:
|
|
1730
|
-
|
|
1731
|
-
newValue: normalizedCurrent
|
|
1732
|
-
// Use normalized value for clarity
|
|
1762
|
+
oldValue: previousRelation.collection,
|
|
1763
|
+
newValue: currentRelation.collection
|
|
1733
1764
|
});
|
|
1734
1765
|
}
|
|
1735
1766
|
if (currentRelation.cascadeDelete !== previousRelation.cascadeDelete) {
|
|
@@ -1739,14 +1770,20 @@ function compareRelationConfigurations(currentField, previousField) {
|
|
|
1739
1770
|
newValue: currentRelation.cascadeDelete
|
|
1740
1771
|
});
|
|
1741
1772
|
}
|
|
1742
|
-
|
|
1773
|
+
const normalizeMax = (val) => val === 1 ? null : val;
|
|
1774
|
+
const currentMax = normalizeMax(currentRelation.maxSelect);
|
|
1775
|
+
const previousMax = normalizeMax(previousRelation.maxSelect);
|
|
1776
|
+
if (currentMax != previousMax) {
|
|
1743
1777
|
changes.push({
|
|
1744
1778
|
property: "relation.maxSelect",
|
|
1745
1779
|
oldValue: previousRelation.maxSelect,
|
|
1746
1780
|
newValue: currentRelation.maxSelect
|
|
1747
1781
|
});
|
|
1748
1782
|
}
|
|
1749
|
-
|
|
1783
|
+
const normalizeMin = (val) => val === 0 ? null : val;
|
|
1784
|
+
const currentMin = normalizeMin(currentRelation.minSelect);
|
|
1785
|
+
const previousMin = normalizeMin(previousRelation.minSelect);
|
|
1786
|
+
if (currentMin != previousMin) {
|
|
1750
1787
|
changes.push({
|
|
1751
1788
|
property: "relation.minSelect",
|
|
1752
1789
|
oldValue: previousRelation.minSelect,
|
|
@@ -1755,7 +1792,7 @@ function compareRelationConfigurations(currentField, previousField) {
|
|
|
1755
1792
|
}
|
|
1756
1793
|
return changes;
|
|
1757
1794
|
}
|
|
1758
|
-
function detectFieldChanges(currentField, previousField) {
|
|
1795
|
+
function detectFieldChanges(currentField, previousField, collectionIdToName) {
|
|
1759
1796
|
const changes = [];
|
|
1760
1797
|
const typeChange = compareFieldTypes(currentField, previousField);
|
|
1761
1798
|
if (typeChange) {
|
|
@@ -1764,7 +1801,7 @@ function detectFieldChanges(currentField, previousField) {
|
|
|
1764
1801
|
changes.push(...compareFieldConstraints(currentField, previousField));
|
|
1765
1802
|
changes.push(...compareFieldOptions(currentField, previousField));
|
|
1766
1803
|
if (currentField.type === "relation" && previousField.type === "relation") {
|
|
1767
|
-
changes.push(...compareRelationConfigurations(currentField, previousField));
|
|
1804
|
+
changes.push(...compareRelationConfigurations(currentField, previousField, collectionIdToName));
|
|
1768
1805
|
}
|
|
1769
1806
|
return changes;
|
|
1770
1807
|
}
|
|
@@ -1775,7 +1812,7 @@ function compareIndexes(currentIndexes = [], previousIndexes = []) {
|
|
|
1775
1812
|
const indexesToRemove = previousIndexes.filter((idx) => !currentSet.has(idx));
|
|
1776
1813
|
return { indexesToAdd, indexesToRemove };
|
|
1777
1814
|
}
|
|
1778
|
-
function compareRules(currentRules, previousRules) {
|
|
1815
|
+
function compareRules(currentRules, previousRules, currentPermissions, previousPermissions) {
|
|
1779
1816
|
const updates = [];
|
|
1780
1817
|
const ruleTypes = [
|
|
1781
1818
|
"listRule",
|
|
@@ -1786,8 +1823,8 @@ function compareRules(currentRules, previousRules) {
|
|
|
1786
1823
|
"manageRule"
|
|
1787
1824
|
];
|
|
1788
1825
|
for (const ruleType of ruleTypes) {
|
|
1789
|
-
const currentValue = currentRules?.[ruleType] ?? null;
|
|
1790
|
-
const previousValue = previousRules?.[ruleType] ?? null;
|
|
1826
|
+
const currentValue = currentRules?.[ruleType] ?? currentPermissions?.[ruleType] ?? null;
|
|
1827
|
+
const previousValue = previousRules?.[ruleType] ?? previousPermissions?.[ruleType] ?? null;
|
|
1791
1828
|
if (currentValue !== previousValue) {
|
|
1792
1829
|
updates.push({
|
|
1793
1830
|
ruleType,
|
|
@@ -1814,7 +1851,7 @@ function comparePermissions(currentPermissions, previousPermissions) {
|
|
|
1814
1851
|
}
|
|
1815
1852
|
return changes;
|
|
1816
1853
|
}
|
|
1817
|
-
function compareCollectionFields(currentCollection, previousCollection, config) {
|
|
1854
|
+
function compareCollectionFields(currentCollection, previousCollection, config, collectionIdToName) {
|
|
1818
1855
|
let fieldsToAdd = findNewFields(currentCollection.fields, previousCollection.fields);
|
|
1819
1856
|
const fieldsToRemove = findRemovedFields(currentCollection.fields, previousCollection.fields);
|
|
1820
1857
|
const fieldsToModify = [];
|
|
@@ -1824,7 +1861,7 @@ function compareCollectionFields(currentCollection, previousCollection, config)
|
|
|
1824
1861
|
}
|
|
1825
1862
|
const matchedFields = matchFieldsByName(currentCollection.fields, previousCollection.fields);
|
|
1826
1863
|
for (const [currentField, previousField] of matchedFields) {
|
|
1827
|
-
const changes = detectFieldChanges(currentField, previousField);
|
|
1864
|
+
const changes = detectFieldChanges(currentField, previousField, collectionIdToName);
|
|
1828
1865
|
if (changes.length > 0) {
|
|
1829
1866
|
fieldsToModify.push({
|
|
1830
1867
|
fieldName: currentField.name,
|
|
@@ -1836,14 +1873,20 @@ function compareCollectionFields(currentCollection, previousCollection, config)
|
|
|
1836
1873
|
}
|
|
1837
1874
|
return { fieldsToAdd, fieldsToRemove, fieldsToModify };
|
|
1838
1875
|
}
|
|
1839
|
-
function buildCollectionModification(currentCollection, previousCollection, config) {
|
|
1876
|
+
function buildCollectionModification(currentCollection, previousCollection, config, collectionIdToName) {
|
|
1840
1877
|
const { fieldsToAdd, fieldsToRemove, fieldsToModify } = compareCollectionFields(
|
|
1841
1878
|
currentCollection,
|
|
1842
1879
|
previousCollection,
|
|
1843
|
-
config
|
|
1880
|
+
config,
|
|
1881
|
+
collectionIdToName
|
|
1844
1882
|
);
|
|
1845
1883
|
const { indexesToAdd, indexesToRemove } = compareIndexes(currentCollection.indexes, previousCollection.indexes);
|
|
1846
|
-
const rulesToUpdate = compareRules(
|
|
1884
|
+
const rulesToUpdate = compareRules(
|
|
1885
|
+
currentCollection.rules,
|
|
1886
|
+
previousCollection.rules,
|
|
1887
|
+
currentCollection.permissions,
|
|
1888
|
+
previousCollection.permissions
|
|
1889
|
+
);
|
|
1847
1890
|
const permissionsToUpdate = comparePermissions(currentCollection.permissions, previousCollection.permissions);
|
|
1848
1891
|
return {
|
|
1849
1892
|
collection: currentCollection.name,
|
|
@@ -1860,6 +1903,14 @@ function hasChanges(modification) {
|
|
|
1860
1903
|
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;
|
|
1861
1904
|
}
|
|
1862
1905
|
function aggregateChanges(currentSchema, previousSnapshot, config) {
|
|
1906
|
+
const collectionIdToName = /* @__PURE__ */ new Map();
|
|
1907
|
+
if (previousSnapshot) {
|
|
1908
|
+
for (const [name, collection] of previousSnapshot.collections) {
|
|
1909
|
+
if (collection.id) {
|
|
1910
|
+
collectionIdToName.set(collection.id, name);
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1863
1914
|
const collectionsToCreate = findNewCollections(currentSchema, previousSnapshot);
|
|
1864
1915
|
const collectionsToDelete = findRemovedCollections(currentSchema, previousSnapshot);
|
|
1865
1916
|
const filteredCollectionsToCreate = collectionsToCreate.filter(
|
|
@@ -1883,7 +1934,7 @@ function aggregateChanges(currentSchema, previousSnapshot, config) {
|
|
|
1883
1934
|
const collectionsToModify = [];
|
|
1884
1935
|
const matchedCollections = matchCollectionsByName(currentSchema, previousSnapshot);
|
|
1885
1936
|
for (const [currentCollection, previousCollection] of matchedCollections) {
|
|
1886
|
-
const modification = buildCollectionModification(currentCollection, previousCollection, config);
|
|
1937
|
+
const modification = buildCollectionModification(currentCollection, previousCollection, config, collectionIdToName);
|
|
1887
1938
|
if (hasChanges(modification)) {
|
|
1888
1939
|
collectionsToModify.push(modification);
|
|
1889
1940
|
}
|
|
@@ -2094,7 +2145,7 @@ function formatValue(value) {
|
|
|
2094
2145
|
return JSON.stringify(value).replace(/","/g, '", "');
|
|
2095
2146
|
}
|
|
2096
2147
|
if (typeof value === "object") {
|
|
2097
|
-
const entries = Object.entries(value).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
|
|
2148
|
+
const entries = Object.entries(value).filter(([_k, v]) => v !== void 0).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
|
|
2098
2149
|
return `{ ${entries} }`;
|
|
2099
2150
|
}
|
|
2100
2151
|
return String(value);
|
|
@@ -2263,6 +2314,9 @@ function getSystemFields() {
|
|
|
2263
2314
|
function generateCollectionCreation(collection, varName = "collection", isLast = false, collectionIdMap) {
|
|
2264
2315
|
const lines = [];
|
|
2265
2316
|
lines.push(` const ${varName} = new Collection({`);
|
|
2317
|
+
if (collection.id) {
|
|
2318
|
+
lines.push(` id: ${formatValue(collection.id)},`);
|
|
2319
|
+
}
|
|
2266
2320
|
lines.push(` name: "${collection.name}",`);
|
|
2267
2321
|
lines.push(` type: "${collection.type}",`);
|
|
2268
2322
|
const permissionsCode = generateCollectionPermissions(collection.permissions);
|
|
@@ -2385,11 +2439,9 @@ function generateFieldModification(collectionName, modification, varName, isLast
|
|
|
2385
2439
|
function generateFieldDeletion(collectionName, fieldName, varName, isLast = false) {
|
|
2386
2440
|
const lines = [];
|
|
2387
2441
|
const collectionVar = varName || `collection_${collectionName}_${fieldName}`;
|
|
2388
|
-
const fieldVar = `${collectionVar}_field`;
|
|
2389
2442
|
lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
|
|
2390
|
-
lines.push(` const ${fieldVar} = ${collectionVar}.fields.getByName("${fieldName}");`);
|
|
2391
2443
|
lines.push(``);
|
|
2392
|
-
lines.push(` ${collectionVar}.fields.
|
|
2444
|
+
lines.push(` ${collectionVar}.fields.removeByName("${fieldName}");`);
|
|
2393
2445
|
lines.push(``);
|
|
2394
2446
|
lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
|
|
2395
2447
|
return lines.join("\n");
|
|
@@ -2506,7 +2558,27 @@ function generateOperationUpMigration(operation, collectionIdMap) {
|
|
|
2506
2558
|
const varName = `collection_${collectionName}`;
|
|
2507
2559
|
lines.push(generateCollectionDeletion(collectionName, varName, true));
|
|
2508
2560
|
}
|
|
2509
|
-
|
|
2561
|
+
let code = lines.join("\n");
|
|
2562
|
+
const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
|
|
2563
|
+
if (!hasReturnStatement) {
|
|
2564
|
+
const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
|
|
2565
|
+
const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
|
|
2566
|
+
const saveMatches = [...code.matchAll(savePattern)];
|
|
2567
|
+
const deleteMatches = [...code.matchAll(deletePattern)];
|
|
2568
|
+
const allMatches = [
|
|
2569
|
+
...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
|
|
2570
|
+
...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
|
|
2571
|
+
].sort((a, b) => b.index - a.index);
|
|
2572
|
+
if (allMatches.length > 0) {
|
|
2573
|
+
const lastMatch = allMatches[0];
|
|
2574
|
+
if (lastMatch.type === "save") {
|
|
2575
|
+
code = code.substring(0, lastMatch.match.index) + lastMatch.match[1] + "return app.save(" + lastMatch.match[2] + ");" + code.substring(lastMatch.match.index + lastMatch.match[0].length);
|
|
2576
|
+
} else {
|
|
2577
|
+
code = code.substring(0, lastMatch.match.index) + lastMatch.match[1] + "return app.delete(" + lastMatch.match[2] + ");" + code.substring(lastMatch.match.index + lastMatch.match[0].length);
|
|
2578
|
+
}
|
|
2579
|
+
}
|
|
2580
|
+
}
|
|
2581
|
+
return code;
|
|
2510
2582
|
}
|
|
2511
2583
|
function generateOperationDownMigration(operation, collectionIdMap) {
|
|
2512
2584
|
const lines = [];
|
|
@@ -2591,7 +2663,27 @@ function generateOperationDownMigration(operation, collectionIdMap) {
|
|
|
2591
2663
|
lines.push(generateCollectionCreation(collection, varName, true, collectionIdMap));
|
|
2592
2664
|
}
|
|
2593
2665
|
}
|
|
2594
|
-
|
|
2666
|
+
let code = lines.join("\n");
|
|
2667
|
+
const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
|
|
2668
|
+
if (!hasReturnStatement) {
|
|
2669
|
+
const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
|
|
2670
|
+
const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
|
|
2671
|
+
const saveMatches = [...code.matchAll(savePattern)];
|
|
2672
|
+
const deleteMatches = [...code.matchAll(deletePattern)];
|
|
2673
|
+
const allMatches = [
|
|
2674
|
+
...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
|
|
2675
|
+
...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
|
|
2676
|
+
].sort((a, b) => b.index - a.index);
|
|
2677
|
+
if (allMatches.length > 0) {
|
|
2678
|
+
const lastMatch = allMatches[0];
|
|
2679
|
+
if (lastMatch.type === "save") {
|
|
2680
|
+
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);
|
|
2681
|
+
} else {
|
|
2682
|
+
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);
|
|
2683
|
+
}
|
|
2684
|
+
}
|
|
2685
|
+
}
|
|
2686
|
+
return code;
|
|
2595
2687
|
}
|
|
2596
2688
|
function generate(diff, config) {
|
|
2597
2689
|
const normalizedConfig = typeof config === "string" ? { migrationDir: config } : config;
|
|
@@ -2640,7 +2732,7 @@ function generate(diff, config) {
|
|
|
2640
2732
|
var SNAPSHOT_VERSION = "1.0.0";
|
|
2641
2733
|
function resolveCollectionIdToName(collectionId) {
|
|
2642
2734
|
if (collectionId === "_pb_users_auth_") {
|
|
2643
|
-
return "
|
|
2735
|
+
return "users";
|
|
2644
2736
|
}
|
|
2645
2737
|
const nameMatch = collectionId.match(/app\.findCollectionByNameOrId\s*\(\s*["']([^"']+)["']\s*\)/);
|
|
2646
2738
|
if (nameMatch) {
|
|
@@ -2648,6 +2740,39 @@ function resolveCollectionIdToName(collectionId) {
|
|
|
2648
2740
|
}
|
|
2649
2741
|
return collectionId;
|
|
2650
2742
|
}
|
|
2743
|
+
function extractFieldOptions2(pbField) {
|
|
2744
|
+
const options = {};
|
|
2745
|
+
if (pbField.options && typeof pbField.options === "object") {
|
|
2746
|
+
Object.assign(options, pbField.options);
|
|
2747
|
+
}
|
|
2748
|
+
const directOptionKeys = [
|
|
2749
|
+
"min",
|
|
2750
|
+
"max",
|
|
2751
|
+
"pattern",
|
|
2752
|
+
"noDecimal",
|
|
2753
|
+
// text/number fields
|
|
2754
|
+
"values",
|
|
2755
|
+
"maxSelect",
|
|
2756
|
+
// select fields
|
|
2757
|
+
"mimeTypes",
|
|
2758
|
+
"maxSize",
|
|
2759
|
+
"thumbs",
|
|
2760
|
+
"protected",
|
|
2761
|
+
// file fields
|
|
2762
|
+
"onCreate",
|
|
2763
|
+
"onUpdate",
|
|
2764
|
+
// autodate fields
|
|
2765
|
+
"exceptDomains",
|
|
2766
|
+
"onlyDomains"
|
|
2767
|
+
// email/url fields
|
|
2768
|
+
];
|
|
2769
|
+
for (const key of directOptionKeys) {
|
|
2770
|
+
if (pbField[key] !== void 0) {
|
|
2771
|
+
options[key] = pbField[key];
|
|
2772
|
+
}
|
|
2773
|
+
}
|
|
2774
|
+
return options;
|
|
2775
|
+
}
|
|
2651
2776
|
function convertPocketBaseCollection(pbCollection) {
|
|
2652
2777
|
const fields = [];
|
|
2653
2778
|
const systemFieldNames = ["id", "created", "updated", "collectionId", "collectionName", "expand"];
|
|
@@ -2665,23 +2790,19 @@ function convertPocketBaseCollection(pbCollection) {
|
|
|
2665
2790
|
type: pbField.type,
|
|
2666
2791
|
required: pbField.required || false
|
|
2667
2792
|
};
|
|
2668
|
-
field.options = pbField
|
|
2669
|
-
if (pbField.type === "select") {
|
|
2670
|
-
if (pbField.values && Array.isArray(pbField.values)) {
|
|
2671
|
-
field.options.values = pbField.values;
|
|
2672
|
-
} else if (pbField.options?.values && Array.isArray(pbField.options.values)) {
|
|
2673
|
-
field.options.values = pbField.options.values;
|
|
2674
|
-
}
|
|
2675
|
-
}
|
|
2793
|
+
field.options = extractFieldOptions2(pbField);
|
|
2676
2794
|
if (pbField.type === "relation") {
|
|
2677
2795
|
const collectionId = pbField.collectionId || pbField.options?.collectionId || "";
|
|
2678
|
-
const collectionName = resolveCollectionIdToName(collectionId);
|
|
2796
|
+
const collectionName = resolveCollectionIdToName(collectionId || "");
|
|
2679
2797
|
field.relation = {
|
|
2680
2798
|
collection: collectionName,
|
|
2681
2799
|
cascadeDelete: pbField.cascadeDelete ?? pbField.options?.cascadeDelete ?? false,
|
|
2682
2800
|
maxSelect: pbField.maxSelect ?? pbField.options?.maxSelect,
|
|
2683
2801
|
minSelect: pbField.minSelect ?? pbField.options?.minSelect
|
|
2684
2802
|
};
|
|
2803
|
+
delete field.options.maxSelect;
|
|
2804
|
+
delete field.options.minSelect;
|
|
2805
|
+
delete field.options.cascadeDelete;
|
|
2685
2806
|
}
|
|
2686
2807
|
const hasOnlyValues = Object.keys(field.options).length === 1 && field.options.values !== void 0;
|
|
2687
2808
|
if (Object.keys(field.options).length === 0) {
|
|
@@ -2695,17 +2816,21 @@ function convertPocketBaseCollection(pbCollection) {
|
|
|
2695
2816
|
type: pbCollection.type || "base",
|
|
2696
2817
|
fields
|
|
2697
2818
|
};
|
|
2819
|
+
if (pbCollection.id) {
|
|
2820
|
+
schema.id = pbCollection.id;
|
|
2821
|
+
}
|
|
2698
2822
|
if (pbCollection.indexes && Array.isArray(pbCollection.indexes)) {
|
|
2699
2823
|
schema.indexes = pbCollection.indexes;
|
|
2700
2824
|
}
|
|
2701
|
-
const
|
|
2702
|
-
if (
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2825
|
+
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;
|
|
2826
|
+
if (hasAnyRule) {
|
|
2827
|
+
const rules = {};
|
|
2828
|
+
if (pbCollection.listRule !== void 0) rules.listRule = pbCollection.listRule;
|
|
2829
|
+
if (pbCollection.viewRule !== void 0) rules.viewRule = pbCollection.viewRule;
|
|
2830
|
+
if (pbCollection.createRule !== void 0) rules.createRule = pbCollection.createRule;
|
|
2831
|
+
if (pbCollection.updateRule !== void 0) rules.updateRule = pbCollection.updateRule;
|
|
2832
|
+
if (pbCollection.deleteRule !== void 0) rules.deleteRule = pbCollection.deleteRule;
|
|
2833
|
+
if (pbCollection.manageRule !== void 0) rules.manageRule = pbCollection.manageRule;
|
|
2709
2834
|
schema.rules = rules;
|
|
2710
2835
|
schema.permissions = { ...rules };
|
|
2711
2836
|
}
|