Registry / auth-security / mikro-orm-better-auth

mikro-orm-better-auth

JSON →
library1.1.1jsnpmunverified

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-auth
INSTALL
IMPORT
SIG · MIKRO-ORM-BETTER-A
M
mikro-orm-better-auth
auth-securityjavascriptv1.1.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mikroOrmAdapter
import { mikroOrmAdapter } from 'mikro-orm-better-auth';
const { mikroOrmAdapter } = require('mikro-orm-better-auth');
This library is primarily ESM-first, common in modern TypeScript projects. Avoid CommonJS `require`.
betterAuth
import { betterAuth } from 'better-auth';
import betterAuth from 'better-auth';
While `better-auth` is a peer dependency, it's a named export, not a default one. Incorrectly importing as a default is a common mistake.
RequestContext
import { RequestContext } from '@mikro-orm/core';
import RequestContext from '@mikro-orm/core';
Used for per-request EntityManager isolation in MikroORM. It's a named export from @mikro-orm/core.

Demonstrates the basic setup of `mikro-orm-better-auth` with MikroORM, including `RequestContext` for per-request EntityManager isolation and configuring entity generation.

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);
Debug
Known issues
breakingStarting 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`.
fix
Manually install all required peer dependencies: `pnpm add better-auth @mikro-orm/core @mikro-orm/knex prettier ts-morph`.
affects: >=1.1.1
gotchaThe `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.
fix
Wrap your `EntityManager` access in a function: `mikroOrmAdapter(() => orm.em)` or `mikroOrmAdapter(() => RequestContext.getEntityManager()! as SqlEntityManager)`.
affects: >=1.1.0
gotchaWhen 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).
fix
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.
affects: >=1.0.0
gotchaDirect 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.
fix
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.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'better-auth' or its corresponding type declarations.
The `better-auth` package is a peer dependency and was not installed in the project.
fix
Run `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
Incorrect import statement for `mikroOrmAdapter`, or trying to use it with CommonJS `require()` in an ESM context.
fix
Ensure 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>>'.
Attempting to pass the `EntityManager` instance directly to `mikroOrmAdapter` instead of a function that returns it (since v1.1.0).
fix
Wrap the `EntityManager` in an arrow function: `mikroOrmAdapter(() => orm.em)`.
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies
@mikro-orm/corerequiredCore MikroORM functionality required for entity management and database interaction.
@mikro-orm/knexrequiredKnex.js driver for MikroORM, used for SQL database connectivity. Required when using SQL drivers.
better-authrequiredThe core authentication library that this package adapts for MikroORM.
prettierrequiredUsed to format generated entity code, ensuring consistency and readability.
ts-morphrequiredRequired for programmatically generating and patching TypeScript entity files.
Agent activity
16 hits · last 30 days
node
12
Amazon
1
OpenAI (training)
1
Resources
mikro-orm-better-auth — npm install mikro-orm-better-auth · libregistry