Seed Client API
The Seed Client API reference documentation is based on the following PostgreSQL database schema and seed.config.ts
configuration file:
createSeedClient
- Type:
(options?: SeedClientOptions) => Promise<SeedClient>
Create a new Seed Client instance.
Example
adapter
- Type:
DatabaseClient
Overrides the adapter defined in the seed.config.ts
configuration file.
Example
connect
- Type:
boolean
If true
, the Seed Client will apply the connect
option to every plan.
Example
dryRun
- Type:
boolean
If true
, the Seed Client will not execute any database operations, it will log the generated SQL queries to stdout instead.
Example
models
- Type:
UserModels
Provide custom data generation functions for the Seed Client instance.
Example
Models functions
- Example type:
(plan: Array<UserPlanInputs> | xCallback<UserPlanInputs>, options?: PlanOptions) => Promise<Store>
Seed one or more records in the database starting from the given model.
plan
Describes the data to seed.
You can provide the plan's inputs as a static array of inputs or more dynamically using the x
function helper.
static array
- Example type:
Array<UserPlanInputs>
Seed a static array of a given model.
Example
Seed 3 users:
x
function helper
- Example type:
(n: number | MinMaxOption, plan?: UserPlanInputs) => Array<UserPlanInputs>
Seed a given model n
times.
Examples
Seed 10 users:
Seed between 1 and 5 users:
Seed 3 posts with the same title:
Seed 3 posts with a specific title depending on the index:
model plan's inputs
- Example type:
PostPlanInputs
Provides a description of the data to seed.
The PostPlanInputs
type is an union of two types:
PostInputs
: an object that describes the data to seed.(ctx: PlanContext) => PostInputs
: a callback receiving the plan's context and which returns an object that describes the data to seed.
Examples
Model's plan inputs as an object:
Model's plan inputs as a callback:
The PostInputs
type is a record of the model's fields.
The fields can be scalar fields or relationships (parents or children fields).
Examples
Scalar field as a string:
Scalar field as a callback:
Scalar field as an async
callback:
Parent field as an object:
Parent field as a callback:
Child field as a static array:
Child field as a callback:
options
Provides additional options for the plan.
connect
- Type:
true | Partial<Store>
Connects the missing relationships to one of the corresponding models in the provided store.
If true
, the relationships will be connected to the Seed Client instance's $store
.
Examples
Connect 10 posts to 2 previously seeded users:
Connect 10 posts to one particular user:
models
- Type:
UserModels
Provides custom data generation functions for the plan.
Example
seed
- Type:
string
Provides a custom seed for the plan.
Example
Client functions
$resetDatabase
- Type:
(selectConfig?: SelectConfig) => Promise<void>
Reset the database by deleting all rows in given tables.
You can provide a custom select
configuration to filter the tables to truncate.
Examples
Truncate all the tables:
Truncate all the tables except the public._prisma_migrations
table:
Client properties
$store
- Type:
Store
The store is an object containing all the seeded data so far for each model.
Example