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.
npm install mikro-orm-better-authVerified import paths — ran on the pinned version, not inferred.
Demonstrates the basic setup of `mikro-orm-better-auth` with MikroORM, including `RequestContext` for per-request EntityManager isolation and configuring entity generation.
Manually install all required peer dependencies: `pnpm add better-auth @mikro-orm/core @mikro-orm/knex prettier ts-morph`.
Wrap your `EntityManager` access in a function: `mikroOrmAdapter(() => orm.em)` or `mikroOrmAdapter(() => RequestContext.getEntityManager()! as SqlEntityManager)`.
Add the generated entity path to your MikroORM configuration, e.g., `entities: ['./dist/auth/entities/**/*.entity.js']` or `entities: ['./src/auth/entities/**/*.entity.ts']` for ts-node environments.
Place custom logic (imports, methods, properties, decorators) outside of the generator-owned blocks. Avoid modifying the `@Entity`, `@PrimaryKey`, and `@Property` decorators or the fields they manage.
Run `pnpm add better-auth` (or `npm install better-auth`, `yarn add better-auth`) to install the missing peer dependency.
Ensure you are using `import { mikroOrmAdapter } from 'mikro-orm-better-auth';` and your project is configured for ESM if appropriate.Wrap the `EntityManager` in an arrow function: `mikroOrmAdapter(() => orm.em)`.