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/index.d.cts
CHANGED
|
@@ -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-
|
|
4
|
-
import '../fields-
|
|
3
|
+
import '../types-CVxPCgWX.cjs';
|
|
4
|
+
import '../fields-DBBm06VU.cjs';
|
|
5
5
|
import 'zod';
|
|
6
6
|
import '../permissions-ZHafVSIx.cjs';
|
|
7
7
|
|
package/dist/cli/index.d.ts
CHANGED
|
@@ -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-
|
|
4
|
-
import '../fields-
|
|
3
|
+
import '../types-Dfp-NP2D.js';
|
|
4
|
+
import '../fields-DBBm06VU.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.
|
|
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
|
-
|
|
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(
|
|
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 (
|
|
1712
|
-
return
|
|
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:
|
|
1726
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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(
|
|
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
|
}
|
|
@@ -2090,7 +2141,7 @@ function formatValue(value) {
|
|
|
2090
2141
|
return JSON.stringify(value).replace(/","/g, '", "');
|
|
2091
2142
|
}
|
|
2092
2143
|
if (typeof value === "object") {
|
|
2093
|
-
const entries = Object.entries(value).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
|
|
2144
|
+
const entries = Object.entries(value).filter(([_k, v]) => v !== void 0).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
|
|
2094
2145
|
return `{ ${entries} }`;
|
|
2095
2146
|
}
|
|
2096
2147
|
return String(value);
|
|
@@ -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);
|
|
@@ -2381,11 +2435,9 @@ function generateFieldModification(collectionName, modification, varName, isLast
|
|
|
2381
2435
|
function generateFieldDeletion(collectionName, fieldName, varName, isLast = false) {
|
|
2382
2436
|
const lines = [];
|
|
2383
2437
|
const collectionVar = varName || `collection_${collectionName}_${fieldName}`;
|
|
2384
|
-
const fieldVar = `${collectionVar}_field`;
|
|
2385
2438
|
lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
|
|
2386
|
-
lines.push(` const ${fieldVar} = ${collectionVar}.fields.getByName("${fieldName}");`);
|
|
2387
2439
|
lines.push(``);
|
|
2388
|
-
lines.push(` ${collectionVar}.fields.
|
|
2440
|
+
lines.push(` ${collectionVar}.fields.removeByName("${fieldName}");`);
|
|
2389
2441
|
lines.push(``);
|
|
2390
2442
|
lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
|
|
2391
2443
|
return lines.join("\n");
|
|
@@ -2502,7 +2554,27 @@ function generateOperationUpMigration(operation, collectionIdMap) {
|
|
|
2502
2554
|
const varName = `collection_${collectionName}`;
|
|
2503
2555
|
lines.push(generateCollectionDeletion(collectionName, varName, true));
|
|
2504
2556
|
}
|
|
2505
|
-
|
|
2557
|
+
let code = lines.join("\n");
|
|
2558
|
+
const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
|
|
2559
|
+
if (!hasReturnStatement) {
|
|
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
|
+
}
|
|
2577
|
+
return code;
|
|
2506
2578
|
}
|
|
2507
2579
|
function generateOperationDownMigration(operation, collectionIdMap) {
|
|
2508
2580
|
const lines = [];
|
|
@@ -2587,7 +2659,27 @@ function generateOperationDownMigration(operation, collectionIdMap) {
|
|
|
2587
2659
|
lines.push(generateCollectionCreation(collection, varName, true, collectionIdMap));
|
|
2588
2660
|
}
|
|
2589
2661
|
}
|
|
2590
|
-
|
|
2662
|
+
let code = lines.join("\n");
|
|
2663
|
+
const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
|
|
2664
|
+
if (!hasReturnStatement) {
|
|
2665
|
+
const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
|
|
2666
|
+
const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
|
|
2667
|
+
const saveMatches = [...code.matchAll(savePattern)];
|
|
2668
|
+
const deleteMatches = [...code.matchAll(deletePattern)];
|
|
2669
|
+
const allMatches = [
|
|
2670
|
+
...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
|
|
2671
|
+
...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
|
|
2672
|
+
].sort((a, b) => b.index - a.index);
|
|
2673
|
+
if (allMatches.length > 0) {
|
|
2674
|
+
const lastMatch = allMatches[0];
|
|
2675
|
+
if (lastMatch.type === "save") {
|
|
2676
|
+
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);
|
|
2677
|
+
} else {
|
|
2678
|
+
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);
|
|
2679
|
+
}
|
|
2680
|
+
}
|
|
2681
|
+
}
|
|
2682
|
+
return code;
|
|
2591
2683
|
}
|
|
2592
2684
|
function generate(diff, config) {
|
|
2593
2685
|
const normalizedConfig = typeof config === "string" ? { migrationDir: config } : config;
|
|
@@ -2636,7 +2728,7 @@ function generate(diff, config) {
|
|
|
2636
2728
|
var SNAPSHOT_VERSION = "1.0.0";
|
|
2637
2729
|
function resolveCollectionIdToName(collectionId) {
|
|
2638
2730
|
if (collectionId === "_pb_users_auth_") {
|
|
2639
|
-
return "
|
|
2731
|
+
return "users";
|
|
2640
2732
|
}
|
|
2641
2733
|
const nameMatch = collectionId.match(/app\.findCollectionByNameOrId\s*\(\s*["']([^"']+)["']\s*\)/);
|
|
2642
2734
|
if (nameMatch) {
|
|
@@ -2644,6 +2736,39 @@ function resolveCollectionIdToName(collectionId) {
|
|
|
2644
2736
|
}
|
|
2645
2737
|
return collectionId;
|
|
2646
2738
|
}
|
|
2739
|
+
function extractFieldOptions2(pbField) {
|
|
2740
|
+
const options = {};
|
|
2741
|
+
if (pbField.options && typeof pbField.options === "object") {
|
|
2742
|
+
Object.assign(options, pbField.options);
|
|
2743
|
+
}
|
|
2744
|
+
const directOptionKeys = [
|
|
2745
|
+
"min",
|
|
2746
|
+
"max",
|
|
2747
|
+
"pattern",
|
|
2748
|
+
"noDecimal",
|
|
2749
|
+
// text/number fields
|
|
2750
|
+
"values",
|
|
2751
|
+
"maxSelect",
|
|
2752
|
+
// select fields
|
|
2753
|
+
"mimeTypes",
|
|
2754
|
+
"maxSize",
|
|
2755
|
+
"thumbs",
|
|
2756
|
+
"protected",
|
|
2757
|
+
// file fields
|
|
2758
|
+
"onCreate",
|
|
2759
|
+
"onUpdate",
|
|
2760
|
+
// autodate fields
|
|
2761
|
+
"exceptDomains",
|
|
2762
|
+
"onlyDomains"
|
|
2763
|
+
// email/url fields
|
|
2764
|
+
];
|
|
2765
|
+
for (const key of directOptionKeys) {
|
|
2766
|
+
if (pbField[key] !== void 0) {
|
|
2767
|
+
options[key] = pbField[key];
|
|
2768
|
+
}
|
|
2769
|
+
}
|
|
2770
|
+
return options;
|
|
2771
|
+
}
|
|
2647
2772
|
function convertPocketBaseCollection(pbCollection) {
|
|
2648
2773
|
const fields = [];
|
|
2649
2774
|
const systemFieldNames = ["id", "created", "updated", "collectionId", "collectionName", "expand"];
|
|
@@ -2661,23 +2786,19 @@ function convertPocketBaseCollection(pbCollection) {
|
|
|
2661
2786
|
type: pbField.type,
|
|
2662
2787
|
required: pbField.required || false
|
|
2663
2788
|
};
|
|
2664
|
-
field.options = pbField
|
|
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
|
-
}
|
|
2789
|
+
field.options = extractFieldOptions2(pbField);
|
|
2672
2790
|
if (pbField.type === "relation") {
|
|
2673
2791
|
const collectionId = pbField.collectionId || pbField.options?.collectionId || "";
|
|
2674
|
-
const collectionName = resolveCollectionIdToName(collectionId);
|
|
2792
|
+
const collectionName = resolveCollectionIdToName(collectionId || "");
|
|
2675
2793
|
field.relation = {
|
|
2676
2794
|
collection: collectionName,
|
|
2677
2795
|
cascadeDelete: pbField.cascadeDelete ?? pbField.options?.cascadeDelete ?? false,
|
|
2678
2796
|
maxSelect: pbField.maxSelect ?? pbField.options?.maxSelect,
|
|
2679
2797
|
minSelect: pbField.minSelect ?? pbField.options?.minSelect
|
|
2680
2798
|
};
|
|
2799
|
+
delete field.options.maxSelect;
|
|
2800
|
+
delete field.options.minSelect;
|
|
2801
|
+
delete field.options.cascadeDelete;
|
|
2681
2802
|
}
|
|
2682
2803
|
const hasOnlyValues = Object.keys(field.options).length === 1 && field.options.values !== void 0;
|
|
2683
2804
|
if (Object.keys(field.options).length === 0) {
|
|
@@ -2691,17 +2812,21 @@ function convertPocketBaseCollection(pbCollection) {
|
|
|
2691
2812
|
type: pbCollection.type || "base",
|
|
2692
2813
|
fields
|
|
2693
2814
|
};
|
|
2815
|
+
if (pbCollection.id) {
|
|
2816
|
+
schema.id = pbCollection.id;
|
|
2817
|
+
}
|
|
2694
2818
|
if (pbCollection.indexes && Array.isArray(pbCollection.indexes)) {
|
|
2695
2819
|
schema.indexes = pbCollection.indexes;
|
|
2696
2820
|
}
|
|
2697
|
-
const
|
|
2698
|
-
if (
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2821
|
+
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;
|
|
2822
|
+
if (hasAnyRule) {
|
|
2823
|
+
const rules = {};
|
|
2824
|
+
if (pbCollection.listRule !== void 0) rules.listRule = pbCollection.listRule;
|
|
2825
|
+
if (pbCollection.viewRule !== void 0) rules.viewRule = pbCollection.viewRule;
|
|
2826
|
+
if (pbCollection.createRule !== void 0) rules.createRule = pbCollection.createRule;
|
|
2827
|
+
if (pbCollection.updateRule !== void 0) rules.updateRule = pbCollection.updateRule;
|
|
2828
|
+
if (pbCollection.deleteRule !== void 0) rules.deleteRule = pbCollection.deleteRule;
|
|
2829
|
+
if (pbCollection.manageRule !== void 0) rules.manageRule = pbCollection.manageRule;
|
|
2705
2830
|
schema.rules = rules;
|
|
2706
2831
|
schema.permissions = { ...rules };
|
|
2707
2832
|
}
|