Files
money.sori.studio/prisma/seed.ts

58 lines
1.8 KiB
TypeScript

import { hash } from 'bcryptjs'
import { PrismaClient } from '@prisma/client'
import { createDefaultMoneyData } from '../server/utils/defaults'
const prisma = new PrismaClient()
async function main() {
const user = await prisma.user.upsert({
where: { email: 'user@example.com' },
update: {},
create: {
email: 'user@example.com',
name: '사용자',
passwordHash: await hash('money1234', 10)
}
})
const assetCount = await prisma.asset.count({ where: { userId: user.id } })
const categoryCount = await prisma.category.count({ where: { userId: user.id } })
if (assetCount === 0 && categoryCount === 0) {
await createDefaultMoneyData(prisma, user.id)
}
const transactionCount = await prisma.transaction.count({ where: { userId: user.id } })
if (transactionCount === 0) {
const cash = await prisma.asset.findFirstOrThrow({ where: { userId: user.id }, orderBy: { sortOrder: 'asc' } })
const income = await prisma.category.findFirstOrThrow({ where: { userId: user.id, type: 'income', name: '기본 수입' } })
const expense = await prisma.category.findFirstOrThrow({ where: { userId: user.id, type: 'expense', name: '기본 지출' } })
await prisma.transaction.createMany({
data: [
{
userId: user.id,
type: 'income',
occurredAt: new Date(),
assetId: cash.id,
categoryId: income.id,
amount: 3200000,
title: '월급'
},
{
userId: user.id,
type: 'expense',
occurredAt: new Date(),
assetId: cash.id,
categoryId: expense.id,
amount: 14980,
title: '이마트칠성점'
}
]
})
}
}
main()
.finally(async () => {
await prisma.$disconnect()
})