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
@@ -1,4 +1,4 @@
1
- import { F as FieldDefinition, a as SchemaSnapshot, S as SchemaDefinition, C as CollectionSchema } from '../types-CWHV6ATd.cjs';
1
+ import { F as FieldDefinition, a as SchemaSnapshot, S as SchemaDefinition, C as CollectionSchema } from '../types-CnzfX6JH.cjs';
2
2
  import '../fields-B96iGprI.cjs';
3
3
  import 'zod';
4
4
 
@@ -23,6 +23,8 @@ interface ParsedCollectionUpdate {
23
23
  indexesToAdd: string[];
24
24
  indexesToRemove: string[];
25
25
  rulesToUpdate: Record<string, string | null>;
26
+ /** New SQL query for a view collection, when the migration updates one */
27
+ viewQuery?: string;
26
28
  }
27
29
 
28
30
  /**
@@ -1,4 +1,4 @@
1
- import { F as FieldDefinition, a as SchemaSnapshot, S as SchemaDefinition, C as CollectionSchema } from '../types-C2nGWHLV.js';
1
+ import { F as FieldDefinition, a as SchemaSnapshot, S as SchemaDefinition, C as CollectionSchema } from '../types-Do3jyFBm.js';
2
2
  import '../fields-B96iGprI.js';
3
3
  import 'zod';
4
4
 
@@ -23,6 +23,8 @@ interface ParsedCollectionUpdate {
23
23
  indexesToAdd: string[];
24
24
  indexesToRemove: string[];
25
25
  rulesToUpdate: Record<string, string | null>;
26
+ /** New SQL query for a view collection, when the migration updates one */
27
+ viewQuery?: string;
26
28
  }
27
29
 
