Added user seeder

This commit is contained in:
Sianida26 2024-01-29 00:18:26 +07:00
parent 991a9ed583
commit 219cc784b1
4 changed files with 52 additions and 1 deletions

View File

@ -1,12 +1,14 @@
import { PrismaClient } from "@prisma/client";
import permissionSeed from "./seeds/permissionSeed";
import roleSeed from "./seeds/roleSeed";
import userSeed from "./seeds/userSeed";
const prisma = new PrismaClient();
async function main() {
await permissionSeed(prisma);
await roleSeed(prisma);
await userSeed(prisma);
}
main()

36
prisma/seeds/userSeed.ts Normal file
View File

@ -0,0 +1,36 @@
import hashPassword from "../../src/features/auth/tools/hashPassword";
import { User, PrismaClient, Prisma } from "@prisma/client";
import { DefaultArgs } from "@prisma/client/runtime/library";
import { log } from "console";
export default async function userSeed(prisma: PrismaClient) {
log("Seeding users...")
const userData: Prisma.UserUncheckedCreateInput[] = [
{
email: "superadmin@example.com",
name: "Super Admin",
roles: {
connect: {
code: "super-admin"
}
},
passwordHash: await hashPassword("123456")
}
] as const;
await Promise.all(
userData.map(async (user) => {
await prisma.user.upsert({
where: {
email: user.email ?? undefined
},
update: user,
create: user
})
})
)
console.log("users is seeded successfully")
}

View File

@ -9,6 +9,7 @@ import UserClaims from "./types/UserClaims";
/**
* Hashes a plain text password using bcrypt.
*
* @deprecated
* @param password - The plain text password to hash.
* @returns The hashed password.
*/

View File

@ -0,0 +1,12 @@
import bcrypt from "bcrypt";
import authConfig from "../../../config/auth";
/**
* Hashes a plain text password using bcrypt.
*
* @param password - The plain text password to hash.
* @returns The hashed password.
*/
export default async function hashPassword(password: string): Promise<string> {
return bcrypt.hash(password, authConfig.saltRounds);
}