Mikro ORM Adapter for Better Auth

0.5.0 · active · verified Wed Apr 22

This package provides a Mikro ORM adapter for Better Auth, allowing developers to integrate Better Auth's authentication functionalities with a MikroORM-managed database. The current stable version is `0.5.0`, and the project demonstrates a relatively active release cadence with frequent minor and patch updates based on the provided changelog. A key differentiator is its specific integration with MikroORM, serving as a bridge for authentication operations. It's crucial to note that this adapter does not handle database schema management directly; users are expected to define and manage their core Better Auth and plugin-specific schemas within MikroORM independently. This also means it cannot be used with `@better-auth/cli` for schema generation or migrations, distinguishing it from adapters that abstract schema concerns.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Better Auth using the `better-auth-mikro-orm` adapter, passing a MikroORM instance and disabling Better Auth's default ID generator.

import { mikroOrmAdapter } from "better-auth-mikro-orm";
import { betterAuth } from "better-auth";
import { MikroORM } from "@mikro-orm/core";

// Assume 'orm' is an initialized MikroORM instance
// For demonstration, we'll create a mock, but in a real app, it would be MikroORM.init
const mockEntityManager = {
  find: async () => [],
  findOne: async () => null,
  persistAndFlush: async () => {},
  removeAndFlush: async () => {},
  nativeInsert: async () => {},
  nativeUpdate: async () => {},
  nativeDelete: async () => {},
  transactional: async (cb: any) => cb(),
  getRepository: (entity: any) => ({ /* mock methods */ }),
  count: async () => 0
};

const orm = { em: mockEntityManager } as unknown as MikroORM; // 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
    }
  }
});

console.log("Better Auth instance initialized with MikroORM adapter.");
// You would typically use 'auth' here, e.g., auth.signIn, auth.createUser
// For example, to check if the adapter works:
async function demonstrateUsage() {
  // This is a conceptual example, actual usage depends on Better Auth's API
  // which would interact with the adapter's methods like find, create, etc.
  // Example: Trying to create a user (conceptual, requires Better Auth's specific API)
  try {
    // This part is highly dependent on Better Auth's actual API for user creation
    // and how it interacts with the adapter. This is illustrative.
    // const newUser = await auth.createUser({ email: "test@example.com", password: "securepassword" });
    // console.log("Attempted to create user via Better Auth with MikroORM adapter.");
    console.log("Adapter setup successful. Ready for Better Auth operations.");
  } catch (error) {
    console.error("Error during conceptual user creation:", error);
  }
}

demonstrateUsage();

view raw JSON →