pocketbase-zod-schema 0.2.5 → 0.3.0

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 (66) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/cli/index.cjs +374 -296
  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 +374 -296
  7. package/dist/cli/index.js.map +1 -1
  8. package/dist/cli/migrate.cjs +374 -296
  9. package/dist/cli/migrate.cjs.map +1 -1
  10. package/dist/cli/migrate.js +374 -296
  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-YjcpBXVp.d.cts → fields-UcOPu1OQ.d.cts} +16 -0
  15. package/dist/{fields-YjcpBXVp.d.ts → fields-UcOPu1OQ.d.ts} +16 -0
  16. package/dist/index.cjs +413 -114
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.cts +4 -4
  19. package/dist/index.d.ts +4 -4
  20. package/dist/index.js +414 -103
  21. package/dist/index.js.map +1 -1
  22. package/dist/migration/analyzer.cjs +12 -2
  23. package/dist/migration/analyzer.cjs.map +1 -1
  24. package/dist/migration/analyzer.d.cts +2 -2
  25. package/dist/migration/analyzer.d.ts +2 -2
  26. package/dist/migration/analyzer.js +12 -2
  27. package/dist/migration/analyzer.js.map +1 -1
  28. package/dist/migration/diff.cjs +76 -1
  29. package/dist/migration/diff.cjs.map +1 -1
  30. package/dist/migration/diff.d.cts +2 -2
  31. package/dist/migration/diff.d.ts +2 -2
  32. package/dist/migration/diff.js +76 -1
  33. package/dist/migration/diff.js.map +1 -1
  34. package/dist/migration/generator.cjs +323 -46
  35. package/dist/migration/generator.cjs.map +1 -1
  36. package/dist/migration/generator.d.cts +59 -12
  37. package/dist/migration/generator.d.ts +59 -12
  38. package/dist/migration/generator.js +319 -47
  39. package/dist/migration/generator.js.map +1 -1
  40. package/dist/migration/index.cjs +399 -49
  41. package/dist/migration/index.cjs.map +1 -1
  42. package/dist/migration/index.d.cts +3 -3
  43. package/dist/migration/index.d.ts +3 -3
  44. package/dist/migration/index.js +399 -49
  45. package/dist/migration/index.js.map +1 -1
  46. package/dist/migration/snapshot.cjs.map +1 -1
  47. package/dist/migration/snapshot.d.cts +2 -2
  48. package/dist/migration/snapshot.d.ts +2 -2
  49. package/dist/migration/snapshot.js.map +1 -1
  50. package/dist/migration/utils/index.cjs +64 -0
  51. package/dist/migration/utils/index.cjs.map +1 -1
  52. package/dist/migration/utils/index.d.cts +39 -202
  53. package/dist/migration/utils/index.d.ts +39 -202
  54. package/dist/migration/utils/index.js +63 -1
  55. package/dist/migration/utils/index.js.map +1 -1
  56. package/dist/schema.cjs +0 -61
  57. package/dist/schema.cjs.map +1 -1
  58. package/dist/schema.d.cts +2 -86
  59. package/dist/schema.d.ts +2 -86
  60. package/dist/schema.js +1 -50
  61. package/dist/schema.js.map +1 -1
  62. package/dist/type-mapper-DrQmtznD.d.cts +208 -0
  63. package/dist/type-mapper-n231Fspm.d.ts +208 -0
  64. package/dist/{types-LFBGHl9Y.d.ts → types-Ds3NQvny.d.ts} +33 -2
  65. package/dist/{types-mhQXWNi3.d.cts → types-YoBjsa-A.d.cts} +33 -2
  66. package/package.json +1 -1
