feat: custom table names and fields for plugins (#570)

This commit is contained in:
Bereket Engida
2024-11-18 11:25:39 +03:00
committed by GitHub
parent a3867bdbba
commit afecf4ce41
32 changed files with 388 additions and 234 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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
},
})
```

View File

@@ -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.