Sync the database schema with the application TypeScript definitions.
Syntax
$ vela syncThis reads the collections from the local database and writes their types, so locals.pb.collection('posts') and Models['posts'] are typed from the schema as it is now. It runs automatically after any database changes during local development: vela dev regenerates the types whenever a collection changes, and the generators sync when they finish.
Where the types are written
.svelte-kit/pocketbase/$types.d.tsThe file augments @velastack/pocketbase with Models, Collections and Schemas. It is generated, lives under the gitignored .svelte-kit, and is picked up through the include list in tsconfig.json:
".svelte-kit/pocketbase/*.d.ts"Before vela 0.31.0 the file was .svelte-kit/types/pocketbase/$types.d.ts. SvelteKit deletes whatever it does not own under .svelte-kit/types whenever a Vite server with its plugin starts, so a test run removed the types and every Models[...] broke until the next vela sync. Nothing has to be done by hand to move: the first vela sync or vela dev after upgrading rewrites the old include entry to the new one in place and says so, and new projects start with the new one. The old path is still written as well, so a project whose tsconfig.json has not been updated keeps working.
Without a database
vela sync is part of a new project’s prepare script, so it runs on every install. On a fresh checkout — a new clone, a CI runner — there is no local database to read yet. The command prints one line saying so and exits successfully, rather than failing the install. vela dev creates the database, and the types with it. A database that is there but cannot be signed in to is still an error.
Read types are always present
Models[...] is what PocketBase returns, and a record always comes back with every field filled in: an unset one holds its zero value, '', 0, false or []. From @velastack/pocketbase-codegen 0.2.0, which vela 0.31.0 uses, the types say so. A field that is not required is no longer optional:
// a `songs` collection: title (required), artist, year, genre (single select)
// before
Models['songs']; // { id: string; title: string; artist?: string; year?: number; genre?: 'rock' | 'pop' }
// now
Models['songs']; // { id: string; title: string; artist: string; year: number; genre: 'rock' | 'pop' }What to change in existing code after upgrading and running vela sync:
- Remove guards against
undefinedthat can no longer happen, such assong.artist ?? ''orsong.year ?? 0. They are harmless, but a linter may now flag them as unnecessary. - A check for “not set” compares against the zero value:
if (song.artist)orsong.artist === '', notsong.artist === undefined. - A single select that is not required is typed as the union of its options, although an unset one arrives as
''. The type leaves that out so a record still fits the collection’s input schema; compare against''yourself where an unset select matters.
Two kinds of field stay optional, because the response really can leave them out: hidden fields such as password and tokenKey, which only superusers are sent, and an auth collection’s email when it is not required. A required auth email, PocketBase’s default, is typed as present.
Schemas[...], the type of what PocketBase accepts on create and update, is unchanged: a non-required field is still optional there.