first commit
This commit is contained in:
52
prisma/seed.mjs
Normal file
52
prisma/seed.mjs
Normal file
@@ -0,0 +1,52 @@
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
import { randomBytes, scryptSync } from 'node:crypto'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
function hashPassword(password) {
|
||||
const salt = randomBytes(16).toString('base64url')
|
||||
const derivedKey = scryptSync(password, salt, 64).toString('base64url')
|
||||
return `scrypt$${salt}$${derivedKey}`
|
||||
}
|
||||
|
||||
async function upsertUser({ email, password }) {
|
||||
const passwordHash = hashPassword(password)
|
||||
|
||||
await prisma.user.upsert({
|
||||
where: { email },
|
||||
update: {
|
||||
passwordHash
|
||||
},
|
||||
create: {
|
||||
email,
|
||||
passwordHash
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await upsertUser({
|
||||
email: 'student@example.com',
|
||||
password: 'student123'
|
||||
})
|
||||
|
||||
await upsertUser({
|
||||
email: 'admin@example.com',
|
||||
password: 'admin123'
|
||||
})
|
||||
|
||||
await upsertUser({
|
||||
email: 'limited@example.com',
|
||||
password: 'limited123'
|
||||
})
|
||||
}
|
||||
|
||||
main()
|
||||
.then(async () => {
|
||||
await prisma.$disconnect()
|
||||
})
|
||||
.catch(async (error) => {
|
||||
console.error('Seed failed:', error)
|
||||
await prisma.$disconnect()
|
||||
process.exit(1)
|
||||
})
|
||||
Reference in New Issue
Block a user