48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
|
|
import { PrismaClient } from '@prisma/client'
|
||
|
|
import { hash } from 'bcryptjs'
|
||
|
|
|
||
|
|
const prisma = new PrismaClient()
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
console.log('🚀 开始执行种子脚本...')
|
||
|
|
|
||
|
|
try {
|
||
|
|
// 首先检查用户是否已存在
|
||
|
|
const existingUser = await prisma.user.findUnique({
|
||
|
|
where: { phone: '17516219072' }
|
||
|
|
})
|
||
|
|
|
||
|
|
if (existingUser) {
|
||
|
|
console.log('✅ 用户已存在:', existingUser)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('🔐 加密密码...')
|
||
|
|
const password = await hash('123456', 10)
|
||
|
|
console.log('✅ 加密完成')
|
||
|
|
|
||
|
|
console.log('👤 创建用户...')
|
||
|
|
const user = await prisma.user.create({
|
||
|
|
data: {
|
||
|
|
phone: '17516219072',
|
||
|
|
password: password,
|
||
|
|
name: '测试用户',
|
||
|
|
},
|
||
|
|
})
|
||
|
|
console.log('✅ 用户创建成功:', user)
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('❌ 种子脚本错误:', error)
|
||
|
|
throw error
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
main()
|
||
|
|
.catch((e) => {
|
||
|
|
console.error('❌ 种子脚本执行失败:', e)
|
||
|
|
process.exit(1)
|
||
|
|
})
|
||
|
|
.finally(async () => {
|
||
|
|
await prisma.$disconnect()
|
||
|
|
console.log('🔚 数据库连接已关闭')
|
||
|
|
})
|