pocketbase-zod-schema 0.7.0 → 0.7.2

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 (70) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cli/index.cjs +449 -50
  3. package/dist/cli/index.cjs.map +1 -1
  4. package/dist/cli/index.d.cts +1 -1
  5. package/dist/cli/index.d.ts +1 -1
  6. package/dist/cli/index.js +449 -50
  7. package/dist/cli/index.js.map +1 -1
  8. package/dist/cli/migrate.cjs +455 -53
  9. package/dist/cli/migrate.cjs.map +1 -1
  10. package/dist/cli/migrate.js +455 -53
  11. package/dist/cli/migrate.js.map +1 -1
  12. package/dist/cli/utils/index.cjs +8 -1
  13. package/dist/cli/utils/index.cjs.map +1 -1
  14. package/dist/cli/utils/index.d.cts +1 -1
  15. package/dist/cli/utils/index.d.ts +1 -1
  16. package/dist/cli/utils/index.js +8 -1
  17. package/dist/cli/utils/index.js.map +1 -1
  18. package/dist/index.cjs +90 -1
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +1 -1
  21. package/dist/index.d.ts +1 -1
  22. package/dist/index.js +87 -2
  23. package/dist/index.js.map +1 -1
  24. package/dist/migration/analyzer.cjs +87 -14
  25. package/dist/migration/analyzer.cjs.map +1 -1
  26. package/dist/migration/analyzer.d.cts +12 -4
  27. package/dist/migration/analyzer.d.ts +12 -4
  28. package/dist/migration/analyzer.js +87 -15
  29. package/dist/migration/analyzer.js.map +1 -1
  30. package/dist/migration/diff.cjs +64 -9
  31. package/dist/migration/diff.cjs.map +1 -1
  32. package/dist/migration/diff.d.cts +12 -2
  33. package/dist/migration/diff.d.ts +12 -2
  34. package/dist/migration/diff.js +64 -10
  35. package/dist/migration/diff.js.map +1 -1
  36. package/dist/migration/generator.cjs +206 -39
  37. package/dist/migration/generator.cjs.map +1 -1
  38. package/dist/migration/generator.d.cts +54 -2
  39. package/dist/migration/generator.d.ts +54 -2
  40. package/dist/migration/generator.js +203 -40
  41. package/dist/migration/generator.js.map +1 -1
  42. package/dist/migration/index.cjs +501 -63
  43. package/dist/migration/index.cjs.map +1 -1
  44. package/dist/migration/index.d.cts +2 -2
  45. package/dist/migration/index.d.ts +2 -2
  46. package/dist/migration/index.js +501 -63
  47. package/dist/migration/index.js.map +1 -1
  48. package/dist/migration/snapshot.cjs +147 -1
  49. package/dist/migration/snapshot.cjs.map +1 -1
  50. package/dist/migration/snapshot.d.cts +3 -1
  51. package/dist/migration/snapshot.d.ts +3 -1
  52. package/dist/migration/snapshot.js +147 -1
  53. package/dist/migration/snapshot.js.map +1 -1
  54. package/dist/migration/utils/index.d.cts +1 -1
  55. package/dist/migration/utils/index.d.ts +1 -1
  56. package/dist/schema.cjs +90 -1
  57. package/dist/schema.cjs.map +1 -1
  58. package/dist/schema.d.cts +128 -2
  59. package/dist/schema.d.ts +128 -2
  60. package/dist/schema.js +87 -2
  61. package/dist/schema.js.map +1 -1
  62. package/dist/server.cjs +545 -65
  63. package/dist/server.cjs.map +1 -1
  64. package/dist/server.d.cts +2 -2
  65. package/dist/server.d.ts +2 -2
  66. package/dist/server.js +542 -66
  67. package/dist/server.js.map +1 -1
  68. package/dist/{types-CWHV6ATd.d.cts → types-CnzfX6JH.d.cts} +20 -2
  69. package/dist/{types-C2nGWHLV.d.ts → types-Do3jyFBm.d.ts} +20 -2
  70. package/package.json +1 -1
