Skip to content

Commit 9d0ece7

Browse files
committed
Preserve custom auth date update hooks.
1 parent 78e6105 commit 9d0ece7

2 files changed

Lines changed: 113 additions & 2 deletions

File tree

‎packages/void/src/auth/drizzle.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,23 @@ function createColumnBuilder(
307307
}
308308
}
309309

310+
function hasBuiltInUpdateHook(options: BetterAuthOptions, model: string, field: string): boolean {
311+
if (
312+
field !== 'updatedAt' ||
313+
(model !== 'user' && model !== 'session' && model !== 'account' && model !== 'verification')
314+
) {
315+
return false;
316+
}
317+
// Only Better Auth's built-in hooks are known to mean the current time.
318+
// A configured field replaces that definition, including its callback.
319+
return (
320+
options[model]?.additionalFields?.updatedAt === undefined &&
321+
!options.plugins?.some((plugin) => plugin.schema?.[model]?.fields.updatedAt !== undefined)
322+
);
323+
}
324+
310325
function createColumnDefinition(
326+
options: BetterAuthOptions,
311327
dialect: 'sqlite' | 'postgresql' | 'mysql',
312328
schema: BetterAuthDBSchema,
313329
idStrategy: AuthIdStrategy,
@@ -372,7 +388,11 @@ function createColumnDefinition(
372388
if (literalDefault !== undefined) {
373389
parts.push(`.$defaultFn(() => ${JSON.stringify(literalDefault)})`);
374390
}
375-
if (field.onUpdate && field.type === 'date') {
391+
if (
392+
field.onUpdate &&
393+
field.type === 'date' &&
394+
hasBuiltInUpdateHook(options, defaultModelName, defaultFieldName)
395+
) {
376396
parts.push('.$onUpdate(() => new Date())');
377397
}
378398

@@ -481,6 +501,7 @@ export function generateVoidAuthDrizzleSchema(
481501
const fieldEntries = Object.entries(table.fields);
482502
const renderedFields = [
483503
` "id": ${createColumnDefinition(
504+
options,
484505
runtime.dialect,
485506
schema,
486507
idStrategy,
@@ -492,6 +513,7 @@ export function generateVoidAuthDrizzleSchema(
492513
...fieldEntries.map(([defaultFieldName, field]) => {
493514
const actualFieldName = resolveFieldName(defaultFieldName, field);
494515
return ` ${JSON.stringify(actualFieldName)}: ${createColumnDefinition(
516+
options,
495517
runtime.dialect,
496518
schema,
497519
idStrategy,

‎packages/void/test/unit/auth-drizzle.test.ts‎

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from 'vite-plus/test';
1+
import { describe, expect, it, vi } from 'vite-plus/test';
22
import {
33
generateVoidAuthDatabaseModule,
44
generateVoidAuthDrizzleSchema,
@@ -152,6 +152,95 @@ describe('Better Auth Drizzle codegen', () => {
152152
);
153153
});
154154

155+
it.each(['sqlite', 'postgresql', 'mysql'] as const)(
156+
'leaves custom date update hooks out of the generated %s schema',
157+
(dialect) => {
158+
const onUpdate = vi.fn(() => new Date('2035-01-01T00:00:00.000Z'));
159+
const code = generateVoidAuthDrizzleSchema({
160+
dialect,
161+
providers: null,
162+
userConfig: ({ defaults }) => ({
163+
...defaults,
164+
user: {
165+
additionalFields: {
166+
expiresAt: { type: 'date', required: false, onUpdate },
167+
},
168+
},
169+
plugins: [
170+
{
171+
id: 'custom-dates',
172+
schema: {
173+
audit: {
174+
fields: { updatedAt: { type: 'date', onUpdate } },
175+
},
176+
},
177+
},
178+
],
179+
}),
180+
});
181+
182+
expect(code.split('\n').find((line) => line.includes('"expiresAt":'))).not.toContain(
183+
'.$onUpdate',
184+
);
185+
const audit = code.slice(code.indexOf('export const auditTable'));
186+
expect(audit.split('\n').find((line) => line.includes('"updatedAt":'))).not.toContain(
187+
'.$onUpdate',
188+
);
189+
expect(code.match(/\.\$onUpdate\(/g)).toHaveLength(4);
190+
expect(onUpdate).not.toHaveBeenCalled();
191+
},
192+
);
193+
194+
it.each(['user', 'session', 'account', 'verification'] as const)(
195+
'does not replace a configured %s.updatedAt hook',
196+
(model) => {
197+
const onUpdate = vi.fn(() => new Date('2035-01-01T00:00:00.000Z'));
198+
for (const source of ['additionalFields', 'plugin'] as const) {
199+
const field = { type: 'date' as const, onUpdate };
200+
const code = generateVoidAuthDrizzleSchema({
201+
dialect: 'sqlite',
202+
providers: null,
203+
userConfig: ({ defaults }) => ({
204+
...defaults,
205+
...(source === 'additionalFields'
206+
? { [model]: { additionalFields: { updatedAt: field } } }
207+
: {
208+
plugins: [
209+
{
210+
id: 'custom-updated-at',
211+
schema: { [model]: { fields: { updatedAt: field } } },
212+
},
213+
],
214+
}),
215+
}),
216+
});
217+
218+
const table = code.slice(code.indexOf(`export const ${model}Table`));
219+
expect(table.split('\n').find((line) => line.includes('"updatedAt":'))).not.toContain(
220+
'.$onUpdate',
221+
);
222+
expect(code.match(/\.\$onUpdate\(/g)).toHaveLength(3);
223+
}
224+
expect(onUpdate).not.toHaveBeenCalled();
225+
},
226+
);
227+
228+
it('keeps the built-in update hook when its table and column are renamed', () => {
229+
const code = generateVoidAuthDrizzleSchema({
230+
dialect: 'sqlite',
231+
providers: null,
232+
userConfig: ({ defaults }) => ({
233+
...defaults,
234+
user: { modelName: 'members', fields: { updatedAt: 'modifiedAt' } },
235+
}),
236+
});
237+
238+
expect(code).toContain(
239+
'"modifiedAt": integer("modifiedAt", { mode: "timestamp_ms" }).notNull().$onUpdate(() => new Date())',
240+
);
241+
expect(code.match(/\.\$onUpdate\(/g)).toHaveLength(4);
242+
});
243+
155244
it('keeps optional defaults out of the migration metadata', () => {
156245
const schema = resolveVoidAuthMigrationSchema({
157246
dialect: 'sqlite',

0 commit comments

Comments
 (0)