rwadurian/backend/services/identity-service/prisma/seed.ts

92 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
// ============================================
// 系统账户定义
// ============================================
const SYSTEM_ACCOUNTS = [
{
userId: BigInt(1),
accountSequence: BigInt(1),
nickname: '总部社区',
referralCode: 'HQ000001',
provinceCode: '000000',
cityCode: '000000',
status: 'SYSTEM',
},
{
userId: BigInt(2),
accountSequence: BigInt(2),
nickname: '成本费账户',
referralCode: 'COST0002',
provinceCode: '000000',
cityCode: '000000',
status: 'SYSTEM',
},
{
userId: BigInt(3),
accountSequence: BigInt(3),
nickname: '运营费账户',
referralCode: 'OPER0003',
provinceCode: '000000',
cityCode: '000000',
status: 'SYSTEM',
},
{
userId: BigInt(4),
accountSequence: BigInt(4),
nickname: 'RWAD底池账户',
referralCode: 'POOL0004',
provinceCode: '000000',
cityCode: '000000',
status: 'SYSTEM',
},
];
async function main() {
console.log('Seeding database...');
// 清理现有数据
await prisma.deadLetterEvent.deleteMany();
await prisma.smsCode.deleteMany();
await prisma.userEvent.deleteMany();
await prisma.deviceToken.deleteMany();
await prisma.walletAddress.deleteMany();
await prisma.userDevice.deleteMany();
await prisma.userAccount.deleteMany();
// 初始化账户序列号生成器 (从100000开始系统账户使用1-99)
await prisma.accountSequenceGenerator.deleteMany();
await prisma.accountSequenceGenerator.create({
data: {
id: 1,
currentSequence: BigInt(100000), // 普通用户从100000开始
},
});
// 创建系统账户
console.log('Creating system accounts...');
for (const account of SYSTEM_ACCOUNTS) {
await prisma.userAccount.upsert({
where: { userId: account.userId },
update: account,
create: account,
});
console.log(` - Created system account: ${account.nickname} (userId=${account.userId})`);
}
console.log('Database seeded successfully!');
console.log('- Initialized account sequence generator starting at 100000');
console.log(`- Created ${SYSTEM_ACCOUNTS.length} system accounts (userId 1-4)`);
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});