@@ -1,209 +1,46 @@
1
- import { z } from 'zod';
2
- import { P as PocketBaseFieldType } from '../../fields-YjcpBXVp.js';
3
- export { F as FIELD_METADATA_KEY, a as FieldMetadata, e as extractFieldMetadata } from '../../fields-YjcpBXVp.js';
1
+ export { E as ExtractedFieldOptions, d as FIELD_TYPE_INFO, F as FieldTypeInfo, C as FieldTypeResult, P as POCKETBASE_FIELD_TYPES, z as extractComprehensiveFieldOptions, o as extractFieldOptions, x as getArrayElementType, v as getDefaultValue, D as getFieldTypeInfo, g as getMaxSelect, c as getMinSelect, w as isArrayType, A as isEditorField, q as isFieldRequired, B as isFileFieldByName, y as isGeoPointType, a as isMultipleRelationField, b as isRelationField, i as isSingleRelationField, j as mapZodArrayType, f as mapZodBooleanType, k as mapZodDateType, h as mapZodEnumType, e as mapZodNumberType, l as mapZodRecordType, m as mapZodStringType, n as mapZodTypeToPocketBase, p as pluralize, r as resolveTargetCollection, s as singularize, t as toCollectionName, u as unwrapZodType } from '../../type-mapper-n231Fspm.js';
2
+ export { F as FIELD_METADATA_KEY, a as FieldMetadata, e as extractFieldMetadata } from '../../fields-UcOPu1OQ.js';
3
+ import 'zod';
4
4
 
5
5
  /**
6
- * Collection name pluralization utilities
6
+ * Collection ID generation utilities for PocketBase migrations
7
7
  */
