diff --git a/package.json b/package.json index 20b94c1d..a0e11748 100644 --- a/package.json +++ b/package.json @@ -9,11 +9,12 @@ "start": "next start", "lint": "next lint", "dev:docker": "npm run db:seed & next dev", - "db:seed": "npx prisma db push & npx prisma db seed", + "db:seed": "npx prisma db push && node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' prisma/seed.ts", "db:studio": "npx prisma studio", "check": "prettier --check \"**/*.{ts,tsx,js,jsx,md,mdx,css}\"", "format": "prettier --write \"**/*.{ts,tsx,js,jsx,md,mdx,css}\"", - "prepare": "husky" + "prepare": "husky", + "db:reset": "npx prisma migrate reset && npm run db:seed" }, "husky": { "hooks": { diff --git a/prisma/seed.ts b/prisma/seed.ts index 33f07ebf..436034ed 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -1,8 +1,9 @@ /* eslint-disable no-console */ -import { Currency, EmployementType, Role, WorkMode } from '@prisma/client'; +import { PrismaClient, Currency, EmployementType, Role, WorkMode } from '@prisma/client'; import { faker } from '@faker-js/faker'; import bcrypt from 'bcryptjs'; -import prisma from '../src/config/prisma.config'; + +const prisma = new PrismaClient(); const users = [ { id: '1', name: 'Jack', email: 'user@gmail.com' }, @@ -288,36 +289,44 @@ let jobs = [ async function seedUsers() { try { const hashedPassword = await bcrypt.hash('123456', 10); - await Promise.all( - users.map( - async (u) => - await prisma.user.upsert({ - where: { id: u.id }, - create: { - id: u.id, - email: u.email, - name: u.name, - password: hashedPassword, - role: u.role || Role.USER, - emailVerified: new Date(), - }, - update: {}, - }) - ) - ); - console.log('✅ user seed successfully'); - await prisma.$disconnect(); + for (const u of users) { + try { + await prisma.user.upsert({ + where: { email: u.email }, + update: {}, + create: { + id: u.id, + email: u.email, + name: u.name, + password: hashedPassword, + role: u.role || Role.USER, + emailVerified: new Date(), + }, + }); + console.log(`User created or updated: ${u.email}`); + } catch (error) { + console.log(`Error processing user ${u.email}:`, error); + } + } + console.log('✅ User seed completed'); } catch (error) { - console.log(error); - await prisma.$disconnect(); - process.exit(1); + console.error('Error seeding users:', error); } } async function seedJobs() { try { + + const existingUsers = await prisma.user.findMany({ + select: { id: true }, + }); + const existingUserIds = new Set(existingUsers.map(user => user.id)); + + + const validJobs = jobs.filter(job => existingUserIds.has(job.userId)); + await Promise.all( - jobs.map(async (j) => + validJobs.map(async (j) => prisma.job.upsert({ where: { id: j.id }, create: { @@ -358,12 +367,9 @@ async function seedJobs() { }) ) ); - console.log('✅ job seed successfully'); + console.log('✅ Job seed completed successfully'); } catch (error) { - console.error(error); - process.exit(1); - } finally { - await prisma.$disconnect(); + console.error('Error seeding jobs:', error); } } @@ -372,4 +378,4 @@ async function main() { await seedJobs(); } -main(); +main(); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 53d0ee4a..34a21d33 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,8 +6,8 @@ "strict": true, "noEmit": true, "esModuleInterop": true, - "module": "esnext", - "moduleResolution": "bundler", + "module": "ESNext", + "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "jsx": "preserve", @@ -19,7 +19,14 @@ ], "paths": { "@/*": ["./src/*"] - } + }, + "allowSyntheticDefaultImports": true, + "outDir": "./dist", + "target": "ES2020" + }, + "ts-node": { + "esm": true, + "experimentalSpecifierResolution": "node" }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], "exclude": ["node_modules"]