添加数据库初始化脚本

This commit is contained in:
wmp
2025-09-16 09:46:25 +08:00
parent 57f1820338
commit a25ce604f0
2 changed files with 61 additions and 48 deletions

61
prisma/init.sql Normal file
View File

@@ -0,0 +1,61 @@
-- jdbox.accounts definition
CREATE TABLE `accounts` (
`id` varchar(191) NOT NULL,
`user_id` int(11) NOT NULL,
`type` varchar(191) NOT NULL,
`provider` varchar(191) NOT NULL,
`provider_account_id` varchar(191) NOT NULL,
`refresh_token` text DEFAULT NULL,
`access_token` text DEFAULT NULL,
`expires_at` int(11) DEFAULT NULL,
`token_type` varchar(191) DEFAULT NULL,
`scope` varchar(191) DEFAULT NULL,
`id_token` text DEFAULT NULL,
`session_state` varchar(191) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `accounts_provider_provider_account_id_key` (`provider`,`provider_account_id`),
KEY `accounts_user_id_fkey` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- jdbox.sessions definition
CREATE TABLE `sessions` (
`id` varchar(191) NOT NULL,
`expires` datetime(3) NOT NULL,
`createdAt` datetime(3) NOT NULL DEFAULT current_timestamp(3),
`userId` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `sessions_userId_idx` (`userId`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- jdbox.users definition
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(191) DEFAULT NULL,
`createdAt` datetime(3) NOT NULL DEFAULT current_timestamp(3),
`password` varchar(191) NOT NULL,
`phone` varchar(191) NOT NULL,
`updatedAt` datetime(3) NOT NULL,
`verifiedPhone` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `users_phone_key` (`phone`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- jdbox.verification_codes definition
CREATE TABLE `verification_codes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`phone` varchar(191) NOT NULL,
`code` varchar(191) NOT NULL,
`type` varchar(191) NOT NULL,
`expiresAt` datetime(3) NOT NULL,
`createdAt` datetime(3) NOT NULL DEFAULT current_timestamp(3),
PRIMARY KEY (`id`),
KEY `verification_codes_phone_type_idx` (`phone`,`type`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- 插入初始用户
INSERT INTO users(phone, password, name) VALUES('admin', '$2a$10$k.p3.s28OdLmGCMtuvBoqOxABp03h0Zhmop4eqqlR8sIjkThCcsnS', '管理员');

View File

@@ -1,48 +0,0 @@
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('🔚 数据库连接已关闭')
})