-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed database seeding script to handle existing users and ensure val…
…id foreign key relationships when creating jobs. (#446)
- Loading branch information
Showing
3 changed files
with
50 additions
and
36 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: '[email protected]' }, | ||
|
@@ -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(); |
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