Seed Quick Start Guide
Assumptions
This guide will take you through the process of using @snaplet/seed
to create realistic, production-like data.
If you already have access to a database with production or production-like data you'd prefer to use as a source, you should instead follow the Snapshot Quick Start guide.
Prerequisites
- You'll need to have the Snaplet CLI installed and have run
snaplet setup
- Node.js and npm installed on your machine
- a PostgreSQL database server running
The diagram above represents the relationships of your database.
From the SQL tab above, copy the SQL statement and insert this into your database to define your schema.
Snaplet seed
We've now got a new database and a schema that lays out the structure of your data, but no actual data. This is where we can now use Snaplet to seed your database with production-like data.
For your example blog application, let's assume we need the following data:
- A post titled "Hello World!"
- A post author, with an email address that ends with "acme.org."
- Three comments on the post.
It's worth noting what we're not explicitly defining - that each comment needs to have a separate, unique author, for instance. We're assuming that @snaplet/seed
will fill in the blanks based on your schema and seed realistic data as needed.
Describing your requirements
In order to have Snaplet seed this data for your blog, we need to describe these requirements to @snaplet/seed
.
First, we need to create a new seed script file that imports @snaplet/seed
.
This can be any javascript file that can be run by Node.js, but we recommend creating a typescript file for the best developer experience.
- If you chose to have Snaplet seed an example for you during
snaplet setup
, you should haveseed.mts
file in your project that looks something like the code below. - If you chose to not have Snaplet seed an example, create a
seed.mts
file in your project (the directory where you ransnaplet setup
) that looks like this:
snaplet
in the code above exposes all your models (i.e. your database tables, or the entities in your application - in this case "user" and "post") as functions.
During snaplet setup
, Snaplet would have read your database structure (using the database connection string you entered), and used this to create these functions.
These functions return what we call a plan. When describing a plan, you always start from a "root" model - a model where we would start describing how to seed seed data, along with data for its related models (more on this soon).
Here we start from the posts
models. await
ing a plan will execute it.
You can directly define the values of your model.
You can also define the values of your model's relationships.
Here we define the author of the post represented by the author
field.
Next, we also define the comments of the post represented by the comments
field.
As we need more than one comment, we can use the x
function which will create a list of similar comments.
You can read it as "comments times 3".
Finally:
- We'll add a
dryRun
option when instantiatingSnapletClient
- in dry run mode, we'll log the sql statements to the console instead of altering the database. - and a
$resetDatabase()
call - this will clear the database of any current data, but keep the structure around
We're done!
Copy the code and add it to your seed.mts
file.
First, we need to create a new seed script file that imports @snaplet/seed
.
This can be any javascript file that can be run by Node.js, but we recommend creating a typescript file for the best developer experience.
- If you chose to have Snaplet seed an example for you during
snaplet setup
, you should haveseed.mts
file in your project that looks something like the code below. - If you chose to not have Snaplet seed an example, create a
seed.mts
file in your project (the directory where you ransnaplet setup
) that looks like this:
snaplet
in the code above exposes all your models (i.e. your database tables, or the entities in your application - in this case "user" and "post") as functions.
During snaplet setup
, Snaplet would have read your database structure (using the database connection string you entered), and used this to create these functions.
These functions return what we call a plan. When describing a plan, you always start from a "root" model - a model where we would start describing how to seed seed data, along with data for its related models (more on this soon).
Here we start from the posts
models. await
ing a plan will execute it.
You can directly define the values of your model.
You can also define the values of your model's relationships.
Here we define the author of the post represented by the author
field.
Next, we also define the comments of the post represented by the comments
field.
As we need more than one comment, we can use the x
function which will create a list of similar comments.
You can read it as "comments times 3".
Finally:
- We'll add a
dryRun
option when instantiatingSnapletClient
- in dry run mode, we'll log the sql statements to the console instead of altering the database. - and a
$resetDatabase()
call - this will clear the database of any current data, but keep the structure around
We're done!
Copy the code and add it to your seed.mts
file.
Executing the plan
At this point, you're ready to run your seed script for the first time!
Let's see what statements are generated by using the DRY
environment variable we defined.
Run this command in your terminal:
You should see the following output:
It's now time to seed this data into your database.
In order to do that, run this command in your terminal (notice how the DRY
environment variable is set to 0
):
All done, you're now ready to code against your generated data!
Next steps
Learn more about how @snaplet/seed
works and define more complex plans in our seed reference guide