pocketbase-zod-schema 0.3.1 → 0.3.3
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 +14 -0
- package/dist/cli/index.cjs +64 -38
- 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 +64 -38
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/migrate.cjs +64 -38
- package/dist/cli/migrate.cjs.map +1 -1
- package/dist/cli/migrate.js +64 -38
- 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-RVj26U-O.d.cts → fields-DBBm06VU.d.cts} +34 -7
- package/dist/{fields-RVj26U-O.d.ts → fields-DBBm06VU.d.ts} +34 -7
- package/dist/index.cjs +167 -75
- 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 +167 -75
- 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 +10 -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 +10 -1
- package/dist/migration/diff.js.map +1 -1
- package/dist/migration/generator.cjs +102 -68
- package/dist/migration/generator.cjs.map +1 -1
- package/dist/migration/generator.d.cts +3 -3
- package/dist/migration/generator.d.ts +3 -3
- package/dist/migration/generator.js +102 -68
- package/dist/migration/generator.js.map +1 -1
- package/dist/migration/index.cjs +112 -69
- 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 +112 -69
- package/dist/migration/index.js.map +1 -1
- package/dist/migration/snapshot.d.cts +2 -2
- package/dist/migration/snapshot.d.ts +2 -2
- package/dist/migration/utils/index.cjs.map +1 -1
- package/dist/migration/utils/index.d.cts +2 -2
- package/dist/migration/utils/index.d.ts +2 -2
- 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-DaBe-1ph.d.cts → type-mapper-DsGgZwUo.d.cts} +1 -1
- package/dist/{type-mapper-CZzVeDj7.d.ts → type-mapper-Dvh4QTM-.d.ts} +1 -1
- package/dist/{types-CUVzgZ9k.d.ts → types-BWhwQxG-.d.ts} +6 -1
- package/dist/{types-D-Fsdn_O.d.cts → types-d0yBwHoN.d.cts} +6 -1
- package/package.json +1 -1
package/dist/cli/migrate.cjs
CHANGED
|
@@ -1967,10 +1967,19 @@ function aggregateChanges(currentSchema, previousSnapshot, config) {
|
|
|
1967
1967
|
collectionsToModify.push(modification);
|
|
1968
1968
|
}
|
|
1969
1969
|
}
|
|
1970
|
+
const existingCollectionIds = /* @__PURE__ */ new Map();
|
|
1971
|
+
if (previousSnapshot) {
|
|
1972
|
+
for (const [name, collection] of previousSnapshot.collections) {
|
|
1973
|
+
if (collection.id) {
|
|
1974
|
+
existingCollectionIds.set(name, collection.id);
|
|
1975
|
+
}
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1970
1978
|
return {
|
|
1971
1979
|
collectionsToCreate: collectionsWithIds,
|
|
1972
1980
|
collectionsToDelete: filteredCollectionsToDelete,
|
|
1973
|
-
collectionsToModify
|
|
1981
|
+
collectionsToModify,
|
|
1982
|
+
existingCollectionIds
|
|
1974
1983
|
};
|
|
1975
1984
|
}
|
|
1976
1985
|
function categorizeChangesBySeverity(diff, _config) {
|
|
@@ -2173,7 +2182,7 @@ function formatValue(value) {
|
|
|
2173
2182
|
return JSON.stringify(value).replace(/","/g, '", "');
|
|
2174
2183
|
}
|
|
2175
2184
|
if (typeof value === "object") {
|
|
2176
|
-
const entries = Object.entries(value).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
|
|
2185
|
+
const entries = Object.entries(value).filter(([_k, v]) => v !== void 0).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
|
|
2177
2186
|
return `{ ${entries} }`;
|
|
2178
2187
|
}
|
|
2179
2188
|
return String(value);
|
|
@@ -2436,7 +2445,7 @@ function generateFieldAddition(collectionName, field, varName, isLast = false, c
|
|
|
2436
2445
|
lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
|
|
2437
2446
|
return lines.join("\n");
|
|
2438
2447
|
}
|
|
2439
|
-
function generateFieldModification(collectionName, modification, varName, isLast = false) {
|
|
2448
|
+
function generateFieldModification(collectionName, modification, varName, isLast = false, collectionIdMap) {
|
|
2440
2449
|
const lines = [];
|
|
2441
2450
|
const collectionVar = varName || `collection_${collectionName}_${modification.fieldName}`;
|
|
2442
2451
|
const fieldVar = `${collectionVar}_field`;
|
|
@@ -2451,7 +2460,14 @@ function generateFieldModification(collectionName, modification, varName, isLast
|
|
|
2451
2460
|
const relationKey = change.property.replace("relation.", "");
|
|
2452
2461
|
if (relationKey === "collection") {
|
|
2453
2462
|
const isUsersCollection = String(change.newValue).toLowerCase() === "users";
|
|
2454
|
-
|
|
2463
|
+
let collectionIdValue;
|
|
2464
|
+
if (isUsersCollection) {
|
|
2465
|
+
collectionIdValue = '"_pb_users_auth_"';
|
|
2466
|
+
} else if (collectionIdMap && collectionIdMap.has(String(change.newValue))) {
|
|
2467
|
+
collectionIdValue = `"${collectionIdMap.get(String(change.newValue))}"`;
|
|
2468
|
+
} else {
|
|
2469
|
+
collectionIdValue = `app.findCollectionByNameOrId("${change.newValue}").id`;
|
|
2470
|
+
}
|
|
2455
2471
|
lines.push(` ${fieldVar}.collectionId = ${collectionIdValue};`);
|
|
2456
2472
|
} else {
|
|
2457
2473
|
lines.push(` ${fieldVar}.${relationKey} = ${formatValue(change.newValue)};`);
|
|
@@ -2467,11 +2483,9 @@ function generateFieldModification(collectionName, modification, varName, isLast
|
|
|
2467
2483
|
function generateFieldDeletion(collectionName, fieldName, varName, isLast = false) {
|
|
2468
2484
|
const lines = [];
|
|
2469
2485
|
const collectionVar = varName || `collection_${collectionName}_${fieldName}`;
|
|
2470
|
-
const fieldVar = `${collectionVar}_field`;
|
|
2471
2486
|
lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
|
|
2472
|
-
lines.push(` const ${fieldVar} = ${collectionVar}.fields.getByName("${fieldName}");`);
|
|
2473
2487
|
lines.push(``);
|
|
2474
|
-
lines.push(` ${collectionVar}.fields.
|
|
2488
|
+
lines.push(` ${collectionVar}.fields.removeByName("${fieldName}");`);
|
|
2475
2489
|
lines.push(``);
|
|
2476
2490
|
lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
|
|
2477
2491
|
return lines.join("\n");
|
|
@@ -2529,9 +2543,10 @@ function generateOperationUpMigration(operation, collectionIdMap) {
|
|
|
2529
2543
|
const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection?.name ?? modification.collection;
|
|
2530
2544
|
let operationCount = 0;
|
|
2531
2545
|
const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + modification.rulesToUpdate.length + modification.permissionsToUpdate.length;
|
|
2532
|
-
for (
|
|
2546
|
+
for (let i = 0; i < modification.fieldsToAdd.length; i++) {
|
|
2547
|
+
const field = modification.fieldsToAdd[i];
|
|
2533
2548
|
operationCount++;
|
|
2534
|
-
const varName = `collection_${collectionName}_add_${field.name}`;
|
|
2549
|
+
const varName = `collection_${collectionName}_add_${field.name}_${i}`;
|
|
2535
2550
|
const isLast = operationCount === totalOperations;
|
|
2536
2551
|
lines.push(generateFieldAddition(collectionName, field, varName, isLast, collectionIdMap));
|
|
2537
2552
|
if (!isLast) lines.push("");
|
|
@@ -2540,7 +2555,7 @@ function generateOperationUpMigration(operation, collectionIdMap) {
|
|
|
2540
2555
|
operationCount++;
|
|
2541
2556
|
const varName = `collection_${collectionName}_modify_${fieldMod.fieldName}`;
|
|
2542
2557
|
const isLast = operationCount === totalOperations;
|
|
2543
|
-
lines.push(generateFieldModification(collectionName, fieldMod, varName, isLast));
|
|
2558
|
+
lines.push(generateFieldModification(collectionName, fieldMod, varName, isLast, collectionIdMap));
|
|
2544
2559
|
if (!isLast) lines.push("");
|
|
2545
2560
|
}
|
|
2546
2561
|
for (const field of modification.fieldsToRemove) {
|
|
@@ -2589,20 +2604,23 @@ function generateOperationUpMigration(operation, collectionIdMap) {
|
|
|
2589
2604
|
lines.push(generateCollectionDeletion(collectionName, varName, true));
|
|
2590
2605
|
}
|
|
2591
2606
|
let code = lines.join("\n");
|
|
2592
|
-
const
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
if (
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2607
|
+
const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
|
|
2608
|
+
if (!hasReturnStatement) {
|
|
2609
|
+
const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
|
|
2610
|
+
const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
|
|
2611
|
+
const saveMatches = [...code.matchAll(savePattern)];
|
|
2612
|
+
const deleteMatches = [...code.matchAll(deletePattern)];
|
|
2613
|
+
const allMatches = [
|
|
2614
|
+
...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
|
|
2615
|
+
...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
|
|
2616
|
+
].sort((a, b) => b.index - a.index);
|
|
2617
|
+
if (allMatches.length > 0) {
|
|
2618
|
+
const lastMatch = allMatches[0];
|
|
2619
|
+
if (lastMatch.type === "save") {
|
|
2620
|
+
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);
|
|
2621
|
+
} else {
|
|
2622
|
+
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);
|
|
2623
|
+
}
|
|
2606
2624
|
}
|
|
2607
2625
|
}
|
|
2608
2626
|
return code;
|
|
@@ -2691,20 +2709,23 @@ function generateOperationDownMigration(operation, collectionIdMap) {
|
|
|
2691
2709
|
}
|
|
2692
2710
|
}
|
|
2693
2711
|
let code = lines.join("\n");
|
|
2694
|
-
const
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
if (
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2712
|
+
const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
|
|
2713
|
+
if (!hasReturnStatement) {
|
|
2714
|
+
const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
|
|
2715
|
+
const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
|
|
2716
|
+
const saveMatches = [...code.matchAll(savePattern)];
|
|
2717
|
+
const deleteMatches = [...code.matchAll(deletePattern)];
|
|
2718
|
+
const allMatches = [
|
|
2719
|
+
...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
|
|
2720
|
+
...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
|
|
2721
|
+
].sort((a, b) => b.index - a.index);
|
|
2722
|
+
if (allMatches.length > 0) {
|
|
2723
|
+
const lastMatch = allMatches[0];
|
|
2724
|
+
if (lastMatch.type === "save") {
|
|
2725
|
+
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);
|
|
2726
|
+
} else {
|
|
2727
|
+
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);
|
|
2728
|
+
}
|
|
2708
2729
|
}
|
|
2709
2730
|
}
|
|
2710
2731
|
return code;
|
|
@@ -2728,6 +2749,11 @@ function generate(diff, config) {
|
|
|
2728
2749
|
collectionIdMap.set(collection.name, collection.id);
|
|
2729
2750
|
}
|
|
2730
2751
|
}
|
|
2752
|
+
if (diff.existingCollectionIds) {
|
|
2753
|
+
for (const [name, id] of diff.existingCollectionIds) {
|
|
2754
|
+
collectionIdMap.set(name, id);
|
|
2755
|
+
}
|
|
2756
|
+
}
|
|
2731
2757
|
const baseTimestamp = generateTimestamp(normalizedConfig);
|
|
2732
2758
|
const operations = splitDiffByCollection(diff, baseTimestamp);
|
|
2733
2759
|
const filePaths = [];
|