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.
Files changed (60) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cli/index.cjs +64 -38
  3. package/dist/cli/index.cjs.map +1 -1
  4. package/dist/cli/index.d.cts +2 -2
  5. package/dist/cli/index.d.ts +2 -2
  6. package/dist/cli/index.js +64 -38
  7. package/dist/cli/index.js.map +1 -1
  8. package/dist/cli/migrate.cjs +64 -38
  9. package/dist/cli/migrate.cjs.map +1 -1
  10. package/dist/cli/migrate.js +64 -38
  11. package/dist/cli/migrate.js.map +1 -1
  12. package/dist/cli/utils/index.d.cts +2 -2
  13. package/dist/cli/utils/index.d.ts +2 -2
  14. package/dist/{fields-RVj26U-O.d.cts → fields-DBBm06VU.d.cts} +34 -7
  15. package/dist/{fields-RVj26U-O.d.ts → fields-DBBm06VU.d.ts} +34 -7
  16. package/dist/index.cjs +167 -75
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.cts +3 -3
  19. package/dist/index.d.ts +3 -3
  20. package/dist/index.js +167 -75
  21. package/dist/index.js.map +1 -1
  22. package/dist/migration/analyzer.cjs.map +1 -1
  23. package/dist/migration/analyzer.d.cts +2 -2
  24. package/dist/migration/analyzer.d.ts +2 -2
  25. package/dist/migration/analyzer.js.map +1 -1
  26. package/dist/migration/diff.cjs +10 -1
  27. package/dist/migration/diff.cjs.map +1 -1
  28. package/dist/migration/diff.d.cts +2 -2
  29. package/dist/migration/diff.d.ts +2 -2
  30. package/dist/migration/diff.js +10 -1
  31. package/dist/migration/diff.js.map +1 -1
  32. package/dist/migration/generator.cjs +102 -68
  33. package/dist/migration/generator.cjs.map +1 -1
  34. package/dist/migration/generator.d.cts +3 -3
  35. package/dist/migration/generator.d.ts +3 -3
  36. package/dist/migration/generator.js +102 -68
  37. package/dist/migration/generator.js.map +1 -1
  38. package/dist/migration/index.cjs +112 -69
  39. package/dist/migration/index.cjs.map +1 -1
  40. package/dist/migration/index.d.cts +3 -3
  41. package/dist/migration/index.d.ts +3 -3
  42. package/dist/migration/index.js +112 -69
  43. package/dist/migration/index.js.map +1 -1
  44. package/dist/migration/snapshot.d.cts +2 -2
  45. package/dist/migration/snapshot.d.ts +2 -2
  46. package/dist/migration/utils/index.cjs.map +1 -1
  47. package/dist/migration/utils/index.d.cts +2 -2
  48. package/dist/migration/utils/index.d.ts +2 -2
  49. package/dist/migration/utils/index.js.map +1 -1
  50. package/dist/schema.cjs +55 -6
  51. package/dist/schema.cjs.map +1 -1
  52. package/dist/schema.d.cts +1 -1
  53. package/dist/schema.d.ts +1 -1
  54. package/dist/schema.js +55 -6
  55. package/dist/schema.js.map +1 -1
  56. package/dist/{type-mapper-DaBe-1ph.d.cts → type-mapper-DsGgZwUo.d.cts} +1 -1
  57. package/dist/{type-mapper-CZzVeDj7.d.ts → type-mapper-Dvh4QTM-.d.ts} +1 -1
  58. package/dist/{types-CUVzgZ9k.d.ts → types-BWhwQxG-.d.ts} +6 -1
  59. package/dist/{types-D-Fsdn_O.d.cts → types-d0yBwHoN.d.cts} +6 -1
  60. package/package.json +1 -1
@@ -3098,10 +3098,19 @@ function aggregateChanges(currentSchema, previousSnapshot, config) {
3098
3098
  collectionsToModify.push(modification);
3099
3099
  }
3100
3100
  }
3101
+ const existingCollectionIds = /* @__PURE__ */ new Map();
3102
+ if (previousSnapshot) {
3103
+ for (const [name, collection] of previousSnapshot.collections) {
3104
+ if (collection.id) {
3105
+ existingCollectionIds.set(name, collection.id);
3106
+ }
3107
+ }
3108
+ }
3101
3109
  return {
3102
3110
  collectionsToCreate: collectionsWithIds,
3103
3111
  collectionsToDelete: filteredCollectionsToDelete,
3104
- collectionsToModify
3112
+ collectionsToModify,
3113
+ existingCollectionIds
3105
3114
  };
3106
3115
  }
