From 2cd70ef581f83d6a86a0f2b8bdbca1ef1fad9026 Mon Sep 17 00:00:00 2001 From: snowgitea Date: Wed, 29 Apr 2026 10:53:17 +0800 Subject: [PATCH] chore: add backend/prisma/seed.js --- backend/prisma/seed.js | 172 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 backend/prisma/seed.js diff --git a/backend/prisma/seed.js b/backend/prisma/seed.js new file mode 100644 index 0000000..c3fef09 --- /dev/null +++ b/backend/prisma/seed.js @@ -0,0 +1,172 @@ +/** + * Prisma Seed Script - 初始化测试数据 + * 用于恢复账务系统的测试数据 + */ +import { PrismaClient } from '@prisma/client'; + +const prisma = new PrismaClient(); + +async function main() { + console.log('开始恢复测试数据...'); + + // 清空现有数据(按依赖顺序) + await prisma.record.deleteMany(); + await prisma.budget.deleteMany(); + await prisma.account.deleteMany(); + await prisma.user.deleteMany(); + + // 1. 创建测试用户 + const user = await prisma.user.create({ + data: { + name: '测试用户', + email: 'test@example.com', + }, + }); + console.log(`创建用户: ${user.name} (ID: ${user.id})`); + + // 2. 创建账户(支付宝、微信、银行卡) + const accounts = await Promise.all([ + prisma.account.create({ + data: { + userId: user.id, + name: '支付宝', + balance: 5000.00, + type: 'Alipay', + color: '#1677FF', + }, + }), + prisma.account.create({ + data: { + userId: user.id, + name: '微信支付', + balance: 3000.00, + type: 'WeChat', + color: '#07C160', + }, + }), + prisma.account.create({ + data: { + userId: user.id, + name: '银行卡', + balance: 10000.00, + type: 'BankCard', + color: '#722ED1', + }, + }), + ]); + console.log(`创建 ${accounts.length} 个账户: ${accounts.map(a => a.name).join(', ')}`); + + // 3. 创建交易记录(至少6条) + const records = await Promise.all([ + // 支付宝 - 支出 + prisma.record.create({ + data: { + userId: user.id, + accountId: accounts[0].id, // 支付宝 + type: 'expense', + amount: 128.50, + category: '餐饮', + description: '午餐', + date: new Date('2026-04-25'), + }, + }), + // 微信 - 支出 + prisma.record.create({ + data: { + userId: user.id, + accountId: accounts[1].id, // 微信 + type: 'expense', + amount: 45.00, + category: '交通', + description: '地铁', + date: new Date('2026-04-25'), + }, + }), + // 银行卡 - 收入 + prisma.record.create({ + data: { + userId: user.id, + accountId: accounts[2].id, // 银行卡 + type: 'income', + amount: 15000.00, + category: '工资', + description: '月薪', + date: new Date('2026-04-20'), + }, + }), + // 支付宝 - 支出 + prisma.record.create({ + data: { + userId: user.id, + accountId: accounts[0].id, // 支付宝 + type: 'expense', + amount: 299.00, + category: '购物', + description: '网购商品', + date: new Date('2026-04-23'), + }, + }), + // 微信 - 收入 + prisma.record.create({ + data: { + userId: user.id, + accountId: accounts[1].id, // 微信 + type: 'income', + amount: 500.00, + category: '转账', + description: '朋友还款', + date: new Date('2026-04-22'), + }, + }), + // 银行卡 - 支出 + prisma.record.create({ + data: { + userId: user.id, + accountId: accounts[2].id, // 银行卡 + type: 'expense', + amount: 2000.00, + category: '房租', + description: '月租', + date: new Date('2026-04-01'), + }, + }), + // 支付宝 - 支出(额外) + prisma.record.create({ + data: { + userId: user.id, + accountId: accounts[0].id, + type: 'expense', + amount: 56.80, + category: '娱乐', + description: '电影票', + date: new Date('2026-04-24'), + }, + }), + ]); + console.log(`创建 ${records.length} 条交易记录`); + + // 4. 创建预算 + await prisma.budget.create({ + data: { + userId: user.id, + category: '餐饮', + amount: 2000.00, + month: '2026-04', + }, + }); + console.log('创建预算数据'); + + console.log('\n✅ 测试数据恢复完成!'); + console.log(`- 用户: 1 个`); + console.log(`- 账户: ${accounts.length} 个`); + console.log(`- 交易记录: ${records.length} 条`); +} + +main() + .catch((e) => { + console.error('❌ 数据恢复失败:', e); + process.exit(1); + }) + .finally(async () => { + await prisma.$disconnect(); + });