pocketbase-zod-schema 0.7.0 → 0.7.1

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 CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.7.1](https://github.com/dastron/pocketbase-zod-schema/compare/pocketbase-zod-schema-v0.7.0...pocketbase-zod-schema-v0.7.1) (2026-05-06)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * missing autodate fields ([e3d6dc6](https://github.com/dastron/pocketbase-zod-schema/commit/e3d6dc60d2e6b7d0fa9a52b6bace3c2d3addfc84))
9
+
3
10
  ## [0.7.0](https://github.com/dastron/pocketbase-zod-schema/compare/pocketbase-zod-schema-v0.6.1...pocketbase-zod-schema-v0.7.0) (2026-02-13)
4
11
 
5
12
 
@@ -2163,6 +2163,54 @@ function parseMigrationOperationsFromContent(content) {
2163
2163
  }
2164
2164
  }
2165
2165
  }
2166
+ const unmarshalRegex = /unmarshal\s*\(/g;
2167
+ let unmarshalMatch;
2168
+ while ((unmarshalMatch = unmarshalRegex.exec(content)) !== null) {
2169
+ let i = unmarshalMatch.index + unmarshalMatch[0].length;
2170
+ while (i < content.length && /\s/.test(content[i])) i++;
2171
+ if (content[i] !== "{") continue;
2172
+ const objStart = i;
2173
+ let braceCount = 1;
2174
+ let inStr = false;
2175
+ let strChar = null;
2176
+ i++;
2177
+ while (i < content.length && braceCount > 0) {
2178
+ const ch = content[i];
2179
+ const prev = i > 0 ? content[i - 1] : "";
2180
+ if (!inStr && (ch === '"' || ch === "'")) {
2181
+ inStr = true;
2182
+ strChar = ch;
2183
+ } else if (inStr && ch === strChar && prev !== "\\") {
2184
+ inStr = false;
2185
+ strChar = null;
2186
+ }
2187
+ if (!inStr) {
2188
+ if (ch === "{") braceCount++;
2189
+ if (ch === "}") braceCount--;
2190
+ }
2191
+ i++;
2192
+ }
2193
+ if (braceCount !== 0) continue;
2194
+ const objStr = content.substring(objStart, i);
2195
+ while (i < content.length && /[\s,]/.test(content[i])) i++;
2196
+ const varStart = i;
2197
+ while (i < content.length && /\w/.test(content[i])) i++;
2198
+ const colVarName = content.substring(varStart, i);
2199
+ const colInfo = variables.get(colVarName);
2200
+ if (!colInfo || colInfo.type !== "collection") continue;
2201
+ const keyValRegex = /"(\w+Rule)"\s*:\s*([^,}\n]+)/g;
2202
+ let kvMatch;
2203
+ while ((kvMatch = keyValRegex.exec(objStr)) !== null) {
2204
+ const ruleKey = kvMatch[1];
2205
+ const valStr = kvMatch[2].trim();
2206
+ try {
2207
+ const value = new Function("app", `return ${valStr}`)(mockApp);
2208
+ getUpdate(colInfo.name).rulesToUpdate[ruleKey] = value;
2209
+ } catch {
2210
+ getUpdate(colInfo.name).rulesToUpdate[ruleKey] = valStr;
2211
+ }
2212
+ }
2213
+ }
2166
2214
  const idxPushRegex = /(\w+)\.indexes\.push\s*\(/g;
2167
2215
  let match;
2168
2216
  while ((match = idxPushRegex.exec(content)) !== null) {
@@ -3227,6 +3275,36 @@ function getSystemFields() {
3227
3275
  }
3228
3276
  ];
3229
3277
  }
