create project
This commit is contained in:
60
backend/prisma/schema.prisma
Normal file
60
backend/prisma/schema.prisma
Normal file
@@ -0,0 +1,60 @@
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(cuid())
|
||||
phone String @unique
|
||||
passwordHash String
|
||||
nickname String
|
||||
email String?
|
||||
familyIds String[]
|
||||
activeFamilyId String?
|
||||
joinDate DateTime @default(now())
|
||||
createdAt DateTime @default(now())
|
||||
bills Bill[]
|
||||
familiesCreated Family[] @relation("FamilyCreator")
|
||||
}
|
||||
|
||||
model Family {
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
inviteCode String @unique
|
||||
createdBy String
|
||||
creator User @relation("FamilyCreator", fields: [createdBy], references: [id])
|
||||
createdAt DateTime @default(now())
|
||||
members FamilyMember[]
|
||||
bills Bill[]
|
||||
}
|
||||
|
||||
model FamilyMember {
|
||||
id String @id @default(cuid())
|
||||
familyId String
|
||||
family Family @relation(fields: [familyId], references: [id], onDelete: Cascade)
|
||||
userId String
|
||||
nickname String
|
||||
role String
|
||||
joinedAt DateTime @default(now())
|
||||
@@unique([familyId, userId])
|
||||
}
|
||||
|
||||
model Bill {
|
||||
id String @id @default(cuid())
|
||||
userId String
|
||||
user User @relation(fields: [userId], references: [id])
|
||||
familyId String?
|
||||
family Family? @relation(fields: [familyId], references: [id])
|
||||
type String
|
||||
amount Float
|
||||
category String
|
||||
note String?
|
||||
emoji String?
|
||||
time String
|
||||
date DateTime
|
||||
createdAt DateTime @default(now())
|
||||
}
|
||||
75
backend/prisma/seed.ts
Normal file
75
backend/prisma/seed.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main() {
|
||||
const passwordHash = await bcrypt.hash('123456', 10);
|
||||
|
||||
const user = await prisma.user.upsert({
|
||||
where: { phone: '13800138000' },
|
||||
update: {},
|
||||
create: {
|
||||
phone: '13800138000',
|
||||
passwordHash,
|
||||
nickname: '小明',
|
||||
joinDate: new Date('2026-03-01'),
|
||||
},
|
||||
});
|
||||
|
||||
const family = await prisma.family.upsert({
|
||||
where: { id: 'f_preset_home' },
|
||||
update: {},
|
||||
create: {
|
||||
id: 'f_preset_home',
|
||||
name: '小明的家',
|
||||
inviteCode: 'ABCDEF',
|
||||
createdBy: user.id,
|
||||
members: {
|
||||
create: {
|
||||
userId: user.id,
|
||||
nickname: '小明',
|
||||
role: 'owner',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: {
|
||||
familyIds: [family.id],
|
||||
activeFamilyId: family.id,
|
||||
},
|
||||
});
|
||||
|
||||
const sampleBills = [
|
||||
{ type: 'expense' as const, amount: 32, category: '餐饮', emoji: '🍜', note: '午餐-兰州拉面', time: '12:30', date: new Date('2026-05-01') },
|
||||
{ type: 'expense' as const, amount: 15, category: '交通', emoji: '🚇', note: '地铁通勤', time: '08:45', date: new Date('2026-05-02') },
|
||||
{ type: 'expense' as const, amount: 128, category: '购物', emoji: '🛒', note: '超市采购', time: '18:20', date: new Date('2026-05-03') },
|
||||
{ type: 'expense' as const, amount: 45, category: '餐饮', emoji: '☕', note: '星巴克咖啡', time: '15:00', date: new Date('2026-05-03') },
|
||||
{ type: 'income' as const, amount: 15000, category: '工资', emoji: '💰', note: '5月工资', time: '10:00', date: new Date('2026-05-05') },
|
||||
{ type: 'expense' as const, amount: 200, category: '居住', emoji: '🏠', note: '物业费', time: '09:00', date: new Date('2026-05-06') },
|
||||
{ type: 'expense' as const, amount: 88, category: '餐饮', emoji: '🍣', note: '和朋友聚餐', time: '19:30', date: new Date('2026-05-07') },
|
||||
{ type: 'expense' as const, amount: 56, category: '交通', emoji: '🚕', note: '打车回家', time: '22:15', date: new Date('2026-05-08') },
|
||||
];
|
||||
|
||||
for (const bill of sampleBills) {
|
||||
await prisma.bill.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
familyId: family.id,
|
||||
...bill,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
console.log('✅ 种子数据已创建:');
|
||||
console.log(' 预设账号: 13800138000 / 123456');
|
||||
console.log(' 家庭: 小明的家');
|
||||
console.log(' 账单: 8 条示例');
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => { console.error(e); process.exit(1); })
|
||||
.finally(() => prisma.$disconnect());
|
||||
Reference in New Issue
Block a user