https://github.com/octet-stream/better-auth-mikro-orm
MikroORM Adapter for Better Auth
https://github.com/octet-stream/better-auth-mikro-orm
better-auth database-adapter mikro-orm
Last synced: 2 months ago
JSON representation
MikroORM Adapter for Better Auth
- Host: GitHub
- URL: https://github.com/octet-stream/better-auth-mikro-orm
- Owner: octet-stream
- License: mit
- Created: 2024-12-13T21:13:42.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-01-01T13:42:13.000Z (4 months ago)
- Last Synced: 2026-02-25T23:03:19.768Z (2 months ago)
- Topics: better-auth, database-adapter, mikro-orm
- Language: TypeScript
- Homepage: https://npmjs.com/package/better-auth-mikro-orm
- Size: 247 KB
- Stars: 30
- Watchers: 1
- Forks: 5
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
- Changelog: CHANGELOG.md
- License: license
Awesome Lists containing this project
README
# better-auth-mikro-orm
[MikroORM](https://mikro-orm.io/) adapter for [Better Auth](https://www.better-auth.com/)
[](https://github.com/octet-stream/better-auth-mikro-orm/actions/workflows/ci.yaml)
[](https://codecov.io/gh/octet-stream/better-auth-mikro-orm)
## Installation
Using npm:
```sh
npm i better-auth-mikro-orm
```
Using yarn:
```sh
yarn add better-auth-mikro-orm
```
Using pnpm:
```sh
pnpm add better-auth-mikro-orm
```
## Usage
1. First you'll need to set up MikroORM and define the [core schema](https://www.better-auth.com/docs/concepts/database#core-schema) for Better Auth.
If you use any plugin, don't forget to check if they have any additional database schema definitions, then define entities you'll need for each plugin.
2. When you're finished with the schema definitions, you can simply pass the result of the `mikroOrmAdapter` call to the `database` option like this:
```ts
import {mikroOrmAdapter} from "better-auth-mikro-orm"
import {betterAuth} from "better-auth"
import {orm} from "./orm.js" // Your Mikro ORM instance
export const auth = betterAuth({
database: mikroOrmAdapter(orm),
// Don't forget to disable the ID generator if it is already managed by MikroORM
advanced: {
database: {
generateId: false
}
}
})
```
## API
### `mikroOrmAdapter(orm: MikroORM): AdapterInstance`
Creates the MikroORM adapter instance. Note that this adapter **does not** manage database schemas for you, so you can't use it with [`@better-auth/cli`](https://www.better-auth.com/docs/concepts/cli).
This means you'll have to manage database schemas on your own.
Please refer to the Better Auth and MikroORM documentations for details.
Returns the `AdapterInstance` function for the Better Auth `database` option.
This function expects a single argument:
* `orm` - An instance of `MikroORM` returned from either `MikroORM.init` or `MikroORM.initSync`.