feat(db) - update user seeding logic to use bcrypt for password hashing; add peer dependencies in package.json; enhance appointment creation with staff association and skipDuplicates option

This commit is contained in:
2026-02-27 21:44:01 -05:00
parent 4cb7ec7e2e
commit b89084e7cb
3 changed files with 86 additions and 37 deletions

View File

@@ -22,7 +22,9 @@
"prisma-zod-generator": "^2.1.2"
},
"devDependencies": {
"@types/bcrypt": "^5.0.2",
"@types/node": "^22.15.17",
"bcrypt": "^5.1.1",
"prisma": "^7.0.0",
"tsx": "^4.19.4",
"typescript": "^5.8.2"

View File

@@ -1,32 +1,43 @@
import dotenv from "dotenv";
import path from "path";
dotenv.config({ path: path.resolve(__dirname, ".env") });
import { PrismaClient } from "../generated/prisma";
const prisma = new PrismaClient();
import { PrismaPg } from "@prisma/adapter-pg";
import bcrypt from "bcrypt";
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
const prisma = new PrismaClient({ adapter });
function formatTime(date: Date): string {
return date.toTimeString().slice(0, 5); // "HH:MM"
}
async function main() {
// Create multiple users
const users = await prisma.user.createMany({
data: [
{ username: "admin2", password: "123456" },
{ username: "bob", password: "123456" },
],
const hash = (pw: string) => bcrypt.hash(pw, 10);
const adminUser = await prisma.user.upsert({
where: { username: "admin" },
update: {},
create: { username: "admin", password: await hash("123456"), role: "ADMIN" },
});
const aaaUser = await prisma.user.upsert({
where: { username: "aaa" },
update: {},
create: { username: "aaa", password: await hash("aaa"), role: "USER" },
});
const createdUsers = await prisma.user.findMany();
// Creatin staff
await prisma.staff.createMany({
data: [
{ name: "Dr. Kai Gao", role: "Doctor" },
{ name: "Dr. Jane Smith", role: "Doctor" },
],
skipDuplicates: true,
});
const staffMembers = await prisma.staff.findMany();
// Create multiple patients
const patients = await prisma.patient.createMany({
data: [
{
@@ -54,33 +65,39 @@ async function main() {
userId: createdUsers[1].id,
},
],
skipDuplicates: true,
});
const createdPatients = await prisma.patient.findMany();
// Create multiple appointments
await prisma.appointment.createMany({
data: [
{
patientId: createdPatients[0].id,
userId: createdUsers[0].id,
title: "Initial Consultation",
date: new Date("2025-06-01"),
startTime: formatTime(new Date("2025-06-01T10:00:00")),
endTime: formatTime(new Date("2025-06-01T10:30:00")),
type: "consultation",
},
{
patientId: createdPatients[1].id,
userId: createdUsers[1].id,
title: "Follow-up",
date: new Date("2025-06-02"),
startTime: formatTime(new Date("2025-06-01T10:00:00")),
endTime: formatTime(new Date("2025-06-01T10:30:00")),
type: "checkup",
},
],
});
const staffMembers = await prisma.staff.findMany();
if (createdPatients.length >= 2 && createdUsers.length >= 2 && staffMembers.length >= 1) {
await prisma.appointment.createMany({
data: [
{
patientId: createdPatients[0].id,
userId: createdUsers[0].id,
staffId: staffMembers[0].id,
title: "Initial Consultation",
date: new Date("2025-06-01"),
startTime: formatTime(new Date("2025-06-01T10:00:00")),
endTime: formatTime(new Date("2025-06-01T10:30:00")),
type: "consultation",
},
{
patientId: createdPatients[1].id,
userId: createdUsers[1].id,
staffId: staffMembers[0].id,
title: "Follow-up",
date: new Date("2025-06-02"),
startTime: formatTime(new Date("2025-06-01T10:00:00")),
endTime: formatTime(new Date("2025-06-01T10:30:00")),
type: "checkup",
},
],
skipDuplicates: true,
});
}
}
main()