Added user seeder
This commit is contained in:
parent
991a9ed583
commit
219cc784b1
|
|
@ -1,12 +1,14 @@
|
||||||
import { PrismaClient } from "@prisma/client";
|
import { PrismaClient } from "@prisma/client";
|
||||||
import permissionSeed from "./seeds/permissionSeed";
|
import permissionSeed from "./seeds/permissionSeed";
|
||||||
import roleSeed from "./seeds/roleSeed";
|
import roleSeed from "./seeds/roleSeed";
|
||||||
|
import userSeed from "./seeds/userSeed";
|
||||||
|
|
||||||
const prisma = new PrismaClient();
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
await permissionSeed(prisma);
|
await permissionSeed(prisma);
|
||||||
await roleSeed(prisma);
|
await roleSeed(prisma);
|
||||||
|
await userSeed(prisma);
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
36
prisma/seeds/userSeed.ts
Normal file
36
prisma/seeds/userSeed.ts
Normal 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")
|
||||||
|
}
|
||||||
|
|
@ -9,6 +9,7 @@ import UserClaims from "./types/UserClaims";
|
||||||
/**
|
/**
|
||||||
* Hashes a plain text password using bcrypt.
|
* Hashes a plain text password using bcrypt.
|
||||||
*
|
*
|
||||||
|
* @deprecated
|
||||||
* @param password - The plain text password to hash.
|
* @param password - The plain text password to hash.
|
||||||
* @returns The hashed password.
|
* @returns The hashed password.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
12
src/features/auth/tools/hashPassword.ts
Normal file
12
src/features/auth/tools/hashPassword.ts
Normal 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);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user