pocketbase-zod-schema 0.2.4 → 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 (67) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +209 -24
  3. package/dist/cli/index.cjs +406 -294
  4. package/dist/cli/index.cjs.map +1 -1
  5. package/dist/cli/index.d.cts +3 -1
  6. package/dist/cli/index.d.ts +3 -1
  7. package/dist/cli/index.js +406 -294
  8. package/dist/cli/index.js.map +1 -1
  9. package/dist/cli/migrate.cjs +406 -294
  10. package/dist/cli/migrate.cjs.map +1 -1
  11. package/dist/cli/migrate.js +406 -294
  12. package/dist/cli/migrate.js.map +1 -1
  13. package/dist/cli/utils/index.d.cts +3 -1
  14. package/dist/cli/utils/index.d.ts +3 -1
  15. package/dist/fields-UcOPu1OQ.d.cts +364 -0
  16. package/dist/fields-UcOPu1OQ.d.ts +364 -0
  17. package/dist/index.cjs +633 -112
  18. package/dist/index.cjs.map +1 -1
  19. package/dist/index.d.cts +4 -3
  20. package/dist/index.d.ts +4 -3
  21. package/dist/index.js +619 -101
  22. package/dist/index.js.map +1 -1
  23. package/dist/migration/analyzer.cjs +44 -0
  24. package/dist/migration/analyzer.cjs.map +1 -1
  25. package/dist/migration/analyzer.d.cts +2 -1
  26. package/dist/migration/analyzer.d.ts +2 -1
  27. package/dist/migration/analyzer.js +44 -0
  28. package/dist/migration/analyzer.js.map +1 -1
  29. package/dist/migration/diff.cjs +76 -1
  30. package/dist/migration/diff.cjs.map +1 -1
  31. package/dist/migration/diff.d.cts +3 -1
  32. package/dist/migration/diff.d.ts +3 -1
  33. package/dist/migration/diff.js +76 -1
  34. package/dist/migration/diff.js.map +1 -1
  35. package/dist/migration/generator.cjs +323 -46
  36. package/dist/migration/generator.cjs.map +1 -1
  37. package/dist/migration/generator.d.cts +60 -11
  38. package/dist/migration/generator.d.ts +60 -11
  39. package/dist/migration/generator.js +319 -47
  40. package/dist/migration/generator.js.map +1 -1
  41. package/dist/migration/index.cjs +433 -47
  42. package/dist/migration/index.cjs.map +1 -1
  43. package/dist/migration/index.d.cts +3 -2
  44. package/dist/migration/index.d.ts +3 -2
  45. package/dist/migration/index.js +432 -48
  46. package/dist/migration/index.js.map +1 -1
  47. package/dist/migration/snapshot.cjs.map +1 -1
  48. package/dist/migration/snapshot.d.cts +3 -1
  49. package/dist/migration/snapshot.d.ts +3 -1
  50. package/dist/migration/snapshot.js.map +1 -1
  51. package/dist/migration/utils/index.cjs +80 -0
  52. package/dist/migration/utils/index.cjs.map +1 -1
  53. package/dist/migration/utils/index.d.cts +39 -202
  54. package/dist/migration/utils/index.d.ts +39 -202
  55. package/dist/migration/utils/index.js +77 -1
  56. package/dist/migration/utils/index.js.map +1 -1
  57. package/dist/schema.cjs +200 -61
  58. package/dist/schema.cjs.map +1 -1
  59. package/dist/schema.d.cts +2 -85
  60. package/dist/schema.d.ts +2 -85
  61. package/dist/schema.js +186 -50
  62. package/dist/schema.js.map +1 -1
  63. package/dist/type-mapper-DrQmtznD.d.cts +208 -0
  64. package/dist/type-mapper-n231Fspm.d.ts +208 -0
  65. package/dist/{types-z1Dkjg8m.d.ts → types-Ds3NQvny.d.ts} +33 -2
  66. package/dist/{types-BbTgmg6H.d.cts → types-YoBjsa-A.d.cts} +33 -2
  67. package/package.json +1 -1
