Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prisma seed for dev dump #66

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
command: >
sh -c "npm install &&
npx prisma generate &&
npx prisma migrate dev &&
npm run migrations:dev &&
npm run start:dev -- --preserveWatchOutput"
db:
container_name: sos-rs-db
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"migrations:run": "npx prisma migrate deploy",
"migrations:dev": "npx prisma migrate dev",
"migrations:dev": "npx prisma migrate dev && npx prisma db seed",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não é necessário rodar o prisma deb seed, ele roda sozinho após o prisma migrate dev.
O que seria legal é retirar o npx dos dois comandos, que é desnecessário, já que os comandos do npm rodam sob o contexto do node_modules/bin no path.

"docker:compose": "docker-compose -f docker-compose.dev.yml up"
},
"prisma": {
"seed": "ts-node prisma/seed.ts"
},
"dependencies": {
"@fastify/static": "^7.0.3",
"@nestjs/common": "^10.0.0",
Expand Down
30 changes: 30 additions & 0 deletions prisma/seed.ts
rkperes marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { PrismaClient } from '@prisma/client';
import * as fs from 'fs';
rkperes marked this conversation as resolved.
Show resolved Hide resolved

const prisma = new PrismaClient();

async function main() {
const devDump = fs.readFileSync('./prisma/dev_dump.sql', 'utf-8');

for (const query of devDump.split('\n')) {
if (query.trim() === '') {
continue;
}

try {
await prisma.$executeRawUnsafe(query);
} catch (e) {
console.error(e);
}
}
}

main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});