8
8
  /**
9
- * Pluralizes a singular noun to its plural form
10
- * Handles special cases and common English pluralization rules
9
+ * Generates a unique collection ID in PocketBase format
10
+ * Format: pb_ followed by 15 alphanumeric lowercase characters
11
11
  *
12
- * @param singular - The singular form of the noun
13
- * @returns The plural form of the noun
14
- */
15
- declare function pluralize(singular: string): string;
16
- /**
17
- * Converts a singular entity name to a collection name
18
- * This is an alias for pluralize for better semantic clarity
19
- *
20
- * @param entityName - The singular entity name (e.g., "User", "Project")
21
- * @returns The collection name (e.g., "Users", "Projects")
22
- */
23
- declare function toCollectionName(entityName: string): string;
24
- /**
25
- * Attempts to singularize a plural noun (reverse of pluralize)
26
- * Note: This is a best-effort implementation and may not handle all edge cases
27
- *
28
- * @param plural - The plural form of the noun
29
- * @returns The singular form of the noun
30
- */
31
- declare function singularize(plural: string): string;
32
-
33
- /**
34
- * Relation field detection utilities
35
- */
36
-
37
- /**
38
- * Detects if a field is a single relation based on naming convention
39
- * Single relation: field name matches a collection name (e.g., "User" -> "Users" collection)
40
- */
41
- declare function isSingleRelationField(fieldName: string, zodType: z.ZodTypeAny): boolean;
42
- /**
43
- * Detects if a field is a multiple relation based on naming convention
44
- * Multiple relation: field name is an array of strings ending with entity name
45
- * (e.g., "SubscriberUsers" -> "Users" collection)
46
- */
47
- declare function isMultipleRelationField(fieldName: string, zodType: z.ZodTypeAny): boolean;
48
- /**
49
- * Resolves the target collection name from a relation field name
50
- * Examples:
51
- * - "User" -> "Users"
52
- * - "SubscriberUsers" -> "Users"
53
- * - "Author" -> "Authors"
54
- * - "Category" -> "Categories"
55
- */
56
- declare function resolveTargetCollection(fieldName: string): string;
57
- /**
58
- * Detects if a field is any type of relation (single or multiple)
59
- */
60
- declare function isRelationField(fieldName: string, zodType: z.ZodTypeAny): boolean;
61
- /**
62
- * Gets the maximum number of relations allowed for a relation field
63
- * Returns 1 for single relations, or the max constraint for multiple relations
64
- */
65
- declare function getMaxSelect(fieldName: string, zodType: z.ZodTypeAny): number;
66
- /**
67
- * Gets the minimum number of relations required for a relation field
68
- * Returns 0 as default for all relation fields (single or multiple)
69
- * PocketBase always expects minSelect to be defined for relation fields
70
- */
71
- declare function getMinSelect(fieldName: string, zodType: z.ZodTypeAny): number;
72
-
73
- /**
74
- * Zod to PocketBase type mapping utilities
75
- *
76
- * This module provides comprehensive mapping between Zod schema types
77
- * and PocketBase field types, including support for all PocketBase field types:
78
- * - text, email, url, editor
79
- * - number, bool
80
- * - date, autodate
81
- * - select (single/multiple)
82
- * - relation (single/multiple)
83
- * - file (single/multiple)
84
- * - json
85
- * - geoPoint
86
- */
87
-
88
- /**
89
- * All supported PocketBase field types
90
- */
91
- declare const POCKETBASE_FIELD_TYPES: readonly PocketBaseFieldType[];
92
- /**
93
- * Field type metadata for documentation and validation
94
- */
95
- interface FieldTypeInfo {
96
- type: PocketBaseFieldType;
97
- description: string;
98
- zodTypes: string[];
99
- supportsMultiple: boolean;
100
- }
101
- /**
102
- * Metadata about each PocketBase field type
103
- */
104
- declare const FIELD_TYPE_INFO: Record<PocketBaseFieldType, FieldTypeInfo>;
105
- /**
106
- * Maps Zod string types to PocketBase field types
107
- */
108
- declare function mapZodStringType(zodType: z.ZodString): PocketBaseFieldType;
109
- /**
110
- * Maps Zod number types to PocketBase number type
111
- */
112
- declare function mapZodNumberType(_zodType: z.ZodNumber): PocketBaseFieldType;
113
- /**
114
- * Maps Zod boolean types to PocketBase bool type
115
- */
116
- declare function mapZodBooleanType(_zodType: z.ZodBoolean): PocketBaseFieldType;
117
- /**
118
- * Maps Zod enum types to PocketBase select type
119
- */
120
- declare function mapZodEnumType(_zodType: z.ZodEnum<any>): PocketBaseFieldType;
121
- /**
122
- * Maps Zod array types to appropriate PocketBase types
123
- * Arrays of strings could be relations or file fields depending on context
124
- */
125
- declare function mapZodArrayType(zodType: z.ZodArray<any>, _fieldName: string): PocketBaseFieldType;
126
- /**
127
- * Maps Zod date types to PocketBase date type
128
- */
129
- declare function mapZodDateType(_zodType: z.ZodDate): PocketBaseFieldType;
130
- /**
131
- * Maps Zod record/object types to PocketBase JSON type
132
- */
133
- declare function mapZodRecordType(_zodType: z.ZodRecord | z.ZodObject<any>): PocketBaseFieldType;
134
- /**
135
- * Main type mapping function that determines PocketBase field type from Zod type
136
- */
137
- declare function mapZodTypeToPocketBase(zodType: z.ZodTypeAny, fieldName: string): PocketBaseFieldType;
138
- /**
139
- * Extracts field options from Zod type (min, max, pattern, etc.)
140
- */
141
- declare function extractFieldOptions(zodType: z.ZodTypeAny): Record<string, any>;
142
- /**
143
- * Determines if a Zod field is required (not optional)
144
- */
145
- declare function isFieldRequired(zodType: z.ZodTypeAny): boolean;
146
- /**
147
- * Unwraps a Zod type to get the inner type
148
- * Handles optional, nullable, and default wrappers
149
- */
150
- declare function unwrapZodType(zodType: z.ZodTypeAny): z.ZodTypeAny;
151
- /**
152
- * Gets the default value from a Zod type if it has one
153
- */
154
- declare function getDefaultValue(zodType: z.ZodTypeAny): any;
155
- /**
156
- * Checks if a Zod type is an array type
157
- */
158
- declare function isArrayType(zodType: z.ZodTypeAny): boolean;
159
- /**
160
- * Gets the element type of an array Zod type
161
- */
162
- declare function getArrayElementType(zodType: z.ZodTypeAny): z.ZodTypeAny | null;
163
- /**
164
- * Checks if a Zod type represents a geo point (object with lon/lat)
165
- */
166
- declare function isGeoPointType(zodType: z.ZodTypeAny): boolean;
167
- /**
168
- * Complete field options extracted from a Zod type
169
- */
170
- interface ExtractedFieldOptions {
171
- min?: number;
172
- max?: number;
173
- pattern?: string;
174
- values?: string[];
175
- minSelect?: number;
176
- maxSelect?: number;
177
- mimeTypes?: string[];
178
- maxSize?: number;
179
- thumbs?: string[];
12
+ * @returns A unique collection ID string (e.g., "pb_a1b2c3d4e5f6g7h")
13
+ */
14
+ declare function generateCollectionId(): string;
15
+ /**
16
+ * Registry to track generated collection IDs and ensure uniqueness within a migration batch
17
+ */
18
+ declare class CollectionIdRegistry {
19
+ private ids;
20
+ constructor();
21
+ /**
22
+ * Generates a unique collection ID for a given collection name
23
+ * Special case: Returns constant "_pb_users_auth_" for users collection
24
+ * Retries up to 10 times if collision occurs (extremely rare)
25
+ *
26
+ * @param collectionName - The name of the collection
27
+ * @returns A unique collection ID
28
+ * @throws Error if unable to generate unique ID after max attempts
29
+ */
30
+ generate(collectionName?: string): string;
31
+ /**
32
+ * Checks if an ID has already been registered
33
+ *
34
+ * @param id - The collection ID to check
35
+ * @returns True if the ID exists in the registry
36
+ */
37
+ has(id: string): boolean;
38
+ /**
39
+ * Registers a collection ID in the registry
40
+ *
41
+ * @param id - The collection ID to register
42
+ */
43
+ register(id: string): void;
180
44
  }
181
- /**
182
- * Extracts comprehensive field options from Zod type
183
- * Includes all constraints that can be mapped to PocketBase field options
184
- */
185
- declare function extractComprehensiveFieldOptions(zodType: z.ZodTypeAny): ExtractedFieldOptions;
186
- /**
187
- * Determines if a field should be treated as an editor field
188
- * based on field name conventions
189
- */
190
- declare function isEditorField(fieldName: string): boolean;
191
- /**
192
- * Determines if a field should be treated as a file field
193
- * based on field name conventions
194
- */
195
- declare function isFileFieldByName(fieldName: string): boolean;
196
- /**
197
- * Gets the PocketBase field type with additional context
198
- */
199
- interface FieldTypeResult {
200
- type: PocketBaseFieldType;
201
- isMultiple: boolean;
202
- options: ExtractedFieldOptions;
203
- }
204
- /**
205
- * Comprehensive type mapping that returns full field information
206
- */
207
- declare function getFieldTypeInfo(zodType: z.ZodTypeAny, fieldName: string): FieldTypeResult;
208
45
 
209
- export { type ExtractedFieldOptions, FIELD_TYPE_INFO, type FieldTypeInfo, type FieldTypeResult, POCKETBASE_FIELD_TYPES, extractComprehensiveFieldOptions, extractFieldOptions, getArrayElementType, getDefaultValue, getFieldTypeInfo, getMaxSelect, getMinSelect, isArrayType, isEditorField, isFieldRequired, isFileFieldByName, isGeoPointType, isMultipleRelationField, isRelationField, isSingleRelationField, mapZodArrayType, mapZodBooleanType, mapZodDateType, mapZodEnumType, mapZodNumberType, mapZodRecordType, mapZodStringType, mapZodTypeToPocketBase, pluralize, resolveTargetCollection, singularize, toCollectionName, unwrapZodType };
46
+ export { CollectionIdRegistry, generateCollectionId };
@@ -1,5 +1,67 @@
1
+ import { randomBytes } from 'crypto';
1
2
  import { z } from 'zod';
2
3
 
4
+ // src/migration/utils/collection-id-generator.ts
5
+ function generateCollectionId() {
6
+ const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
7
+ const idLength = 15;
8
+ const bytes = randomBytes(idLength);
9
+ let id = "pb_";
10
+ for (let i = 0; i < idLength; i++) {
11
+ const index = bytes[i] % chars.length;
12
+ id += chars[index];
13
+ }
14
+ return id;
15
+ }
16
+ var CollectionIdRegistry = class {
17
+ ids;
18
+ constructor() {
19
+ this.ids = /* @__PURE__ */ new Set();
20
+ }
21
+ /**
22
+ * Generates a unique collection ID for a given collection name
23
+ * Special case: Returns constant "_pb_users_auth_" for users collection
24
+ * Retries up to 10 times if collision occurs (extremely rare)
25
+ *
26
+ * @param collectionName - The name of the collection
27
+ * @returns A unique collection ID
28
+ * @throws Error if unable to generate unique ID after max attempts
29
+ */
30
+ generate(collectionName) {
31
+ if (collectionName && collectionName.toLowerCase() === "users") {
32
+ const usersId = "_pb_users_auth_";
33
+ this.register(usersId);
34
+ return usersId;
35
+ }
36
+ const maxAttempts = 10;
37
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
38
+ const id = generateCollectionId();
39
+ if (!this.has(id)) {
40
+ this.register(id);
41
+ return id;
42
+ }
43
+ }
44
+ throw new Error("Failed to generate unique collection ID after maximum attempts");
45
+ }
46
+ /**
47
+ * Checks if an ID has already been registered
48
+ *
49
+ * @param id - The collection ID to check
50
+ * @returns True if the ID exists in the registry
51
+ */
52
+ has(id) {
53
+ return this.ids.has(id);
54
+ }
55
+ /**
56
+ * Registers a collection ID in the registry
57
+ *
58
+ * @param id - The collection ID to register
59
+ */
60
+ register(id) {
61
+ this.ids.add(id);
62
+ }
63
+ };
64
+
3
65
  // src/migration/utils/pluralize.ts
