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
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { e as SchemaDiff, C as CollectionSchema, F as FieldDefinition, c as FieldModification, f as CollectionOperation } from '../types-
|
|
2
|
-
import '../fields-
|
|
1
|
+
import { e as SchemaDiff, C as CollectionSchema, F as FieldDefinition, c as FieldModification, f as CollectionOperation } from '../types-d0yBwHoN.cjs';
|
|
2
|
+
import '../fields-DBBm06VU.cjs';
|
|
3
3
|
import 'zod';
|
|
4
4
|
import '../permissions-ZHafVSIx.cjs';
|
|
5
5
|
|
|
@@ -176,7 +176,7 @@ declare function generateFieldAddition(collectionName: string, field: FieldDefin
|
|
|
176
176
|
* @param isLast - Whether this is the last operation (will return the result)
|
|
177
177
|
* @returns JavaScript code for modifying the field
|
|
178
178
|
*/
|
|
179
|
-
declare function generateFieldModification(collectionName: string, modification: FieldModification, varName?: string, isLast?: boolean): string;
|
|
179
|
+
declare function generateFieldModification(collectionName: string, modification: FieldModification, varName?: string, isLast?: boolean, collectionIdMap?: Map<string, string>): string;
|
|
180
180
|
/**
|
|
181
181
|
* Generates code for deleting a field from a collection
|
|
182
182
|
*
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { e as SchemaDiff, C as CollectionSchema, F as FieldDefinition, c as FieldModification, f as CollectionOperation } from '../types-
|
|
2
|
-
import '../fields-
|
|
1
|
+
import { e as SchemaDiff, C as CollectionSchema, F as FieldDefinition, c as FieldModification, f as CollectionOperation } from '../types-BWhwQxG-.js';
|
|
2
|
+
import '../fields-DBBm06VU.js';
|
|
3
3
|
import 'zod';
|
|
4
4
|
import '../permissions-ZHafVSIx.js';
|
|
5
5
|
|
|
@@ -176,7 +176,7 @@ declare function generateFieldAddition(collectionName: string, field: FieldDefin
|
|
|
176
176
|
* @param isLast - Whether this is the last operation (will return the result)
|
|
177
177
|
* @returns JavaScript code for modifying the field
|
|
178
178
|
*/
|
|
179
|
-
declare function generateFieldModification(collectionName: string, modification: FieldModification, varName?: string, isLast?: boolean): string;
|
|
179
|
+
declare function generateFieldModification(collectionName: string, modification: FieldModification, varName?: string, isLast?: boolean, collectionIdMap?: Map<string, string>): string;
|
|
180
180
|
/**
|
|
181
181
|
* Generates code for deleting a field from a collection
|
|
182
182
|
*
|
|
@@ -273,7 +273,7 @@ function formatValue(value) {
|
|
|
273
273
|
return JSON.stringify(value).replace(/","/g, '", "');
|
|
274
274
|
}
|
|
275
275
|
if (typeof value === "object") {
|
|
276
|
-
const entries = Object.entries(value).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
|
|
276
|
+
const entries = Object.entries(value).filter(([_k, v]) => v !== void 0).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
|
|
277
277
|
return `{ ${entries} }`;
|
|
278
278
|
}
|
|
279
279
|
return String(value);
|
|
@@ -536,7 +536,7 @@ function generateFieldAddition(collectionName, field, varName, isLast = false, c
|
|
|
536
536
|
lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
|
|
537
537
|
return lines.join("\n");
|
|
538
538
|
}
|
|
539
|
-
function generateFieldModification(collectionName, modification, varName, isLast = false) {
|
|
539
|
+
function generateFieldModification(collectionName, modification, varName, isLast = false, collectionIdMap) {
|
|
540
540
|
const lines = [];
|
|
541
541
|
const collectionVar = varName || `collection_${collectionName}_${modification.fieldName}`;
|
|
542
542
|
const fieldVar = `${collectionVar}_field`;
|
|
@@ -551,7 +551,14 @@ function generateFieldModification(collectionName, modification, varName, isLast
|
|
|
551
551
|
const relationKey = change.property.replace("relation.", "");
|
|
552
552
|
if (relationKey === "collection") {
|
|
553
553
|
const isUsersCollection = String(change.newValue).toLowerCase() === "users";
|
|
554
|
-
|
|
554
|
+
let collectionIdValue;
|
|
555
|
+
if (isUsersCollection) {
|
|
556
|
+
collectionIdValue = '"_pb_users_auth_"';
|
|
557
|
+
} else if (collectionIdMap && collectionIdMap.has(String(change.newValue))) {
|
|
558
|
+
collectionIdValue = `"${collectionIdMap.get(String(change.newValue))}"`;
|
|
559
|
+
} else {
|
|
560
|
+
collectionIdValue = `app.findCollectionByNameOrId("${change.newValue}").id`;
|
|
561
|
+
}
|
|
555
562
|
lines.push(` ${fieldVar}.collectionId = ${collectionIdValue};`);
|
|
556
563
|
} else {
|
|
557
564
|
lines.push(` ${fieldVar}.${relationKey} = ${formatValue(change.newValue)};`);
|
|
@@ -567,11 +574,9 @@ function generateFieldModification(collectionName, modification, varName, isLast
|
|
|
567
574
|
function generateFieldDeletion(collectionName, fieldName, varName, isLast = false) {
|
|
568
575
|
const lines = [];
|
|
569
576
|
const collectionVar = varName || `collection_${collectionName}_${fieldName}`;
|
|
570
|
-
const fieldVar = `${collectionVar}_field`;
|
|
571
577
|
lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
|
|
572
|
-
lines.push(` const ${fieldVar} = ${collectionVar}.fields.getByName("${fieldName}");`);
|
|
573
578
|
lines.push(``);
|
|
574
|
-
lines.push(` ${collectionVar}.fields.
|
|
579
|
+
lines.push(` ${collectionVar}.fields.removeByName("${fieldName}");`);
|
|
575
580
|
lines.push(``);
|
|
576
581
|
lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
|
|
577
582
|
return lines.join("\n");
|
|
@@ -629,9 +634,10 @@ function generateOperationUpMigration(operation, collectionIdMap) {
|
|
|
629
634
|
const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection?.name ?? modification.collection;
|
|
630
635
|
let operationCount = 0;
|
|
631
636
|
const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + modification.rulesToUpdate.length + modification.permissionsToUpdate.length;
|
|
632
|
-
for (
|
|
637
|
+
for (let i = 0; i < modification.fieldsToAdd.length; i++) {
|
|
638
|
+
const field = modification.fieldsToAdd[i];
|
|
633
639
|
operationCount++;
|
|
634
|
-
const varName = `collection_${collectionName}_add_${field.name}`;
|
|
640
|
+
const varName = `collection_${collectionName}_add_${field.name}_${i}`;
|
|
635
641
|
const isLast = operationCount === totalOperations;
|
|
636
642
|
lines.push(generateFieldAddition(collectionName, field, varName, isLast, collectionIdMap));
|
|
637
643
|
if (!isLast) lines.push("");
|
|
@@ -640,7 +646,7 @@ function generateOperationUpMigration(operation, collectionIdMap) {
|
|
|
640
646
|
operationCount++;
|
|
641
647
|
const varName = `collection_${collectionName}_modify_${fieldMod.fieldName}`;
|
|
642
648
|
const isLast = operationCount === totalOperations;
|
|
643
|
-
lines.push(generateFieldModification(collectionName, fieldMod, varName, isLast));
|
|
649
|
+
lines.push(generateFieldModification(collectionName, fieldMod, varName, isLast, collectionIdMap));
|
|
644
650
|
if (!isLast) lines.push("");
|
|
645
651
|
}
|
|
646
652
|
for (const field of modification.fieldsToRemove) {
|
|
@@ -689,20 +695,23 @@ function generateOperationUpMigration(operation, collectionIdMap) {
|
|
|
689
695
|
lines.push(generateCollectionDeletion(collectionName, varName, true));
|
|
690
696
|
}
|
|
691
697
|
let code = lines.join("\n");
|
|
692
|
-
const
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
if (
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
698
|
+
const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
|
|
699
|
+
if (!hasReturnStatement) {
|
|
700
|
+
const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
|
|
701
|
+
const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
|
|
702
|
+
const saveMatches = [...code.matchAll(savePattern)];
|
|
703
|
+
const deleteMatches = [...code.matchAll(deletePattern)];
|
|
704
|
+
const allMatches = [
|
|
705
|
+
...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
|
|
706
|
+
...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
|
|
707
|
+
].sort((a, b) => b.index - a.index);
|
|
708
|
+
if (allMatches.length > 0) {
|
|
709
|
+
const lastMatch = allMatches[0];
|
|
710
|
+
if (lastMatch.type === "save") {
|
|
711
|
+
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);
|
|
712
|
+
} else {
|
|
713
|
+
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);
|
|
714
|
+
}
|
|
706
715
|
}
|
|
707
716
|
}
|
|
708
717
|
return code;
|
|
@@ -791,20 +800,23 @@ function generateOperationDownMigration(operation, collectionIdMap) {
|
|
|
791
800
|
}
|
|
792
801
|
}
|
|
793
802
|
let code = lines.join("\n");
|
|
794
|
-
const
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
if (
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
803
|
+
const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
|
|
804
|
+
if (!hasReturnStatement) {
|
|
805
|
+
const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
|
|
806
|
+
const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
|
|
807
|
+
const saveMatches = [...code.matchAll(savePattern)];
|
|
808
|
+
const deleteMatches = [...code.matchAll(deletePattern)];
|
|
809
|
+
const allMatches = [
|
|
810
|
+
...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
|
|
811
|
+
...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
|
|
812
|
+
].sort((a, b) => b.index - a.index);
|
|
813
|
+
if (allMatches.length > 0) {
|
|
814
|
+
const lastMatch = allMatches[0];
|
|
815
|
+
if (lastMatch.type === "save") {
|
|
816
|
+
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);
|
|
817
|
+
} else {
|
|
818
|
+
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);
|
|
819
|
+
}
|
|
808
820
|
}
|
|
809
821
|
}
|
|
810
822
|
return code;
|
|
@@ -819,6 +831,11 @@ function generateUpMigration(diff) {
|
|
|
819
831
|
collectionIdMap.set(collection.name, collection.id);
|
|
820
832
|
}
|
|
821
833
|
}
|
|
834
|
+
if (diff.existingCollectionIds) {
|
|
835
|
+
for (const [name, id] of diff.existingCollectionIds) {
|
|
836
|
+
collectionIdMap.set(name, id);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
822
839
|
if (diff.collectionsToCreate.length > 0) {
|
|
823
840
|
lines.push(` // Create new collections`);
|
|
824
841
|
for (let i = 0; i < diff.collectionsToCreate.length; i++) {
|
|
@@ -834,8 +851,9 @@ function generateUpMigration(diff) {
|
|
|
834
851
|
const collectionName = modification.collection;
|
|
835
852
|
if (modification.fieldsToAdd.length > 0) {
|
|
836
853
|
lines.push(` // Add fields to ${collectionName}`);
|
|
837
|
-
for (
|
|
838
|
-
const
|
|
854
|
+
for (let i = 0; i < modification.fieldsToAdd.length; i++) {
|
|
855
|
+
const field = modification.fieldsToAdd[i];
|
|
856
|
+
const varName = `collection_${collectionName}_add_${field.name}_${i}`;
|
|
839
857
|
lines.push(generateFieldAddition(collectionName, field, varName, false, collectionIdMap));
|
|
840
858
|
lines.push(``);
|
|
841
859
|
}
|
|
@@ -844,7 +862,7 @@ function generateUpMigration(diff) {
|
|
|
844
862
|
lines.push(` // Modify fields in ${collectionName}`);
|
|
845
863
|
for (const fieldMod of modification.fieldsToModify) {
|
|
846
864
|
const varName = `collection_${collectionName}_modify_${fieldMod.fieldName}`;
|
|
847
|
-
lines.push(generateFieldModification(collectionName, fieldMod, varName));
|
|
865
|
+
lines.push(generateFieldModification(collectionName, fieldMod, varName, false, collectionIdMap));
|
|
848
866
|
lines.push(``);
|
|
849
867
|
}
|
|
850
868
|
}
|
|
@@ -905,20 +923,23 @@ function generateUpMigration(diff) {
|
|
|
905
923
|
lines.push(``);
|
|
906
924
|
}
|
|
907
925
|
let code = lines.join("\n");
|
|
908
|
-
const
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
if (
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
926
|
+
const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
|
|
927
|
+
if (!hasReturnStatement) {
|
|
928
|
+
const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
|
|
929
|
+
const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
|
|
930
|
+
const saveMatches = [...code.matchAll(savePattern)];
|
|
931
|
+
const deleteMatches = [...code.matchAll(deletePattern)];
|
|
932
|
+
const allMatches = [
|
|
933
|
+
...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
|
|
934
|
+
...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
|
|
935
|
+
].sort((a, b) => b.index - a.index);
|
|
936
|
+
if (allMatches.length > 0) {
|
|
937
|
+
const lastMatch = allMatches[0];
|
|
938
|
+
if (lastMatch.type === "save") {
|
|
939
|
+
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);
|
|
940
|
+
} else {
|
|
941
|
+
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);
|
|
942
|
+
}
|
|
922
943
|
}
|
|
923
944
|
}
|
|
924
945
|
return code;
|
|
@@ -938,6 +959,11 @@ function generateDownMigration(diff) {
|
|
|
938
959
|
collectionIdMap.set(collection.name, collection.id);
|
|
939
960
|
}
|
|
940
961
|
}
|
|
962
|
+
if (diff.existingCollectionIds) {
|
|
963
|
+
for (const [name, id] of diff.existingCollectionIds) {
|
|
964
|
+
collectionIdMap.set(name, id);
|
|
965
|
+
}
|
|
966
|
+
}
|
|
941
967
|
if (diff.collectionsToDelete.length > 0) {
|
|
942
968
|
lines.push(` // Recreate deleted collections`);
|
|
943
969
|
for (let i = 0; i < diff.collectionsToDelete.length; i++) {
|
|
@@ -1035,20 +1061,23 @@ function generateDownMigration(diff) {
|
|
|
1035
1061
|
lines.push(``);
|
|
1036
1062
|
}
|
|
1037
1063
|
let code = lines.join("\n");
|
|
1038
|
-
const
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
if (
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1064
|
+
const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
|
|
1065
|
+
if (!hasReturnStatement) {
|
|
1066
|
+
const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
|
|
1067
|
+
const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
|
|
1068
|
+
const saveMatches = [...code.matchAll(savePattern)];
|
|
1069
|
+
const deleteMatches = [...code.matchAll(deletePattern)];
|
|
1070
|
+
const allMatches = [
|
|
1071
|
+
...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
|
|
1072
|
+
...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
|
|
1073
|
+
].sort((a, b) => b.index - a.index);
|
|
1074
|
+
if (allMatches.length > 0) {
|
|
1075
|
+
const lastMatch = allMatches[0];
|
|
1076
|
+
if (lastMatch.type === "save") {
|
|
1077
|
+
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);
|
|
1078
|
+
} else {
|
|
1079
|
+
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);
|
|
1080
|
+
}
|
|
1052
1081
|
}
|
|
1053
1082
|
}
|
|
1054
1083
|
return code;
|
|
@@ -1072,6 +1101,11 @@ function generate(diff, config) {
|
|
|
1072
1101
|
collectionIdMap.set(collection.name, collection.id);
|
|
1073
1102
|
}
|
|
1074
1103
|
}
|
|
1104
|
+
if (diff.existingCollectionIds) {
|
|
1105
|
+
for (const [name, id] of diff.existingCollectionIds) {
|
|
1106
|
+
collectionIdMap.set(name, id);
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1075
1109
|
const baseTimestamp = generateTimestamp(normalizedConfig);
|
|
1076
1110
|
const operations = splitDiffByCollection(diff, baseTimestamp);
|
|
1077
1111
|
const filePaths = [];
|