Cli

Seed

Seed your Better-Auth database with deterministic, yet realistic, fake data to populate your database.

Our seeding tool allows you to generate realistic sample data, such as users, for your Better Auth database. Inspired by Drizzle Seed.

Usage

Install

npm install @better-auth-kit/seed

Create

Create a seed.ts file next to your auth.ts file

Learn more about adding users here.

seed.ts
import { Seed, users } from "@better-auth-kit/seed";
 
export const seed = Seed({
    // Adds 100 users (including sessions and accounts)
    ...users(),
});

Run

npx @better-auth-kit/cli@latest seed

And that's it! 🔥


Adding Users

Handles the creation of user, session, and account tables.

import { users, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    ...users() 
});

You can modify or append new fields to the user, session, or account tables, as well as add custom types.

import { users, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    // Optional: pass in custom types for the tables
    ...users<{
        user: MyCustomUser,
        session: MyCustomSession,
        account: MyCustomAccount
    }>({
        user: { 
            role: $.randomChoice( 
                $.custom(() => "admin"), 
                $.custom(() => "user"), 
                $.custom(() => "super-admin"), 
            ), 
            banned: $.boolean({ probability: 0.5 }), 
            banReason: $.custom(() => "Some hard-coded ban reason"), 
            banExpires: $.randomDate(), 
        }, 
        session: { 
            ipAddress: $.ip(), 
            userAgent: $.userAgent(), 
        }, 
        account: { 
            password: $.password(), 
        } 
    })
});

Additional Options

Adding Organizations

Handles the creation of the organization, member, invitation (if enabled), and team (if enabled) tables.

import { users, organizations, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    ...users(), // Require at least 100 users in your DB to create 100 organizations.
    ...organizations() 
});

You can modify or append new fields to the organizations, members, invitations, and teams tables, as well as add custom types.

import { organizations, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
     // Optional: pass in custom types for the tables
    ...organizations<{
        organization: MyCustomOrganization,
        member: MyCustomMember,
        invitation: MyCustomInvitation,
        team: MyCustomTeam
    }>({
        organization: { 
            name: $.firstname(), 
        }, 
        member: { 
            role: $.randomChoice($.custom(() => "admin"), $.custom(() => "member")), 
        } 
        //...
    })
});

Additional Options

Adding API keys

Handles the creation of API keys on the apikey table.

import { users, apiKeys, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    ...users(), // Require at least 100 users in your DB to create 100 api keys.
    ...apiKeys() 
});

You can modify or append new fields to the apikey table, as well as add custom types.

import { apiKeys, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    ...apiKeys<MyCustomApiKey>({
        name: $.custom(() => "My API Key"), 
    })
});

Additional Options


Multiple Seed Files

You can execute multiple seed files by creating a seed folder next to your auth.ts file.

auth.ts
1-users.ts
2-organizations.ts
3-api-keys.ts

Within the seed folder, you can create any number of files, with any name you want, and they will be executed in alphabetical order.

Terminal
npx @better-auth-kit/cli@latest seed

By default, the CLI will prompt you to select which seed files to execute. However, you can pass the --run-all flag to execute all seed files.

Terminal
npx @better-auth-kit/cli@latest seed --run-all

$ Functions

We have a number of functions that you can use to generate random data.

Custom

This is a special function that allows you to hard code a value, and isn't used to generate random data.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        greeting: $.custom(() => "Hello, world! This can be any value!"), 
    }),
});

Foreign Key

Generates a foreign key.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        foreignKey: $.foreignKey({ model: "myOtherTable", field: "id" }), 
    }),
});

Emails

Generates a random email address.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        email: $.email(), 
    }),
});

First Names

Generates a random first name.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        firstName: $.firstname(), 
    }),
});

Last Names

Generates a random last name.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        lastName: $.lastname(), 
    }),
});

Full Name

Generates a random full name.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        fullName: $.first_and_lastname(), 
    }),
});

UUID

Generates a random UUID.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        id: $.uuid(), 
    }),
});

Random Boolean

Generates a random boolean.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        isActive: $.randomBoolean(), 
    }),
});

Random Date

Generates a random date.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        createdAt: $.randomDate(), 
    }),
});

Random Characters

Generates a random string of characters.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        randomCharacters: $.randomCharacters(10), // 10 characters
    }),
});

Random Numbers

Generates a random number between 0 and 100. (configurable)

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        randomNumber: $.randomNumber(), 
    }),
});

Random Choice

Generates a random choice from an array of values.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        randomChoice: $.randomChoice(
            $.custom(() => "option1"),
            $.custom(() => "option2"),
            $.custom(() => "option3"),
            $.randomNumber({ min: 0, max: 100 }),
        ), 
    }),
});

Password

Generates a random password.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        password: $.password(), 
    }),
});

Random Image URL

Generates a random image URL.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        image: $.image(), 
    }),
});

Company Name Suffixes

Generates a random company name suffix.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        companySuffix: $.companyNameSuffixes(), 
    }),
});

Countries

Generates a random country.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        country: $.countries(), 
    }),
});

Job Titles

Generates a random job title.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        jobTitle: $.jobTitles(), 
    }),
});

Lorem Ipsum Sentence

Generates a random lorem ipsum sentence.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        random_sentence: $.loremIpsum(), 
    }),
});

Phone Numbers

Generates a random phone number.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        phoneNumber: $.phoneNumbers(), 
    }),
});

States

Generates a random state.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        state: $.states(), 
    }),
});

Street Suffixes

Generates a random street suffix.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        streetSuffix: $.streetSuffixes(), 
    }),
});

Cities

Generates a random city.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        city: $.cityNames(), 
    }),
});

IP Address

Generates a random IP address.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        ipAddress: $.ip(), 
    }),
});

User Agent

Generates a random user agent.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        userAgent: $.userAgent(), 
    }),
});

Null Value

Generates a null value.

import { table, $, Seed } from "@better-auth-kit/seed";
 
export const seed = Seed({
    myTable: table({
        nullValue: $.nullValue(), 
    }),
});

Configuration

You can export a config object to configure the seeding process.

seed.ts
import { Seed, type SeedConfig } from "@better-auth-kit/seed";
 
export const seed = Seed({
    // Your seeding tables...
});
 
export const config: SeedConfig = {
    deleteRowsBeforeSeeding: false,
    rows: 100,
};

deleteRowsBeforeSeeding

Delete all rows in the tables before seeding.

Default is false.

The table rows will be deleted in the order of the models array.

import { type SeedConfig } from "@better-auth-kit/seed";
 
export const config: SeedConfig = {
    deleteRowsBeforeSeeding: {
        enabled: true,
        // Make sure to delete the tables in order of foreign key dependencies
        models: ["user", "session", "account"],
    },
};

rows

The default number of rows to seed.

Default is 100.

If a given table has a count option, that will take precedence over this.

import { Seed, type SeedConfig } from "@better-auth-kit/seed";
 
export const config: SeedConfig = {
    rows: 1000,
};

Credit

This project was heavily inspired by Drizzle Seed, and the datasets used in this project are from Drizzle Seed as well. Big thanks to the Drizzle team for the awesome work! 💖