import { Code } from "@/components/Code"

<img align="right" src="/img/adapters/mikro-orm.svg" width="64" height="64" />

# MikroORM Adapter

## Resources

- [MikroORM documentation](https://mikro-orm.io/docs/installation)

## Setup

### Installation

```bash npm2yarn
npm install @mikro-orm/core @auth/mikro-orm-adapter
```

### Environment Variables

```sh
DATABASE_CONNECTION_STRING=./db.sqlite
```

### Configuration

<Code>
<Code.Next>

```ts filename="./auth.ts"
import NextAuth from "next-auth"
import { MikroOrmAdapter } from "@auth/mikro-orm-adapter"

export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: MikroOrmAdapter({
    // MikroORM options object - https://mikro-orm.io/docs/next/configuration#driver
    dbName: process.env.DATABASE_CONNECTION_STRING,
    type: "sqlite",
    debug: true,
  }),
  providers: [],
})
```

</Code.Next>
<Code.Qwik>

```ts filename="/src/routes/plugin@auth.ts"
import { QwikAuth$ } from "@auth/qwik"
import { MikroOrmAdapter } from "@auth/mikro-orm-adapter"

export const { onRequest, useSession, useSignIn, useSignOut } = QwikAuth$(
  () => ({
    providers: [],
    adapter: MikroOrmAdapter({
      // MikroORM options object - https://mikro-orm.io/docs/next/configuration#driver
      dbName: import.meta.env.DATABASE_CONNECTION_STRING,
      type: "sqlite",
      debug: true,
    }),
  })
)
```

</Code.Qwik>
<Code.Svelte>

```ts filename="./src/auth.ts"
import { SvelteKitAuth } from "@auth/sveltekit"
import { MikroOrmAdapter } from "@auth/mikro-orm-adapter"

export const { handle, signIn, signOut } = SvelteKitAuth({
  adapter: MikroOrmAdapter({
    // MikroORM options object - https://mikro-orm.io/docs/next/configuration#driver
    dbName: process.env.DATABASE_CONNECTION_STRING,
    type: "sqlite",
    debug: true,
  }),
  providers: [],
})
```

</Code.Svelte>
<Code.Express>

```ts filename="./src/routes/auth.route.ts"
import { ExpressAuth } from "@auth/express"
import { MikroOrmAdapter } from "@auth/mikro-orm-adapter"

const app = express()

app.set("trust proxy", true)
app.use(
  "/auth/*",
  ExpressAuth({
    providers: [],
    adapter: MikroOrmAdapter({
      // MikroORM options object - https://mikro-orm.io/docs/next/configuration#driver
      dbName: process.env.DATABASE_CONNECTION_STRING,
      type: "sqlite",
      debug: true,
    }),
  })
)
```

</Code.Express>
</Code>

### Advanced usage

#### Passing custom entities

The MikroORM adapter ships with its own set of entities. If you'd like to extend them, you can optionally pass them to the adapter.

> This schema is adapted for use in MikroORM and based upon our main [schema](https://authjs.dev/reference/core/adapters#models)

```ts filename="./auth.ts"
import config from "config/mikro-orm.ts"
import {
  Cascade,
  Collection,
  Entity,
  OneToMany,
  PrimaryKey,
  Property,
  Unique,
} from "@mikro-orm/core"
import { defaultEntities } from "@auth/mikro-orm-adapter"

const type { Account, Session } = defaultEntities

@Entity()
export class User implements defaultEntities.User {
  @PrimaryKey()
  id: string = randomUUID()

  @Property({ nullable: true })
  name?: string

  @Property({ nullable: true })
  @Unique()
  email?: string

  @Property({ type: "Date", nullable: true })
  emailVerified: Date | null = null

  @Property({ nullable: true })
  image?: string

  @OneToMany({
    entity: () => Session,
    mappedBy: (session) => session.user,
    hidden: true,
    orphanRemoval: true,
    cascade: [Cascade.ALL],
  })
  sessions = new Collection<Session>(this)

  @OneToMany({
    entity: () => Account,
    mappedBy: (account) => account.user,
    hidden: true,
    orphanRemoval: true,
    cascade: [Cascade.ALL],
  })
  accounts = new Collection<Account>(this)

  @Enum({ hidden: true })
  role = "ADMIN"
}

export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: MikroOrmAdapter(config, { entities: { User } }),
})
```

#### Including default entities

You may want to include the defaultEntities in your MikroORM configuration to include them in Migrations etc.

To achieve that include them in your "entities" array:

```typescript filename="config/mikro-orm.ts"
import { Options } from "@mikro-orm/core"
import { defaultEntities } from "@auth/mikro-orm-adapter"

const config: Options = {
  entities: [VeryImportantEntity, ...Object.values(defaultEntities)],
}

export default config
```
