MikroORM Better Auth Adapter
mikro-orm-better-auth is an adapter for the 'better-auth' authentication library, specifically designed to integrate with MikroORM. It provides a robust database layer for better-auth, abstracting common CRUD operations and transactional support through MikroORM's `EntityManager`. A key differentiator is its `createSchema` implementation, which leverages `ts-morph` to generate MikroORM entity files automatically. These generated entities are intelligently patched in-place on regeneration, preserving user-owned code like custom imports, decorators, methods, and comments, while only updating generator-managed fields and decorators. The current stable version is 1.1.1, released in March 2026, indicating active development with a focus on stability and feature enhancements. It is primarily SQL-oriented, utilizing MikroORM's SQL entity manager, and defers native join support for future development.
Common errors
-
Error: Cannot find module 'better-auth' or its corresponding type declarations.
cause The `better-auth` package is a peer dependency and was not installed in the project.fixRun `pnpm add better-auth` (or `npm install better-auth`, `yarn add better-auth`) to install the missing peer dependency. -
TypeError: mikroOrmAdapter is not a function or is undefined
cause Incorrect import statement for `mikroOrmAdapter`, or trying to use it with CommonJS `require()` in an ESM context.fixEnsure you are using `import { mikroOrmAdapter } from 'mikro-orm-better-auth';` and your project is configured for ESM if appropriate. -
Argument of type 'EntityManager<IDatabaseDriver<Connection>>' is not assignable to parameter of type '() => EntityManager<IDatabaseDriver<Connection>>'.
cause Attempting to pass the `EntityManager` instance directly to `mikroOrmAdapter` instead of a function that returns it (since v1.1.0).fixWrap the `EntityManager` in an arrow function: `mikroOrmAdapter(() => orm.em)`.
Warnings
- breaking Starting from v1.1.1, `@mikro-orm/core`, `@mikro-orm/knex`, `better-auth`, `prettier`, and `ts-morph` were moved from direct dependencies to peer dependencies. This means these packages are no longer automatically installed and must be explicitly added to your project's `dependencies`.
- gotcha The `mikroOrmAdapter` now accepts a closure that returns the `EntityManager` (e.g., `() => orm.em` or `() => RequestContext.getEntityManager()`). Direct passing of the `EntityManager` instance is no longer supported.
- gotcha When using entity generation, ensure the output directory for generated entities (`generateEntity.outputDir`) is correctly configured and included in your MikroORM `entities` array during `MikroORM.init` (or handled by an entity discovery mechanism).
- gotcha Direct edits to generator-owned field definitions or MikroORM decorator arguments within generated entities may be overwritten during subsequent regeneration. The patching mechanism preserves user-owned code but not modifications to managed elements.
Install
-
npm install mikro-orm-better-auth -
yarn add mikro-orm-better-auth -
pnpm add mikro-orm-better-auth
Imports
- mikroOrmAdapter
const { mikroOrmAdapter } = require('mikro-orm-better-auth');import { mikroOrmAdapter } from 'mikro-orm-better-auth'; - betterAuth
import betterAuth from 'better-auth';
import { betterAuth } from 'better-auth'; - RequestContext
import RequestContext from '@mikro-orm/core';
import { RequestContext } from '@mikro-orm/core';
Quickstart
import { betterAuth } from 'better-auth';
import { mikroOrmAdapter } from 'mikro-orm-better-auth';
import { MikroORM, RequestContext } from '@mikro-orm/core';
import { SqliteDriver, SqlEntityManager } from '@mikro-orm/sqlite';
async function bootstrap() {
const orm = await MikroORM.init<SqliteDriver>({
driver: SqliteDriver,
dbName: 'app.sqlite',
entities: [], // Mikro-orm-better-auth generates entities, so leave this empty initially or add manually
});
// For per-request EntityManager isolation (recommended in web servers)
const auth = betterAuth({
database: mikroOrmAdapter(() => RequestContext.getEntityManager()! as SqlEntityManager),
});
// Example of generating entities
const adapter = mikroOrmAdapter(() => orm.em, {
generateEntity: {
outputDir: 'src/auth/entities', // Output directory for generated MikroORM entities
},
});
// In a real application, you would pass the adapter to betterAuth and then call auth methods.
// This snippet focuses on setup and entity generation.
console.log('MikroORM initialized and adapter configured. Entities will be generated to src/auth/entities.');
}
bootstrap().catch(console.error);