@@ -0,0 +1,364 @@
1
+ import { z } from 'zod';
2
+
3
+ /**
4
+ * Internal marker for field metadata
5
+ * Used by the migration generator to detect explicit field type definitions
6
+ */
7
+ declare const FIELD_METADATA_KEY = "__pocketbase_field__";
8
+ /**
9
+ * PocketBase field types
10
+ */
11
+ type PocketBaseFieldType = "text" | "email" | "url" | "editor" | "number" | "bool" | "date" | "autodate" | "select" | "relation" | "file" | "json" | "geoPoint";
12
+ /**
13
+ * Field metadata structure embedded in Zod schema descriptions
14
+ */
15
+ interface FieldMetadata {
16
+ type: PocketBaseFieldType;
17
+ options?: Record<string, any>;
18
+ }
19
+ /**
20
+ * Extracts field metadata from a Zod type's description
21
+ * Used by the migration generator to detect explicit field type definitions
22
+ *
23
+ * @param description - The Zod type's description string
24
+ * @returns Field metadata if present, null otherwise
25
+ *
26
+ * @example
27
+ * const schema = TextField({ min: 1, max: 100 });
28
+ * const metadata = extractFieldMetadata(schema.description);
29
+ * // Returns: { type: "text", options: { min: 1, max: 100 } }
30
+ */
31
+ declare function extractFieldMetadata(description: string | undefined): FieldMetadata | null;
32
+ /**
33
+ * Text field configuration options
34
+ */
35
+ interface TextFieldOptions {
36
+ /**
37
+ * Minimum length constraint
38
+ */
39
+ min?: number;
40
+ /**
41
+ * Maximum length constraint
42
+ */
43
+ max?: number;
44
+ /**
45
+ * Pattern constraint (regex)
46
+ */
47
+ pattern?: RegExp | string;
48
+ /**
49
+ * Auto-generate pattern for automatic value generation
50
+ * Example: "[A-Z]{3}-[0-9]{6}" generates values like "ABC-123456"
51
+ */
52
+ autogeneratePattern?: string;
53
+ }
54
+ /**
55
+ * Number field configuration options
56
+ */
57
+ interface NumberFieldOptions {
58
+ /**
59
+ * Minimum value constraint
60
+ */
61
+ min?: number;
62
+ /**
63
+ * Maximum value constraint
64
+ */
65
+ max?: number;
66
+ /**
67
+ * Whether to disallow decimal values (integers only)
68
+ */
69
+ noDecimal?: boolean;
70
+ /**
71
+ * Whether the field is required
72
+ * @default false
73
+ *
74
+ * Note: In PocketBase, `required: true` for number fields means the value must be non-zero.
75
+ * If you want to allow zero values (e.g., for progress: 0-100), keep this as `false`.
76
+ * Set to `true` only if you want to enforce non-zero values.
77
+ */
78
+ required?: boolean;
79
+ }
80
+ /**
81
+ * Date field configuration options
82
+ */
83
+ interface DateFieldOptions {
84
+ /**
85
+ * Minimum date constraint
86
+ */
87
+ min?: Date | string;
88
+ /**
89
+ * Maximum date constraint
90
+ */
91
+ max?: Date | string;
92
+ }
93
+ /**
94
+ * Autodate field configuration options
95
+ */
96
+ interface AutodateFieldOptions {
97
+ /**
98
+ * Set date automatically on record creation
99
+ * @default false
100
+ */
101
+ onCreate?: boolean;
102
+ /**
103
+ * Update date automatically on record update
104
+ * @default false
105
+ */
106
+ onUpdate?: boolean;
107
+ }
108
+ /**
109
+ * Select field configuration options
110
+ */
111
+ interface SelectFieldOptions {
112
+ /**
113
+ * Maximum number of selections allowed
114
+ * If > 1, enables multiple selection
115
+ * @default 1
116
+ */
117
+ maxSelect?: number;
118
+ }
119
+ /**
120
+ * File field configuration options
121
+ */
122
+ interface FileFieldOptions {
123
+ /**
124
+ * Allowed MIME types
125
+ * Example: ["image/*", "application/pdf"]
126
+ */
127
+ mimeTypes?: string[];
128
+ /**
129
+ * Maximum file size in bytes
130
+ */
131
+ maxSize?: number;
132
+ /**
133
+ * Thumbnail sizes to generate
134
+ * Example: ["100x100", "200x200"]
135
+ */
136
+ thumbs?: string[];
137
+ /**
138
+ * Whether the file is protected (requires auth to access)
139
+ * @default false
140
+ */
141
+ protected?: boolean;
142
+ }
143
+ /**
144
+ * Multiple files field configuration options
145
+ */
146
+ interface FilesFieldOptions extends FileFieldOptions {
147
+ /**
148
+ * Minimum number of files required
149
+ */
150
+ minSelect?: number;
151
+ /**
152
+ * Maximum number of files allowed
153
+ */
154
+ maxSelect?: number;
155
+ }
156
+ /**
157
+ * Creates a boolean field schema
158
+ * Maps to PocketBase 'bool' field type
159
+ *
160
+ * @returns Zod boolean schema with PocketBase metadata
161
+ *
162
+ * @example
163
+ * const ProductSchema = z.object({
164
+ * active: BoolField(),
165
+ * featured: BoolField().optional(),
166
+ * });
167
+ */
168
+ declare function BoolField(): z.ZodBoolean;
169
+ /**
170
+ * Creates a number field schema with optional constraints
171
+ * Maps to PocketBase 'number' field type
172
+ *
173
+ * @param options - Optional constraints for the number field
174
+ * @returns Zod number schema with PocketBase metadata
175
+ *
176
+ * @example
177
+ * const ProductSchema = z.object({
178
+ * price: NumberField({ min: 0 }),
179
+ * quantity: NumberField({ min: 0, noDecimal: true }),
180
+ * rating: NumberField({ min: 0, max: 5 }),
181
+ * progress: NumberField({ min: 0, max: 100 }), // required defaults to false, allowing zero
182
+ * score: NumberField({ min: 1, max: 10, required: true }), // requires non-zero value
183
+ * });
184
+ *
185
+ * @remarks
186
+ * By default, number fields are not required (`required: false`), which allows zero values.
187
+ * In PocketBase, `required: true` for number fields means the value must be non-zero.
188
+ * If you set `min: 0` and want to allow zero, keep `required: false` (the default).
189
+ */
190
+ declare function NumberField(options?: NumberFieldOptions): z.ZodNumber;
191
+ /**
192
+ * Creates a text field schema with optional constraints
193
+ * Maps to PocketBase 'text' field type
194
+ *
195
+ * @param options - Optional constraints for the text field
196
+ * @returns Zod string schema with PocketBase metadata
197
+ *
198
+ * @example
199
+ * const ProductSchema = z.object({
200
+ * name: TextField({ min: 1, max: 200 }),
201
+ * sku: TextField({ autogeneratePattern: "[A-Z]{3}-[0-9]{6}" }),
202
+ * description: TextField({ max: 1000 }),
203
+ * });
204
+ */
205
+ declare function TextField(options?: TextFieldOptions): z.ZodString;
206
+ /**
207
+ * Creates an email field schema
208
+ * Maps to PocketBase 'email' field type
209
+ *
210
+ * @returns Zod string schema with email validation and PocketBase metadata
211
+ *
212
+ * @example
213
+ * const UserSchema = z.object({
214
+ * email: EmailField(),
215
+ * alternateEmail: EmailField().optional(),
216
+ * });
217
+ */
218
+ declare function EmailField(): z.ZodString;
219
+ /**
220
+ * Creates a URL field schema
221
+ * Maps to PocketBase 'url' field type
222
+ *
223
+ * @returns Zod string schema with URL validation and PocketBase metadata
224
+ *
225
+ * @example
226
+ * const ProductSchema = z.object({
227
+ * website: URLField(),
228
+ * documentation: URLField().optional(),
229
+ * });
230
+ */
231
+ declare function URLField(): z.ZodString;
232
+ /**
233
+ * Creates a rich text editor field schema
234
+ * Maps to PocketBase 'editor' field type
235
+ *
236
+ * @returns Zod string schema with PocketBase metadata
237
+ *
238
+ * @example
239
+ * const PostSchema = z.object({
240
+ * content: EditorField(),
241
+ * summary: EditorField().optional(),
242
+ * });
243
+ */
244
+ declare function EditorField(): z.ZodString;
245
+ /**
246
+ * Creates a date field schema with optional constraints
247
+ * Maps to PocketBase 'date' field type
248
+ *
249
+ * @param options - Optional date constraints
250
+ * @returns Zod string schema with PocketBase metadata
251
+ *
252
+ * @example
253
+ * const EventSchema = z.object({
254
+ * startDate: DateField(),
255
+ * endDate: DateField({ min: new Date('2024-01-01') }),
256
+ * releaseDate: DateField().optional(),
257
+ * });
258
+ */
259
+ declare function DateField(options?: DateFieldOptions): z.ZodString;
260
+ /**
261
+ * Creates an autodate field schema with automatic timestamp management
262
+ * Maps to PocketBase 'autodate' field type
263
+ *
264
+ * @param options - Optional autodate configuration
265
+ * @returns Zod string schema with PocketBase metadata
266
+ *
267
+ * @example
268
+ * const PostSchema = z.object({
269
+ * createdAt: AutodateField({ onCreate: true }),
270
+ * updatedAt: AutodateField({ onUpdate: true }),
271
+ * publishedAt: AutodateField({ onCreate: true, onUpdate: false }),
272
+ * });
273
+ */
274
+ declare function AutodateField(options?: AutodateFieldOptions): z.ZodString;
275
+ /**
276
+ * Creates a select field schema from enum values
277
+ * Maps to PocketBase 'select' field type
278
+ *
279
+ * @param values - Array of allowed string values
280
+ * @param options - Optional select configuration
281
+ * @returns Zod enum or array schema with PocketBase metadata
282
+ *
283
+ * @example
284
+ * // Single select
285
+ * const PostSchema = z.object({
286
+ * status: SelectField(["draft", "published", "archived"]),
287
+ * });
288
+ *
289
+ * @example
290
+ * // Multiple select
291
+ * const ProductSchema = z.object({
292
+ * categories: SelectField(["electronics", "clothing", "food"], { maxSelect: 3 }),
293
+ * });
294
+ */
295
+ declare function SelectField<T extends [string, ...string[]]>(values: T, options?: SelectFieldOptions): z.ZodEnum<T> | z.ZodArray<z.ZodEnum<T>>;
296
+ /**
297
+ * Creates a single file field schema
298
+ * Maps to PocketBase 'file' field type with maxSelect=1
299
+ *
300
+ * @param options - Optional file constraints
301
+ * @returns Zod File schema with PocketBase metadata
302
+ *
303
+ * @example
304
+ * const ProductSchema = z.object({
305
+ * thumbnail: FileField({ mimeTypes: ["image/*"], maxSize: 5242880 }),
306
+ * document: FileField({ mimeTypes: ["application/pdf"] }),
307
+ * });
308
+ */
309
+ declare function FileField(options?: FileFieldOptions): z.ZodType<File>;
310
+ /**
311
+ * Creates a multiple files field schema
312
+ * Maps to PocketBase 'file' field type with maxSelect>1
313
+ *
314
+ * @param options - Optional file constraints
315
+ * @returns Zod array of File schema with PocketBase metadata
316
+ *
317
+ * @example
318
+ * const ProductSchema = z.object({
319
+ * images: FilesField({ mimeTypes: ["image/*"], maxSelect: 5 }),
320
+ * attachments: FilesField({ minSelect: 1, maxSelect: 10 }),
321
+ * });
322
+ */
323
+ declare function FilesField(options?: FilesFieldOptions): z.ZodArray<z.ZodType<File>>;
324
+ /**
325
+ * Creates a JSON field schema with optional inner schema validation
326
+ * Maps to PocketBase 'json' field type
327
+ *
328
+ * @param schema - Optional Zod schema for the JSON structure
329
+ * @returns Zod schema with PocketBase metadata
330
+ *
331
+ * @example
332
+ * // Any JSON
333
+ * const ProductSchema = z.object({
334
+ * metadata: JSONField(),
335
+ * });
336
+ *
337
+ * @example
338
+ * // Typed JSON
339
+ * const ProductSchema = z.object({
340
+ * settings: JSONField(z.object({
341
+ * theme: z.string(),
342
+ * notifications: z.boolean(),
343
+ * })),
344
+ * });
345
+ */
346
+ declare function JSONField<T extends z.ZodTypeAny>(schema?: T): T | z.ZodRecord<z.ZodString, z.ZodAny>;
347
+ /**
348
+ * Creates a geographic point field schema
349
+ * Maps to PocketBase 'geoPoint' field type
350
+ *
351
+ * @returns Zod object schema with lon/lat fields and PocketBase metadata
352
+ *
353
+ * @example
354
+ * const LocationSchema = z.object({
355
+ * coordinates: GeoPointField(),
356
+ * homeLocation: GeoPointField().optional(),
357
+ * });
358
+ */
359
+ declare function GeoPointField(): z.ZodObject<{
360
+ lon: z.ZodNumber;
361
+ lat: z.ZodNumber;
362
+ }>;
363
+
364
+ export { type AutodateFieldOptions as A, BoolField as B, type DateFieldOptions as D, EmailField as E, FIELD_METADATA_KEY as F, GeoPointField as G, JSONField as J, type NumberFieldOptions as N, type PocketBaseFieldType as P, type SelectFieldOptions as S, type TextFieldOptions as T, URLField as U, type FieldMetadata as a, type FileFieldOptions as b, type FilesFieldOptions as c, NumberField as d, extractFieldMetadata as e, TextField as f, EditorField as g, DateField as h, AutodateField as i, SelectField as j, FileField as k, FilesField as l };