pocketbase-zod-schema 0.1.2 → 0.1.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 (63) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +329 -99
  3. package/dist/cli/index.cjs +176 -55
  4. package/dist/cli/index.cjs.map +1 -1
  5. package/dist/cli/index.js +176 -55
  6. package/dist/cli/index.js.map +1 -1
  7. package/dist/cli/migrate.cjs +196 -58
  8. package/dist/cli/migrate.cjs.map +1 -1
  9. package/dist/cli/migrate.js +194 -57
  10. package/dist/cli/migrate.js.map +1 -1
  11. package/dist/cli/utils/index.cjs +1 -1
  12. package/dist/cli/utils/index.cjs.map +1 -1
  13. package/dist/cli/utils/index.js +1 -1
  14. package/dist/cli/utils/index.js.map +1 -1
  15. package/dist/index.cjs +197 -96
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.cts +3 -3
  18. package/dist/index.d.ts +3 -3
  19. package/dist/index.js +197 -95
  20. package/dist/index.js.map +1 -1
  21. package/dist/migration/analyzer.cjs +101 -28
  22. package/dist/migration/analyzer.cjs.map +1 -1
  23. package/dist/migration/analyzer.js +101 -28
  24. package/dist/migration/analyzer.js.map +1 -1
  25. package/dist/migration/generator.cjs +60 -25
  26. package/dist/migration/generator.cjs.map +1 -1
  27. package/dist/migration/generator.d.cts +9 -5
  28. package/dist/migration/generator.d.ts +9 -5
  29. package/dist/migration/generator.js +60 -25
  30. package/dist/migration/generator.js.map +1 -1
  31. package/dist/migration/index.cjs +162 -53
  32. package/dist/migration/index.cjs.map +1 -1
  33. package/dist/migration/index.js +162 -53
  34. package/dist/migration/index.js.map +1 -1
  35. package/dist/migration/snapshot.cjs +1 -0
  36. package/dist/migration/snapshot.cjs.map +1 -1
  37. package/dist/migration/snapshot.js +1 -0
  38. package/dist/migration/snapshot.js.map +1 -1
  39. package/dist/migration/utils/index.cjs +19 -17
  40. package/dist/migration/utils/index.cjs.map +1 -1
  41. package/dist/migration/utils/index.d.cts +3 -1
  42. package/dist/migration/utils/index.d.ts +3 -1
  43. package/dist/migration/utils/index.js +19 -17
  44. package/dist/migration/utils/index.js.map +1 -1
  45. package/dist/mutator.cjs +9 -11
  46. package/dist/mutator.cjs.map +1 -1
  47. package/dist/mutator.d.cts +5 -9
  48. package/dist/mutator.d.ts +5 -9
  49. package/dist/mutator.js +9 -11
  50. package/dist/mutator.js.map +1 -1
  51. package/dist/schema.cjs +50 -53
  52. package/dist/schema.cjs.map +1 -1
  53. package/dist/schema.d.cts +94 -12
  54. package/dist/schema.d.ts +94 -12
  55. package/dist/schema.js +50 -52
  56. package/dist/schema.js.map +1 -1
  57. package/dist/types.d.cts +2 -5
  58. package/dist/types.d.ts +2 -5
  59. package/dist/user-C39DQ40N.d.cts +53 -0
  60. package/dist/user-C39DQ40N.d.ts +53 -0
  61. package/package.json +2 -3
  62. package/dist/user-jS1aYoeD.d.cts +0 -123
  63. package/dist/user-jS1aYoeD.d.ts +0 -123
@@ -131,7 +131,7 @@ declare function generateIndexesArray(indexes?: string[]): string;
131
131
  * @param varName - Variable name to use for the collection (default: 'collection')
132
132
  * @returns JavaScript code for creating the collection
133
133
  */
134
- declare function generateCollectionCreation(collection: CollectionSchema, varName?: string): string;
134
+ declare function generateCollectionCreation(collection: CollectionSchema, varName?: string, isLast?: boolean): string;
135
135
  /**
136
136
  * Generates code for adding a field to an existing collection
137
137
  * Uses the appropriate Field constructor based on field type
@@ -139,9 +139,10 @@ declare function generateCollectionCreation(collection: CollectionSchema, varNam
139
139
  * @param collectionName - Name of the collection
140
140
  * @param field - Field definition to add
141
141
  * @param varName - Variable name to use for the collection (default: auto-generated)
142
+ * @param isLast - Whether this is the last operation (will return the result)
142
143
  * @returns JavaScript code for adding the field
143
144
  */
