pocketbase-zod-schema 0.7.2 → 1.0.1
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.
- package/CHANGELOG.md +33 -0
- package/README.md +203 -27
- package/dist/cli/index.cjs +3892 -1245
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.d.cts +2 -2
- package/dist/cli/index.d.ts +2 -2
- package/dist/cli/index.js +3889 -1244
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/migrate.cjs +3959 -1205
- package/dist/cli/migrate.cjs.map +1 -1
- package/dist/cli/migrate.js +3955 -1203
- package/dist/cli/migrate.js.map +1 -1
- package/dist/cli/utils/index.cjs +52 -13
- package/dist/cli/utils/index.cjs.map +1 -1
- package/dist/cli/utils/index.d.cts +48 -3
- package/dist/cli/utils/index.d.ts +48 -3
- package/dist/cli/utils/index.js +51 -13
- package/dist/cli/utils/index.js.map +1 -1
- package/dist/{fields-B96iGprI.d.cts → fields-D2ZkO9Pe.d.cts} +33 -2
- package/dist/{fields-B96iGprI.d.ts → fields-D2ZkO9Pe.d.ts} +33 -2
- package/dist/index.cjs +28 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +28 -6
- package/dist/index.js.map +1 -1
- package/dist/migration/analyzer.cjs +176 -51
- package/dist/migration/analyzer.cjs.map +1 -1
- package/dist/migration/analyzer.d.cts +2 -2
- package/dist/migration/analyzer.d.ts +2 -2
- package/dist/migration/analyzer.js +176 -51
- package/dist/migration/analyzer.js.map +1 -1
- package/dist/migration/diff.cjs +42 -13
- package/dist/migration/diff.cjs.map +1 -1
- package/dist/migration/diff.d.cts +2 -2
- package/dist/migration/diff.d.ts +2 -2
- package/dist/migration/diff.js +42 -13
- package/dist/migration/diff.js.map +1 -1
- package/dist/migration/engine.cjs +3340 -0
- package/dist/migration/engine.cjs.map +1 -0
- package/dist/migration/engine.d.cts +105 -0
- package/dist/migration/engine.d.ts +105 -0
- package/dist/migration/engine.js +3250 -0
- package/dist/migration/engine.js.map +1 -0
- package/dist/migration/generator.cjs +77 -19
- package/dist/migration/generator.cjs.map +1 -1
- package/dist/migration/generator.d.cts +47 -4
- package/dist/migration/generator.d.ts +47 -4
- package/dist/migration/generator.js +76 -20
- package/dist/migration/generator.js.map +1 -1
- package/dist/migration/index.cjs +3638 -1163
- package/dist/migration/index.cjs.map +1 -1
- package/dist/migration/index.d.cts +24 -7
- package/dist/migration/index.d.ts +24 -7
- package/dist/migration/index.js +3591 -1160
- package/dist/migration/index.js.map +1 -1
- package/dist/migration/snapshot.cjs +2277 -804
- package/dist/migration/snapshot.cjs.map +1 -1
- package/dist/migration/snapshot.d.cts +25 -67
- package/dist/migration/snapshot.d.ts +25 -67
- package/dist/migration/snapshot.js +2275 -801
- package/dist/migration/snapshot.js.map +1 -1
- package/dist/migration/utils/index.cjs +106 -9
- package/dist/migration/utils/index.cjs.map +1 -1
- package/dist/migration/utils/index.d.cts +3 -3
- package/dist/migration/utils/index.d.ts +3 -3
- package/dist/migration/utils/index.js +104 -10
- package/dist/migration/utils/index.js.map +1 -1
- package/dist/schema.cjs +28 -6
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.d.cts +2 -2
- package/dist/schema.d.ts +2 -2
- package/dist/schema.js +28 -6
- package/dist/schema.js.map +1 -1
- package/dist/server.cjs +4026 -1274
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +8 -6
- package/dist/server.d.ts +8 -6
- package/dist/server.js +3979 -1271
- package/dist/server.js.map +1 -1
- package/dist/{type-mapper-JBaVuKE2.d.ts → type-mapper-DElLGPUA.d.ts} +43 -2
- package/dist/{type-mapper-Bu9qjCdd.d.cts → type-mapper-DRxu96CN.d.cts} +43 -2
- package/dist/{types-CnzfX6JH.d.cts → types-Bs7N3Gwt.d.cts} +1 -1
- package/dist/types-BxJs7mmh.d.ts +504 -0
- package/dist/{types-Do3jyFBm.d.ts → types-ByOyly4V.d.ts} +1 -1
- package/dist/types-CY5e-ORN.d.cts +504 -0
- package/dist/verify-CkMwxocW.d.cts +402 -0
- package/dist/verify-DqY4TpV6.d.ts +402 -0
- package/package.json +7 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.0.1](https://github.com/dastron/pocketbase-zod-schema/compare/pocketbase-zod-schema-v1.0.0...pocketbase-zod-schema-v1.0.1) (2026-07-28)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* bug fixes for schema ([7007f4d](https://github.com/dastron/pocketbase-zod-schema/commit/7007f4dda37bef732cfcdb126e1a8dba79ece120))
|
|
9
|
+
* bug fixes for schema ([17bd233](https://github.com/dastron/pocketbase-zod-schema/commit/17bd23392a0455ff5b94d8cb54b30fbdcad7ffbc))
|
|
10
|
+
|
|
11
|
+
## [1.0.0](https://github.com/dastron/pocketbase-zod-schema/compare/pocketbase-zod-schema-v0.7.2...pocketbase-zod-schema-v1.0.0) (2026-07-28)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### ⚠ BREAKING CHANGES
|
|
15
|
+
|
|
16
|
+
* remove the static migration parser, execute migrations only
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* add migration execution engine replacing static analysis ([5fc9e81](https://github.com/dastron/pocketbase-zod-schema/commit/5fc9e8191fb7863dd1b96f87c165a44033f05341))
|
|
21
|
+
* add migration execution engine replacing static analysis as default ([eb094c7](https://github.com/dastron/pocketbase-zod-schema/commit/eb094c79f4f57c71402e68223e12122c99b8f474))
|
|
22
|
+
* add migration execution engine replacing static analysis as default ([b787cf0](https://github.com/dastron/pocketbase-zod-schema/commit/b787cf0ba4a9e0e3d80f4eb0951e90890bf33ccd))
|
|
23
|
+
* deprecation warning for static processor ([865f96d](https://github.com/dastron/pocketbase-zod-schema/commit/865f96db51eefd05fc53ff8e412cd4f49e9853e5))
|
|
24
|
+
* **engine:** _migrations awareness, record simulation, goja lint ([ce799d3](https://github.com/dastron/pocketbase-zod-schema/commit/ce799d38d2d50e6596cf7bb880656caff7c5631e))
|
|
25
|
+
* **engine:** verify down migrations round-trip ([35509f5](https://github.com/dastron/pocketbase-zod-schema/commit/35509f500ae8034a095115e8263779daef2aed94))
|
|
26
|
+
* **engine:** verify down migrations round-trip ([0b74452](https://github.com/dastron/pocketbase-zod-schema/commit/0b74452ff8c564c579de2462407908b2207ff0cb))
|
|
27
|
+
* remove the static migration parser, execute migrations only ([b5bb6cb](https://github.com/dastron/pocketbase-zod-schema/commit/b5bb6cb204a7132041b112a1c68051bdd4238a02))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
### Bug Fixes
|
|
31
|
+
|
|
32
|
+
* address review comments ([61e5441](https://github.com/dastron/pocketbase-zod-schema/commit/61e544128d57b49c3377ec9938d06e47d17a3df5))
|
|
33
|
+
* e2e test regression ([cb6e166](https://github.com/dastron/pocketbase-zod-schema/commit/cb6e166b0fa4850087344ae999b9e5098950e16b))
|
|
34
|
+
* flakey test ([4902cf2](https://github.com/dastron/pocketbase-zod-schema/commit/4902cf23aa944872a072d814f12590bd3182236c))
|
|
35
|
+
|
|
3
36
|
## [0.7.2](https://github.com/dastron/pocketbase-zod-schema/compare/pocketbase-zod-schema-v0.7.1...pocketbase-zod-schema-v0.7.2) (2026-07-26)
|
|
4
37
|
|
|
5
38
|
|
package/README.md
CHANGED
|
@@ -7,8 +7,13 @@ Define your PocketBase collections using Zod schemas and automatically generate
|
|
|
7
7
|
- **Type-safe schema definitions** - Use Zod to define your PocketBase collections with full TypeScript support
|
|
8
8
|
- **Automatic migrations** - Generate PocketBase-compatible migration files from your schema changes
|
|
9
9
|
- **Relation support** - Easily define single and multiple relations between collections
|
|
10
|
+
- **View collections** - Read-only collections backed by a SQL query, declared alongside the rest
|
|
10
11
|
- **Permission templates** - Built-in templates for common permission patterns
|
|
11
12
|
- **Index definitions** - Declare indexes alongside your schema
|
|
13
|
+
- **Execution engine** - Current state is reconstructed by *executing* your migrations in a
|
|
14
|
+
simulated PocketBase JSVM, so loops, helper functions and computed values are understood
|
|
15
|
+
- **Verification & linting** - Round-trip `up()`/`down()` before writing a migration, and catch
|
|
16
|
+
JavaScript that PocketBase's goja runtime cannot run
|
|
12
17
|
- **CLI & programmatic API** - Use the CLI for quick generation or the API for custom workflows
|
|
13
18
|
|
|
14
19
|
## Installation
|
|
@@ -207,7 +212,7 @@ The library provides explicit field helper functions for all PocketBase field ty
|
|
|
207
212
|
| `SelectField(values, options?)` | select | Single or multiple select field | `status: SelectField(["draft", "published"])` |
|
|
208
213
|
| `FileField(options?)` | file | Single file upload field | `avatar: FileField({ mimeTypes: ["image/*"] })` |
|
|
209
214
|
| `FilesField(options?)` | file | Multiple file upload field | `images: FilesField({ maxSelect: 5 })` |
|
|
210
|
-
| `JSONField(schema?)` | json | JSON field with optional schema | `metadata: JSONField()` |
|
|
215
|
+
| `JSONField(schema?, options?)` | json | JSON field with optional schema and size cap | `metadata: JSONField({ maxSize: "5M" })` |
|
|
211
216
|
| `GeoPointField()` | geoPoint | Geographic coordinates field | `location: GeoPointField()` |
|
|
212
217
|
| `RelationField(config)` | relation | Single relation field | `author: RelationField({ collection: "users" })` |
|
|
213
218
|
| `RelationsField(config)` | relation | Multiple relation field | `tags: RelationsField({ collection: "tags" })` |
|
|
@@ -260,19 +265,24 @@ The library provides explicit field helper functions for all PocketBase field ty
|
|
|
260
265
|
|
|
261
266
|
**FileField(options?)**
|
|
262
267
|
- `mimeTypes?: string[]` - Allowed MIME types (e.g., `["image/*", "application/pdf"]`)
|
|
263
|
-
- `maxSize?:
|
|
268
|
+
- `maxSize?: ByteSize` - Maximum file size: a number of bytes, or a string with a `K`/`M`/`G`
|
|
269
|
+
suffix (`"5M"`, `"1G"`; max `"8G"`)
|
|
264
270
|
- `thumbs?: string[]` - Thumbnail sizes to generate (e.g., `["100x100", "200x200"]`)
|
|
265
271
|
- `protected?: boolean` - Whether file requires auth to access
|
|
266
|
-
- Returns: `z.ZodType<File>`
|
|
272
|
+
- Returns: `z.ZodType<string, File | string>` — accepts a `File` on input, parses to the stored
|
|
273
|
+
filename string
|
|
267
274
|
|
|
268
275
|
**FilesField(options?)**
|
|
269
276
|
- All `FileField` options plus:
|
|
270
277
|
- `minSelect?: number` - Minimum number of files required
|
|
271
278
|
- `maxSelect?: number` - Maximum number of files allowed
|
|
272
|
-
- Returns: `z.
|
|
279
|
+
- Returns: `z.ZodType<string[], (File | string)[]>`
|
|
273
280
|
|
|
274
|
-
**JSONField(schema?)**
|
|
281
|
+
**JSONField(schema?, options?)**
|
|
275
282
|
- `schema?: z.ZodTypeAny` - Optional Zod schema for JSON structure validation
|
|
283
|
+
- `options?.maxSize?: number | "5M"` - Maximum serialized size, in bytes or with a `K`/`M`/`G`
|
|
284
|
+
suffix. PocketBase applies a 1MB default when unset, so larger payloads must set it. Either
|
|
285
|
+
argument may be passed alone: `JSONField({ maxSize: "5M" })`
|
|
276
286
|
- Returns: `T | z.ZodRecord<z.ZodString, z.ZodAny>`
|
|
277
287
|
|
|
278
288
|
**GeoPointField()**
|
|
@@ -419,13 +429,61 @@ const ProjectSchema = z.object({
|
|
|
419
429
|
**`RelationField(config)`** - Single relation
|
|
420
430
|
- `collection: string` - Target collection name (required)
|
|
421
431
|
- `cascadeDelete?: boolean` - Delete related records when this record is deleted (default: `false`)
|
|
432
|
+
- `displayFields?: string[] | null` - Fields to display in the PocketBase admin UI
|
|
422
433
|
|
|
423
434
|
**`RelationsField(config)`** - Multiple relations
|
|
424
|
-
-
|
|
425
|
-
- `cascadeDelete?: boolean` - Delete related records when this record is deleted (default: `false`)
|
|
435
|
+
- All `RelationField` options plus:
|
|
426
436
|
- `minSelect?: number` - Minimum number of relations required (default: `0`)
|
|
427
437
|
- `maxSelect?: number` - Maximum number of relations allowed (default: `999`)
|
|
428
438
|
|
|
439
|
+
> Relations can also be detected from field naming (a `z.string()` starting with an uppercase
|
|
440
|
+
> letter, or a `z.array(z.string())` containing one). That fallback exists for backward
|
|
441
|
+
> compatibility — prefer the helpers, which name the target collection explicitly.
|
|
442
|
+
|
|
443
|
+
### Defining View Collections
|
|
444
|
+
|
|
445
|
+
Use `defineView()` for a read-only collection backed by a SQL query. PocketBase derives the
|
|
446
|
+
collection's fields by running the query, so the Zod schema describes the row shape for TypeScript
|
|
447
|
+
only:
|
|
448
|
+
|
|
449
|
+
```typescript
|
|
450
|
+
import { z } from "zod";
|
|
451
|
+
import { baseSchema, defineView, sql } from "pocketbase-zod-schema/schema";
|
|
452
|
+
|
|
453
|
+
export const ProductStatsSchema = z
|
|
454
|
+
.object({
|
|
455
|
+
vendor: z.string(),
|
|
456
|
+
productCount: z.number(),
|
|
457
|
+
})
|
|
458
|
+
.extend(baseSchema);
|
|
459
|
+
|
|
460
|
+
export default defineView({
|
|
461
|
+
collectionName: "ProductStats",
|
|
462
|
+
schema: ProductStatsSchema,
|
|
463
|
+
viewQuery: sql`
|
|
464
|
+
SELECT p.vendor AS id,
|
|
465
|
+
p.vendor AS vendor,
|
|
466
|
+
COUNT(*) AS productCount
|
|
467
|
+
FROM products p
|
|
468
|
+
GROUP BY p.vendor
|
|
469
|
+
`,
|
|
470
|
+
permissions: {
|
|
471
|
+
listRule: "vendor.owner = @request.auth.id",
|
|
472
|
+
viewRule: "vendor.owner = @request.auth.id",
|
|
473
|
+
},
|
|
474
|
+
});
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
- Views are read-only: `defineView()` accepts only `listRule` and `viewRule`, and rejects indexes.
|
|
478
|
+
- The outermost `SELECT` must expose a unique `id` column.
|
|
479
|
+
- The generated migration contains the query and **no** `fields`/`indexes` array.
|
|
480
|
+
- Editing the SQL produces an in-place update that keeps the collection id stable; re-indenting it
|
|
481
|
+
produces no migration at all.
|
|
482
|
+
- Deleting a view is not a destructive change — a view stores no data.
|
|
483
|
+
|
|
484
|
+
`defineCollection({ type: "view", viewQuery })` is equivalent, but `defineView()` turns those
|
|
485
|
+
constraints into compile errors instead of apply-time failures.
|
|
486
|
+
|
|
429
487
|
### Defining Permissions
|
|
430
488
|
|
|
431
489
|
Use `defineCollection()` with permissions, or `withPermissions()` to attach API rules to your schema:
|
|
@@ -475,7 +533,22 @@ const PostSchemaAlt = withPermissions(
|
|
|
475
533
|
|----------|-------------|
|
|
476
534
|
| `"public"` | All operations are public (no auth required) |
|
|
477
535
|
| `"authenticated"` | All operations require authentication |
|
|
478
|
-
| `"owner-only"` | Only the owner can perform operations |
|
|
536
|
+
| `"owner-only"` | Only the owner can perform operations (`ownerField`, default `"User"`) |
|
|
537
|
+
| `"admin-only"` | Requires `@request.auth.<roleField> = "admin"` (`roleField`, default `"role"`) |
|
|
538
|
+
| `"read-public"` | Public read, authenticated write |
|
|
539
|
+
| `"custom"` | No base rules — `customRules` supplies everything |
|
|
540
|
+
|
|
541
|
+
Two more have no template name; call them and pass the result as `permissions`:
|
|
542
|
+
|
|
543
|
+
```typescript
|
|
544
|
+
import { PermissionTemplates } from "pocketbase-zod-schema/schema";
|
|
545
|
+
|
|
546
|
+
PermissionTemplates.locked(); // every rule null (superusers only)
|
|
547
|
+
PermissionTemplates.readOnlyAuthenticated(); // authenticated read, writes locked
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
Rule values: `null` = superusers only, `""` = public, any other string = a PocketBase filter
|
|
551
|
+
expression. `manageRule` applies to auth collections only.
|
|
479
552
|
|
|
480
553
|
#### Template with Custom Overrides
|
|
481
554
|
|
|
@@ -557,11 +630,76 @@ npx pocketbase-migrate generate
|
|
|
557
630
|
# Show what would be generated without writing files
|
|
558
631
|
npx pocketbase-migrate status
|
|
559
632
|
|
|
633
|
+
# Compare the files on disk against PocketBase's _migrations table
|
|
634
|
+
npx pocketbase-migrate status --verify
|
|
635
|
+
|
|
636
|
+
# Check migrations against PocketBase's goja runtime (Node-only globals, async, ...)
|
|
637
|
+
npx pocketbase-migrate lint
|
|
638
|
+
|
|
560
639
|
# Generate TypeScript definitions from schemas
|
|
561
640
|
npx pocketbase-migrate generate-types
|
|
562
641
|
|
|
563
642
|
# Force generation even with destructive changes
|
|
564
643
|
npx pocketbase-migrate generate --force
|
|
644
|
+
|
|
645
|
+
# Round-trip up() and down() before writing; refuse a migration that doesn't roll back
|
|
646
|
+
npx pocketbase-migrate generate --verify
|
|
647
|
+
|
|
648
|
+
# Restrict the diff to matching collection or field names (regex supported)
|
|
649
|
+
npx pocketbase-migrate generate Posts Comments
|
|
650
|
+
```
|
|
651
|
+
|
|
652
|
+
### `generate` Command
|
|
653
|
+
|
|
654
|
+
```bash
|
|
655
|
+
pocketbase-migrate generate [filters...] [options]
|
|
656
|
+
|
|
657
|
+
Options:
|
|
658
|
+
-o, --output <directory> Output directory for migration files
|
|
659
|
+
-f, --force Force generation even with destructive changes or duplicates
|
|
660
|
+
--dry-run Show what would be generated without writing files
|
|
661
|
+
--schema-dir <directory> Directory containing Zod schema files
|
|
662
|
+
--verify Execute up() and down() before writing
|
|
663
|
+
--no-verify Skip verification even when enabled in the config file
|
|
664
|
+
```
|
|
665
|
+
|
|
666
|
+
One file is written per collection operation.
|
|
667
|
+
|
|
668
|
+
### `status` Command
|
|
669
|
+
|
|
670
|
+
```bash
|
|
671
|
+
pocketbase-migrate status [options]
|
|
672
|
+
|
|
673
|
+
Options:
|
|
674
|
+
--schema-dir <directory> Directory containing Zod schema files
|
|
675
|
+
--json Output status as JSON
|
|
676
|
+
--verify Compare disk against PocketBase's _migrations table; exit non-zero on drift
|
|
677
|
+
--pb-data <path> PocketBase data directory or data.db file
|
|
678
|
+
```
|
|
679
|
+
|
|
680
|
+
`--verify` needs Node >= 22.5 (`node:sqlite`).
|
|
681
|
+
|
|
682
|
+
### `lint` Command
|
|
683
|
+
|
|
684
|
+
```bash
|
|
685
|
+
pocketbase-migrate lint [files...] [options]
|
|
686
|
+
|
|
687
|
+
Options:
|
|
688
|
+
-o, --output <directory> Directory containing migration files
|
|
689
|
+
--no-execute Static checks only
|
|
690
|
+
```
|
|
691
|
+
|
|
692
|
+
Catches JavaScript that runs in Node but not in goja: `require`, `process`, `fetch`, `setTimeout`,
|
|
693
|
+
`async`/`await`, `import`/`export`, class fields. Exits non-zero on any error-severity finding.
|
|
694
|
+
|
|
695
|
+
### Global Options
|
|
696
|
+
|
|
697
|
+
```
|
|
698
|
+
-c, --config <path> Configuration file path
|
|
699
|
+
-v, --version Print the version ← -v is version, not verbose
|
|
700
|
+
--verbose Enable verbose logging
|
|
701
|
+
--quiet Suppress non-essential output
|
|
702
|
+
--no-color Disable colored output
|
|
565
703
|
```
|
|
566
704
|
|
|
567
705
|
### `generate-types` Command
|
|
@@ -572,10 +710,8 @@ Generate type-safe TypeScript definitions from your Zod schemas.
|
|
|
572
710
|
pocketbase-migrate generate-types [options]
|
|
573
711
|
|
|
574
712
|
Options:
|
|
575
|
-
-c, --config <path> Configuration file path
|
|
576
713
|
-o, --output <path> Output file path (default: pocketbase-types.ts)
|
|
577
714
|
--schema-dir <directory> Directory containing Zod schema files
|
|
578
|
-
-v, --verbose Enable verbose logging
|
|
579
715
|
```
|
|
580
716
|
|
|
581
717
|
**What it generates:**
|
|
@@ -642,24 +778,42 @@ export default {
|
|
|
642
778
|
schema: {
|
|
643
779
|
// Directory containing your Zod schema files
|
|
644
780
|
directory: "./src/schema",
|
|
645
|
-
|
|
646
|
-
// Files to exclude from schema discovery
|
|
781
|
+
|
|
782
|
+
// Files to exclude from schema discovery (replaces the default list)
|
|
647
783
|
exclude: ["*.test.ts", "*.spec.ts", "base.ts", "index.ts"],
|
|
648
784
|
},
|
|
649
785
|
migrations: {
|
|
650
|
-
// Directory to output migration files
|
|
786
|
+
// Directory to output migration files, and to reconstruct state from
|
|
651
787
|
directory: "./pocketbase/pb_migrations",
|
|
788
|
+
|
|
789
|
+
// Round-trip up()/down() before writing a migration (default: false)
|
|
790
|
+
verify: false,
|
|
791
|
+
|
|
792
|
+
// PocketBase data directory, for reading the _migrations table.
|
|
793
|
+
// "" means the pb_data directory next to the migrations directory.
|
|
794
|
+
dataDirectory: "",
|
|
652
795
|
},
|
|
653
796
|
diff: {
|
|
654
797
|
// Warn when collections or fields would be deleted
|
|
655
798
|
warnOnDelete: true,
|
|
656
|
-
|
|
799
|
+
|
|
657
800
|
// Require --force flag for destructive changes
|
|
658
801
|
requireForceForDestructive: true,
|
|
659
802
|
},
|
|
803
|
+
typeGen: {
|
|
804
|
+
// Default output for `generate-types`
|
|
805
|
+
outPath: "pocketbase-types.ts",
|
|
806
|
+
},
|
|
660
807
|
};
|
|
661
808
|
```
|
|
662
809
|
|
|
810
|
+
There is no snapshot file to configure. The current database state is reconstructed by executing
|
|
811
|
+
the migration files: the newest `*_collections_snapshot.js` plus everything after it.
|
|
812
|
+
|
|
813
|
+
Environment overrides: `MIGRATION_SCHEMA_DIR`, `MIGRATION_SCHEMA_EXCLUDE`, `MIGRATION_OUTPUT_DIR`,
|
|
814
|
+
`MIGRATION_VERIFY`, `MIGRATION_DATA_DIR`, `MIGRATION_REQUIRE_FORCE`. Precedence is
|
|
815
|
+
CLI > environment > config file > defaults.
|
|
816
|
+
|
|
663
817
|
---
|
|
664
818
|
|
|
665
819
|
## Programmatic API
|
|
@@ -671,7 +825,7 @@ import {
|
|
|
671
825
|
parseSchemaFiles,
|
|
672
826
|
compare,
|
|
673
827
|
generate,
|
|
674
|
-
|
|
828
|
+
loadSnapshotWithMigrations,
|
|
675
829
|
} from "pocketbase-zod-schema/migration";
|
|
676
830
|
|
|
677
831
|
async function generateMigration() {
|
|
@@ -681,26 +835,31 @@ async function generateMigration() {
|
|
|
681
835
|
// Parse all schema files
|
|
682
836
|
const currentSchema = await parseSchemaFiles(schemaDir);
|
|
683
837
|
|
|
684
|
-
//
|
|
685
|
-
|
|
686
|
-
|
|
838
|
+
// Reconstruct the current database state by executing the existing migrations.
|
|
839
|
+
// Returns null when there is nothing to replay; throws SnapshotError if a
|
|
840
|
+
// migration cannot be executed.
|
|
841
|
+
const previousSnapshot = loadSnapshotWithMigrations({
|
|
842
|
+
migrationsPath: migrationsDir,
|
|
687
843
|
});
|
|
688
844
|
|
|
689
845
|
// Compare schemas and detect changes
|
|
690
846
|
const diff = compare(currentSchema, previousSnapshot);
|
|
691
847
|
|
|
692
|
-
//
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
const migrationPath = generate(diff, migrationsDir);
|
|
697
|
-
console.log(`Migration created: ${migrationPath}`);
|
|
698
|
-
} else {
|
|
699
|
-
console.log("No changes detected");
|
|
700
|
-
}
|
|
848
|
+
// Write one migration file per collection operation; returns the paths written
|
|
849
|
+
const paths = generate(diff, migrationsDir);
|
|
850
|
+
|
|
851
|
+
console.log(paths.length ? `Migrations created:\n${paths.join("\n")}` : "No changes detected");
|
|
701
852
|
}
|
|
702
853
|
```
|
|
703
854
|
|
|
855
|
+
> Use `loadSnapshotWithMigrations`, not `loadSnapshotIfExists`. The latter executes **only** the
|
|
856
|
+
> newest snapshot file and ignores every migration after it, so it is almost never the current
|
|
857
|
+
> state.
|
|
858
|
+
|
|
859
|
+
The execution engine, round-trip verification and the goja lint are also available directly from
|
|
860
|
+
`pocketbase-zod-schema/migration/engine` — see the
|
|
861
|
+
[Execution Engine guide](https://github.com/dastron/pocketbase-zod-schema/blob/main/docs/EXECUTION_ENGINE.md).
|
|
862
|
+
|
|
704
863
|
---
|
|
705
864
|
|
|
706
865
|
## Complete Example
|
|
@@ -821,6 +980,23 @@ export const CommentCollection = defineCollection({
|
|
|
821
980
|
|
|
822
981
|
---
|
|
823
982
|
|
|
983
|
+
## Documentation
|
|
984
|
+
|
|
985
|
+
Full guides live in the repository:
|
|
986
|
+
|
|
987
|
+
- [API Reference](https://github.com/dastron/pocketbase-zod-schema/blob/main/docs/API.md)
|
|
988
|
+
- [Execution Engine](https://github.com/dastron/pocketbase-zod-schema/blob/main/docs/EXECUTION_ENGINE.md)
|
|
989
|
+
- [Configuration](https://github.com/dastron/pocketbase-zod-schema/blob/main/docs/CONFIGURATION.md)
|
|
990
|
+
- [Migration Guide](https://github.com/dastron/pocketbase-zod-schema/blob/main/docs/MIGRATION_GUIDE.md)
|
|
991
|
+
- [Type Mapping](https://github.com/dastron/pocketbase-zod-schema/blob/main/docs/TYPE_MAPPING.md)
|
|
992
|
+
- [View Collections](https://github.com/dastron/pocketbase-zod-schema/blob/main/docs/VIEW_COLLECTIONS.md)
|
|
993
|
+
- [Naming Conventions](https://github.com/dastron/pocketbase-zod-schema/blob/main/docs/NAMING_CONVENTIONS.md)
|
|
994
|
+
|
|
995
|
+
## Requirements
|
|
996
|
+
|
|
997
|
+
Node.js 20 or higher. `status --verify`, which reads PocketBase's `_migrations` table, additionally
|
|
998
|
+
requires Node 22.5+ for `node:sqlite`.
|
|
999
|
+
|
|
824
1000
|
## License
|
|
825
1001
|
|
|
826
1002
|
MIT
|