@a77ay/better-auth-mikro-orm 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License Copyright (c) 2026 A77AY
2
+
3
+ Permission is hereby granted, free of charge,
4
+ to any person obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish, distribute,
7
+ sublicense, and/or sell copies of the Software, and to permit persons to whom the
8
+ Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above
11
+ copyright notice and this permission notice (including the next paragraph) shall
12
+ be included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE
15
+ IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
16
+ BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17
+ PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18
+ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19
+ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,191 @@
1
+ # @a77ay/better-auth-mikro-orm
2
+
3
+ MikroORM database adapter and entity generator for [Better Auth](https://www.better-auth.com).
4
+
5
+ [**🔌 Database Adapter**](#1-adapter-setup)  •  [**⚡ CLI Entity Generator**](#2-schema--entity-generation)
6
+
7
+ [![npm version](https://img.shields.io/npm/v/@a77ay/better-auth-mikro-orm.svg?color=blue&logo=npm&logoColor=white)](https://www.npmjs.com/package/@a77ay/better-auth-mikro-orm)
8
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/@a77ay/better-auth-mikro-orm.svg?logo=vite&logoColor=white)](https://bundlephobia.com/package/@a77ay/better-auth-mikro-orm)
9
+ [![Tests](https://img.shields.io/github/actions/workflow/status/A77AY/better-auth-mikro-orm/ci.yml?branch=main&label=550%2B%20tests&logo=github&logoColor=white)](https://github.com/A77AY/better-auth-mikro-orm/actions/workflows/ci.yml)
10
+ [![codecov](https://img.shields.io/codecov/c/github/A77AY/better-auth-mikro-orm?logo=codecov&logoColor=white)](https://codecov.io/gh/A77AY/better-auth-mikro-orm)
11
+
12
+ ## Features
13
+
14
+ - 🔌 **Native MikroORM Adapter**: Seamlessly integrates with MikroORM's `EntityManager`, Identity Map, and Unit of Work.
15
+ - ⚡ **Entity & Schema Generator**: Generate MikroORM entities compatible with Better Auth core and all active plugins.
16
+ - 🗄️ **Multi-Database Support**: Full support for PostgreSQL, MySQL, MariaDB, SQLite, LibSQL, MongoDB, and custom drivers.
17
+ - 🚀 **Optimized Relations**: Batched queries for joined relations to prevent N+1 issues.
18
+ - 🔒 **Type-Safe**: Complete TypeScript support with automatic type inference.
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ # Using npm
24
+ npm install @a77ay/better-auth-mikro-orm @mikro-orm/core
25
+
26
+ # Using pnpm
27
+ pnpm add @a77ay/better-auth-mikro-orm @mikro-orm/core
28
+
29
+ # Using yarn
30
+ yarn add @a77ay/better-auth-mikro-orm @mikro-orm/core
31
+
32
+ # Using bun
33
+ bun add @a77ay/better-auth-mikro-orm @mikro-orm/core
34
+ ```
35
+
36
+ ## Compatibility
37
+
38
+ | `@a77ay/better-auth-mikro-orm` | `better-auth` | `@mikro-orm/core` |
39
+ | ------------------------------ | ------------- | ----------------- |
40
+ | `0.x` (latest) | `^1.7.0` | `^7.0.0` |
41
+
42
+ ## Quick Start
43
+
44
+ ### 1. Adapter Setup
45
+
46
+ ```typescript
47
+ import { betterAuth } from "better-auth";
48
+ import { mikroOrmAdapter } from "@a77ay/better-auth-mikro-orm";
49
+ import { orm } from "./mikro-orm.config";
50
+
51
+ export const auth = betterAuth({
52
+ database: mikroOrmAdapter(orm),
53
+ });
54
+ ```
55
+
56
+ Pass your `MikroORM` instance, `EntityManager`, or a getter function. When passing `orm`, the adapter automatically uses `RequestContext.getEntityManager()` per HTTP request and falls back to `orm.em` for CLI commands and background scripts.
57
+
58
+ ### 2. Schema / Entity Generation
59
+
60
+ #### Running the Generator
61
+
62
+ Use the package CLI to generate individual entity files and an `index.ts` barrel export:
63
+
64
+ ```bash
65
+ # Using npx
66
+ npx @a77ay/better-auth-mikro-orm generate \
67
+ --config ./src/auth.ts \
68
+ --output ./src/entities/auth \
69
+ --yes
70
+
71
+ # Using pnpm
72
+ pnpm exec @a77ay/better-auth-mikro-orm generate \
73
+ --config ./src/auth.ts \
74
+ --output ./src/entities/auth \
75
+ --yes
76
+ ```
77
+
78
+ #### Register in MikroORM
79
+
80
+ The generated `index.ts` exports each entity and a `betterAuthEntities` array. Register that array in your MikroORM configuration:
81
+
82
+ ```typescript
83
+ import { betterAuthEntities } from "./src/entities/auth";
84
+
85
+ export default {
86
+ entities: [...betterAuthEntities],
87
+ };
88
+ ```
89
+
90
+ Every Better Auth model must be registered as a MikroORM entity. Its `tableName` must match the
91
+ corresponding Better Auth model name (including configured model renames or pluralization).
92
+
93
+ The adapter handles data access and transactions; MikroORM migrations remain responsible for creating
94
+ and updating the database schema. After generating entities, use MikroORM's migration tooling to create
95
+ and apply the database migration.
96
+
97
+ #### CLI Flags
98
+
99
+ - `--config <path>`: Better Auth config (default: `./src/auth.ts`).
100
+ - `--output <path>`: Output directory (default: `src/entities/auth`).
101
+ - `--style <style>`: `define-entity` or `decorators`; overrides the adapter option.
102
+ - `--casing <casing>`: `camelCase` or `snake_case`; overrides the adapter option.
103
+ - `--file-suffix <suffix>`: Custom entity file suffix (e.g. `.entity` for `user.entity.ts`); overrides the adapter option.
104
+ - `--yes`: Overwrite generated files. Without it, existing generated files are protected.
105
+
106
+ #### Configuring Defaults in `auth.ts`
107
+
108
+ Instead of passing CLI flags every time, generator options such as `entityStyle`, `casing`, `fileSuffix`, and the default output `directory` can be configured directly in `mikroOrmAdapter`:
109
+
110
+ ```typescript
111
+ export const auth = betterAuth({
112
+ database: mikroOrmAdapter(orm, {
113
+ entityStyle: "define-entity", // "define-entity" (default) | "decorators"
114
+ casing: "snake_case", // "snake_case" | "camelCase" (default)
115
+ directory: "src/entities/auth", // Default CLI output directory
116
+ fileSuffix: ".entity", // Optional file suffix: user.entity.ts (default: "")
117
+ }),
118
+ });
119
+ ```
120
+
121
+ #### Single-File Generation (Official Better Auth CLI)
122
+
123
+ If you prefer generating a single schema file instead of a modular directory, you can use the official Better Auth CLI:
124
+
125
+ ```bash
126
+ # Using npx
127
+ npx auth@latest generate \
128
+ --config ./src/auth.ts \
129
+ --output ./src/entities/auth.ts \
130
+ --yes
131
+
132
+ # Using pnpm
133
+ pnpm dlx auth@latest generate \
134
+ --config ./src/auth.ts \
135
+ --output ./src/entities/auth.ts \
136
+ --yes
137
+ ```
138
+
139
+ The adapter exposes the official Better Auth `createSchema` hook used by this command.
140
+
141
+ ## Configuration Options
142
+
143
+ The `mikroOrmAdapter` accepts the database instance or getter as its first argument and an optional `options` object as its second argument:
144
+
145
+ ```typescript
146
+ mikroOrmAdapter(em, options?)
147
+ ```
148
+
149
+ | Parameter / Option | Type | Default | Description |
150
+ | ------------------ | ---------------------------------------------------- | ------------------------ | --------------------------------------------------------------------------------------- |
151
+ | `em` (1st arg) | `MikroORM \| EntityManager \| (() => EntityManager)` | **Required** | MikroORM instance, `EntityManager`, or getter returning the request-scoped instance. |
152
+ | `provider` | `DatabaseProvider` | Auto-detected | Database provider (`"postgresql"`, `"sqlite"`, `"mysql"`, `"mongodb"`, or custom). |
153
+ | `casing` | `"snake_case" \| "camelCase"` | `"camelCase"` | Naming convention for generated database columns. |
154
+ | `entityStyle` | `"define-entity" \| "decorators"` | `"define-entity"` | Entity definition style (`defineEntity` fluent schema or `@Entity()` class decorators). |
155
+ | `fileSuffix` | `string` | `""` | Custom file suffix for entity files (e.g. `".entity"` generates `user.entity.ts`). |
156
+ | `directory` | `string` | `"src/entities/auth"` | Default output directory used by the package CLI. |
157
+ | `schemaFile` | `string` | `"src/entities/auth.ts"` | Default output file path for CLI schema generation. |
158
+ | `debugLogs` | `boolean \| DBAdapterDebugLogOption` | `false` | Enable Better Auth adapter debug logging. |
159
+ | `usePlural` | `boolean` | `false` | Use plural table names for generated entities. |
160
+ | `transactions` | `boolean` | `true` | Whether to execute multi-operation callbacks in a transaction. |
161
+
162
+ ## Development
163
+
164
+ This project uses [Vite+](https://viteplus.dev) for tooling:
165
+
166
+ - Install dependencies:
167
+
168
+ ```bash
169
+ vp install
170
+ ```
171
+
172
+ - Run tests (SQLite in-memory):
173
+
174
+ ```bash
175
+ vp test
176
+ ```
177
+
178
+ - Type check, lint and format:
179
+
180
+ ```bash
181
+ vp check
182
+ ```
183
+
184
+ - Build library:
185
+ ```bash
186
+ vp pack
187
+ ```
188
+
189
+ ## License
190
+
191
+ [MIT](LICENSE) © [A77AY](https://github.com/A77AY)