Skip to content

Commit 2bf85c6

Browse files
refactor: simplify function signatures in drizzle, kysely, mongodb, and prisma adapters for improved clarity and type safety
1 parent 35c5b01 commit 2bf85c6

File tree

4 files changed

+31
-125
lines changed

4 files changed

+31
-125
lines changed

src/adapters/drizzle/drizzle-adapter.ts

+8-31
Original file line numberDiff line numberDiff line change
@@ -249,27 +249,19 @@ export function drizzleAdapter<
249249
}
250250
}
251251
return {
252-
async create<M extends keyof Models>({
252+
async create({
253253
model,
254254
data: values,
255-
}: {
256-
model: M & string
257-
data: Omit<Models[M], 'id'>
258-
select?: string[]
259255
}) {
260256
const schemaModel = getSchema(model)
261257
checkMissingFields(schemaModel, model, values)
262258
const builder = db.insert(schemaModel).values(values)
263259
const returned = await withReturning(model, builder, values)
264260
return returned
265261
},
266-
async findOne<M extends keyof Models>({
262+
async findOne({
267263
model,
268264
where,
269-
}: {
270-
model: M & string
271-
where: Where[]
272-
select?: string[]
273265
}) {
274266
const schemaModel = getSchema(model)
275267
const clause = convertWhereClause(where, model)
@@ -279,21 +271,14 @@ export function drizzleAdapter<
279271
.where(...clause)
280272
if (!res.length)
281273
return null
282-
return res[0] as Models[M] | null
274+
return res[0]
283275
},
284-
async findMany<M extends keyof Models>({
276+
async findMany({
285277
model,
286278
where,
287279
sortBy,
288280
limit,
289281
offset,
290-
}: {
291-
model: M & string
292-
where?: Where[]
293-
sortBy?: { field: string, direction: 'asc' | 'desc' }
294-
limit?: number
295-
offset?: number
296-
select?: string[]
297282
}) {
298283
const schemaModel = getSchema(model)
299284
const clause = where ? convertWhereClause(where, model) : []
@@ -311,7 +296,7 @@ export function drizzleAdapter<
311296
),
312297
)
313298
}
314-
return (await builder.where(...clause)) as Models[M][]
299+
return (await builder.where(...clause))
315300
},
316301
async count({
317302
model,
@@ -328,31 +313,23 @@ export function drizzleAdapter<
328313
.where(...clause)
329314
return res[0].count
330315
},
331-
async update<M extends keyof Models>({
316+
async update({
332317
model,
333318
where,
334319
update: values,
335-
}: {
336-
model: M & string
337-
where: Where[]
338-
update: Partial<Models[M]>
339320
}) {
340321
const schemaModel = getSchema(model)
341322
const clause = convertWhereClause(where, model)
342323
const builder = db
343324
.update(schemaModel)
344325
.set(values)
345326
.where(...clause)
346-
return await withReturning(model, builder, values as any, where) as Models[M] | null
327+
return await withReturning(model, builder, values as any, where)
347328
},
348-
async updateMany<M extends keyof Models>({
329+
async updateMany({
349330
model,
350331
where,
351332
update: values,
352-
}: {
353-
model: M & string
354-
where: Where[]
355-
update: Partial<Models[M]>
356333
}) {
357334
const schemaModel = getSchema(model)
358335
const clause = convertWhereClause(where, model)

src/adapters/kysely/kysely-adapter.ts

+9-25
Original file line numberDiff line numberDiff line change
@@ -203,26 +203,18 @@ export function kyselyAdapter<
203203
}
204204
}
205205
return {
206-
async create<M extends keyof Models>({
206+
async create({
207207
data,
208208
model,
209-
}: {
210-
model: M & string
211-
data: Omit<Models[M], 'id'>
212-
select?: string[]
213209
}) {
214210
const builder = db.insertInto(model).values(data)
215-
return (await withReturning(data, builder, model, [])) as any
211+
return (await withReturning(data, builder, model, []))
216212
},
217213

218-
async findOne<M extends keyof Models>({
214+
async findOne({
219215
model,
220216
where,
221217
select,
222-
}: {
223-
model: M & string
224-
where: Where[]
225-
select?: string[]
226218
}) {
227219
const { and, or } = convertWhereClause(model, where)
228220
let query = db.selectFrom(model).selectAll()
@@ -235,7 +227,7 @@ export function kyselyAdapter<
235227
const res = await query.executeTakeFirst()
236228
if (!res)
237229
return null
238-
return res as Models[M] | null
230+
return res
239231
},
240232
async findMany({ model, where, limit, offset, sortBy }) {
241233
const { and, or } = convertWhereClause(model, where)
@@ -275,17 +267,13 @@ export function kyselyAdapter<
275267
const res = await query.selectAll().execute()
276268
if (!res)
277269
return []
278-
return res as Models[M][]
270+
return res
279271
},
280272

281-
async update<M extends keyof Models>({
273+
async update({
282274
model,
283275
where,
284276
update: values,
285-
}: {
286-
model: M & string
287-
where: Where[]
288-
update: Partial<Models[M]>
289277
}) {
290278
const { and, or } = convertWhereClause(model, where)
291279

@@ -296,17 +284,13 @@ export function kyselyAdapter<
296284
if (or) {
297285
query = query.where(eb => eb.or(or.map(expr => expr(eb))))
298286
}
299-
return await withReturning(values as any, query, model, where) as Models[M] | null
287+
return await withReturning(values as any, query, model, where)
300288
},
301289

302-
async updateMany<M extends keyof Models>({
290+
async updateMany({
303291
model,
304292
where,
305293
update: values,
306-
}: {
307-
model: M & string
308-
where: Where[]
309-
update: Partial<Models[M]>
310294
}) {
311295
const { and, or } = convertWhereClause(model, where)
312296
let query = db.updateTable(model).set(values as any)
@@ -339,7 +323,7 @@ export function kyselyAdapter<
339323
query = query.where(eb => eb.or(or.map(expr => expr(eb))))
340324
}
341325
const res = await query.execute()
342-
return res[0].count as number
326+
return res[0].count
343327
},
344328

345329
async delete({

src/adapters/mongodb/mongodb-adapter.ts

+8-40
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,10 @@ export function mongodbAdapter<
243243
const hasCustomId = options.advanced?.generateId
244244
return {
245245
id: 'mongodb-adapter',
246-
async create<M extends keyof Models>({
246+
async create({
247247
model,
248248
data: values,
249249
select,
250-
}: {
251-
model: M & string
252-
data: Omit<Models[M], 'id'>
253-
select?: string[]
254250
}) {
255251
const transformedData = transform.transformInput(values, model, 'create')
256252
if (transformedData.id && !hasCustomId) {
@@ -265,14 +261,10 @@ export function mongodbAdapter<
265261
const t = transform.transformOutput(insertedData, model, select)
266262
return t
267263
},
268-
async findOne<M extends keyof Models>({
264+
async findOne({
269265
model,
270266
where,
271267
select,
272-
}: {
273-
model: M & string
274-
where: Where[]
275-
select?: string[]
276268
}) {
277269
const clause = transform.convertWhereClause(where, model)
278270
const res = await db
@@ -281,22 +273,15 @@ export function mongodbAdapter<
281273
if (!res)
282274
return null
283275
const transformedData = transform.transformOutput(res, model, select)
284-
return transformedData as Models[M] | null
276+
return transformedData
285277
},
286-
async findMany<M extends keyof Models>({
278+
async findMany({
287279
model,
288280
where,
289281
limit,
290282
offset,
291283
sortBy,
292284
select,
293-
}: {
294-
model: M & string
295-
where?: Where[]
296-
limit?: number
297-
offset?: number
298-
sortBy?: { field: string, direction: 'asc' | 'desc' }
299-
select?: string[]
300285
}) {
301286
const clause = where ? transform.convertWhereClause(where, model) : {}
302287
const cursor = db.collection(transform.getModelName(model)).find(clause)
@@ -311,29 +296,22 @@ export function mongodbAdapter<
311296
)
312297
}
313298
const res = await cursor.toArray()
314-
return res.map(r => transform.transformOutput(r, model, select)) as Models[M][]
299+
return res.map(r => transform.transformOutput(r, model, select))
315300
},
316301
async count({
317302
model,
318303
where,
319-
}: {
320-
model: string
321-
where?: Where[]
322304
}) {
323305
const clause = where ? transform.convertWhereClause(where, model) : {}
324306
const res = await db
325307
.collection(transform.getModelName(model))
326308
.countDocuments(clause)
327309
return res
328310
},
329-
async update<M extends keyof Models>({
311+
async update({
330312
model,
331313
where,
332314
update: values,
333-
}: {
334-
model: M & string
335-
where: Where[]
336-
update: Partial<Models[M]>
337315
}) {
338316
const clause = transform.convertWhereClause(where, model)
339317

@@ -350,16 +328,12 @@ export function mongodbAdapter<
350328
)
351329
if (!res)
352330
return null
353-
return transform.transformOutput(res, model) as Models[M] | null
331+
return transform.transformOutput(res, model)
354332
},
355-
async updateMany<M extends keyof Models>({
333+
async updateMany({
356334
model,
357335
where,
358336
update: values,
359-
}: {
360-
model: M & string
361-
where: Where[]
362-
update: Partial<Models[M]>
363337
}) {
364338
const clause = transform.convertWhereClause(where, model)
365339
const transformedData = transform.transformInput(values, model, 'update')
@@ -371,9 +345,6 @@ export function mongodbAdapter<
371345
async delete({
372346
model,
373347
where,
374-
}: {
375-
model: string
376-
where: Where[]
377348
}) {
378349
const clause = transform.convertWhereClause(where, model)
379350
await db
@@ -383,9 +354,6 @@ export function mongodbAdapter<
383354
async deleteMany({
384355
model,
385356
where,
386-
}: {
387-
model: string
388-
where: Where[]
389357
}) {
390358
const clause = transform.convertWhereClause(where, model)
391359
const res = await db

0 commit comments

Comments
 (0)