triston-notes/Cards/dev/Prisma Tips.md

70 lines
1.7 KiB
Markdown
Raw Normal View History

2023-10-21 23:52:54 +00:00
---
banner: "https://cdn.discordapp.com/attachments/1050995657797816341/1090574124960727070/Triston_Prisma_magical_magistic_spacial_ffbc2d7d-fcd2-4bd0-b78a-f6ebc8776501.png"
---
up:: [[Boilerplate Code]]
X:: [[JavaScript]]
tags:: #boilerplate
### Updating schema file
after the file has been updated with new schema details, a new migration needs to be run
```bash
npx prisma migrate --name init
```
then we can run the command below to reset and set new seed
### How to clear the Database and rerun seeds
``` bash
npx prisma migrate reset
```
### how to run seed command
before being able to run seed command, you need to enable seeding in the packag.json file
add this key/val pair to package json root
```json
"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
}
```
the `--compiler-options {\"module\":\"CommonJS\"}` part is for next.js
now we can run seed command
`npx prisma db seed`
should work if seed file created already
### Boiler-plate seed file
```ts
import { PrismaClient, Prisma } from "@prisma/client";
const prisma = new PrismaClient();
const userData: Prisma.UserCreateInput[] = [
{
email: "email@gmail.com",
password: "$2b$04$C/l6kQt/LCNxp4HTR.fZde2mN68ppAyhx7rf1nFAWOza.YqzGNylK",
},
];
async function main() {
console.log(`Start seeding ...`);
for (const u of userData) {
const user = await prisma.user.create({
data: u,
});
console.log(`Created user with id: ${user.id}`);
}
console.log(`Seeding finished.`);
}
main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});
```