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

Automaticly seeds your database with dummy data.

Notifications You must be signed in to change notification settings

Clonex/prisma-generator-seeder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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'));

About

Automaticly seeds your database with dummy data.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published