@@ -104,6 +104,30 @@ Cause: ${this.originalError.message}`);
104
104
  return parts.join("");
105
105
  }
106
106
  };
107
+ function dedentSql(value) {
108
+ const lines = value.replace(/\r\n/g, "\n").split("\n");
109
+ while (lines.length > 0 && lines[0].trim() === "") {
110
+ lines.shift();
111
+ }
112
+ while (lines.length > 0 && lines[lines.length - 1].trim() === "") {
113
+ lines.pop();
114
+ }
115
+ if (lines.length === 0) {
116
+ return "";
117
+ }
118
+ let minIndent = Infinity;
119
+ for (const line of lines) {
120
+ if (line.trim() === "") continue;
121
+ const indent = line.length - line.trimStart().length;
122
+ if (indent < minIndent) {
123
+ minIndent = indent;
124
+ }
125
+ }
126
+ if (!Number.isFinite(minIndent) || minIndent === 0) {
127
+ return lines.map((line) => line.trimEnd()).join("\n");
128
+ }
129
+ return lines.map((line) => line.slice(minIndent).trimEnd()).join("\n");
130
+ }
107
131
 
108
132
  // src/migration/pocketbase-converter.ts
109
133
  var SNAPSHOT_VERSION = "1.0.0";
@@ -187,8 +211,9 @@ function convertPocketBaseField(pbField) {
187
211
  }
188
212
  function convertPocketBaseCollection(pbCollection) {
189
213
  const fields = [];
214
+ const isView = pbCollection.type === "view";
190
215
  const systemFieldNames = ["id", "created", "updated", "collectionId", "collectionName", "expand"];
191
- if (pbCollection.fields && Array.isArray(pbCollection.fields)) {
216
+ if (!isView && pbCollection.fields && Array.isArray(pbCollection.fields)) {
192
217
  for (const pbField of pbCollection.fields) {
193
218
  if (pbField.system || systemFieldNames.includes(pbField.name)) {
194
219
  const isAuthSystemField = pbCollection.type === "auth" && ["email", "emailVisibility", "verified", "password", "tokenKey"].includes(pbField.name);
@@ -208,6 +233,9 @@ function convertPocketBaseCollection(pbCollection) {
208
233
  if (pbCollection.id) {
209
234
  schema.id = pbCollection.id;
210
235
  }
236
+ if (typeof pbCollection.viewQuery === "string") {
237
+ schema.viewQuery = dedentSql(pbCollection.viewQuery);
238
+ }
211
239
  if (pbCollection.indexes && Array.isArray(pbCollection.indexes)) {
212
240
  schema.indexes = pbCollection.indexes;
213
241
  }
@@ -309,6 +337,24 @@ function findMigrationsAfterSnapshot(migrationsPath, snapshotTimestamp) {
309
337
  return [];
310
338
  }
311
339
  }
340
+ function skipTemplateLiteral(content, start) {
341
+ let i = start + 1;
342
+ while (i < content.length) {
343
+ const char = content[i];
344
+ if (char === "\\") {
345
+ i += 2;
346
+ continue;
347
+ }
348
+ if (char === "`") {
349
+ return i + 1;
350
+ }
351
+ i++;
352
+ }
353
+ return i;
354
+ }
355
+ function unescapeTemplateLiteral(raw) {
356
+ return raw.replace(/\\(`|\$\{|\\)/g, "$1");
357
+ }
312
358
  function parseMigrationOperationsFromContent(content) {
313
359
  const collectionsToCreate = [];
314
360
  const collectionsToDelete = [];
@@ -397,6 +443,10 @@ function parseMigrationOperationsFromContent(content) {
397
443
  while (i < content.length && bCount > 0) {
398
444
  const char = content[i];
399
445
  const prev = i > 0 ? content[i - 1] : "";
446
+ if (!inStr && char === "`") {
447
+ i = skipTemplateLiteral(content, i);
448
+ continue;
449
+ }
400
450
  if (!inStr && (char === '"' || char === "'")) {
401
451
  inStr = true;
402
452
  strChar = char;
@@ -586,6 +636,95 @@ function parseMigrationOperationsFromContent(content) {
586
636
  }
587
637
  }
588
638
  }
639
+ const viewQueryRegex = /(\w+)\.viewQuery\s*=\s*/g;
640
+ let viewQueryMatch;
641
+ while ((viewQueryMatch = viewQueryRegex.exec(content)) !== null) {
642
+ const varInfo = variables.get(viewQueryMatch[1]);
643
+ if (!varInfo || varInfo.type !== "collection") continue;
644
+ const valueStart = viewQueryMatch.index + viewQueryMatch[0].length;
645
+ if (content[valueStart] === "`") {
646
+ const end = skipTemplateLiteral(content, valueStart);
647
+ const raw = content.substring(valueStart + 1, end - 1);
648
+ getUpdate(varInfo.name).viewQuery = dedentSql(unescapeTemplateLiteral(raw));
649
+ viewQueryRegex.lastIndex = end;
650
+ } else {
651
+ const terminator = content.indexOf(";", valueStart);
652
+ const valueStr = content.substring(valueStart, terminator === -1 ? content.length : terminator);
653
+ try {
654
+ getUpdate(varInfo.name).viewQuery = dedentSql(new Function("app", `return ${valueStr}`)(mockApp));
655
+ } catch {
656
+ getUpdate(varInfo.name).viewQuery = dedentSql(valueStr.trim());
657
+ }
658
+ }
659
+ }
660
+ const unmarshalRegex = /unmarshal\s*\(/g;
661
+ let unmarshalMatch;
662
+ while ((unmarshalMatch = unmarshalRegex.exec(content)) !== null) {
663
+ let i = unmarshalMatch.index + unmarshalMatch[0].length;
664
+ while (i < content.length && /\s/.test(content[i])) i++;
665
+ if (content[i] !== "{") continue;
666
+ const objStart = i;
667
+ let braceCount = 1;
668
+ let inStr = false;
669
+ let strChar = null;
670
+ i++;
671
+ while (i < content.length && braceCount > 0) {
672
+ const ch = content[i];
673
+ const prev = i > 0 ? content[i - 1] : "";
674
+ if (!inStr && ch === "`") {
675
+ i = skipTemplateLiteral(content, i);
676
+ continue;
677
+ }
678
+ if (!inStr && (ch === '"' || ch === "'")) {
679
+ inStr = true;
680
+ strChar = ch;
681
+ } else if (inStr && ch === strChar && prev !== "\\") {
682
+ inStr = false;
683
+ strChar = null;
684
+ }
685
+ if (!inStr) {
686
+ if (ch === "{") braceCount++;
687
+ if (ch === "}") braceCount--;
688
+ }
689
+ i++;
690
+ }
691
+ if (braceCount !== 0) continue;
692
+ const objStr = content.substring(objStart, i);
693
+ while (i < content.length && /[\s,]/.test(content[i])) i++;
694
+ const varStart = i;
695
+ while (i < content.length && /\w/.test(content[i])) i++;
696
+ const colVarName = content.substring(varStart, i);
697
+ const colInfo = variables.get(colVarName);
698
+ if (!colInfo || colInfo.type !== "collection") continue;
699
+ const keyValRegex = /"(\w+Rule)"\s*:\s*([^,}\n]+)/g;
700
+ let kvMatch;
701
+ while ((kvMatch = keyValRegex.exec(objStr)) !== null) {
702
+ const ruleKey = kvMatch[1];
703
+ const valStr = kvMatch[2].trim();
704
+ try {
705
+ const value = new Function("app", `return ${valStr}`)(mockApp);
706
+ getUpdate(colInfo.name).rulesToUpdate[ruleKey] = value;
707
+ } catch {
708
+ getUpdate(colInfo.name).rulesToUpdate[ruleKey] = valStr;
709
+ }
710
+ }
711
+ const viewQueryKey = objStr.match(/"viewQuery"\s*:\s*/);
712
+ if (viewQueryKey) {
713
+ const valueStart = viewQueryKey.index + viewQueryKey[0].length;
714
+ if (objStr[valueStart] === "`") {
715
+ const end = skipTemplateLiteral(objStr, valueStart);
716
+ const raw = objStr.substring(valueStart + 1, end - 1);
717
+ getUpdate(colInfo.name).viewQuery = dedentSql(unescapeTemplateLiteral(raw));
718
+ } else {
719
+ const valStr = objStr.substring(valueStart).replace(/[,}\s]+$/, "");
720
+ try {
721
+ getUpdate(colInfo.name).viewQuery = dedentSql(new Function("app", `return ${valStr}`)(mockApp));
722
+ } catch {
723
+ getUpdate(colInfo.name).viewQuery = dedentSql(valStr);
724
+ }
725
+ }
726
+ }
727
+ }
589
728
  const idxPushRegex = /(\w+)\.indexes\.push\s*\(/g;
590
729
  let match;
591
730
  while ((match = idxPushRegex.exec(content)) !== null) {
@@ -715,6 +854,10 @@ function parseMigrationOperations(migrationContent) {
715
854
  while (i < migrationContent.length && braceCount > 0) {
716
855
  const char = migrationContent[i];
717
856
  const prevChar = i > 0 ? migrationContent[i - 1] : "";
857
+ if (!inString && char === "`") {
858
+ i = skipTemplateLiteral(migrationContent, i);
859
+ continue;
860
+ }
718
861
  if (!inString && (char === '"' || char === "'")) {
719
862
  inString = true;
720
863
  stringChar = char;
@@ -1050,6 +1193,9 @@ function applyMigrationOperations(snapshot, operations) {
1050
1193
  collection.indexes = collection.indexes.filter((idx) => !update.indexesToRemove.includes(idx));
1051
1194
  }
1052
1195
  }
1196
+ if (update.viewQuery !== void 0) {
1197
+ collection.viewQuery = update.viewQuery;
1198
+ }
1053
1199
  if (Object.keys(update.rulesToUpdate).length > 0) {
1054
1200
  if (!collection.rules) collection.rules = {};
1055
1201
  if (!collection.permissions) collection.permissions = {};