1
1
import { db } from "@/db/drizzle" ;
2
+ import { sql } from "drizzle-orm" ;
2
3
import {
3
4
users ,
4
5
orgs ,
@@ -9,6 +10,7 @@ import {
9
10
groups ,
10
11
groupMemberships ,
11
12
sessionTable ,
13
+ SelectUser ,
12
14
} from "@/db/schema" ;
13
15
import { hashPassword } from "./password" ;
14
16
@@ -45,6 +47,15 @@ const seedConfig = {
45
47
46
48
export async function seedDatabase ( config = seedConfig ) {
47
49
try {
50
+ // Check if database is already populated
51
+ /*
52
+ const isEmpty = await isDatabaseEmpty();
53
+ if (!isEmpty) {
54
+ console.log("Database already contains data. Skipping seed operation.");
55
+ return false;
56
+ }
57
+ */
58
+
48
59
// Create users
49
60
const users = await createUsers ( config . users ) ;
50
61
const orgs = await createOrganizations ( config . organizations , users ) ;
@@ -59,9 +70,9 @@ export async function seedDatabase(config = seedConfig) {
59
70
60
71
async function createUsers ( userConfig : typeof seedConfig . users ) {
61
72
const createdUsers = {
62
- admins : [ ] as any [ ] ,
63
- organizers : [ ] as any [ ] ,
64
- members : [ ] as any [ ] ,
73
+ admins : [ ] as SelectUser [ ] ,
74
+ organizers : [ ] as SelectUser [ ] ,
75
+ members : [ ] as SelectUser [ ] ,
65
76
} ;
66
77
67
78
const hashedPassword = await hashPassword ( userConfig . password ) ;
@@ -114,7 +125,11 @@ async function createUsers(userConfig: typeof seedConfig.users) {
114
125
115
126
async function createOrganizations (
116
127
orgConfig : typeof seedConfig . organizations ,
117
- users : ReturnType < typeof createUsers > extends Promise < infer T > ? T : never ,
128
+ users : {
129
+ admins : SelectUser [ ] ;
130
+ organizers : SelectUser [ ] ;
131
+ members : SelectUser [ ] ;
132
+ }
118
133
) {
119
134
const createdOrgs = [ ] ;
120
135
@@ -151,33 +166,40 @@ async function createOrganizations(
151
166
return createdOrgs ;
152
167
}
153
168
154
- async function createOrgMemberships ( orgId : number , users : any ) {
155
- const memberships = [
169
+ async function createOrgMemberships (
170
+ orgId : number ,
171
+ users : {
172
+ admins : SelectUser [ ] ;
173
+ organizers : SelectUser [ ] ;
174
+ members : SelectUser [ ] ;
175
+ }
176
+ ) {
177
+ const membershipValues = [
156
178
// Assign first admin as owner
157
179
{
158
180
userId : users . admins [ 0 ] . id ,
159
181
orgId,
160
182
role : "owner" as const ,
161
183
} ,
162
184
// Assign organizers
163
- ...users . organizers . map ( ( user ) => ( {
185
+ ...users . organizers . map ( ( user : SelectUser ) => ( {
164
186
userId : user . id ,
165
187
orgId,
166
188
role : "organizer" as const ,
167
189
} ) ) ,
168
190
// Assign members
169
- ...users . members . map ( ( user ) => ( {
191
+ ...users . members . map ( ( user : SelectUser ) => ( {
170
192
userId : user . id ,
171
193
orgId,
172
194
role : "member" as const ,
173
195
} ) ) ,
174
196
] ;
175
197
176
- await db . insert ( memberships ) . values ( memberships ) ;
198
+ await db . insert ( memberships ) . values ( membershipValues ) ;
177
199
}
178
200
179
201
async function createProblems ( orgId : number , count : number ) {
180
- const createdProblems = [ ] ;
202
+ const createdProblems : Array < typeof problems . $inferSelect > = [ ] ;
181
203
182
204
for ( let i = 0 ; i < count ; i ++ ) {
183
205
const [ problem ] = await db
@@ -198,7 +220,7 @@ async function createProblems(orgId: number, count: number) {
198
220
199
221
async function createContests (
200
222
orgId : number ,
201
- problems : any [ ] ,
223
+ problems : Array < typeof problems . $inferSelect > ,
202
224
contestConfig : typeof seedConfig . contests ,
203
225
) {
204
226
const now = new Date ( ) ;
@@ -225,6 +247,8 @@ async function createContests(
225
247
) ,
226
248
organizerId : orgId ,
227
249
organizerKind : "org" ,
250
+ allowList : [ ] , // Add missing required fields
251
+ disallowList : [ ] , // Add missing required fields
228
252
} )
229
253
. returning ( ) ;
230
254
@@ -243,7 +267,11 @@ async function createContests(
243
267
244
268
async function createGroups (
245
269
orgId : number ,
246
- users : any ,
270
+ users : {
271
+ admins : SelectUser [ ] ;
272
+ organizers : SelectUser [ ] ;
273
+ members : SelectUser [ ] ;
274
+ } ,
247
275
groupCount : number ,
248
276
membersPerGroup : number ,
249
277
) {
@@ -259,7 +287,7 @@ async function createGroups(
259
287
. returning ( ) ;
260
288
261
289
// Add random members to group
262
- const memberUsers = users . members . slice ( 0 , membersPerGroup ) . map ( ( user ) => ( {
290
+ const memberUsers = users . members . slice ( 0 , membersPerGroup ) . map ( ( user : SelectUser ) => ( {
263
291
groupId : group . id ,
264
292
userId : user . id ,
265
293
} ) ) ;
@@ -291,6 +319,6 @@ export async function clearDatabase() {
291
319
}
292
320
293
321
export async function isDatabaseEmpty ( ) {
294
- const userCount = await db . select ( { count : db . fn . count ( ) } ) . from ( users ) ;
295
- return userCount [ 0 ] . count === 0 ;
322
+ const userCount = await db . select ( { count : sql ` count(*)` } ) . from ( users ) ;
323
+ return parseInt ( userCount [ 0 ] . count as string ) === 0 ;
296
324
}
0 commit comments