Seed migration
From 0.76.0
and above
In this version, we are removing the run
function from the generate
configuration.
Instead, we're introducing a new package called @snaplet/seed
.
We extracted the data client into its own package so you can use it everywhere in your codebase!
Seed scripts or tests, data are now one import away! Let's see how to migrate to it.
First, install the new package:
Like the Prisma Client (opens in a new tab), this package is in fact a proxy to the local data client you will generate with Snaplet CLI based on your database schema.
In order to do that, we repurposed the command snaplet generate
to generate the data client! You can now run it.
It will create a folder containing your data client source code under node_modules/.snaplet
. This package is then re-exported by @snaplet/seed
.
Now, let's edit your snaplet.config.ts
and port your run
function to a seed script.
Given this configuration.
- Copy the content of your
run
function and remove it. - Rename the
generate
key in your config toseed
:
Create a new seed.mts
file and import the Snaplet data client.
Create a new snaplet
client instance.
If you were relying on snaplet to clear any existing data from your database first, you now do this explicitly with snaplet.$resetDatabase()
.
Paste what you copied earlier from your run
function.
Given this configuration.
- Copy the content of your
run
function and remove it. - Rename the
generate
key in your config toseed
:
Create a new seed.mts
file and import the Snaplet data client.
Create a new snaplet
client instance.
If you were relying on snaplet to clear any existing data from your database first, you now do this explicitly with snaplet.$resetDatabase()
.
Paste what you copied earlier from your run
function.
The snaplet
data client is automatically connected to your target database defined in either your .snaplet/config.json
file or your SNAPLET_TARGET_DATABASE_URL
environment variable.
You can now run your seed script with any TypeScript runner like tsx
(opens in a new tab) or ts-node
(opens in a new tab).
If you were relying on the --sql
option to generate a SQL file instead of having the data client writing directly to your database, you can now use the dryRun
option in the data client.
To know more about the data client, check out the documentation.
From 0.66.0
and above
The generate
API is evolving! After a great number of feedbacks from the community during the alpha, we decided to make it more flexible and powerful.
The new API is still in beta, but we encourage you to try it out and give us feedbacks.
Here is a step-by-step guide to migrate your existing generate
plan to the new API.
You can start by regenerating your types by running:
Now, let's edit your snaplet.config.ts
file.
This is what we had before version 0.66.0
.
Rename plan
to run
, and make it async. You are no longer required to return a plan. Calling await
on a plan will automatically run it.
We inject directly the snaplet
data client in the run
function.
The utilities and operators are now available directly on the snaplet
object: snaplet.$pipe
, snaplet.$merge
and snaplet.$createStore
.
We no longer need count
and data
, you can pass the values directly.
To create lists of data, you can either pass an array or use our x
helper function passed in a callback.
Congrats, you are now using the new API!
This is what we had before version 0.66.0
.
Rename plan
to run
, and make it async. You are no longer required to return a plan. Calling await
on a plan will automatically run it.
We inject directly the snaplet
data client in the run
function.
The utilities and operators are now available directly on the snaplet
object: snaplet.$pipe
, snaplet.$merge
and snaplet.$createStore
.
We no longer need count
and data
, you can pass the values directly.
To create lists of data, you can either pass an array or use our x
helper function passed in a callback.
Congrats, you are now using the new API!
To learn more about the new API, check out the documentation.