Vasta Logo
Models

Migrations & Seeders

Defining migrations and seeders using kysely-ctl and Vasta

Migrations

Kysely natevly supports migrations with kysely-ctl. You can read about migrations with Kysely in the documenation.

Migrations can be run with kysely migrate:latest

You can check out the /tests/database/migrations/ directory in this package for examples of migrations.

/database/migrations/1732594612817_add_people_table.ts
import { Kysely, sql } from "kysely";

export async function up(db: Kysely<any>): Promise<void> {
  // Migration code
  await db.schema
    .createTable("people")
    .addColumn("id", "serial", (col) => col.notNull().primaryKey())
    .addColumn("created_at", "timestamp", (col) => col.notNull().defaultTo(sql`now()`))
    .addColumn("updated_at", "timestamp", (col) => col.notNull().defaultTo(sql`now()`))
    .addColumn("name", "varchar", (col) => col.notNull())
    .addColumn("birthday", "date")
    .addColumn("email", "varchar")
    .addColumn("phone", "varchar")
    .addColumn("favorite_color", "varchar")
    .addColumn("secret", "varchar")
    .execute();
}

export async function down(db: Kysely<any>): Promise<void> {
  // Migration code
  await db.schema.dropTable("people").execute();
}

Seeders

Kysely also supports seeders through the kysely-ctl utility. Check out the documentation for seeder instructions. A basic example is provided here for reference.

Seeders can be executed with kysely seed:run

You can check out this package's /test/database/seeders directory for an example of seeders used by this package for testing. You can use Vasta models as part of your seeders as well, but the examples here are just basic Kysely seeders.

01-PersonSeeder.ts
import { Database } from "@/types/database";
import type { Kysely } from "kysely";

// replace `any` with your database interface.
export async function seed(db: Kysely<Database>): Promise<void> {
  // seed code goes here...
  // note: this function is mandatory. you must implement this function.
  await db
    .insertInto("people")
    .values([
      {
        name: "David",
        birthday: new Date("1986-07-20"),
        email: "david.nahodyl@gmail.com",
        favorite_color: "blue",
        phone: "404-123-1234",
        secret: "password123",
      },
      {
        name: "Kate",
        birthday: new Date("1986-11-20"),
        email: "kate.nahodyl@gmail.com",
        favorite_color: "green",
        phone: "404-555-0123",
        secret: "kate_secret",
      },
      {
        name: "Alex",
        birthday: new Date("1990-03-14"),
        email: "alex.morgan@example.com",
        favorite_color: "purple",
        phone: "770-555-0191",
        secret: "alex_secret",
      },
      {
        name: "Jordan",
        birthday: new Date("1992-09-02"),
        email: "jordan.lee@example.com",
        favorite_color: "orange",
        secret: "jordan_secret",
      },
      {
        name: "Priya",
        birthday: new Date("1989-12-05"),
        email: "priya.shah@example.com",
        favorite_color: "teal",
        phone: "770-555-0177",
        secret: "priya_secret",
      },
      {
        name: "Morgan",
        birthday: new Date("1994-06-18"),
        email: "morgan.rivera@example.com",
        favorite_color: "red",
        phone: "678-555-0168",
        secret: "morgan_secret",
      },
    ])
    .execute();
}
Copyright © 2026