144
- declare function generateFieldAddition(collectionName: string, field: FieldDefinition, varName?: string): string;
145
+ declare function generateFieldAddition(collectionName: string, field: FieldDefinition, varName?: string, isLast?: boolean): string;
145
146
  /**
146
147
  * Generates code for modifying an existing field
147
148
  * Updates field properties based on detected changes
@@ -149,18 +150,20 @@ declare function generateFieldAddition(collectionName: string, field: FieldDefin
149
150
  * @param collectionName - Name of the collection
150
151
  * @param modification - Field modification details
151
152
  * @param varName - Variable name to use for the collection (default: auto-generated)
153
+ * @param isLast - Whether this is the last operation (will return the result)
152
154
  * @returns JavaScript code for modifying the field
153
155
  */
154
- declare function generateFieldModification(collectionName: string, modification: FieldModification, varName?: string): string;
156
+ declare function generateFieldModification(collectionName: string, modification: FieldModification, varName?: string, isLast?: boolean): string;
155
157
  /**
156
158
  * Generates code for deleting a field from a collection
157
159
  *
158
160
  * @param collectionName - Name of the collection
159
161
  * @param fieldName - Name of the field to delete
160
162
  * @param varName - Variable name to use for the collection (default: auto-generated)
163
+ * @param isLast - Whether this is the last operation (will return the result)
161
164
  * @returns JavaScript code for deleting the field
162
165
  */
163
- declare function generateFieldDeletion(collectionName: string, fieldName: string, varName?: string): string;
166
+ declare function generateFieldDeletion(collectionName: string, fieldName: string, varName?: string, isLast?: boolean): string;
164
167
  /**
165
168
  * Generates code for updating collection permissions
166
169
  * Handles permission rule updates including manageRule for auth collections
@@ -169,9 +172,10 @@ declare function generateFieldDeletion(collectionName: string, fieldName: string
169
172
  * @param ruleType - Type of permission rule to update
170
173
  * @param newValue - New permission rule value
171
174
  * @param varName - Variable name to use for the collection (default: auto-generated)
175
+ * @param isLast - Whether this is the last operation (will return the result)
172
176
  * @returns JavaScript code for updating the permission
173
177
  */
174
- declare function generatePermissionUpdate(collectionName: string, ruleType: string, newValue: string | null, varName?: string): string;
178
+ declare function generatePermissionUpdate(collectionName: string, ruleType: string, newValue: string | null, varName?: string, isLast?: boolean): string;
175
179
  /**
176
180
  * Generates the up migration function code
177
181
  * Applies all changes from the diff in the correct order
@@ -131,7 +131,7 @@ declare function generateIndexesArray(indexes?: string[]): string;
131
131
  * @param varName - Variable name to use for the collection (default: 'collection')
132
132
  * @returns JavaScript code for creating the collection
133
133
  */
134
- declare function generateCollectionCreation(collection: CollectionSchema, varName?: string): string;
134
+ declare function generateCollectionCreation(collection: CollectionSchema, varName?: string, isLast?: boolean): string;
135
135
  /**
136
136
  * Generates code for adding a field to an existing collection
137
137
  * Uses the appropriate Field constructor based on field type
@@ -139,9 +139,10 @@ declare function generateCollectionCreation(collection: CollectionSchema, varNam
139
139
  * @param collectionName - Name of the collection
140
140
  * @param field - Field definition to add
141
141
  * @param varName - Variable name to use for the collection (default: auto-generated)
142
+ * @param isLast - Whether this is the last operation (will return the result)
142
143
  * @returns JavaScript code for adding the field
143
144
  */
144
- declare function generateFieldAddition(collectionName: string, field: FieldDefinition, varName?: string): string;
145
+ declare function generateFieldAddition(collectionName: string, field: FieldDefinition, varName?: string, isLast?: boolean): string;
145
146
  /**
146
147
  * Generates code for modifying an existing field
147
148
  * Updates field properties based on detected changes
@@ -149,18 +150,20 @@ declare function generateFieldAddition(collectionName: string, field: FieldDefin
149
150
  * @param collectionName - Name of the collection
150
151
  * @param modification - Field modification details
151
152
  * @param varName - Variable name to use for the collection (default: auto-generated)
153
+ * @param isLast - Whether this is the last operation (will return the result)
152
154
  * @returns JavaScript code for modifying the field
153
155
  */