4
66
  var SPECIAL_CASES = {
5
67
  // Common irregular plurals
@@ -652,6 +714,6 @@ function getFieldTypeInfo(zodType, fieldName) {
652
714
  };
653
715
  }
654
716
 
655
- export { FIELD_METADATA_KEY, FIELD_TYPE_INFO, POCKETBASE_FIELD_TYPES, extractComprehensiveFieldOptions, extractFieldMetadata, extractFieldOptions, getArrayElementType, getDefaultValue, getFieldTypeInfo, getMaxSelect, getMinSelect, isArrayType, isEditorField, isFieldRequired, isFileFieldByName, isGeoPointType, isMultipleRelationField, isRelationField, isSingleRelationField, mapZodArrayType, mapZodBooleanType, mapZodDateType, mapZodEnumType, mapZodNumberType, mapZodRecordType, mapZodStringType, mapZodTypeToPocketBase, pluralize, resolveTargetCollection, singularize, toCollectionName, unwrapZodType };
717
+ export { CollectionIdRegistry, FIELD_METADATA_KEY, FIELD_TYPE_INFO, POCKETBASE_FIELD_TYPES, extractComprehensiveFieldOptions, extractFieldMetadata, extractFieldOptions, generateCollectionId, getArrayElementType, getDefaultValue, getFieldTypeInfo, getMaxSelect, getMinSelect, isArrayType, isEditorField, isFieldRequired, isFileFieldByName, isGeoPointType, isMultipleRelationField, isRelationField, isSingleRelationField, mapZodArrayType, mapZodBooleanType, mapZodDateType, mapZodEnumType, mapZodNumberType, mapZodRecordType, mapZodStringType, mapZodTypeToPocketBase, pluralize, resolveTargetCollection, singularize, toCollectionName, unwrapZodType };
656
718
  //# sourceMappingURL=index.js.map
657
719
  //# sourceMappingURL=index.js.map