seed
Getting Started
Installation

Installation

Snappy installing Seed

With the Setup Wizard

Run the following command in your terminal to start our handy install wizard:

>_ terminal

npx @snaplet/seed init

It will guide you through the installation process and help you set up your project.

Manually

If you prefer to do things manually, you can follow these steps.

1. Install the required dev dependencies

>_ terminal

npm install -D @snaplet/seed @snaplet/copycat

2. Configure Seed

Seed is configured using the seed.config.ts file.

Your need to specify an adapter in order for Seed to connect to your database.

For this example, we assume you are using PostgreSQL as your database and want to connect to it with the Postgres.js adapter:

seed.config.ts

import { SeedPostgres } from "@snaplet/seed/adapter-postgres";
import { defineConfig } from "@snaplet/seed/config";
import postgres from "postgres";
export default defineConfig({
adapter: () => {
const client = postgres(process.env.DATABASE_URL);
return new SeedPostgres(client);
},
});

3. Generate your Seed Client

Now that you have configured Seed, you can generate your Seed Client by running the following command:

>_ terminal

npx @snaplet/seed sync

This command will introspect your database schema and generate the necessary files for your Seed Client.

4. Create and execute your first Seed script

Create a new seed.mts file.

We assume that you have a table called users in your database.

seed.mts

import { createSeedClient } from "@snaplet/seed";
const seed = await createSeedClient();
// Truncate all tables in the database
await seed.$resetDatabase();
// Seed the database with 10 users
await seed.users((x) => x(10));
process.exit();

⚠️

Executing this script will delete all data in your database and seed it with 10 users. Make sure to adjust the script to your needs.

You can now execute your Seed script by running the following command:

>_ terminal

npx tsx seed.mts

Happy seeding! 🌱