3278
+ function getSystemTimestampFields() {
3279
+ return [
3280
+ {
3281
+ name: "created",
3282
+ id: "autodate2990389176",
3283
+ type: "autodate",
3284
+ required: false,
3285
+ options: {
3286
+ hidden: false,
3287
+ onCreate: true,
3288
+ onUpdate: false,
3289
+ presentable: false,
3290
+ system: true
3291
+ }
3292
+ },
3293
+ {
3294
+ name: "updated",
3295
+ id: "autodate3332085495",
3296
+ type: "autodate",
3297
+ required: false,
3298
+ options: {
3299
+ hidden: false,
3300
+ onCreate: true,
3301
+ onUpdate: true,
3302
+ presentable: false,
3303
+ system: true
3304
+ }
3305
+ }
3306
+ ];
3307
+ }
3230
3308
  function getAuthSystemFields() {
3231
3309
  return [
3232
3310
  {
@@ -3581,6 +3659,18 @@ function generatePermissionUpdate(collectionName, ruleType, newValue, varName, i
3581
3659
  lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
3582
3660
  return lines.join("\n");
3583
3661
  }
3662
+ function generateGroupedRuleUpdates(collectionName, entries, varSuffix, isLast = false, collectionIdMap) {
3663
+ const collectionVar = `collection_${collectionName}_${varSuffix}`;
3664
+ const lines = [];
3665
+ lines.push(` const ${collectionVar} = ${generateFindCollectionCode(collectionName, collectionIdMap)};`);
3666
+ lines.push(` unmarshal({`);
3667
+ for (const entry of entries) {
3668
+ lines.push(` "${entry.ruleType}": ${formatValue(entry.value)},`);
3669
+ }
3670
+ lines.push(` }, ${collectionVar})`);
3671
+ lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
3672
+ return lines.join("\n");
3673
+ }
3584
3674
 
3585
3675
  // src/migration/generator/collections.ts
3586
3676
  function generateCollectionCreation(collection, varName = "collection", isLast = false, collectionIdMap) {
@@ -3604,11 +3694,12 @@ function generateCollectionCreation(collection, varName = "collection", isLast =
3604
3694
  systemFieldNames.push(...getAuthSystemFields().map((f) => f.name));
3605
3695
  }
3606
3696
  const userFields = collection.fields.filter((f) => !systemFieldNames.includes(f.name));
3607
- const allFields = [...getSystemFields().filter((f) => f.name === "id")];
3697
+ const allFields = [...getSystemFields()];
3608
3698
  if (collection.type === "auth") {
3609
3699
  allFields.push(...getAuthSystemFields());
3610
3700
  }
3611
3701
  allFields.push(...userFields);
3702
+ allFields.push(...getSystemTimestampFields());
3612
3703
  lines.push(` "fields": ${generateFieldsArray(allFields, collectionIdMap)},`);
3613
3704
  let allIndexes = [...collection.indexes || []];
3614
3705
  if (collection.type === "auth") {
@@ -3638,7 +3729,7 @@ function generateOperationUpMigration(operation, collectionIdMap) {
3638
3729
  const modification = operation.modifications;
3639
3730
  const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection?.name ?? modification.collection;
3640
3731
  let operationCount = 0;
3641
- const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + modification.rulesToUpdate.length + modification.permissionsToUpdate.length;
3732
+ const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + (modification.permissionsToUpdate.length > 0 ? 1 : modification.rulesToUpdate.length > 0 ? 1 : 0);
3642
3733
  for (let i = 0; i < modification.fieldsToAdd.length; i++) {
3643
3734
  const field = modification.fieldsToAdd[i];
3644
3735
  operationCount++;
@@ -3678,23 +3769,29 @@ function generateOperationUpMigration(operation, collectionIdMap) {
3678
3769
  if (!isLast) lines.push("");
3679
3770
  }
3680
3771
  if (modification.permissionsToUpdate && modification.permissionsToUpdate.length > 0) {
3681
- for (const permission of modification.permissionsToUpdate) {
3682
- operationCount++;
3772
+ operationCount++;
3773
+ const isLast = operationCount === totalOperations;
3774
+ if (modification.permissionsToUpdate.length >= 2) {
3775
+ const entries = modification.permissionsToUpdate.map((p) => ({ ruleType: p.ruleType, value: p.newValue }));
3776
+ lines.push(generateGroupedRuleUpdates(collectionName, entries, "rules", isLast, collectionIdMap));
3777
+ } else {
3778
+ const permission = modification.permissionsToUpdate[0];
3683
3779
  const varName = `collection_${collectionName}_perm_${permission.ruleType}`;
3684
- const isLast = operationCount === totalOperations;
3685
- lines.push(
3686
- generatePermissionUpdate(collectionName, permission.ruleType, permission.newValue, varName, isLast, collectionIdMap)
3687
- );
3688
- if (!isLast) lines.push("");
3780
+ lines.push(generatePermissionUpdate(collectionName, permission.ruleType, permission.newValue, varName, isLast, collectionIdMap));
3689
3781
  }
3782
+ if (!isLast) lines.push("");
3690
3783
  } else if (modification.rulesToUpdate.length > 0) {
3691
- for (const rule of modification.rulesToUpdate) {
3692
- operationCount++;
3784
+ operationCount++;
3785
+ const isLast = operationCount === totalOperations;
3786
+ if (modification.rulesToUpdate.length >= 2) {
3787
+ const entries = modification.rulesToUpdate.map((r) => ({ ruleType: r.ruleType, value: r.newValue }));
3788
+ lines.push(generateGroupedRuleUpdates(collectionName, entries, "rules", isLast, collectionIdMap));
3789
+ } else {
3790
+ const rule = modification.rulesToUpdate[0];
3693
3791
  const varName = `collection_${collectionName}_rule_${rule.ruleType}`;
3694
- const isLast = operationCount === totalOperations;
3695
3792
  lines.push(generateRuleUpdate(collectionName, rule.ruleType, rule.newValue, varName, isLast, collectionIdMap));
3696
- if (!isLast) lines.push("");
3697
3793
  }
3794
+ if (!isLast) lines.push("");
3698
3795
  }
3699
3796
  } else if (operation.type === "delete") {
3700
3797
  const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection.name;
@@ -3733,25 +3830,31 @@ function generateOperationDownMigration(operation, collectionIdMap) {
3733
3830
  const modification = operation.modifications;
3734
3831
  const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection?.name ?? modification.collection;
3735
3832
  let operationCount = 0;
3736
- const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + modification.rulesToUpdate.length + modification.permissionsToUpdate.length;
3833
+ const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + (modification.permissionsToUpdate.length > 0 ? 1 : modification.rulesToUpdate.length > 0 ? 1 : 0);
3737
3834
  if (modification.permissionsToUpdate && modification.permissionsToUpdate.length > 0) {
3738
- for (const permission of modification.permissionsToUpdate) {
3739
- operationCount++;
3835
+ operationCount++;
3836
+ const isLast = operationCount === totalOperations;
3837
+ if (modification.permissionsToUpdate.length >= 2) {
3838
+ const entries = modification.permissionsToUpdate.map((p) => ({ ruleType: p.ruleType, value: p.oldValue }));
3839
+ lines.push(generateGroupedRuleUpdates(collectionName, entries, "revert_rules", isLast, collectionIdMap));
3840
+ } else {
3841
+ const permission = modification.permissionsToUpdate[0];
3740
3842
  const varName = `collection_${collectionName}_revert_perm_${permission.ruleType}`;
3741
- const isLast = operationCount === totalOperations;
3742
- lines.push(
3743
- generatePermissionUpdate(collectionName, permission.ruleType, permission.oldValue, varName, isLast, collectionIdMap)
3744
- );
3745
- if (!isLast) lines.push("");
3843
+ lines.push(generatePermissionUpdate(collectionName, permission.ruleType, permission.oldValue, varName, isLast, collectionIdMap));
3746
3844
  }
3845
+ if (!isLast) lines.push("");
3747
3846
  } else if (modification.rulesToUpdate.length > 0) {
3748
- for (const rule of modification.rulesToUpdate) {
3749
- operationCount++;
3847
+ operationCount++;
3848
+ const isLast = operationCount === totalOperations;
3849
+ if (modification.rulesToUpdate.length >= 2) {
3850
+ const entries = modification.rulesToUpdate.map((r) => ({ ruleType: r.ruleType, value: r.oldValue }));
3851
+ lines.push(generateGroupedRuleUpdates(collectionName, entries, "revert_rules", isLast, collectionIdMap));
3852
+ } else {
3853
+ const rule = modification.rulesToUpdate[0];
3750
3854
  const varName = `collection_${collectionName}_revert_rule_${rule.ruleType}`;
3751
- const isLast = operationCount === totalOperations;
3752
3855
  lines.push(generateRuleUpdate(collectionName, rule.ruleType, rule.oldValue, varName, isLast, collectionIdMap));
3753
- if (!isLast) lines.push("");
3754
3856
  }
3857
+ if (!isLast) lines.push("");
3755
3858
  }
3756
3859
  for (let i = 0; i < modification.indexesToRemove.length; i++) {
3757
3860
  operationCount++;