Last active
May 6, 2022 08:22
-
-
Save moritzmorgenroth/10fa13fbb563263d7ec4d0259b044b7d to your computer and use it in GitHub Desktop.
Prisma seeding script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Prisma } from './../generated/prisma'; | |
import { readFileSync } from 'fs'; | |
// add to yarn: | |
// "seed": "dotenv -e .env -- ts-node ./src/seed/seed.ts" | |
const prisma = new Prisma({ | |
endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma DB service (value is set in .env) | |
secret: process.env.PRISMA_SECRET, // taken from database/prisma.yml (value is set in .env) | |
debug: false // log all GraphQL queries & mutations | |
}); | |
async function main() { | |
// example seed function | |
await seedUnits(); | |
// call your custom seed functions here | |
} | |
// seed something awesome :) | |
async function seedUnits() { | |
const units = JSON.parse(readFileSync(`${__dirname}/units.json`, 'utf8')); | |
for (let unit of units) { | |
await prisma.mutation.createUnit({ | |
data: { name: unit.unit } | |
}); | |
} | |
} | |
main() | |
.then(() => { | |
console.log('🎉 Seed successful'); | |
process.exit(0); | |
}) | |
.catch(e => { | |
console.error(e); | |
console.error('\n❌ Seed failed. See above.'); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment