seed-user.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { MenuType, PrismaClient } from '@prisma/mysql/client';
  2. const prisma = new PrismaClient();
  3. async function createUsers() {
  4. const role = await prisma.role.create({
  5. data: {
  6. name: '管理员',
  7. remark: '管理员专用',
  8. },
  9. });
  10. const adminUser = await prisma.user.create({
  11. data: {
  12. username: 'admin',
  13. password: '$2b$12$iS0UJ1YqSal0N3uwin/OvOABUINAclcZGjHNyGFC7mlwRYTFjGQ26',
  14. // $2b$12$W9.5wXqjTtHgBilPFpPx2.lpIvrY.HpnHSrEURg9YBiV3e/8tfn3m
  15. remark: '默认拥有所有菜单权限,不需要配置角色',
  16. // allowIps: ['127.0.0.1'],
  17. // quota: 1000000,
  18. },
  19. });
  20. await prisma.userRole.create({
  21. data: {
  22. userId: adminUser.id,
  23. roleId: role.id,
  24. },
  25. });
  26. }
  27. async function main() {
  28. console.log('开始初始化数据库...');
  29. await createUsers();
  30. console.log('数据库初始化完成');
  31. }
  32. main()
  33. .then(async () => {
  34. await prisma.$disconnect();
  35. })
  36. .catch(async (e) => {
  37. console.error(e);
  38. await prisma.$disconnect();
  39. process.exit(1);
  40. });