154
- declare function generateFieldModification(collectionName: string, modification: FieldModification, varName?: string): string;
156
+ declare function generateFieldModification(collectionName: string, modification: FieldModification, varName?: string, isLast?: boolean): string;
155
157
  /**
156
158
  * Generates code for deleting a field from a collection
157
159
  *
158
160
  * @param collectionName - Name of the collection
159
161
  * @param fieldName - Name of the field to delete
160
162
  * @param varName - Variable name to use for the collection (default: auto-generated)
163
+ * @param isLast - Whether this is the last operation (will return the result)
161
164
  * @returns JavaScript code for deleting the field
162
165
  */
163
- declare function generateFieldDeletion(collectionName: string, fieldName: string, varName?: string): string;
166
+ declare function generateFieldDeletion(collectionName: string, fieldName: string, varName?: string, isLast?: boolean): string;
164
167
  /**
165
168
  * Generates code for updating collection permissions
166
169
  * Handles permission rule updates including manageRule for auth collections
@@ -169,9 +172,10 @@ declare function generateFieldDeletion(collectionName: string, fieldName: string
169
172
  * @param ruleType - Type of permission rule to update
170
173
  * @param newValue - New permission rule value
171
174
  * @param varName - Variable name to use for the collection (default: auto-generated)
175
+ * @param isLast - Whether this is the last operation (will return the result)
172
176
  * @returns JavaScript code for updating the permission
173
177
  */
