Cerial
Extension

Snippets

Complete reference for the 16 code snippets included with the Cerial VS Code extension.

The Cerial extension ships with 16 code snippets for common schema patterns. Type a prefix and press Tab to expand the snippet. Tab stops let you jump between placeholders to fill in names and values.

Block Types

PrefixDescriptionExpansion
modelNew modelmodel Name { id Record @id ... }
abstractNew abstract modelabstract model Name { ... }
objectNew object typeobject Name { ... }
tupleNew tuple typetuple Name { ... }
enumNew enum typeenum Name { Value1, Value2 ... }
literalNew literal typeliteral Name { ... }

Each block snippet places your cursor inside the body so you can start adding fields immediately. The model snippet includes an id Record @id line by default. The enum snippet includes two placeholder values.

Relations

PrefixDescriptionExpansion
rel11One-to-one relationTwo models with FK + Relation fields
rel1nOne-to-many relationParent with Relation[] and Child with FK
relnnMany-to-many relationBoth models with Record[] + Relation[]

These snippets generate a complete pair of models with correctly wired relation fields. Tab through the placeholders to name each model and its fields.

One-to-one (rel11)

# One-to-one relation
model Source {
  id Record @id
  targetId Record
  target Relation @field(targetId) @model(Target)
}

model Target {
  id Record @id
  source Relation @model(Source)
}

The Source model holds the FK (targetId Record) and the forward Relation pointing to Target. The Target model has a reverse Relation back to Source.

One-to-many (rel1n)

# One-to-many relation
model Parent {
  id Record @id
  children Relation[] @model(Child)
}

model Child {
  id Record @id
  parentId Record
  parent Relation @field(parentId) @model(Parent)
}

The Parent has a Relation[] (array) to represent "many children". The Child holds the FK and a single Relation back to its Parent.

Many-to-many (relnn)

# Many-to-many relation
model Source {
  id Record @id
  targetIds Record[]
  targets Relation[] @field(targetIds) @model(Target)
}

model Target {
  id Record @id
  sourceIds Record[]
  sources Relation[] @field(sourceIds) @model(Source)
}

Both sides store an array of FK references (Record[]) and a matching Relation[]. Cerial keeps both sides in sync automatically during connect and disconnect operations.

Fields and Decorators

PrefixDescriptionExpansion
idStandard record IDid Record @id
tidTyped record IDid Record(int|string|uuid) @id
fdefField with @defaultname String @default(value)
timestampsTimestamp fieldscreatedAt Date @createdAt + updatedAt Date @updatedAt
extendsInherit from parentextends ParentName

The tid snippet presents a choice between int, string, and uuid for the typed ID. The timestamps snippet expands to two lines at once, giving you both standard timestamp fields in a single Tab press.

Directives

PrefixDescriptionExpansion
@@uniqueComposite unique constraint@@unique(name, [field1, field2])
@@indexComposite index@@index(name, [field1, field2])

Place these at the bottom of a model block, after the field definitions. Tab through to name the constraint and specify which fields it covers.

On this page