Additional Fields Issue In Database Hooks
The database hooks seem unaware of additional fields even when:
When using hooks, we need to cast to access these fields:
Is this expected behavior? Should database hooks be aware of additional fields?
- Fields are declared in
additionalFieldsadditionalFields - Fields exist in the schema's user table
// Schema definition has fields
const user = pgTable("user", {
firstName: text("first_name"),
lastName: text("last_name"),
// ...other fields
});
// Auth config includes them
const auth = betterAuth({
database: drizzleAdapter(db, {
schema: { user } // User table with firstName/lastName
}),
user: {
additionalFields: {
firstName: {
type: "string",
fieldName: "firstName",
},
lastName: {
type: "string",
fieldName: "lastName",
}
}
}
});// Schema definition has fields
const user = pgTable("user", {
firstName: text("first_name"),
lastName: text("last_name"),
// ...other fields
});
// Auth config includes them
const auth = betterAuth({
database: drizzleAdapter(db, {
schema: { user } // User table with firstName/lastName
}),
user: {
additionalFields: {
firstName: {
type: "string",
fieldName: "firstName",
},
lastName: {
type: "string",
fieldName: "lastName",
}
}
}
});When using hooks, we need to cast to access these fields:
databaseHooks: {
user: {
create: {
after: async (user) => {
// TypeScript complains about firstName not existing
// but running this shows firstName is actually there:
console.log('Full user object:', user); // {id: '123', firstName: 'John', ...}
// Need type assertion even though data exists at runtime
const extendedUser = user as { firstName: string };
console.log(extendedUser.firstName); // Works and shows 'John'
// Direct access gives TS error even though it works
console.log(user.firstName); --> TY Error
// TS Error: Property 'firstName' does not exist on type '{ id: string; email: string; emailVerified: boolean; name: string; createdAt: Date; updatedAt: Date; image?: string | null | undefined; }'
}
}
}
}databaseHooks: {
user: {
create: {
after: async (user) => {
// TypeScript complains about firstName not existing
// but running this shows firstName is actually there:
console.log('Full user object:', user); // {id: '123', firstName: 'John', ...}
// Need type assertion even though data exists at runtime
const extendedUser = user as { firstName: string };
console.log(extendedUser.firstName); // Works and shows 'John'
// Direct access gives TS error even though it works
console.log(user.firstName); --> TY Error
// TS Error: Property 'firstName' does not exist on type '{ id: string; email: string; emailVerified: boolean; name: string; createdAt: Date; updatedAt: Date; image?: string | null | undefined; }'
}
}
}
}Is this expected behavior? Should database hooks be aware of additional fields?