pocketbase-zod-schema 0.2.5 → 0.3.0
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 +8 -0
- package/dist/cli/index.cjs +374 -296
- 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 +374 -296
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/migrate.cjs +374 -296
- package/dist/cli/migrate.cjs.map +1 -1
- package/dist/cli/migrate.js +374 -296
- 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-YjcpBXVp.d.cts → fields-UcOPu1OQ.d.cts} +16 -0
- package/dist/{fields-YjcpBXVp.d.ts → fields-UcOPu1OQ.d.ts} +16 -0
- package/dist/index.cjs +413 -114
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +414 -103
- package/dist/index.js.map +1 -1
- package/dist/migration/analyzer.cjs +12 -2
- 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 +12 -2
- package/dist/migration/analyzer.js.map +1 -1
- package/dist/migration/diff.cjs +76 -1
- package/dist/migration/diff.cjs.map +1 -1
- package/dist/migration/diff.d.cts +2 -2
- package/dist/migration/diff.d.ts +2 -2
- package/dist/migration/diff.js +76 -1
- package/dist/migration/diff.js.map +1 -1
- package/dist/migration/generator.cjs +323 -46
- package/dist/migration/generator.cjs.map +1 -1
- package/dist/migration/generator.d.cts +59 -12
- package/dist/migration/generator.d.ts +59 -12
- package/dist/migration/generator.js +319 -47
- package/dist/migration/generator.js.map +1 -1
- package/dist/migration/index.cjs +399 -49
- 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 +399 -49
- package/dist/migration/index.js.map +1 -1
- 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.map +1 -1
- package/dist/migration/utils/index.cjs +64 -0
- package/dist/migration/utils/index.cjs.map +1 -1
- package/dist/migration/utils/index.d.cts +39 -202
- package/dist/migration/utils/index.d.ts +39 -202
- package/dist/migration/utils/index.js +63 -1
- package/dist/migration/utils/index.js.map +1 -1
- package/dist/schema.cjs +0 -61
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.d.cts +2 -86
- package/dist/schema.d.ts +2 -86
- package/dist/schema.js +1 -50
- package/dist/schema.js.map +1 -1
- package/dist/type-mapper-DrQmtznD.d.cts +208 -0
- package/dist/type-mapper-n231Fspm.d.ts +208 -0
- package/dist/{types-LFBGHl9Y.d.ts → types-Ds3NQvny.d.ts} +33 -2
- package/dist/{types-mhQXWNi3.d.cts → types-YoBjsa-A.d.cts} +33 -2
- package/package.json +1 -1
package/dist/cli/migrate.cjs
CHANGED
|
@@ -7,6 +7,7 @@ var fs5 = require('fs');
|
|
|
7
7
|
var path5 = require('path');
|
|
8
8
|
var url = require('url');
|
|
9
9
|
var zod = require('zod');
|
|
10
|
+
var crypto = require('crypto');
|
|
10
11
|
var ora = require('ora');
|
|
11
12
|
|
|
12
13
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -1320,12 +1321,22 @@ function isAuthCollection(fields) {
|
|
|
1320
1321
|
function buildFieldDefinition(fieldName, zodType) {
|
|
1321
1322
|
const fieldMetadata = extractFieldMetadata(zodType.description);
|
|
1322
1323
|
if (fieldMetadata) {
|
|
1323
|
-
|
|
1324
|
+
let required2;
|
|
1325
|
+
if (fieldMetadata.type === "number") {
|
|
1326
|
+
if (fieldMetadata.options?.required !== void 0) {
|
|
1327
|
+
required2 = fieldMetadata.options.required;
|
|
1328
|
+
} else {
|
|
1329
|
+
required2 = false;
|
|
1330
|
+
}
|
|
1331
|
+
} else {
|
|
1332
|
+
required2 = isFieldRequired(zodType);
|
|
1333
|
+
}
|
|
1334
|
+
const { required: _required, ...options2 } = fieldMetadata.options || {};
|
|
1324
1335
|
const fieldDef2 = {
|
|
1325
1336
|
name: fieldName,
|
|
1326
1337
|
type: fieldMetadata.type,
|
|
1327
1338
|
required: required2,
|
|
1328
|
-
options:
|
|
1339
|
+
options: Object.keys(options2).length > 0 ? options2 : void 0
|
|
1329
1340
|
};
|
|
1330
1341
|
if (fieldMetadata.type === "relation") {
|
|
1331
1342
|
const relationMetadata2 = extractRelationMetadata(zodType.description);
|
|
@@ -1495,6 +1506,65 @@ async function buildSchemaDefinition(config) {
|
|
|
1495
1506
|
async function parseSchemaFiles(config) {
|
|
1496
1507
|
return buildSchemaDefinition(config);
|
|
1497
1508
|
}
|
|
1509
|
+
function generateCollectionId() {
|
|
1510
|
+
const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
1511
|
+
const idLength = 15;
|
|
1512
|
+
const bytes = crypto.randomBytes(idLength);
|
|
1513
|
+
let id = "pb_";
|
|
1514
|
+
for (let i = 0; i < idLength; i++) {
|
|
1515
|
+
const index = bytes[i] % chars.length;
|
|
1516
|
+
id += chars[index];
|
|
1517
|
+
}
|
|
1518
|
+
return id;
|
|
1519
|
+
}
|
|
1520
|
+
var CollectionIdRegistry = class {
|
|
1521
|
+
ids;
|
|
1522
|
+
constructor() {
|
|
1523
|
+
this.ids = /* @__PURE__ */ new Set();
|
|
1524
|
+
}
|
|
1525
|
+
/**
|
|
1526
|
+
* Generates a unique collection ID for a given collection name
|
|
1527
|
+
* Special case: Returns constant "_pb_users_auth_" for users collection
|
|
1528
|
+
* Retries up to 10 times if collision occurs (extremely rare)
|
|
1529
|
+
*
|
|
1530
|
+
* @param collectionName - The name of the collection
|
|
1531
|
+
* @returns A unique collection ID
|
|
1532
|
+
* @throws Error if unable to generate unique ID after max attempts
|
|
1533
|
+
*/
|
|
1534
|
+
generate(collectionName) {
|
|
1535
|
+
if (collectionName && collectionName.toLowerCase() === "users") {
|
|
1536
|
+
const usersId = "_pb_users_auth_";
|
|
1537
|
+
this.register(usersId);
|
|
1538
|
+
return usersId;
|
|
1539
|
+
}
|
|
1540
|
+
const maxAttempts = 10;
|
|
1541
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
1542
|
+
const id = generateCollectionId();
|
|
1543
|
+
if (!this.has(id)) {
|
|
1544
|
+
this.register(id);
|
|
1545
|
+
return id;
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
throw new Error("Failed to generate unique collection ID after maximum attempts");
|
|
1549
|
+
}
|
|
1550
|
+
/**
|
|
1551
|
+
* Checks if an ID has already been registered
|
|
1552
|
+
*
|
|
1553
|
+
* @param id - The collection ID to check
|
|
1554
|
+
* @returns True if the ID exists in the registry
|
|
1555
|
+
*/
|
|
1556
|
+
has(id) {
|
|
1557
|
+
return this.ids.has(id);
|
|
1558
|
+
}
|
|
1559
|
+
/**
|
|
1560
|
+
* Registers a collection ID in the registry
|
|
1561
|
+
*
|
|
1562
|
+
* @param id - The collection ID to register
|
|
1563
|
+
*/
|
|
1564
|
+
register(id) {
|
|
1565
|
+
this.ids.add(id);
|
|
1566
|
+
}
|
|
1567
|
+
};
|
|
1498
1568
|
|
|
1499
1569
|
// src/migration/diff.ts
|
|
1500
1570
|
var DEFAULT_CONFIG2 = {
|
|
@@ -1826,6 +1896,18 @@ function aggregateChanges(currentSchema, previousSnapshot, config) {
|
|
|
1826
1896
|
const filteredCollectionsToDelete = collectionsToDelete.filter(
|
|
1827
1897
|
(collection) => !isSystemCollection(collection.name, config)
|
|
1828
1898
|
);
|
|
1899
|
+
const registry = new CollectionIdRegistry();
|
|
1900
|
+
const collectionsWithIds = filteredCollectionsToCreate.map((collection) => {
|
|
1901
|
+
if (collection.id) {
|
|
1902
|
+
registry.register(collection.id);
|
|
1903
|
+
return collection;
|
|
1904
|
+
}
|
|
1905
|
+
const id = registry.generate(collection.name);
|
|
1906
|
+
return {
|
|
1907
|
+
...collection,
|
|
1908
|
+
id
|
|
1909
|
+
};
|
|
1910
|
+
});
|
|
1829
1911
|
const collectionsToModify = [];
|
|
1830
1912
|
const matchedCollections = matchCollectionsByName(currentSchema, previousSnapshot);
|
|
1831
1913
|
for (const [currentCollection, previousCollection] of matchedCollections) {
|
|
@@ -1835,7 +1917,7 @@ function aggregateChanges(currentSchema, previousSnapshot, config) {
|
|
|
1835
1917
|
}
|
|
1836
1918
|
}
|
|
1837
1919
|
return {
|
|
1838
|
-
collectionsToCreate:
|
|
1920
|
+
collectionsToCreate: collectionsWithIds,
|
|
1839
1921
|
collectionsToDelete: filteredCollectionsToDelete,
|
|
1840
1922
|
collectionsToModify
|
|
1841
1923
|
};
|
|
@@ -1918,42 +2000,48 @@ function generateTimestamp(config) {
|
|
|
1918
2000
|
}
|
|
1919
2001
|
return Math.floor(Date.now() / 1e3).toString();
|
|
1920
2002
|
}
|
|
1921
|
-
function
|
|
1922
|
-
const
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
if (diff.collectionsToDelete.length === 1) {
|
|
1932
|
-
parts.push(`deleted_${diff.collectionsToDelete[0].name}`);
|
|
1933
|
-
} else {
|
|
1934
|
-
parts.push(`deleted_${diff.collectionsToDelete.length}_collections`);
|
|
1935
|
-
}
|
|
1936
|
-
}
|
|
1937
|
-
if (diff.collectionsToModify.length > 0) {
|
|
1938
|
-
if (diff.collectionsToModify.length === 1) {
|
|
1939
|
-
parts.push(`updated_${diff.collectionsToModify[0].collection}`);
|
|
1940
|
-
} else {
|
|
1941
|
-
parts.push(`updated_${diff.collectionsToModify.length}_collections`);
|
|
1942
|
-
}
|
|
2003
|
+
function splitDiffByCollection(diff, baseTimestamp) {
|
|
2004
|
+
const operations = [];
|
|
2005
|
+
let currentTimestamp = parseInt(baseTimestamp, 10);
|
|
2006
|
+
for (const collection of diff.collectionsToCreate) {
|
|
2007
|
+
operations.push({
|
|
2008
|
+
type: "create",
|
|
2009
|
+
collection,
|
|
2010
|
+
timestamp: currentTimestamp.toString()
|
|
2011
|
+
});
|
|
2012
|
+
currentTimestamp += 1;
|
|
1943
2013
|
}
|
|
1944
|
-
|
|
1945
|
-
|
|
2014
|
+
for (const modification of diff.collectionsToModify) {
|
|
2015
|
+
operations.push({
|
|
2016
|
+
type: "modify",
|
|
2017
|
+
collection: modification.collection,
|
|
2018
|
+
modifications: modification,
|
|
2019
|
+
timestamp: currentTimestamp.toString()
|
|
2020
|
+
});
|
|
2021
|
+
currentTimestamp += 1;
|
|
1946
2022
|
}
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
2023
|
+
for (const collection of diff.collectionsToDelete) {
|
|
2024
|
+
operations.push({
|
|
2025
|
+
type: "delete",
|
|
2026
|
+
collection: collection.name || collection,
|
|
2027
|
+
// Handle both object and string
|
|
2028
|
+
timestamp: currentTimestamp.toString()
|
|
2029
|
+
});
|
|
2030
|
+
currentTimestamp += 1;
|
|
1950
2031
|
}
|
|
1951
|
-
return
|
|
2032
|
+
return operations;
|
|
1952
2033
|
}
|
|
1953
|
-
function
|
|
1954
|
-
const timestamp =
|
|
1955
|
-
const
|
|
1956
|
-
|
|
2034
|
+
function generateCollectionMigrationFilename(operation) {
|
|
2035
|
+
const timestamp = operation.timestamp;
|
|
2036
|
+
const operationType = operation.type === "modify" ? "updated" : operation.type === "create" ? "created" : "deleted";
|
|
2037
|
+
let collectionName;
|
|
2038
|
+
if (typeof operation.collection === "string") {
|
|
2039
|
+
collectionName = operation.collection;
|
|
2040
|
+
} else {
|
|
2041
|
+
collectionName = operation.collection.name;
|
|
2042
|
+
}
|
|
2043
|
+
const sanitizedName = collectionName.replace(/[^a-zA-Z0-9_]/g, "_").toLowerCase();
|
|
2044
|
+
return `${timestamp}_${operationType}_${sanitizedName}.js`;
|
|
1957
2045
|
}
|
|
1958
2046
|
function createMigrationFileStructure(upCode, downCode, config) {
|
|
1959
2047
|
const mergedConfig = config ? mergeConfig3(config) : DEFAULT_CONFIG3;
|
|
@@ -2025,14 +2113,13 @@ function formatValue(value) {
|
|
|
2025
2113
|
return "null";
|
|
2026
2114
|
}
|
|
2027
2115
|
if (typeof value === "string") {
|
|
2028
|
-
return
|
|
2116
|
+
return JSON.stringify(value);
|
|
2029
2117
|
}
|
|
2030
2118
|
if (typeof value === "number" || typeof value === "boolean") {
|
|
2031
2119
|
return String(value);
|
|
2032
2120
|
}
|
|
2033
2121
|
if (Array.isArray(value)) {
|
|
2034
|
-
|
|
2035
|
-
return `[${items}]`;
|
|
2122
|
+
return JSON.stringify(value).replace(/","/g, '", "');
|
|
2036
2123
|
}
|
|
2037
2124
|
if (typeof value === "object") {
|
|
2038
2125
|
const entries = Object.entries(value).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
|
|
@@ -2040,7 +2127,7 @@ function formatValue(value) {
|
|
|
2040
2127
|
}
|
|
2041
2128
|
return String(value);
|
|
2042
2129
|
}
|
|
2043
|
-
function generateFieldDefinitionObject(field) {
|
|
2130
|
+
function generateFieldDefinitionObject(field, collectionIdMap) {
|
|
2044
2131
|
const parts = [];
|
|
2045
2132
|
parts.push(` name: "${field.name}"`);
|
|
2046
2133
|
parts.push(` type: "${field.type}"`);
|
|
@@ -2048,34 +2135,47 @@ function generateFieldDefinitionObject(field) {
|
|
|
2048
2135
|
if (field.unique !== void 0) {
|
|
2049
2136
|
parts.push(` unique: ${field.unique}`);
|
|
2050
2137
|
}
|
|
2138
|
+
if (field.type === "select") {
|
|
2139
|
+
const maxSelect = field.options?.maxSelect ?? 1;
|
|
2140
|
+
parts.push(` maxSelect: ${maxSelect}`);
|
|
2141
|
+
const values = field.options?.values ?? [];
|
|
2142
|
+
parts.push(` values: ${formatValue(values)}`);
|
|
2143
|
+
}
|
|
2051
2144
|
if (field.options && Object.keys(field.options).length > 0) {
|
|
2052
2145
|
for (const [key, value] of Object.entries(field.options)) {
|
|
2146
|
+
if (field.type === "select" && (key === "maxSelect" || key === "values")) {
|
|
2147
|
+
continue;
|
|
2148
|
+
}
|
|
2053
2149
|
parts.push(` ${key}: ${formatValue(value)}`);
|
|
2054
2150
|
}
|
|
2055
2151
|
}
|
|
2056
2152
|
if (field.relation) {
|
|
2057
2153
|
const isUsersCollection = field.relation.collection.toLowerCase() === "users";
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
}
|
|
2066
|
-
if (field.relation.cascadeDelete !== void 0) {
|
|
2067
|
-
parts.push(` cascadeDelete: ${field.relation.cascadeDelete}`);
|
|
2154
|
+
let collectionIdValue;
|
|
2155
|
+
if (isUsersCollection) {
|
|
2156
|
+
collectionIdValue = '"_pb_users_auth_"';
|
|
2157
|
+
} else if (collectionIdMap && collectionIdMap.has(field.relation.collection)) {
|
|
2158
|
+
collectionIdValue = `"${collectionIdMap.get(field.relation.collection)}"`;
|
|
2159
|
+
} else {
|
|
2160
|
+
collectionIdValue = `app.findCollectionByNameOrId("${field.relation.collection}").id`;
|
|
2068
2161
|
}
|
|
2162
|
+
parts.push(` collectionId: ${collectionIdValue}`);
|
|
2163
|
+
const maxSelect = field.relation.maxSelect ?? 1;
|
|
2164
|
+
parts.push(` maxSelect: ${maxSelect}`);
|
|
2165
|
+
const minSelect = field.relation.minSelect ?? null;
|
|
2166
|
+
parts.push(` minSelect: ${minSelect}`);
|
|
2167
|
+
const cascadeDelete = field.relation.cascadeDelete ?? false;
|
|
2168
|
+
parts.push(` cascadeDelete: ${cascadeDelete}`);
|
|
2069
2169
|
}
|
|
2070
2170
|
return ` {
|
|
2071
2171
|
${parts.join(",\n")},
|
|
2072
2172
|
}`;
|
|
2073
2173
|
}
|
|
2074
|
-
function generateFieldsArray(fields) {
|
|
2174
|
+
function generateFieldsArray(fields, collectionIdMap) {
|
|
2075
2175
|
if (fields.length === 0) {
|
|
2076
2176
|
return "[]";
|
|
2077
2177
|
}
|
|
2078
|
-
const fieldObjects = fields.map((field) => generateFieldDefinitionObject(field));
|
|
2178
|
+
const fieldObjects = fields.map((field) => generateFieldDefinitionObject(field, collectionIdMap));
|
|
2079
2179
|
return `[
|
|
2080
2180
|
${fieldObjects.join(",\n")},
|
|
2081
2181
|
]`;
|
|
@@ -2134,7 +2234,7 @@ function generateIndexesArray(indexes) {
|
|
|
2134
2234
|
if (!indexes || indexes.length === 0) {
|
|
2135
2235
|
return "[]";
|
|
2136
2236
|
}
|
|
2137
|
-
const indexStrings = indexes.map((idx) =>
|
|
2237
|
+
const indexStrings = indexes.map((idx) => JSON.stringify(idx));
|
|
2138
2238
|
return `[
|
|
2139
2239
|
${indexStrings.join(",\n ")},
|
|
2140
2240
|
]`;
|
|
@@ -2188,7 +2288,7 @@ function getSystemFields() {
|
|
|
2188
2288
|
}
|
|
2189
2289
|
];
|
|
2190
2290
|
}
|
|
2191
|
-
function generateCollectionCreation(collection, varName = "collection", isLast = false) {
|
|
2291
|
+
function generateCollectionCreation(collection, varName = "collection", isLast = false, collectionIdMap) {
|
|
2192
2292
|
const lines = [];
|
|
2193
2293
|
lines.push(` const ${varName} = new Collection({`);
|
|
2194
2294
|
lines.push(` name: "${collection.name}",`);
|
|
@@ -2202,7 +2302,7 @@ function generateCollectionCreation(collection, varName = "collection", isLast =
|
|
|
2202
2302
|
}
|
|
2203
2303
|
const systemFields = getSystemFields();
|
|
2204
2304
|
const allFields = [...systemFields, ...collection.fields];
|
|
2205
|
-
lines.push(` fields: ${generateFieldsArray(allFields)},`);
|
|
2305
|
+
lines.push(` fields: ${generateFieldsArray(allFields, collectionIdMap)},`);
|
|
2206
2306
|
lines.push(` indexes: ${generateIndexesArray(collection.indexes)},`);
|
|
2207
2307
|
lines.push(` });`);
|
|
2208
2308
|
lines.push(``);
|
|
@@ -2224,42 +2324,59 @@ function getFieldConstructorName(fieldType) {
|
|
|
2224
2324
|
};
|
|
2225
2325
|
return constructorMap[fieldType] || "TextField";
|
|
2226
2326
|
}
|
|
2227
|
-
function generateFieldConstructorOptions(field) {
|
|
2327
|
+
function generateFieldConstructorOptions(field, collectionIdMap) {
|
|
2228
2328
|
const parts = [];
|
|
2229
2329
|
parts.push(` name: "${field.name}"`);
|
|
2230
2330
|
parts.push(` required: ${field.required}`);
|
|
2231
2331
|
if (field.unique !== void 0) {
|
|
2232
2332
|
parts.push(` unique: ${field.unique}`);
|
|
2233
2333
|
}
|
|
2334
|
+
if (field.type === "select") {
|
|
2335
|
+
const maxSelect = field.options?.maxSelect ?? 1;
|
|
2336
|
+
parts.push(` maxSelect: ${maxSelect}`);
|
|
2337
|
+
const values = field.options?.values ?? [];
|
|
2338
|
+
parts.push(` values: ${formatValue(values)}`);
|
|
2339
|
+
}
|
|
2234
2340
|
if (field.options && Object.keys(field.options).length > 0) {
|
|
2235
2341
|
for (const [key, value] of Object.entries(field.options)) {
|
|
2236
|
-
|
|
2342
|
+
if (field.type === "select" && (key === "maxSelect" || key === "values")) {
|
|
2343
|
+
continue;
|
|
2344
|
+
}
|
|
2345
|
+
if (field.type === "number" && key === "noDecimal") {
|
|
2346
|
+
parts.push(` onlyInt: ${formatValue(value)}`);
|
|
2347
|
+
} else {
|
|
2348
|
+
parts.push(` ${key}: ${formatValue(value)}`);
|
|
2349
|
+
}
|
|
2237
2350
|
}
|
|
2238
2351
|
}
|
|
2239
2352
|
if (field.relation && field.type === "relation") {
|
|
2240
2353
|
const isUsersCollection = field.relation.collection.toLowerCase() === "users";
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
}
|
|
2249
|
-
if (field.relation.cascadeDelete !== void 0) {
|
|
2250
|
-
parts.push(` cascadeDelete: ${field.relation.cascadeDelete}`);
|
|
2354
|
+
let collectionIdValue;
|
|
2355
|
+
if (isUsersCollection) {
|
|
2356
|
+
collectionIdValue = '"_pb_users_auth_"';
|
|
2357
|
+
} else if (collectionIdMap && collectionIdMap.has(field.relation.collection)) {
|
|
2358
|
+
collectionIdValue = `"${collectionIdMap.get(field.relation.collection)}"`;
|
|
2359
|
+
} else {
|
|
2360
|
+
collectionIdValue = `app.findCollectionByNameOrId("${field.relation.collection}").id`;
|
|
2251
2361
|
}
|
|
2362
|
+
parts.push(` collectionId: ${collectionIdValue}`);
|
|
2363
|
+
const maxSelect = field.relation.maxSelect ?? 1;
|
|
2364
|
+
parts.push(` maxSelect: ${maxSelect}`);
|
|
2365
|
+
const minSelect = field.relation.minSelect ?? null;
|
|
2366
|
+
parts.push(` minSelect: ${minSelect}`);
|
|
2367
|
+
const cascadeDelete = field.relation.cascadeDelete ?? false;
|
|
2368
|
+
parts.push(` cascadeDelete: ${cascadeDelete}`);
|
|
2252
2369
|
}
|
|
2253
2370
|
return parts.join(",\n");
|
|
2254
2371
|
}
|
|
2255
|
-
function generateFieldAddition(collectionName, field, varName, isLast = false) {
|
|
2372
|
+
function generateFieldAddition(collectionName, field, varName, isLast = false, collectionIdMap) {
|
|
2256
2373
|
const lines = [];
|
|
2257
2374
|
const constructorName = getFieldConstructorName(field.type);
|
|
2258
2375
|
const collectionVar = varName || `collection_${collectionName}_${field.name}`;
|
|
2259
2376
|
lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
|
|
2260
2377
|
lines.push(``);
|
|
2261
2378
|
lines.push(` ${collectionVar}.fields.add(new ${constructorName}({`);
|
|
2262
|
-
lines.push(generateFieldConstructorOptions(field));
|
|
2379
|
+
lines.push(generateFieldConstructorOptions(field, collectionIdMap));
|
|
2263
2380
|
lines.push(` }));`);
|
|
2264
2381
|
lines.push(``);
|
|
2265
2382
|
lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
|
|
@@ -2309,7 +2426,7 @@ function generateIndexAddition(collectionName, index, varName, isLast = false) {
|
|
|
2309
2426
|
const lines = [];
|
|
2310
2427
|
const collectionVar = varName || `collection_${collectionName}_idx`;
|
|
2311
2428
|
lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
|
|
2312
|
-
lines.push(` ${collectionVar}.indexes.push(
|
|
2429
|
+
lines.push(` ${collectionVar}.indexes.push(${JSON.stringify(index)});`);
|
|
2313
2430
|
lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
|
|
2314
2431
|
return lines.join("\n");
|
|
2315
2432
|
}
|
|
@@ -2318,7 +2435,7 @@ function generateIndexRemoval(collectionName, index, varName, isLast = false) {
|
|
|
2318
2435
|
const collectionVar = varName || `collection_${collectionName}_idx`;
|
|
2319
2436
|
const indexVar = `${collectionVar}_indexToRemove`;
|
|
2320
2437
|
lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
|
|
2321
|
-
lines.push(` const ${indexVar} = ${collectionVar}.indexes.findIndex(idx => idx ===
|
|
2438
|
+
lines.push(` const ${indexVar} = ${collectionVar}.indexes.findIndex(idx => idx === ${JSON.stringify(index)});`);
|
|
2322
2439
|
lines.push(` if (${indexVar} !== -1) {`);
|
|
2323
2440
|
lines.push(` ${collectionVar}.indexes.splice(${indexVar}, 1);`);
|
|
2324
2441
|
lines.push(` }`);
|
|
@@ -2347,243 +2464,194 @@ function generateCollectionDeletion(collectionName, varName = "collection", isLa
|
|
|
2347
2464
|
lines.push(isLast ? ` return app.delete(${varName});` : ` app.delete(${varName});`);
|
|
2348
2465
|
return lines.join("\n");
|
|
2349
2466
|
}
|
|
2350
|
-
function
|
|
2467
|
+
function generateOperationUpMigration(operation, collectionIdMap) {
|
|
2351
2468
|
const lines = [];
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
lines.push(
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
if (modification.fieldsToAdd.length > 0) {
|
|
2368
|
-
lines.push(` // Add fields to ${collectionName}`);
|
|
2369
|
-
for (const field of modification.fieldsToAdd) {
|
|
2370
|
-
const varName = `collection_${collectionName}_add_${field.name}`;
|
|
2371
|
-
lines.push(generateFieldAddition(collectionName, field, varName));
|
|
2372
|
-
lines.push(``);
|
|
2373
|
-
}
|
|
2374
|
-
}
|
|
2375
|
-
if (modification.fieldsToModify.length > 0) {
|
|
2376
|
-
lines.push(` // Modify fields in ${collectionName}`);
|
|
2377
|
-
for (const fieldMod of modification.fieldsToModify) {
|
|
2378
|
-
const varName = `collection_${collectionName}_modify_${fieldMod.fieldName}`;
|
|
2379
|
-
lines.push(generateFieldModification(collectionName, fieldMod, varName));
|
|
2380
|
-
lines.push(``);
|
|
2381
|
-
}
|
|
2382
|
-
}
|
|
2383
|
-
if (modification.fieldsToRemove.length > 0) {
|
|
2384
|
-
lines.push(` // Remove fields from ${collectionName}`);
|
|
2385
|
-
for (const field of modification.fieldsToRemove) {
|
|
2386
|
-
const varName = `collection_${collectionName}_remove_${field.name}`;
|
|
2387
|
-
lines.push(generateFieldDeletion(collectionName, field.name, varName));
|
|
2388
|
-
lines.push(``);
|
|
2389
|
-
}
|
|
2390
|
-
}
|
|
2391
|
-
if (modification.indexesToAdd.length > 0) {
|
|
2392
|
-
lines.push(` // Add indexes to ${collectionName}`);
|
|
2393
|
-
for (let i = 0; i < modification.indexesToAdd.length; i++) {
|
|
2394
|
-
const index = modification.indexesToAdd[i];
|
|
2395
|
-
const varName = `collection_${collectionName}_addidx_${i}`;
|
|
2396
|
-
lines.push(generateIndexAddition(collectionName, index, varName));
|
|
2397
|
-
lines.push(``);
|
|
2398
|
-
}
|
|
2399
|
-
}
|
|
2400
|
-
if (modification.indexesToRemove.length > 0) {
|
|
2401
|
-
lines.push(` // Remove indexes from ${collectionName}`);
|
|
2402
|
-
for (let i = 0; i < modification.indexesToRemove.length; i++) {
|
|
2403
|
-
const index = modification.indexesToRemove[i];
|
|
2404
|
-
const varName = `collection_${collectionName}_rmidx_${i}`;
|
|
2405
|
-
lines.push(generateIndexRemoval(collectionName, index, varName));
|
|
2406
|
-
lines.push(``);
|
|
2407
|
-
}
|
|
2408
|
-
}
|
|
2409
|
-
if (modification.permissionsToUpdate && modification.permissionsToUpdate.length > 0) {
|
|
2410
|
-
lines.push(` // Update permissions for ${collectionName}`);
|
|
2411
|
-
for (const permission of modification.permissionsToUpdate) {
|
|
2412
|
-
const varName = `collection_${collectionName}_perm_${permission.ruleType}`;
|
|
2413
|
-
lines.push(generatePermissionUpdate(collectionName, permission.ruleType, permission.newValue, varName));
|
|
2414
|
-
lines.push(``);
|
|
2415
|
-
}
|
|
2416
|
-
} else if (modification.rulesToUpdate.length > 0) {
|
|
2417
|
-
lines.push(` // Update rules for ${collectionName}`);
|
|
2418
|
-
for (const rule of modification.rulesToUpdate) {
|
|
2419
|
-
const varName = `collection_${collectionName}_rule_${rule.ruleType}`;
|
|
2420
|
-
lines.push(generateRuleUpdate(collectionName, rule.ruleType, rule.newValue, varName));
|
|
2421
|
-
lines.push(``);
|
|
2422
|
-
}
|
|
2423
|
-
}
|
|
2469
|
+
if (operation.type === "create") {
|
|
2470
|
+
const collection = operation.collection;
|
|
2471
|
+
const varName = `collection_${collection.name}`;
|
|
2472
|
+
lines.push(generateCollectionCreation(collection, varName, true, collectionIdMap));
|
|
2473
|
+
} else if (operation.type === "modify") {
|
|
2474
|
+
const modification = operation.modifications;
|
|
2475
|
+
const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection?.name ?? modification.collection;
|
|
2476
|
+
let operationCount = 0;
|
|
2477
|
+
const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + modification.rulesToUpdate.length + modification.permissionsToUpdate.length;
|
|
2478
|
+
for (const field of modification.fieldsToAdd) {
|
|
2479
|
+
operationCount++;
|
|
2480
|
+
const varName = `collection_${collectionName}_add_${field.name}`;
|
|
2481
|
+
const isLast = operationCount === totalOperations;
|
|
2482
|
+
lines.push(generateFieldAddition(collectionName, field, varName, isLast, collectionIdMap));
|
|
2483
|
+
if (!isLast) lines.push("");
|
|
2424
2484
|
}
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
lines.push(generateCollectionDeletion(collection.name, varName));
|
|
2432
|
-
lines.push(``);
|
|
2433
|
-
}
|
|
2434
|
-
}
|
|
2435
|
-
if (lines.length === 2) {
|
|
2436
|
-
lines.push(` // No changes detected`);
|
|
2437
|
-
lines.push(``);
|
|
2438
|
-
}
|
|
2439
|
-
let code = lines.join("\n");
|
|
2440
|
-
const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
|
|
2441
|
-
const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
|
|
2442
|
-
const saveMatches = [...code.matchAll(savePattern)];
|
|
2443
|
-
const deleteMatches = [...code.matchAll(deletePattern)];
|
|
2444
|
-
const allMatches = [
|
|
2445
|
-
...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
|
|
2446
|
-
...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
|
|
2447
|
-
].sort((a, b) => b.index - a.index);
|
|
2448
|
-
if (allMatches.length > 0) {
|
|
2449
|
-
const lastMatch = allMatches[0];
|
|
2450
|
-
if (lastMatch.type === "save") {
|
|
2451
|
-
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);
|
|
2452
|
-
} else {
|
|
2453
|
-
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);
|
|
2485
|
+
for (const fieldMod of modification.fieldsToModify) {
|
|
2486
|
+
operationCount++;
|
|
2487
|
+
const varName = `collection_${collectionName}_modify_${fieldMod.fieldName}`;
|
|
2488
|
+
const isLast = operationCount === totalOperations;
|
|
2489
|
+
lines.push(generateFieldModification(collectionName, fieldMod, varName, isLast));
|
|
2490
|
+
if (!isLast) lines.push("");
|
|
2454
2491
|
}
|
|
2492
|
+
for (const field of modification.fieldsToRemove) {
|
|
2493
|
+
operationCount++;
|
|
2494
|
+
const varName = `collection_${collectionName}_remove_${field.name}`;
|
|
2495
|
+
const isLast = operationCount === totalOperations;
|
|
2496
|
+
lines.push(generateFieldDeletion(collectionName, field.name, varName, isLast));
|
|
2497
|
+
if (!isLast) lines.push("");
|
|
2498
|
+
}
|
|
2499
|
+
for (let i = 0; i < modification.indexesToAdd.length; i++) {
|
|
2500
|
+
operationCount++;
|
|
2501
|
+
const index = modification.indexesToAdd[i];
|
|
2502
|
+
const varName = `collection_${collectionName}_addidx_${i}`;
|
|
2503
|
+
const isLast = operationCount === totalOperations;
|
|
2504
|
+
lines.push(generateIndexAddition(collectionName, index, varName, isLast));
|
|
2505
|
+
if (!isLast) lines.push("");
|
|
2506
|
+
}
|
|
2507
|
+
for (let i = 0; i < modification.indexesToRemove.length; i++) {
|
|
2508
|
+
operationCount++;
|
|
2509
|
+
const index = modification.indexesToRemove[i];
|
|
2510
|
+
const varName = `collection_${collectionName}_rmidx_${i}`;
|
|
2511
|
+
const isLast = operationCount === totalOperations;
|
|
2512
|
+
lines.push(generateIndexRemoval(collectionName, index, varName, isLast));
|
|
2513
|
+
if (!isLast) lines.push("");
|
|
2514
|
+
}
|
|
2515
|
+
if (modification.permissionsToUpdate && modification.permissionsToUpdate.length > 0) {
|
|
2516
|
+
for (const permission of modification.permissionsToUpdate) {
|
|
2517
|
+
operationCount++;
|
|
2518
|
+
const varName = `collection_${collectionName}_perm_${permission.ruleType}`;
|
|
2519
|
+
const isLast = operationCount === totalOperations;
|
|
2520
|
+
lines.push(generatePermissionUpdate(collectionName, permission.ruleType, permission.newValue, varName, isLast));
|
|
2521
|
+
if (!isLast) lines.push("");
|
|
2522
|
+
}
|
|
2523
|
+
} else if (modification.rulesToUpdate.length > 0) {
|
|
2524
|
+
for (const rule of modification.rulesToUpdate) {
|
|
2525
|
+
operationCount++;
|
|
2526
|
+
const varName = `collection_${collectionName}_rule_${rule.ruleType}`;
|
|
2527
|
+
const isLast = operationCount === totalOperations;
|
|
2528
|
+
lines.push(generateRuleUpdate(collectionName, rule.ruleType, rule.newValue, varName, isLast));
|
|
2529
|
+
if (!isLast) lines.push("");
|
|
2530
|
+
}
|
|
2531
|
+
}
|
|
2532
|
+
} else if (operation.type === "delete") {
|
|
2533
|
+
const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection.name;
|
|
2534
|
+
const varName = `collection_${collectionName}`;
|
|
2535
|
+
lines.push(generateCollectionDeletion(collectionName, varName, true));
|
|
2455
2536
|
}
|
|
2456
|
-
return
|
|
2537
|
+
return lines.join("\n");
|
|
2457
2538
|
}
|
|
2458
|
-
function
|
|
2539
|
+
function generateOperationDownMigration(operation, collectionIdMap) {
|
|
2459
2540
|
const lines = [];
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
lines.push(
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2541
|
+
if (operation.type === "create") {
|
|
2542
|
+
const collection = operation.collection;
|
|
2543
|
+
const varName = `collection_${collection.name}`;
|
|
2544
|
+
lines.push(generateCollectionDeletion(collection.name, varName, true));
|
|
2545
|
+
} else if (operation.type === "modify") {
|
|
2546
|
+
const modification = operation.modifications;
|
|
2547
|
+
const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection?.name ?? modification.collection;
|
|
2548
|
+
let operationCount = 0;
|
|
2549
|
+
const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + modification.rulesToUpdate.length + modification.permissionsToUpdate.length;
|
|
2550
|
+
if (modification.permissionsToUpdate && modification.permissionsToUpdate.length > 0) {
|
|
2551
|
+
for (const permission of modification.permissionsToUpdate) {
|
|
2552
|
+
operationCount++;
|
|
2553
|
+
const varName = `collection_${collectionName}_revert_perm_${permission.ruleType}`;
|
|
2554
|
+
const isLast = operationCount === totalOperations;
|
|
2555
|
+
lines.push(generatePermissionUpdate(collectionName, permission.ruleType, permission.oldValue, varName, isLast));
|
|
2556
|
+
if (!isLast) lines.push("");
|
|
2557
|
+
}
|
|
2558
|
+
} else if (modification.rulesToUpdate.length > 0) {
|
|
2559
|
+
for (const rule of modification.rulesToUpdate) {
|
|
2560
|
+
operationCount++;
|
|
2561
|
+
const varName = `collection_${collectionName}_revert_rule_${rule.ruleType}`;
|
|
2562
|
+
const isLast = operationCount === totalOperations;
|
|
2563
|
+
lines.push(generateRuleUpdate(collectionName, rule.ruleType, rule.oldValue, varName, isLast));
|
|
2564
|
+
if (!isLast) lines.push("");
|
|
2565
|
+
}
|
|
2566
|
+
}
|
|
2567
|
+
for (let i = 0; i < modification.indexesToRemove.length; i++) {
|
|
2568
|
+
operationCount++;
|
|
2569
|
+
const index = modification.indexesToRemove[i];
|
|
2570
|
+
const varName = `collection_${collectionName}_restore_idx_${i}`;
|
|
2571
|
+
const isLast = operationCount === totalOperations;
|
|
2572
|
+
lines.push(generateIndexAddition(collectionName, index, varName, isLast));
|
|
2573
|
+
if (!isLast) lines.push("");
|
|
2574
|
+
}
|
|
2575
|
+
for (let i = 0; i < modification.indexesToAdd.length; i++) {
|
|
2576
|
+
operationCount++;
|
|
2577
|
+
const index = modification.indexesToAdd[i];
|
|
2578
|
+
const varName = `collection_${collectionName}_revert_idx_${i}`;
|
|
2579
|
+
const isLast = operationCount === totalOperations;
|
|
2580
|
+
lines.push(generateIndexRemoval(collectionName, index, varName, isLast));
|
|
2581
|
+
if (!isLast) lines.push("");
|
|
2469
2582
|
}
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
if (
|
|
2476
|
-
lines.push(` // Revert permissions for ${collectionName}`);
|
|
2477
|
-
for (const permission of modification.permissionsToUpdate) {
|
|
2478
|
-
const varName = `collection_${collectionName}_revert_perm_${permission.ruleType}`;
|
|
2479
|
-
lines.push(generatePermissionUpdate(collectionName, permission.ruleType, permission.oldValue, varName));
|
|
2480
|
-
lines.push(``);
|
|
2481
|
-
}
|
|
2482
|
-
} else if (modification.rulesToUpdate.length > 0) {
|
|
2483
|
-
lines.push(` // Revert rules for ${collectionName}`);
|
|
2484
|
-
for (const rule of modification.rulesToUpdate) {
|
|
2485
|
-
const varName = `collection_${collectionName}_revert_rule_${rule.ruleType}`;
|
|
2486
|
-
lines.push(generateRuleUpdate(collectionName, rule.ruleType, rule.oldValue, varName));
|
|
2487
|
-
lines.push(``);
|
|
2488
|
-
}
|
|
2489
|
-
}
|
|
2490
|
-
if (modification.indexesToRemove.length > 0) {
|
|
2491
|
-
lines.push(` // Restore indexes to ${collectionName}`);
|
|
2492
|
-
for (let i = 0; i < modification.indexesToRemove.length; i++) {
|
|
2493
|
-
const index = modification.indexesToRemove[i];
|
|
2494
|
-
const varName = `collection_${collectionName}_restore_idx_${i}`;
|
|
2495
|
-
lines.push(generateIndexAddition(collectionName, index, varName));
|
|
2496
|
-
lines.push(``);
|
|
2497
|
-
}
|
|
2498
|
-
}
|
|
2499
|
-
if (modification.indexesToAdd.length > 0) {
|
|
2500
|
-
lines.push(` // Remove indexes from ${collectionName}`);
|
|
2501
|
-
for (let i = 0; i < modification.indexesToAdd.length; i++) {
|
|
2502
|
-
const index = modification.indexesToAdd[i];
|
|
2503
|
-
const varName = `collection_${collectionName}_revert_idx_${i}`;
|
|
2504
|
-
lines.push(generateIndexRemoval(collectionName, index, varName));
|
|
2505
|
-
lines.push(``);
|
|
2506
|
-
}
|
|
2507
|
-
}
|
|
2508
|
-
if (modification.fieldsToRemove.length > 0) {
|
|
2509
|
-
lines.push(` // Restore fields to ${collectionName}`);
|
|
2510
|
-
for (const field of modification.fieldsToRemove) {
|
|
2511
|
-
const varName = `collection_${collectionName}_restore_${field.name}`;
|
|
2512
|
-
lines.push(generateFieldAddition(collectionName, field, varName));
|
|
2513
|
-
lines.push(``);
|
|
2514
|
-
}
|
|
2515
|
-
}
|
|
2516
|
-
if (modification.fieldsToModify.length > 0) {
|
|
2517
|
-
lines.push(` // Revert field modifications in ${collectionName}`);
|
|
2518
|
-
for (const fieldMod of modification.fieldsToModify) {
|
|
2519
|
-
const reverseChanges = fieldMod.changes.map((change) => ({
|
|
2520
|
-
property: change.property,
|
|
2521
|
-
oldValue: change.newValue,
|
|
2522
|
-
newValue: change.oldValue
|
|
2523
|
-
}));
|
|
2524
|
-
const reverseMod = {
|
|
2525
|
-
fieldName: fieldMod.fieldName,
|
|
2526
|
-
currentDefinition: fieldMod.newDefinition,
|
|
2527
|
-
newDefinition: fieldMod.currentDefinition,
|
|
2528
|
-
changes: reverseChanges
|
|
2529
|
-
};
|
|
2530
|
-
const varName = `collection_${collectionName}_revert_${fieldMod.fieldName}`;
|
|
2531
|
-
lines.push(generateFieldModification(collectionName, reverseMod, varName));
|
|
2532
|
-
lines.push(``);
|
|
2533
|
-
}
|
|
2534
|
-
}
|
|
2535
|
-
if (modification.fieldsToAdd.length > 0) {
|
|
2536
|
-
lines.push(` // Remove added fields from ${collectionName}`);
|
|
2537
|
-
for (const field of modification.fieldsToAdd) {
|
|
2538
|
-
const varName = `collection_${collectionName}_revert_add_${field.name}`;
|
|
2539
|
-
lines.push(generateFieldDeletion(collectionName, field.name, varName));
|
|
2540
|
-
lines.push(``);
|
|
2541
|
-
}
|
|
2542
|
-
}
|
|
2583
|
+
for (const field of modification.fieldsToRemove) {
|
|
2584
|
+
operationCount++;
|
|
2585
|
+
const varName = `collection_${collectionName}_restore_${field.name}`;
|
|
2586
|
+
const isLast = operationCount === totalOperations;
|
|
2587
|
+
lines.push(generateFieldAddition(collectionName, field, varName, isLast, collectionIdMap));
|
|
2588
|
+
if (!isLast) lines.push("");
|
|
2543
2589
|
}
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2590
|
+
for (const fieldMod of modification.fieldsToModify) {
|
|
2591
|
+
operationCount++;
|
|
2592
|
+
const reverseChanges = fieldMod.changes.map((change) => ({
|
|
2593
|
+
property: change.property,
|
|
2594
|
+
oldValue: change.newValue,
|
|
2595
|
+
newValue: change.oldValue
|
|
2596
|
+
}));
|
|
2597
|
+
const reverseMod = {
|
|
2598
|
+
fieldName: fieldMod.fieldName,
|
|
2599
|
+
currentDefinition: fieldMod.newDefinition,
|
|
2600
|
+
newDefinition: fieldMod.currentDefinition,
|
|
2601
|
+
changes: reverseChanges
|
|
2602
|
+
};
|
|
2603
|
+
const varName = `collection_${collectionName}_revert_${fieldMod.fieldName}`;
|
|
2604
|
+
const isLast = operationCount === totalOperations;
|
|
2605
|
+
lines.push(generateFieldModification(collectionName, reverseMod, varName, isLast));
|
|
2606
|
+
if (!isLast) lines.push("");
|
|
2607
|
+
}
|
|
2608
|
+
for (const field of modification.fieldsToAdd) {
|
|
2609
|
+
operationCount++;
|
|
2610
|
+
const varName = `collection_${collectionName}_revert_add_${field.name}`;
|
|
2611
|
+
const isLast = operationCount === totalOperations;
|
|
2612
|
+
lines.push(generateFieldDeletion(collectionName, field.name, varName, isLast));
|
|
2613
|
+
if (!isLast) lines.push("");
|
|
2614
|
+
}
|
|
2615
|
+
} else if (operation.type === "delete") {
|
|
2616
|
+
const collection = operation.collection;
|
|
2617
|
+
if (typeof collection !== "string") {
|
|
2618
|
+
const varName = `collection_${collection.name}`;
|
|
2619
|
+
lines.push(generateCollectionCreation(collection, varName, true, collectionIdMap));
|
|
2573
2620
|
}
|
|
2574
2621
|
}
|
|
2575
|
-
return
|
|
2622
|
+
return lines.join("\n");
|
|
2576
2623
|
}
|
|
2577
2624
|
function generate(diff, config) {
|
|
2578
2625
|
const normalizedConfig = typeof config === "string" ? { migrationDir: config } : config;
|
|
2579
2626
|
try {
|
|
2580
2627
|
const migrationDir = resolveMigrationDir(normalizedConfig);
|
|
2581
|
-
const
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
const
|
|
2586
|
-
|
|
2628
|
+
const hasChanges4 = diff.collectionsToCreate.length > 0 || diff.collectionsToModify.length > 0 || diff.collectionsToDelete.length > 0;
|
|
2629
|
+
if (!hasChanges4) {
|
|
2630
|
+
return [];
|
|
2631
|
+
}
|
|
2632
|
+
const collectionIdMap = /* @__PURE__ */ new Map();
|
|
2633
|
+
for (const collection of diff.collectionsToCreate) {
|
|
2634
|
+
if (collection.id) {
|
|
2635
|
+
collectionIdMap.set(collection.name, collection.id);
|
|
2636
|
+
}
|
|
2637
|
+
}
|
|
2638
|
+
for (const collection of diff.collectionsToDelete) {
|
|
2639
|
+
if (collection.id) {
|
|
2640
|
+
collectionIdMap.set(collection.name, collection.id);
|
|
2641
|
+
}
|
|
2642
|
+
}
|
|
2643
|
+
const baseTimestamp = generateTimestamp(normalizedConfig);
|
|
2644
|
+
const operations = splitDiffByCollection(diff, baseTimestamp);
|
|
2645
|
+
const filePaths = [];
|
|
2646
|
+
for (const operation of operations) {
|
|
2647
|
+
const upCode = generateOperationUpMigration(operation, collectionIdMap);
|
|
2648
|
+
const downCode = generateOperationDownMigration(operation, collectionIdMap);
|
|
2649
|
+
const content = createMigrationFileStructure(upCode, downCode, normalizedConfig);
|
|
2650
|
+
const filename = generateCollectionMigrationFilename(operation);
|
|
2651
|
+
const filePath = writeMigrationFile(migrationDir, filename, content);
|
|
2652
|
+
filePaths.push(filePath);
|
|
2653
|
+
}
|
|
2654
|
+
return filePaths;
|
|
2587
2655
|
} catch (error) {
|
|
2588
2656
|
if (error instanceof MigrationGenerationError || error instanceof FileSystemError) {
|
|
2589
2657
|
throw error;
|
|
@@ -3604,15 +3672,25 @@ async function executeGenerate(options) {
|
|
|
3604
3672
|
process.exit(1);
|
|
3605
3673
|
}
|
|
3606
3674
|
logSection("\u{1F4DD} Generating Migration");
|
|
3607
|
-
const
|
|
3675
|
+
const migrationPaths = await withProgress(
|
|
3608
3676
|
"Creating migration file...",
|
|
3609
3677
|
() => Promise.resolve(generate(diff, migrationsDir))
|
|
3610
3678
|
);
|
|
3611
|
-
|
|
3679
|
+
if (migrationPaths.length === 0) {
|
|
3680
|
+
logWarning("No migration files were generated (no changes detected).");
|
|
3681
|
+
return;
|
|
3682
|
+
}
|
|
3683
|
+
if (migrationPaths.length === 1) {
|
|
3684
|
+
logSuccess(`Migration file created: ${path5__namespace.basename(migrationPaths[0])}`);
|
|
3685
|
+
} else {
|
|
3686
|
+
logSuccess(`Created ${migrationPaths.length} migration files`);
|
|
3687
|
+
}
|
|
3612
3688
|
logSection("\u2705 Next Steps");
|
|
3613
3689
|
console.log();
|
|
3614
|
-
console.log(" 1. Review the generated migration file:");
|
|
3615
|
-
|
|
3690
|
+
console.log(" 1. Review the generated migration file(s):");
|
|
3691
|
+
migrationPaths.forEach((migrationPath) => {
|
|
3692
|
+
console.log(` ${migrationPath}`);
|
|
3693
|
+
});
|
|
3616
3694
|
console.log();
|
|
3617
3695
|
console.log(" 2. Apply the migration by running PocketBase:");
|
|
3618
3696
|
console.log(" yarn pb");
|