Generate
Vela provides a generator for creating database models, forms, resources, schemas and full CRUD scaffolds. Each of these shares a common syntax. Within the syntax, singular and plural forms have significance.
Syntax
$ vela generate <type> <name> [fields...] Types
form- A form and a Zod schema, with no database modelschema- A Zod schema on its ownresource- A database model and schema, with no frontendscaffold- A full CRUD interface, model, schema and migrationmigration- A migration that alters an existing collectionworkflow- A background workflow, with no database model
form and schema work in any SvelteKit project, with or without a backend. The rest need a backend, and scaffold --remote also needs shadcn-svelte.
Name
The plural name of the model, form, resource, schema or scaffold, for example pets or todos. Model names are always plural and will be converted to plural if the singular form is provided. Names can be nested within directories by using a slash (/) separator. For example, users/pets will create a resource at /users/pets.
Fields
The fields are defined as name:type pairs, for example name:string or age:number. If the model already exists in the database, the fields option can be omitted and the generator will use the existing fields.
Field types
Vela supports the following field types:
text
number
bool
date
email
password
url
editor
autodate
select
file/files
json
geoPoint
relation Aliases
Familiar names from other ecosystems are accepted and normalized to the types above:
string → text
boolean → bool
integer, int, float, decimal, double → number
datetime, timestamp → date
geopoint → geoPoint
references → relation autodate
The autodate field type is a special field type that automatically sets the field to the current date and time on creation or update. The field names for autodate can be: created/created_at or updated/updated_at.
select
The select field has special syntax for defining the options. The options are defined as value:label pairs. The singular or plural form of the field name determines whether the select acts as a single or multi-select.
$ vela generate scaffold pets type:select(dog:Dog,cat:Cat,bird:Bird) $ vela generate scaffold products colors:select(red:Red,green:Green,blue:Blue) file
The file field is used to upload files to the server. The single or plural form of the field name determines whether the field accepts a single file or an array of files.
$ vela generate scaffold owners avatar:file $ vela generate scaffold pets photos:files Required fields
Fields are marked as required by appending ! to the field name. This marks the field as required in the database and the Zod schema for form validation.
$ vela generate scaffold pets name:string! age:number! Relationships
In addition to the field types above, Vela supports passing in the name of any existing model to create a relationship. Single vs plural has significance here, as it determines whether the relationship is a one-to-many or many-to-many.
$ vela generate scaffold pets name:string owner:user $ vela generate scaffold teams name:string members:users Similarly to Ruby on Rails, the shorthand references can be used to create relationships based on the field name.
$ vela generate scaffold posts title:string author:user tags:references Changing an existing model
Once a collection exists, fields are added, removed and renamed with vela generate migration:
$ vela generate migration pets add birthday:date Ownership and permissions
If authentication is enabled with the vela enable auth command, it’s possible to associate models with the authenticated user. This can be done directly on the model with the special current_user field type, which will automatically set the field to the authenticated user on creation (and exclude the field from the frontend form).
$ vela generate scaffold posts title:string author:current_user If the model is nested within another model, vela traverses the model hierarchy to find the nearest parent model with a relationship to the authenticated user.
$ vela generate resource teams name:string
$ vela generate resource users email:string team:references
$ vela generate scaffold projects title:text team:references In this example, the users model is associated with the teams model. Access to the projects model is restricted to users who are members of the project’s team.
Usage
In the generated SvelteKit project, there’s two ways to retrieve data from the database. The first is the admin API locals.admin equivalent to querying the database directly. The second is the user-scoped API locals.pb which provides access to the database scoped to the authenticated user.
Admin API
export const load = async ({ locals }) => {
const posts = await locals.admin.collection('posts').getFullList();
return { posts };
} User-scoped API
export const load = async ({ locals }) => {
const posts = await locals.pb.collection('posts').getFullList();
return { posts };
} Limitations
Ownership via backreference is not supported.