mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-09 20:27:44 +00:00
feat: custom table names and fields for plugins (#570)
This commit is contained in:
@@ -470,6 +470,27 @@ export const auth = betterAuth({
|
||||
Type inference in your code will still use the original field names (e.g., `user.name`, not `user.full_name`).
|
||||
</Callout>
|
||||
|
||||
To customize table names and column name for plugins, you can use the `schema` property in the plugin config:
|
||||
|
||||
```ts title="auth.ts"
|
||||
import { betterAuth } from "better-auth";
|
||||
|
||||
export const auth = betterAuth({
|
||||
plugins: {
|
||||
twoFactor: {
|
||||
schema: {
|
||||
user: {
|
||||
fields: {
|
||||
twoFactorEnabled: "two_factor_enabled",
|
||||
twoFactorSecret: "two_factor_secret"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
### Extending Core Schema
|
||||
|
||||
|
||||
@@ -118,7 +118,7 @@ const myPlugin = ()=> {
|
||||
type: "string"
|
||||
}
|
||||
},
|
||||
tableName: "myTable" // optional if you want to use a different name than the key
|
||||
modelName: "myTable" // optional if you want to use a different name than the key
|
||||
}
|
||||
}
|
||||
} satisfies BetterAuthPlugin
|
||||
|
||||
@@ -64,7 +64,7 @@ export const auth = betterAuth({
|
||||
//...other options
|
||||
rateLimit: {
|
||||
storage: "database",
|
||||
tableName: "rateLimit", //optional by default "rateLimit" is used
|
||||
modelName: "rateLimit", //optional by default "rateLimit" is used
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
@@ -655,6 +655,25 @@ Table Name: `invitation`
|
||||
]}
|
||||
/>
|
||||
|
||||
### Customizing the Schema
|
||||
|
||||
To change the schema table name or fields, you can pass `schema` option to the organization plugin.
|
||||
|
||||
```ts title="auth.ts"
|
||||
const auth = betterAuth({
|
||||
plugins: [organization({
|
||||
schema: {
|
||||
organization: {
|
||||
modelName: "organizations", //map the organization table to organizations
|
||||
fields: {
|
||||
name: "title" //map the name field to title
|
||||
}
|
||||
}
|
||||
}
|
||||
})]
|
||||
})
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
**allowUserToCreateOrganization**: `boolean` | `((user: User) => Promise<boolean> | boolean)` - A function that determines whether a user can create an organization. By default, it's `true`. You can set it to `false` to restrict users from creating organizations.
|
||||
|
||||
Reference in New Issue
Block a user