drizzle-seed can only be used with drizzle-orm@0.36.4 or higher. Versions lower than this may work at runtime but could have type issues and identity column issues, as this patch was introduced in drizzle-orm@0.36.4
drizzle-seed is a TypeScript library that helps you generate deterministic, yet realistic,
fake data to populate your database. By leveraging a seedable pseudorandom number generator (pRNG),
it ensures that the data you generate is consistent and reproducible across different runs.
This is especially useful for testing, development, and debugging purposes.
What is Deterministic Data Generation?
Deterministic data generation means that the same input will always produce the same output.
In the context of drizzle-seed, when you initialize the library with the same seed number,
it will generate the same sequence of fake data every time. This allows for predictable and repeatable data sets.
Pseudorandom Number Generator (pRNG)
A pseudorandom number generator is an algorithm that produces a sequence of numbers
that approximates the properties of random numbers. However, because it’s based on an initial value
called a seed, you can control its randomness. By using the same seed, the pRNG will produce the
same sequence of numbers, making your data generation process reproducible.
Benefits of Using a pRNG:
Consistency: Ensures that your tests run on the same data every time.
Debugging: Makes it easier to reproduce and fix bugs by providing a consistent data set.
Collaboration: Team members can share seed numbers to work with the same data sets.
With drizzle-seed, you get the best of both worlds: the ability to generate realistic fake data and the control to reproduce it whenever needed.
Installation
npm
yarn
pnpm
bun
Basic Usage
In this example we will create 10 users with random names and ids
Options
count
By default, the seed function will create 10 entities.
However, if you need more for your tests, you can specify this in the seed options object
seed
If you need a seed to generate a different set of values for all subsequent runs, you can define a different number
in the seed option. Any new number will generate a unique set of values
Reset database
With drizzle-seed, you can easily reset your database and seed it with new values, for example, in your test suites
Different dialects will have different strategies for database resetting
PostgreSQL
MySQL
SQLite
For PostgreSQL, the drizzle-seed package will generate TRUNCATE statements with the CASCADE option to
ensure that all tables are empty after running the reset function
For MySQL, the drizzle-seed package will first disable FOREIGN_KEY_CHECKS to ensure the next step won’t fail, and then
generate TRUNCATE statements to empty the content of all tables
For SQLite, the drizzle-seed package will first disable the foreign_keys pragma to ensure the next step won’t fail,
and then generate DELETE FROM statements to empty the content of all tables
Refinements
In case you need to change the behavior of the seed generator functions that drizzle-seed uses by default, you can specify your own implementation and even use your own list of values for the seeding process
.refine is a callback that receives a list of all available generator functions from drizzle-seed. It should return an object with keys representing the tables you want to refine, defining their behavior as needed.
Each table can specify several properties to simplify seeding your database:
columns: Refine the default behavior of each column by specifying the required generator function.
count: Specify the number of rows to insert into the database. By default, it’s 10. If a global count is defined in the seed() options, the count defined here will override it for this specific table.
with: Define how many referenced entities to create for each parent table if you want to generate associated entities.
info
You can also specify a weighted random distribution for the number of referenced values you want to create. For details on this API, you can refer to Weighted Random docs docs section
API
Let’s check a few examples with an explanation of what will happen:
Example 1: Seed only the users table with 20 entities and with refined seed logic for the name column
Example 2: Seed the users table with 20 entities and add 10 posts for each user by seeding the posts table and creating a reference from posts to users
Example 3: Seed the users table with 5 entities and populate the database with 100 posts without connecting them to the users entities. Refine id generation for users so
that it will give any int from 10000 to 20000 and remains unique, and refine posts to retrieve values from a self-defined array
IMPORTANT
There are many more possibilities that we will define in these docs, but for now, you can explore a few sections in this documentation. Check the Generators section to get familiar with all the available generator functions you can use.
A particularly great feature is the ability to use weighted randomization, both for generator values created for a column and for determining the number of related entities that can be generated by drizzle-seed.
There may be cases where you need to use multiple datasets with a different priority that should be inserted into your database during the seed stage. For such cases, drizzle-seed provides an API called weighted random
The Drizzle Seed package has a few places where weighted random can be used:
Columns inside each table refinements
The with property, determining the amount of related entities to be created
Let’s check an example for both:
Example 1: Refine the unitPrice generation logic to generate 5000 random prices, with a 30% chance of prices between 10-100 and a 70% chance of prices between 100-300
Example 2: For each order, generate 1 to 3 details with a 60% chance, 5 to 7 details with a 30% chance, and 8 to 10 details with a 10% chance
Complex example
main.ts
schema.ts
Limitations
Types limitations for with
Due to certain TypeScript limitations and the current API in Drizzle, it is not possible to properly infer references between tables, especially when circular dependencies between tables exist.
This means the with option will display all tables in the schema, and you will need to manually select the one that has a one-to-many relationship
warning
The with option works for one-to-many relationships. For example, if you have one user and many posts, you can use users with posts, but you cannot use posts with users
Type limitations for the third parameter in Drizzle tables:
Currently, we do not have type support for the third parameter in Drizzle tables. While it will work at runtime, it will not function correctly at the type level