28
30
  /**
@@ -81,6 +81,30 @@ Cause: ${this.originalError.message}`);
81
81
  return parts.join("");
82
82
  }
83
83
  };
84
+ function dedentSql(value) {
85
+ const lines = value.replace(/\r\n/g, "\n").split("\n");
86
+ while (lines.length > 0 && lines[0].trim() === "") {
87
+ lines.shift();
88
+ }
89
+ while (lines.length > 0 && lines[lines.length - 1].trim() === "") {
90
+ lines.pop();
91
+ }
92
+ if (lines.length === 0) {
93
+ return "";
94
+ }
95
+ let minIndent = Infinity;
96
+ for (const line of lines) {
97
+ if (line.trim() === "") continue;
98
+ const indent = line.length - line.trimStart().length;
99
+ if (indent < minIndent) {
100
+ minIndent = indent;
101
+ }
102
+ }
103
+ if (!Number.isFinite(minIndent) || minIndent === 0) {
104
+ return lines.map((line) => line.trimEnd()).join("\n");
105
+ }
106
+ return lines.map((line) => line.slice(minIndent).trimEnd()).join("\n");
107
+ }
84
108
 
85
109
  // src/migration/pocketbase-converter.ts
86
110
  var SNAPSHOT_VERSION = "1.0.0";
@@ -164,8 +188,9 @@ function convertPocketBaseField(pbField) {
164
188
  }
165
189
  function convertPocketBaseCollection(pbCollection) {
166
190
  const fields = [];
191
+ const isView = pbCollection.type === "view";
167
192
  const systemFieldNames = ["id", "created", "updated", "collectionId", "collectionName", "expand"];
168
- if (pbCollection.fields && Array.isArray(pbCollection.fields)) {
193
+ if (!isView && pbCollection.fields && Array.isArray(pbCollection.fields)) {
169
194
  for (const pbField of pbCollection.fields) {
170
195
  if (pbField.system || systemFieldNames.includes(pbField.name)) {
171
196
  const isAuthSystemField = pbCollection.type === "auth" && ["email", "emailVisibility", "verified", "password", "tokenKey"].includes(pbField.name);
@@ -185,6 +210,9 @@ function convertPocketBaseCollection(pbCollection) {
185
210
  if (pbCollection.id) {
186
211
  schema.id = pbCollection.id;
187
212
  }
213
+ if (typeof pbCollection.viewQuery === "string") {
214
+ schema.viewQuery = dedentSql(pbCollection.viewQuery);
215
+ }
188
216
  if (pbCollection.indexes && Array.isArray(pbCollection.indexes)) {
189
217
  schema.indexes = pbCollection.indexes;
190
218
  }
@@ -286,6 +314,24 @@ function findMigrationsAfterSnapshot(migrationsPath, snapshotTimestamp) {
286
314
  return [];
287
315
  }
288
316
  }
317
+ function skipTemplateLiteral(content, start) {
318
+ let i = start + 1;
319
+ while (i < content.length) {
320
+ const char = content[i];
321
+ if (char === "\\") {
322
+ i += 2;
323
+ continue;
324
+ }
325
+ if (char === "`") {
326
+ return i + 1;
327
+ }
328
+ i++;
329
+ }
330
+ return i;
331
+ }
332
+ function unescapeTemplateLiteral(raw) {
333
+ return raw.replace(/\\(`|\$\{|\\)/g, "$1");
334
+ }
289
335
  function parseMigrationOperationsFromContent(content) {
290
336
  const collectionsToCreate = [];
291
337
  const collectionsToDelete = [];
@@ -374,6 +420,10 @@ function parseMigrationOperationsFromContent(content) {
374
420
  while (i < content.length && bCount > 0) {
375
421
  const char = content[i];
376
422
  const prev = i > 0 ? content[i - 1] : "";
423
+ if (!inStr && char === "`") {
424
+ i = skipTemplateLiteral(content, i);
425
+ continue;
426
+ }
377
427
  if (!inStr && (char === '"' || char === "'")) {
378
428
  inStr = true;
379
429
  strChar = char;
@@ -563,6 +613,95 @@ function parseMigrationOperationsFromContent(content) {
563
613
  }
564
614
  }
565
615
  }
616
+ const viewQueryRegex = /(\w+)\.viewQuery\s*=\s*/g;
617
+ let viewQueryMatch;
618
+ while ((viewQueryMatch = viewQueryRegex.exec(content)) !== null) {
619
+ const varInfo = variables.get(viewQueryMatch[1]);
620
+ if (!varInfo || varInfo.type !== "collection") continue;
621
+ const valueStart = viewQueryMatch.index + viewQueryMatch[0].length;
622
+ if (content[valueStart] === "`") {
623
+ const end = skipTemplateLiteral(content, valueStart);
624
+ const raw = content.substring(valueStart + 1, end - 1);
625
+ getUpdate(varInfo.name).viewQuery = dedentSql(unescapeTemplateLiteral(raw));
626
+ viewQueryRegex.lastIndex = end;
627
+ } else {
628
+ const terminator = content.indexOf(";", valueStart);
629
+ const valueStr = content.substring(valueStart, terminator === -1 ? content.length : terminator);
630
+ try {
631
+ getUpdate(varInfo.name).viewQuery = dedentSql(new Function("app", `return ${valueStr}`)(mockApp));
632
+ } catch {
633
+ getUpdate(varInfo.name).viewQuery = dedentSql(valueStr.trim());
634
+ }
635
+ }
636
+ }
637
+ const unmarshalRegex = /unmarshal\s*\(/g;
638
+ let unmarshalMatch;
639
+ while ((unmarshalMatch = unmarshalRegex.exec(content)) !== null) {
640
+ let i = unmarshalMatch.index + unmarshalMatch[0].length;
641
+ while (i < content.length && /\s/.test(content[i])) i++;
642
+ if (content[i] !== "{") continue;
643
+ const objStart = i;
644
+ let braceCount = 1;
645
+ let inStr = false;
646
+ let strChar = null;
647
+ i++;
648
+ while (i < content.length && braceCount > 0) {
649
+ const ch = content[i];
650
+ const prev = i > 0 ? content[i - 1] : "";
651
+ if (!inStr && ch === "`") {
652
+ i = skipTemplateLiteral(content, i);
653
+ continue;
654
+ }
655
+ if (!inStr && (ch === '"' || ch === "'")) {
656
+ inStr = true;
657
+ strChar = ch;
658
+ } else if (inStr && ch === strChar && prev !== "\\") {
659
+ inStr = false;
660
+ strChar = null;
661
+ }
662
+ if (!inStr) {
663
+ if (ch === "{") braceCount++;
664
+ if (ch === "}") braceCount--;
665
+ }
666
+ i++;
667
+ }
668
+ if (braceCount !== 0) continue;
669
+ const objStr = content.substring(objStart, i);
670
+ while (i < content.length && /[\s,]/.test(content[i])) i++;
671
+ const varStart = i;
672
+ while (i < content.length && /\w/.test(content[i])) i++;
673
+ const colVarName = content.substring(varStart, i);
674
+ const colInfo = variables.get(colVarName);
675
+ if (!colInfo || colInfo.type !== "collection") continue;
676
+ const keyValRegex = /"(\w+Rule)"\s*:\s*([^,}\n]+)/g;
677
+ let kvMatch;
678
+ while ((kvMatch = keyValRegex.exec(objStr)) !== null) {
679
+ const ruleKey = kvMatch[1];
680
+ const valStr = kvMatch[2].trim();
681
+ try {
682
+ const value = new Function("app", `return ${valStr}`)(mockApp);
683
+ getUpdate(colInfo.name).rulesToUpdate[ruleKey] = value;
684
+ } catch {
685
+ getUpdate(colInfo.name).rulesToUpdate[ruleKey] = valStr;
686
+ }
687
+ }
688
+ const viewQueryKey = objStr.match(/"viewQuery"\s*:\s*/);
689
+ if (viewQueryKey) {
690
+ const valueStart = viewQueryKey.index + viewQueryKey[0].length;
691
+ if (objStr[valueStart] === "`") {
692
+ const end = skipTemplateLiteral(objStr, valueStart);
693
+ const raw = objStr.substring(valueStart + 1, end - 1);
694
+ getUpdate(colInfo.name).viewQuery = dedentSql(unescapeTemplateLiteral(raw));
695
+ } else {
696
+ const valStr = objStr.substring(valueStart).replace(/[,}\s]+$/, "");
697
+ try {
698
+ getUpdate(colInfo.name).viewQuery = dedentSql(new Function("app", `return ${valStr}`)(mockApp));
699
+ } catch {
700
+ getUpdate(colInfo.name).viewQuery = dedentSql(valStr);
701
+ }
702
+ }
703
+ }
704
+ }
566
705
  const idxPushRegex = /(\w+)\.indexes\.push\s*\(/g;
567
706
  let match;
568
707
  while ((match = idxPushRegex.exec(content)) !== null) {
@@ -692,6 +831,10 @@ function parseMigrationOperations(migrationContent) {
692
831
  while (i < migrationContent.length && braceCount > 0) {
693
832
  const char = migrationContent[i];
694
833
  const prevChar = i > 0 ? migrationContent[i - 1] : "";
834
+ if (!inString && char === "`") {
835
+ i = skipTemplateLiteral(migrationContent, i);
836
+ continue;
837
+ }
695
838
  if (!inString && (char === '"' || char === "'")) {
696
839
  inString = true;
697
840
  stringChar = char;
@@ -1027,6 +1170,9 @@ function applyMigrationOperations(snapshot, operations) {
1027
1170
  collection.indexes = collection.indexes.filter((idx) => !update.indexesToRemove.includes(idx));
1028
1171
  }
1029
1172
  }
1173
+ if (update.viewQuery !== void 0) {
1174
+ collection.viewQuery = update.viewQuery;
1175
+ }
1030
1176
  if (Object.keys(update.rulesToUpdate).length > 0) {
1031
1177
  if (!collection.rules) collection.rules = {};
1032
1178
  if (!collection.permissions) collection.permissions = {};