174
- declare function generatePermissionUpdate(collectionName: string, ruleType: string, newValue: string | null, varName?: string): string;
178
+ declare function generatePermissionUpdate(collectionName: string, ruleType: string, newValue: string | null, varName?: string, isLast?: boolean): string;
175
179
  /**
176
180
  * Generates the up migration function code
177
181
  * Applies all changes from the diff in the correct order
@@ -80,10 +80,8 @@ Cause: ${this.originalError.message}`);
80
80
  var DEFAULT_TEMPLATE = `/// <reference path="{{TYPES_PATH}}" />
81
81
  migrate((app) => {
82
82
  {{UP_CODE}}
83
- return true;
84
83
  }, (app) => {
85
84
  {{DOWN_CODE}}
86
- return true;
87
85
  });
88
86
  `;
89
87
  var DEFAULT_CONFIG = {
@@ -248,7 +246,8 @@ function generateFieldDefinitionObject(field) {
248
246
  }
249
247
  }
250
248
  if (field.relation) {
251
- const collectionIdPlaceholder = field.relation.collection === "Users" ? '"_pb_users_auth_"' : `app.findCollectionByNameOrId("${field.relation.collection}").id`;
249
+ const isUsersCollection = field.relation.collection.toLowerCase() === "users";
250
+ const collectionIdPlaceholder = isUsersCollection ? '"_pb_users_auth_"' : `app.findCollectionByNameOrId("${field.relation.collection}").id`;
252
251
  parts.push(` collectionId: ${collectionIdPlaceholder}`);
253
252
  if (field.relation.maxSelect !== void 0) {
254
253
  parts.push(` maxSelect: ${field.relation.maxSelect}`);
@@ -332,7 +331,7 @@ function generateIndexesArray(indexes) {
332
331
  ${indexStrings.join(",\n ")},
333
332
  ]`;
334
333
  }
335
- function generateCollectionCreation(collection, varName = "collection") {
334
+ function generateCollectionCreation(collection, varName = "collection", isLast = false) {
336
335
  const lines = [];
337
336
  lines.push(` const ${varName} = new Collection({`);
338
337
  lines.push(` name: "${collection.name}",`);
@@ -348,7 +347,7 @@ function generateCollectionCreation(collection, varName = "collection") {
348
347
  lines.push(` indexes: ${generateIndexesArray(collection.indexes)},`);
349
348
  lines.push(` });`);
350
349
  lines.push(``);
351
- lines.push(` app.save(${varName});`);
350
+ lines.push(isLast ? ` return app.save(${varName});` : ` app.save(${varName});`);
352
351
  return lines.join("\n");
353
352
  }
354
353
  function getFieldConstructorName(fieldType) {
@@ -379,7 +378,8 @@ function generateFieldConstructorOptions(field) {
379
378
  }
380
379
  }
381
380
  if (field.relation && field.type === "relation") {
382
- const collectionIdPlaceholder = field.relation.collection === "Users" ? '"_pb_users_auth_"' : `app.findCollectionByNameOrId("${field.relation.collection}").id`;
381
+ const isUsersCollection = field.relation.collection.toLowerCase() === "users";
382
+ const collectionIdPlaceholder = isUsersCollection ? '"_pb_users_auth_"' : `app.findCollectionByNameOrId("${field.relation.collection}").id`;
383
383
  parts.push(` collectionId: ${collectionIdPlaceholder}`);
384
384
  if (field.relation.maxSelect !== void 0) {
385
385
  parts.push(` maxSelect: ${field.relation.maxSelect}`);
@@ -393,7 +393,7 @@ function generateFieldConstructorOptions(field) {
393
393
  }
394
394
  return parts.join(",\n");
395
395
  }
396
- function generateFieldAddition(collectionName, field, varName) {
396
+ function generateFieldAddition(collectionName, field, varName, isLast = false) {
397
397
  const lines = [];
398
398
  const constructorName = getFieldConstructorName(field.type);
399
399
  const collectionVar = varName || `collection_${collectionName}_${field.name}`;
@@ -403,10 +403,10 @@ function generateFieldAddition(collectionName, field, varName) {
403
403
  lines.push(generateFieldConstructorOptions(field));
404
404
  lines.push(` }));`);
405
405
  lines.push(``);
406
- lines.push(` app.save(${collectionVar});`);
406
+ lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
407
407
  return lines.join("\n");
408
408
  }
409
- function generateFieldModification(collectionName, modification, varName) {
409
+ function generateFieldModification(collectionName, modification, varName, isLast = false) {
410
410
  const lines = [];
411
411
  const collectionVar = varName || `collection_${collectionName}_${modification.fieldName}`;
412
412
  const fieldVar = `${collectionVar}_field`;
@@ -420,7 +420,8 @@ function generateFieldModification(collectionName, modification, varName) {
420
420
  } else if (change.property.startsWith("relation.")) {
421
421
  const relationKey = change.property.replace("relation.", "");
422
422
  if (relationKey === "collection") {
423
- const collectionIdValue = change.newValue === "Users" ? '"_pb_users_auth_"' : `app.findCollectionByNameOrId("${change.newValue}").id`;
423
+ const isUsersCollection = String(change.newValue).toLowerCase() === "users";
424
+ const collectionIdValue = isUsersCollection ? '"_pb_users_auth_"' : `app.findCollectionByNameOrId("${change.newValue}").id`;
424
425
  lines.push(` ${fieldVar}.collectionId = ${collectionIdValue};`);
425
426
  } else {
426
427
  lines.push(` ${fieldVar}.${relationKey} = ${formatValue(change.newValue)};`);
@@ -430,10 +431,10 @@ function generateFieldModification(collectionName, modification, varName) {
430
431
  }
431
432
  }
432
433
  lines.push(``);
433
- lines.push(` app.save(${collectionVar});`);
434
+ lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
434
435
  return lines.join("\n");
435
436
  }
436
- function generateFieldDeletion(collectionName, fieldName, varName) {
437
+ function generateFieldDeletion(collectionName, fieldName, varName, isLast = false) {
437
438
  const lines = [];
438
439
  const collectionVar = varName || `collection_${collectionName}_${fieldName}`;
439
440
  const fieldVar = `${collectionVar}_field`;
@@ -442,18 +443,18 @@ function generateFieldDeletion(collectionName, fieldName, varName) {
442
443
  lines.push(``);
443
444
  lines.push(` ${collectionVar}.fields.remove(${fieldVar}.id);`);
444
445
  lines.push(``);
445
- lines.push(` app.save(${collectionVar});`);
446
+ lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
446
447
  return lines.join("\n");
447
448
  }
448
- function generateIndexAddition(collectionName, index, varName) {
449
+ function generateIndexAddition(collectionName, index, varName, isLast = false) {
449
450
  const lines = [];
450
451
  const collectionVar = varName || `collection_${collectionName}_idx`;
451
452
  lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
452
453
  lines.push(` ${collectionVar}.indexes.push("${index}");`);
453
- lines.push(` app.save(${collectionVar});`);
454
+ lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
454
455
  return lines.join("\n");
455
456
  }
456
- function generateIndexRemoval(collectionName, index, varName) {
457
+ function generateIndexRemoval(collectionName, index, varName, isLast = false) {
457
458
  const lines = [];
458
459
  const collectionVar = varName || `collection_${collectionName}_idx`;
459
460
  const indexVar = `${collectionVar}_indexToRemove`;
@@ -462,29 +463,29 @@ function generateIndexRemoval(collectionName, index, varName) {
462
463
  lines.push(` if (${indexVar} !== -1) {`);
463
464
  lines.push(` ${collectionVar}.indexes.splice(${indexVar}, 1);`);
464
465
  lines.push(` }`);
465
- lines.push(` app.save(${collectionVar});`);
466
+ lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
466
467
  return lines.join("\n");
467
468
  }
468
- function generateRuleUpdate(collectionName, ruleType, newValue, varName) {
469
+ function generateRuleUpdate(collectionName, ruleType, newValue, varName, isLast = false) {
469
470
  const lines = [];
470
471
  const collectionVar = varName || `collection_${collectionName}_${ruleType}`;
471
472
  lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
472
473
  lines.push(` ${collectionVar}.${ruleType} = ${formatValue(newValue)};`);
473
- lines.push(` app.save(${collectionVar});`);
474
+ lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
474
475
  return lines.join("\n");
475
476
  }
476
- function generatePermissionUpdate(collectionName, ruleType, newValue, varName) {
477
+ function generatePermissionUpdate(collectionName, ruleType, newValue, varName, isLast = false) {
477
478
  const lines = [];
478
479
  const collectionVar = varName || `collection_${collectionName}_${ruleType}`;
479
480
  lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
480
481
  lines.push(` ${collectionVar}.${ruleType} = ${formatValue(newValue)};`);
481
- lines.push(` app.save(${collectionVar});`);
482
+ lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
482
483
  return lines.join("\n");
483
484
  }
484
- function generateCollectionDeletion(collectionName, varName = "collection") {
485
+ function generateCollectionDeletion(collectionName, varName = "collection", isLast = false) {
485
486
  const lines = [];
486
487
  lines.push(` const ${varName} = app.findCollectionByNameOrId("${collectionName}");`);
487
- lines.push(` app.delete(${varName});`);
488
+ lines.push(isLast ? ` return app.delete(${varName});` : ` app.delete(${varName});`);
488
489
  return lines.join("\n");
489
490
  }
490
491
  function generateUpMigration(diff) {
@@ -576,7 +577,24 @@ function generateUpMigration(diff) {
576
577
  lines.push(` // No changes detected`);
577
578
  lines.push(``);
578
579
  }
579
- return lines.join("\n");
580
+ let code = lines.join("\n");
581
+ const savePattern = /^(\s*)app\.save\((\w+)\);$/gm;
582
+ const deletePattern = /^(\s*)app\.delete\((\w+)\);$/gm;
583
+ const saveMatches = [...code.matchAll(savePattern)];
584
+ const deleteMatches = [...code.matchAll(deletePattern)];
585
+ const allMatches = [
586
+ ...saveMatches.map((m) => ({ match: m, type: "save", index: m.index })),
587
+ ...deleteMatches.map((m) => ({ match: m, type: "delete", index: m.index }))
588
+ ].sort((a, b) => b.index - a.index);
589
+ if (allMatches.length > 0) {
590
+ const lastMatch = allMatches[0];
591
+ if (lastMatch.type === "save") {
592
+ 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);
593
+ } else {
594
+ 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);
595
+ }
596
+ }
597
+ return code;
580
598
  }
581
599
  function generateDownMigration(diff) {
582
600
  const lines = [];
@@ -678,7 +696,24 @@ function generateDownMigration(diff) {
678
696
  lines.push(` // No changes to revert`);
679
697
  lines.push(``);
680
698
  }
681
- return lines.join("\n");
699
+ let code = lines.join("\n");
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
+ }
715
+ }
716
+ return code;
682
717
  }
683
718
  function generate(diff, config) {
684
719
  const normalizedConfig = typeof config === "string" ? { migrationDir: config } : config;