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 model
  • schema - A Zod schema on its own
  • resource - A database model, its migration and schema, with no pages
  • scaffold - A full CRUD interface, model, schema and migration
  • migration - A migration that alters an existing collection
  • workflow - A background workflow, with no database model
  • shop - A product listing and product pages for the cart, in the payments provider’s own terms

form and schema work in any SvelteKit project, with or without a backend, and shop wherever the cart is. 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.

A field name starts with a letter and holds letters, digits and underscores: title, firstName, first_name.

The whole syntax on this page is also printed by --help on the generators that take fields:

$ vela generate scaffold --help

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 set by PocketBase, never by a form. The field name says when: a name holding create (created, created_at, createdAt) is set when the record is created, a name holding update (updated, updated_at, updatedAt) is set then and again on every update. Any other name is an error.

Every collection a generator creates gets a created and an updated autodate field without being asked, as collections made in the admin panel do, so sort: '-created' works. They are part of the collection only: they are left out of the Zod schema, the forms, the pages and the fixtures. Declare a field named created or updated yourself and yours is used instead.

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 type. This marks the field as required in the database and the Zod schema for form validation.

$ vela generate scaffold pets name:string! age:number!

Unique fields

A third segment, :unique, gives the collection a unique index on the field, so no two records can hold the same value. It comes after the type and its !:

$ vela generate resource coupons code:text!:unique
$ vela generate scaffold posts title:text! slug:text:unique

An empty value counts as a value, so a unique field that is not required can be left blank by one record only: combine it with ! unless that is what you want. vela generate migration adds the index along with the field: vela generate migration coupons add code:text!:unique.

A field that holds no single value cannot be unique: json, geoPoint, file, password, autodate, bool, and a select or relation that takes several. unique is the only modifier; anything else in the third segment is an error.

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.

Rules

A new collection’s five API rules (list, view, create, update, delete) follow from the fields:

  • with auth enabled and a relation path to users, the owner rule, for example @request.auth.id = user.id;
  • with auth enabled and no such path, @request.auth.id != "": every signed-in user can read and write;
  • without auth, no rules: only the server’s superuser client (locals.admin) can read or write.

--rules picks one outright, on resource and scaffold:

$ vela generate resource audit_logs action:text! --rules superuser
$ vela generate scaffold notes body:editor author:current_user --rules owner
  • auth - @request.auth.id != "" on all five, even when an owner path exists
  • owner - The inferred owner rule. An error when no relation path leads to users
  • superuser - All five closed: only the superuser client can read or write, and the scaffold’s pages use locals.admin. Those pages then let everyone who can open them read and write every record, so put them behind a check of your own

auth and owner need auth. Rules are set as the collection is created, so passing --rules for a collection that already exists is an error; change an existing collection’s rules in the admin panel and capture them with vela migrate collections. generate schema and generate form create no collection and do not take the flag.