Skip to content

Commit e0ae7ef

Browse files
authored
Merge pull request #4 from udecode/feat-before-hooks
2 parents f2c98af + bfc2ad3 commit e0ae7ef

File tree

4 files changed

+243
-15
lines changed

4 files changed

+243
-15
lines changed

.changeset/add-before-hooks.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"better-auth-convex": minor
3+
---
4+
5+
Add `beforeCreate`, `beforeUpdate`, and `beforeDelete` hook support across the Convex adapter so triggers can transform payloads before database writes.

src/adapter.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,15 @@ export const httpAdapter = <
196196
authFunctions.onCreate
197197
)) as FunctionHandle<'mutation'>)
198198
: undefined;
199+
const beforeCreateHandle =
200+
authFunctions.beforeCreate && triggers?.[model]?.beforeCreate
201+
? ((await createFunctionHandle(
202+
authFunctions.beforeCreate
203+
)) as FunctionHandle<'mutation'>)
204+
: undefined;
199205

200206
return ctx.runMutation(authFunctions.create, {
207+
beforeCreateHandle: beforeCreateHandle,
201208
input: { data, model },
202209
select,
203210
onCreateHandle: onCreateHandle,
@@ -214,7 +221,14 @@ export const httpAdapter = <
214221
authFunctions.onDelete
215222
)) as FunctionHandle<'mutation'>)
216223
: undefined;
224+
const beforeDeleteHandle =
225+
authFunctions.beforeDelete && triggers?.[data.model]?.beforeDelete
226+
? ((await createFunctionHandle(
227+
authFunctions.beforeDelete
228+
)) as FunctionHandle<'mutation'>)
229+
: undefined;
217230
await ctx.runMutation(authFunctions.deleteOne, {
231+
beforeDeleteHandle: beforeDeleteHandle,
218232
input: {
219233
model: data.model,
220234
where: parseWhere(data.where),
@@ -233,8 +247,15 @@ export const httpAdapter = <
233247
authFunctions.onDelete
234248
)) as FunctionHandle<'mutation'>)
235249
: undefined;
250+
const beforeDeleteHandle =
251+
authFunctions.beforeDelete && triggers?.[data.model]?.beforeDelete
252+
? ((await createFunctionHandle(
253+
authFunctions.beforeDelete
254+
)) as FunctionHandle<'mutation'>)
255+
: undefined;
236256
const result = await handlePagination(async ({ paginationOpts }) => {
237257
return await ctx.runMutation(authFunctions.deleteMany, {
258+
beforeDeleteHandle: beforeDeleteHandle,
238259
input: {
239260
...data,
240261
where: parseWhere(data.where),
@@ -297,8 +318,15 @@ export const httpAdapter = <
297318
authFunctions.onUpdate
298319
)) as FunctionHandle<'mutation'>)
299320
: undefined;
321+
const beforeUpdateHandle =
322+
authFunctions.beforeUpdate && triggers?.[data.model]?.beforeUpdate
323+
? ((await createFunctionHandle(
324+
authFunctions.beforeUpdate
325+
)) as FunctionHandle<'mutation'>)
326+
: undefined;
300327

301328
return ctx.runMutation(authFunctions.updateOne, {
329+
beforeUpdateHandle: beforeUpdateHandle,
302330
input: {
303331
model: data.model as any,
304332
update: data.update as any,
@@ -321,9 +349,16 @@ export const httpAdapter = <
321349
authFunctions.onUpdate
322350
)) as FunctionHandle<'mutation'>)
323351
: undefined;
352+
const beforeUpdateHandle =
353+
authFunctions.beforeUpdate && triggers?.[data.model]?.beforeUpdate
354+
? ((await createFunctionHandle(
355+
authFunctions.beforeUpdate
356+
)) as FunctionHandle<'mutation'>)
357+
: undefined;
324358

325359
const result = await handlePagination(async ({ paginationOpts }) => {
326360
return await ctx.runMutation(authFunctions.updateMany, {
361+
beforeUpdateHandle: beforeUpdateHandle,
327362
input: {
328363
...(data as any),
329364
where: parseWhere(data.where),
@@ -398,10 +433,17 @@ export const dbAdapter = <
398433
authFunctions.onCreate
399434
)) as FunctionHandle<'mutation'>)
400435
: undefined;
436+
const beforeCreateHandle =
437+
authFunctions.beforeCreate && triggers?.[model]?.beforeCreate
438+
? ((await createFunctionHandle(
439+
authFunctions.beforeCreate
440+
)) as FunctionHandle<'mutation'>)
441+
: undefined;
401442

402443
return createHandler(
403444
ctx,
404445
{
446+
beforeCreateHandle: beforeCreateHandle,
405447
input: { data, model },
406448
select,
407449
onCreateHandle: onCreateHandle,
@@ -417,10 +459,17 @@ export const dbAdapter = <
417459
authFunctions.onDelete
418460
)) as FunctionHandle<'mutation'>)
419461
: undefined;
462+
const beforeDeleteHandle =
463+
authFunctions.beforeDelete && triggers?.[data.model]?.beforeDelete
464+
? ((await createFunctionHandle(
465+
authFunctions.beforeDelete
466+
)) as FunctionHandle<'mutation'>)
467+
: undefined;
420468

421469
await deleteOneHandler(
422470
ctx,
423471
{
472+
beforeDeleteHandle: beforeDeleteHandle,
424473
input: {
425474
model: data.model,
426475
where: parseWhere(data.where),
@@ -438,11 +487,18 @@ export const dbAdapter = <
438487
authFunctions.onDelete
439488
)) as FunctionHandle<'mutation'>)
440489
: undefined;
490+
const beforeDeleteHandle =
491+
authFunctions.beforeDelete && triggers?.[data.model]?.beforeDelete
492+
? ((await createFunctionHandle(
493+
authFunctions.beforeDelete
494+
)) as FunctionHandle<'mutation'>)
495+
: undefined;
441496

442497
const result = await handlePagination(async ({ paginationOpts }) => {
443498
return await deleteManyHandler(
444499
ctx,
445500
{
501+
beforeDeleteHandle: beforeDeleteHandle,
446502
input: {
447503
...data,
448504
where: parseWhere(data.where),
@@ -520,10 +576,17 @@ export const dbAdapter = <
520576
authFunctions.onUpdate
521577
)) as FunctionHandle<'mutation'>)
522578
: undefined;
579+
const beforeUpdateHandle =
580+
authFunctions.beforeUpdate && triggers?.[data.model]?.beforeUpdate
581+
? ((await createFunctionHandle(
582+
authFunctions.beforeUpdate
583+
)) as FunctionHandle<'mutation'>)
584+
: undefined;
523585

524586
return updateOneHandler(
525587
ctx,
526588
{
589+
beforeUpdateHandle: beforeUpdateHandle,
527590
input: {
528591
model: data.model as any,
529592
update: data.update as any,
@@ -545,11 +608,18 @@ export const dbAdapter = <
545608
authFunctions.onUpdate
546609
)) as FunctionHandle<'mutation'>)
547610
: undefined;
611+
const beforeUpdateHandle =
612+
authFunctions.beforeUpdate && triggers?.[data.model]?.beforeUpdate
613+
? ((await createFunctionHandle(
614+
authFunctions.beforeUpdate
615+
)) as FunctionHandle<'mutation'>)
616+
: undefined;
548617

549618
const result = await handlePagination(async ({ paginationOpts }) => {
550619
return await updateManyHandler(
551620
ctx,
552621
{
622+
beforeUpdateHandle: beforeUpdateHandle,
553623
input: {
554624
...(data as any),
555625
where: parseWhere(data.where),

0 commit comments

Comments
 (0)