Skip to content

Commit cace2ea

Browse files
committed
chore: add seed data
1 parent fc0461a commit cace2ea

2 files changed

Lines changed: 152 additions & 0 deletions

File tree

apps/api/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,8 @@
3838
"superjson": "^2.2.5",
3939
"zeptomail": "^6.2.1",
4040
"zod": "^4.1.9"
41+
},
42+
"prisma": {
43+
"seed": "tsx prisma/seed.ts"
4144
}
4245
}

apps/api/prisma/seed.ts

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import { PrismaClient } from '@prisma/client';
2+
3+
const prisma = new PrismaClient();
4+
5+
async function main() {
6+
console.log('🌱 Starting database seed...');
7+
8+
// Clear existing data (optional - only if you want fresh data each time)
9+
// Uncomment if you want to reset data on each seed
10+
// await prisma.testimonial.deleteMany();
11+
// await prisma.payment.deleteMany();
12+
// await prisma.subscription.deleteMany();
13+
// await prisma.account.deleteMany();
14+
// await prisma.user.deleteMany();
15+
// await prisma.plan.deleteMany();
16+
17+
// Create test plan (1 rupee test plan)
18+
const testPlan = await prisma.plan.upsert({
19+
where: { id: '385b8215-d70f-473e-81c9-68a673c0d2fc-test' },
20+
update: {},
21+
create: {
22+
id: '385b8215-d70f-473e-81c9-68a673c0d2fc-test',
23+
name: 'Test Plan',
24+
interval: 'yearly',
25+
price: 100, // 1 rupee in paise
26+
currency: 'INR',
27+
},
28+
});
29+
console.log('✅ Created test plan:', testPlan.id);
30+
31+
// Create test user
32+
const testUser = await prisma.user.upsert({
33+
where: { email: 'test@example.com' },
34+
update: {},
35+
create: {
36+
email: 'test@example.com',
37+
firstName: 'Test User',
38+
authMethod: 'google',
39+
},
40+
});
41+
console.log('✅ Created test user:', testUser.email);
42+
43+
// Create test user with premium subscription
44+
const premiumUser = await prisma.user.upsert({
45+
where: { email: 'premium@example.com' },
46+
update: {},
47+
create: {
48+
email: 'premium@example.com',
49+
firstName: 'Premium User',
50+
authMethod: 'github',
51+
},
52+
});
53+
console.log('✅ Created premium user:', premiumUser.email);
54+
55+
// Create premium subscription for premium user
56+
const existingSubscription = await prisma.subscription.findFirst({
57+
where: {
58+
userId: premiumUser.id,
59+
},
60+
});
61+
62+
const premiumSubscription = existingSubscription
63+
? await prisma.subscription.update({
64+
where: { id: existingSubscription.id },
65+
data: {
66+
planId: testPlan.id,
67+
status: 'active',
68+
startDate: new Date(),
69+
endDate: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000), // 1 year from now
70+
autoRenew: true,
71+
},
72+
})
73+
: await prisma.subscription.create({
74+
data: {
75+
userId: premiumUser.id,
76+
planId: testPlan.id,
77+
status: 'active',
78+
startDate: new Date(),
79+
endDate: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000), // 1 year from now
80+
autoRenew: true,
81+
},
82+
});
83+
console.log('✅ Created/updated premium subscription');
84+
85+
// Create test payment
86+
const testPayment = await prisma.payment.upsert({
87+
where: {
88+
razorpayPaymentId: 'pay_test_123456789',
89+
},
90+
update: {},
91+
create: {
92+
userId: premiumUser.id,
93+
subscriptionId: premiumSubscription.id,
94+
razorpayPaymentId: 'pay_test_123456789',
95+
razorpayOrderId: 'order_test_123456789',
96+
amount: 100, // 1 rupee in paise
97+
currency: 'INR',
98+
status: 'captured',
99+
},
100+
});
101+
console.log('✅ Created test payment');
102+
103+
// Create test testimonial (if Testimonial model exists)
104+
try {
105+
const testTestimonial = await (prisma as any).testimonial.upsert({
106+
where: {
107+
userId: premiumUser.id,
108+
},
109+
update: {},
110+
create: {
111+
userId: premiumUser.id,
112+
name: 'Premium User',
113+
content: 'Opensox has been amazing! The platform makes managing open source projects so much easier.',
114+
avatar: 'https://i.pravatar.cc/150?u=premium',
115+
},
116+
});
117+
console.log('✅ Created test testimonial');
118+
} catch (error) {
119+
// Testimonial model might not exist in current schema
120+
console.log('⚠️ Skipping testimonial (model may not exist)');
121+
}
122+
123+
// Create QueryCount if it doesn't exist
124+
try {
125+
await prisma.queryCount.upsert({
126+
where: { id: 1 },
127+
update: {},
128+
create: {
129+
id: 1,
130+
total_queries: BigInt(2212),
131+
},
132+
});
133+
console.log('✅ Created QueryCount');
134+
} catch (error) {
135+
console.log('⚠️ QueryCount already exists or error:', error);
136+
}
137+
138+
console.log('✅ Database seed completed successfully!');
139+
}
140+
141+
main()
142+
.catch((e) => {
143+
console.error('❌ Error seeding database:', e);
144+
process.exit(1);
145+
})
146+
.finally(async () => {
147+
await prisma.$disconnect();
148+
});
149+

0 commit comments

Comments
 (0)