3107
3116
  function detectDestructiveChanges(diff, config) {
@@ -3494,7 +3503,7 @@ function formatValue(value) {
3494
3503
  return JSON.stringify(value).replace(/","/g, '", "');
3495
3504
  }
3496
3505
  if (typeof value === "object") {
3497
- const entries = Object.entries(value).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
3506
+ const entries = Object.entries(value).filter(([_k, v]) => v !== void 0).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
3498
3507
  return `{ ${entries} }`;
3499
3508
  }
3500
3509
  return String(value);
@@ -3757,7 +3766,7 @@ function generateFieldAddition(collectionName, field, varName, isLast = false, c
3757
3766
  lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
3758
3767
  return lines.join("\n");
3759
3768
  }
3760
- function generateFieldModification(collectionName, modification, varName, isLast = false) {
3769
+ function generateFieldModification(collectionName, modification, varName, isLast = false, collectionIdMap) {
3761
3770
  const lines = [];
3762
3771
  const collectionVar = varName || `collection_${collectionName}_${modification.fieldName}`;
3763
3772
  const fieldVar = `${collectionVar}_field`;
@@ -3772,7 +3781,14 @@ function generateFieldModification(collectionName, modification, varName, isLast
3772
3781
  const relationKey = change.property.replace("relation.", "");
3773
3782
  if (relationKey === "collection") {
3774
3783
  const isUsersCollection = String(change.newValue).toLowerCase() === "users";
3775
- const collectionIdValue = isUsersCollection ? '"_pb_users_auth_"' : `app.findCollectionByNameOrId("${change.newValue}").id`;
3784
+ let collectionIdValue;
3785
+ if (isUsersCollection) {
3786
+ collectionIdValue = '"_pb_users_auth_"';
3787
+ } else if (collectionIdMap && collectionIdMap.has(String(change.newValue))) {
3788
+ collectionIdValue = `"${collectionIdMap.get(String(change.newValue))}"`;
3789
+ } else {
3790
+ collectionIdValue = `app.findCollectionByNameOrId("${change.newValue}").id`;
3791
+ }
3776
3792
  lines.push(` ${fieldVar}.collectionId = ${collectionIdValue};`);
3777
3793
  } else {
3778
3794
  lines.push(` ${fieldVar}.${relationKey} = ${formatValue(change.newValue)};`);
@@ -3788,11 +3804,9 @@ function generateFieldModification(collectionName, modification, varName, isLast
3788
3804
  function generateFieldDeletion(collectionName, fieldName, varName, isLast = false) {
3789
3805
  const lines = [];
3790
3806
  const collectionVar = varName || `collection_${collectionName}_${fieldName}`;
3791
- const fieldVar = `${collectionVar}_field`;
3792
3807
  lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
3793
- lines.push(` const ${fieldVar} = ${collectionVar}.fields.getByName("${fieldName}");`);
3794
3808
  lines.push(``);
3795
- lines.push(` ${collectionVar}.fields.remove(${fieldVar}.id);`);
3809
+ lines.push(` ${collectionVar}.fields.removeByName("${fieldName}");`);
3796
3810
  lines.push(``);
3797
3811
  lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
3798
3812
  return lines.join("\n");
@@ -3850,9 +3864,10 @@ function generateOperationUpMigration(operation, collectionIdMap) {
3850
3864
  const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection?.name ?? modification.collection;
3851
3865
  let operationCount = 0;
3852
3866
  const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + modification.rulesToUpdate.length + modification.permissionsToUpdate.length;
3853
- for (const field of modification.fieldsToAdd) {
3867
+ for (let i = 0; i < modification.fieldsToAdd.length; i++) {
3868
+ const field = modification.fieldsToAdd[i];
3854
3869
  operationCount++;
3855
- const varName = `collection_${collectionName}_add_${field.name}`;
3870
+ const varName = `collection_${collectionName}_add_${field.name}_${i}`;
3856
3871
  const isLast = operationCount === totalOperations;
3857
3872
  lines.push(generateFieldAddition(collectionName, field, varName, isLast, collectionIdMap));
3858
3873
  if (!isLast) lines.push("");
@@ -3861,7 +3876,7 @@ function generateOperationUpMigration(operation, collectionIdMap) {
3861
3876
  operationCount++;
3862
3877
  const varName = `collection_${collectionName}_modify_${fieldMod.fieldName}`;
3863
3878
  const isLast = operationCount === totalOperations;
3864
- lines.push(generateFieldModification(collectionName, fieldMod, varName, isLast));
3879
+ lines.push(generateFieldModification(collectionName, fieldMod, varName, isLast, collectionIdMap));
3865
3880
  if (!isLast) lines.push("");
3866
3881
  }
3867
3882
  for (const field of modification.fieldsToRemove) {
@@ -3910,20 +3925,23 @@ function generateOperationUpMigration(operation, collectionIdMap) {
3910
3925
  lines.push(generateCollectionDeletion(collectionName, varName, true));
3911
3926
  }
3912
3927
  let code = lines.join("\n");
3913
- const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
3914
- const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
3915
- const saveMatches = [...code.matchAll(savePattern)];
3916
- const deleteMatches = [...code.matchAll(deletePattern)];
3917
- const allMatches = [
3918
- ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
3919
- ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
3920
- ].sort((a, b) => b.index - a.index);
3921
- if (allMatches.length > 0) {
3922
- const lastMatch = allMatches[0];
3923
- if (lastMatch.type === "save") {
3924
- 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);
3925
- } else {
3926
- 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);
3928
+ const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
3929
+ if (!hasReturnStatement) {
3930
+ const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
3931
+ const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
3932
+ const saveMatches = [...code.matchAll(savePattern)];
3933
+ const deleteMatches = [...code.matchAll(deletePattern)];
3934
+ const allMatches = [
3935
+ ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
3936
+ ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
3937
+ ].sort((a, b) => b.index - a.index);
3938
+ if (allMatches.length > 0) {
3939
+ const lastMatch = allMatches[0];
3940
+ if (lastMatch.type === "save") {
3941
+ 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);
3942
+ } else {
3943
+ 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);
3944
+ }
3927
3945
  }
3928
3946
  }
3929
3947
  return code;
@@ -4012,20 +4030,23 @@ function generateOperationDownMigration(operation, collectionIdMap) {
4012
4030
  }
4013
4031
  }
4014
4032
  let code = lines.join("\n");
4015
- const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
4016
- const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
4017
- const saveMatches = [...code.matchAll(savePattern)];
4018
- const deleteMatches = [...code.matchAll(deletePattern)];
4019
- const allMatches = [
4020
- ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
4021
- ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
4022
- ].sort((a, b) => b.index - a.index);
4023
- if (allMatches.length > 0) {
4024
- const lastMatch = allMatches[0];
4025
- if (lastMatch.type === "save") {
4026
- 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);
4027
- } else {
4028
- 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);
4033
+ const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
4034
+ if (!hasReturnStatement) {
4035
+ const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
4036
+ const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
4037
+ const saveMatches = [...code.matchAll(savePattern)];
4038
+ const deleteMatches = [...code.matchAll(deletePattern)];
4039
+ const allMatches = [
4040
+ ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
4041
+ ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
4042
+ ].sort((a, b) => b.index - a.index);
4043
+ if (allMatches.length > 0) {
4044
+ const lastMatch = allMatches[0];
4045
+ if (lastMatch.type === "save") {
4046
+ 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);
4047
+ } else {
4048
+ 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);
4049
+ }
4029
4050
  }
4030
4051
  }
4031
4052
  return code;
@@ -4040,6 +4061,11 @@ function generateUpMigration(diff) {
4040
4061
  collectionIdMap.set(collection.name, collection.id);
4041
4062
  }
4042
4063
  }
4064
+ if (diff.existingCollectionIds) {
4065
+ for (const [name, id] of diff.existingCollectionIds) {
4066
+ collectionIdMap.set(name, id);
4067
+ }
4068
+ }
4043
4069
  if (diff.collectionsToCreate.length > 0) {
4044
4070
  lines.push(` // Create new collections`);
4045
4071
  for (let i = 0; i < diff.collectionsToCreate.length; i++) {
@@ -4055,8 +4081,9 @@ function generateUpMigration(diff) {
4055
4081
  const collectionName = modification.collection;
4056
4082
  if (modification.fieldsToAdd.length > 0) {
4057
4083
  lines.push(` // Add fields to ${collectionName}`);
4058
- for (const field of modification.fieldsToAdd) {
4059
- const varName = `collection_${collectionName}_add_${field.name}`;
4084
+ for (let i = 0; i < modification.fieldsToAdd.length; i++) {
4085
+ const field = modification.fieldsToAdd[i];
4086
+ const varName = `collection_${collectionName}_add_${field.name}_${i}`;
4060
4087
  lines.push(generateFieldAddition(collectionName, field, varName, false, collectionIdMap));
4061
4088
  lines.push(``);
4062
4089
  }
@@ -4065,7 +4092,7 @@ function generateUpMigration(diff) {
4065
4092
  lines.push(` // Modify fields in ${collectionName}`);
4066
4093
  for (const fieldMod of modification.fieldsToModify) {
4067
4094
  const varName = `collection_${collectionName}_modify_${fieldMod.fieldName}`;
4068
- lines.push(generateFieldModification(collectionName, fieldMod, varName));
4095
+ lines.push(generateFieldModification(collectionName, fieldMod, varName, false, collectionIdMap));
4069
4096
  lines.push(``);
4070
4097
  }
4071
4098
  }
@@ -4126,20 +4153,23 @@ function generateUpMigration(diff) {
4126
4153
  lines.push(``);
4127
4154
  }
4128
4155
  let code = lines.join("\n");
4129
- const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
4130
- const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
4131
- const saveMatches = [...code.matchAll(savePattern)];
4132
- const deleteMatches = [...code.matchAll(deletePattern)];
4133
- const allMatches = [
4134
- ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
4135
- ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
4136
- ].sort((a, b) => b.index - a.index);
4137
- if (allMatches.length > 0) {
4138
- const lastMatch = allMatches[0];
4139
- if (lastMatch.type === "save") {
4140
- 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);
4141
- } else {
4142
- 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);
4156
+ const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
4157
+ if (!hasReturnStatement) {
4158
+ const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
4159
+ const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
4160
+ const saveMatches = [...code.matchAll(savePattern)];
4161
+ const deleteMatches = [...code.matchAll(deletePattern)];
4162
+ const allMatches = [
4163
+ ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
4164
+ ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
4165
+ ].sort((a, b) => b.index - a.index);
4166
+ if (allMatches.length > 0) {
4167
+ const lastMatch = allMatches[0];
4168
+ if (lastMatch.type === "save") {
4169
+ 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);
4170
+ } else {
4171
+ 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);
4172
+ }
4143
4173
  }
4144
4174
  }
4145
4175
  return code;
@@ -4159,6 +4189,11 @@ function generateDownMigration(diff) {
4159
4189
  collectionIdMap.set(collection.name, collection.id);
4160
4190
  }
4161
4191
  }
4192
+ if (diff.existingCollectionIds) {
4193
+ for (const [name, id] of diff.existingCollectionIds) {
4194
+ collectionIdMap.set(name, id);
4195
+ }
4196
+ }
4162
4197
  if (diff.collectionsToDelete.length > 0) {
4163
4198
  lines.push(` // Recreate deleted collections`);
4164
4199
  for (let i = 0; i < diff.collectionsToDelete.length; i++) {
@@ -4256,20 +4291,23 @@ function generateDownMigration(diff) {
4256
4291
  lines.push(``);
4257
4292
  }
4258
4293
  let code = lines.join("\n");
4259
- const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
4260
- const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
4261
- const saveMatches = [...code.matchAll(savePattern)];
4262
- const deleteMatches = [...code.matchAll(deletePattern)];
4263
- const allMatches = [
4264
- ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
4265
- ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
4266
- ].sort((a, b) => b.index - a.index);
4267
- if (allMatches.length > 0) {
4268
- const lastMatch = allMatches[0];
4269
- if (lastMatch.type === "save") {
4270
- 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);
4271
- } else {
4272
- 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);
4294
+ const hasReturnStatement = /return\s+app\.(save|delete)\(/m.test(code);
4295
+ if (!hasReturnStatement) {
4296
+ const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
4297
+ const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
4298
+ const saveMatches = [...code.matchAll(savePattern)];
4299
+ const deleteMatches = [...code.matchAll(deletePattern)];
4300
+ const allMatches = [
4301
+ ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
4302
+ ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
4303
+ ].sort((a, b) => b.index - a.index);
4304
+ if (allMatches.length > 0) {
4305
+ const lastMatch = allMatches[0];
4306
+ if (lastMatch.type === "save") {
4307
+ 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);
4308
+ } else {
4309
+ 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);
4310
+ }
4273
4311
  }
4274
4312
  }
4275
4313
  return code;
@@ -4293,6 +4331,11 @@ function generate(diff, config) {
4293
4331
  collectionIdMap.set(collection.name, collection.id);
4294
4332
  }
4295
4333
  }
4334
+ if (diff.existingCollectionIds) {
4335
+ for (const [name, id] of diff.existingCollectionIds) {
4336
+ collectionIdMap.set(name, id);
4337
+ }
4338
+ }
4296
4339
  const baseTimestamp = generateTimestamp(normalizedConfig);
4297
4340
  const operations = splitDiffByCollection(diff, baseTimestamp);
4298
4341
  const filePaths = [];