Files
gerenjizhang/backend/init-data.js
T

206 lines
5.3 KiB
JavaScript

// Initialize test data
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function main() {
console.log('🚀 开始初始化测试数据...');
// 1. 创建测试用户
console.log('📝 创建测试用户...');
const user = await prisma.user.create({
data: {
name: '测试用户',
email: 'test@example.com',
},
});
console.log(`✅ 用户创建成功: ${user.name} (ID: ${user.id})`);
// 2. 创建测试账户
console.log('💰 创建测试账户...');
const accounts = await Promise.all([
prisma.account.create({
data: {
userId: user.id,
name: '支付宝',
type: 'payment',
color: '#1890FF',
balance: 5000,
},
}),
prisma.account.create({
data: {
userId: user.id,
name: '微信钱包',
type: 'payment',
color: '#52C41A',
balance: 3000,
},
}),
prisma.account.create({
data: {
userId: user.id,
name: '招商银行',
type: 'bank',
color: '#FAAD14',
balance: 10000,
},
}),
]);
console.log(`${accounts.length} 个账户创建成功`);
// 3. 创建测试交易记录
console.log('📊 创建测试交易记录...');
const today = new Date();
const records = await Promise.all([
// 收入记录
prisma.record.create({
data: {
userId: user.id,
accountId: accounts[0].id,
type: 'income',
amount: 8500,
category: '工资',
description: '2026年4月工资',
date: new Date(today.getFullYear(), today.getMonth(), 1),
},
}),
prisma.record.create({
data: {
userId: user.id,
accountId: accounts[2].id,
type: 'income',
amount: 500,
category: '奖金',
description: '绩效奖金',
date: new Date(today.getFullYear(), today.getMonth(), 5),
},
}),
// 支出记录
prisma.record.create({
data: {
userId: user.id,
accountId: accounts[0].id,
type: 'expense',
amount: 68,
category: '餐饮',
description: '午饭',
date: new Date(today.getFullYear(), today.getMonth(), today.getDate()),
},
}),
prisma.record.create({
data: {
userId: user.id,
accountId: accounts[1].id,
type: 'expense',
amount: 25,
category: '交通',
description: '打车',
date: new Date(today.getFullYear(), today.getMonth(), today.getDate()),
},
}),
prisma.record.create({
data: {
userId: user.id,
accountId: accounts[0].id,
type: 'expense',
amount: 299,
category: '购物',
description: '买衣服',
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - 2),
},
}),
prisma.record.create({
data: {
userId: user.id,
accountId: accounts[1].id,
type: 'expense',
amount: 128,
category: '娱乐',
description: '游戏充值',
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - 3),
},
}),
prisma.record.create({
data: {
userId: user.id,
accountId: accounts[0].id,
type: 'expense',
amount: 38,
category: '餐饮',
description: '星巴克',
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - 4),
},
}),
prisma.record.create({
data: {
userId: user.id,
accountId: accounts[1].id,
type: 'expense',
amount: 150,
category: '娱乐',
description: '电影票',
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - 5),
},
}),
]);
console.log(`${records.length} 条交易记录创建成功`);
// 4. 创建测试预算
console.log('📋 创建测试预算...');
const budgets = await Promise.all([
prisma.budget.create({
data: {
userId: user.id,
category: '餐饮',
amount: 1500,
month: `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}`,
},
}),
prisma.budget.create({
data: {
userId: user.id,
category: '交通',
amount: 500,
month: `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}`,
},
}),
prisma.budget.create({
data: {
userId: user.id,
category: '购物',
amount: 1000,
month: `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}`,
},
}),
prisma.budget.create({
data: {
userId: user.id,
category: '娱乐',
amount: 500,
month: `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}`,
},
}),
]);
console.log(`${budgets.length} 个预算创建成功`);
console.log('\n🎉 测试数据初始化完成!');
console.log('\n📌 使用信息:');
console.log(`- User ID: ${user.id}`);
console.log(`- Email: ${user.email}`);
console.log(`- 账户数: ${accounts.length}`);
console.log(`- 记录数: ${records.length}`);
console.log(`- 预算数: ${budgets.length}`);
}
main()
.catch((e) => {
console.error('❌ 初始化失败:', e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});