Seeders
JetQuery provides a framework for managing database seeders, just like migrations.
The details below cover how seeders work and the structure of the seeder file. It is recommended to use the Command Line Tools to create and run seeders.
Seeder Format
A seeder is a file named with a timestamp prefix followed by an underscore and an arbitrary string with a .zig extension. Jetzig seeders are located in src/app/database/seeders/.
A seeder file contains only one Zig function, run. This function receives a Repo which is then used to populate the database with some initial data required to run and use the application.
These functions can be used anywhere that you have access to a Repo. It is recommended that you limit seeders to populate the database with only data, but there are no hard restrictions on how you use the Repo within a seeder.
run
The run function is called when a seeder is executed. Use this function to populate the database with initial data.
Example Seeder
The following example is taken from this jetzig's demo repository:
This example creates two users when the seeder is executed
const std = @import("std");
pub fn run(repo: anytype) !void {
try repo.insert(
.User,
.{
.email = "iguana@jetzig.dev",
.password_hash = "not_secure",
},
);
try repo.insert(
.User,
.{
.email = "admin@jetzig.dev",
.password_hash = "do_not_use",
},
);
}