Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Latest commit

 

History

History
75 lines (60 loc) · 2.08 KB

README.md

File metadata and controls

75 lines (60 loc) · 2.08 KB

prisma-generator-seeder

Automaticly seeds your database with dummy data.

It will automaticly create each model, and the relations required for it.

yarn add prisma-generator-seeder

This generator was bootstraped using create-prisma-generator

Generated files

Export Description
seedModel(target: ModelName, modelSeeds: ModelSeeds) Automaticly creates the given model, and its relations.
seedDatabase(modelSeeds: ModelSeeds) Automaticly seeds all models for the whole schema.
ModelSeeds Type for the object that defines how to create each model.
NullableModelSeeds Similar to ModelSeeds, but no model is required.

Setup

  1. Add the generator to your schema.prisma file.
generator DatabaseSeeder {
  provider = "prisma-generator-seeder"
  output   = "./seeding.ts" // Path for the genereated types, relative to your prisma.schema.
}
  1. Generate types
yarn prisma generate
  1. Define how each prisma model should be seeded in your seed.ts file.
import { seedDatabase, ModelSeeds } from './seeding';
import { database } from '../../database';
import { faker } from '@faker-js/faker';

const modelSeeds: ModelSeeds = {
	Country: () => {
		return database.country.create({
			data: {
				name: faker.company.name(),
			},
		});
	},
	House: ({ Country }) => {
		return database.house.create({
			data: {
				address: faker.location.streetAddress(),
				countryId: Country.id,
			},
		});
	},
	Person: ({ Country, House }) => {
		return database.person.create({
			data: {
				fullName: faker.person.fullName(),
				countryId: Country.id,
				houseId: House.id,
			},
		});
	},
};

seedDatabase(modelSeeds).then(() => console.log('Seeded database'));