@forklaunch/better-auth-mikro-orm-fork 0.3.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/lib/adapter.cjs +313 -0
- package/lib/adapter.d.cts +15 -0
- package/lib/adapter.d.ts +15 -0
- package/lib/adapter.js +286 -0
- package/license +21 -0
- package/package.json +86 -0
- package/readme.md +53 -0
package/lib/adapter.cjs
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
mikroOrmAdapter: () => mikroOrmAdapter
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
|
|
27
|
+
// src/adapter.ts
|
|
28
|
+
var import_better_auth2 = require("better-auth");
|
|
29
|
+
var import_dset2 = require("dset");
|
|
30
|
+
|
|
31
|
+
// src/utils/adapterUtils.ts
|
|
32
|
+
var import_core = require("@mikro-orm/core");
|
|
33
|
+
var import_dset = require("dset");
|
|
34
|
+
|
|
35
|
+
// src/utils/createAdapterError.ts
|
|
36
|
+
var import_better_auth = require("better-auth");
|
|
37
|
+
function createAdapterError(message) {
|
|
38
|
+
throw new import_better_auth.BetterAuthError(`[Mikro ORM Adapter] ${message}`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/utils/adapterUtils.ts
|
|
42
|
+
var ownReferences = [import_core.ReferenceKind.SCALAR, import_core.ReferenceKind.ONE_TO_MANY];
|
|
43
|
+
function createAdapterUtils(orm) {
|
|
44
|
+
const naming = orm.config.getNamingStrategy();
|
|
45
|
+
const metadata = orm.getMetadata();
|
|
46
|
+
const normalizeEntityName = (name) => naming.getEntityName(naming.classToTableName(name));
|
|
47
|
+
const getEntityMetadata = (entityName) => {
|
|
48
|
+
entityName = normalizeEntityName(entityName);
|
|
49
|
+
if (!metadata.has(entityName)) {
|
|
50
|
+
createAdapterError(
|
|
51
|
+
`Cannot find metadata for "${entityName}" entity. Make sure it defined and listed in your Mikro ORM config.`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
return metadata.get(entityName);
|
|
55
|
+
};
|
|
56
|
+
function getPropertyMetadata(metadata2, fieldName) {
|
|
57
|
+
const prop = metadata2.props.find((prop2) => {
|
|
58
|
+
if (ownReferences.includes(prop2.kind) && prop2.name === fieldName) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
if (prop2.kind === import_core.ReferenceKind.MANY_TO_ONE && (prop2.name === fieldName || prop2.fieldNames.includes(naming.propertyToColumnName(fieldName)))) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
if (prop2.kind === import_core.ReferenceKind.MANY_TO_MANY && (prop2.name === fieldName || prop2.fieldNames.includes(naming.propertyToColumnName(fieldName)))) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
});
|
|
69
|
+
if (!prop) {
|
|
70
|
+
createAdapterError(
|
|
71
|
+
`Can't find property "${fieldName}" on entity "${metadata2.className}".`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return prop;
|
|
75
|
+
}
|
|
76
|
+
function getReferencedColumnName(entityName, prop) {
|
|
77
|
+
if (ownReferences.includes(prop.kind)) {
|
|
78
|
+
return prop.name;
|
|
79
|
+
}
|
|
80
|
+
if (prop.kind === import_core.ReferenceKind.MANY_TO_ONE) {
|
|
81
|
+
return naming.joinColumnName(prop.name);
|
|
82
|
+
}
|
|
83
|
+
if (prop.kind === import_core.ReferenceKind.MANY_TO_MANY) {
|
|
84
|
+
return prop.name;
|
|
85
|
+
}
|
|
86
|
+
createAdapterError(
|
|
87
|
+
`Reference kind ${prop.kind} is not supported. Defined in "${entityName}" entity for "${prop.name}" field.`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
const getReferencedPropertyName = (metadata2, prop) => naming.columnNameToProperty(
|
|
91
|
+
getReferencedColumnName(metadata2.className, prop)
|
|
92
|
+
);
|
|
93
|
+
const getFieldPath = (metadata2, fieldName, throwOnShadowProps = false) => {
|
|
94
|
+
const prop = getPropertyMetadata(metadata2, fieldName);
|
|
95
|
+
if (prop.persist === false && throwOnShadowProps) {
|
|
96
|
+
createAdapterError(
|
|
97
|
+
`Cannot serialize "${fieldName}" into path, because it cannot be persisted in "${metadata2.tableName}" table.`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
if (prop.kind === import_core.ReferenceKind.SCALAR) {
|
|
101
|
+
return [prop.name];
|
|
102
|
+
}
|
|
103
|
+
if (prop.kind === import_core.ReferenceKind.MANY_TO_ONE) {
|
|
104
|
+
if (prop.referencedPKs.length > 1) {
|
|
105
|
+
createAdapterError(
|
|
106
|
+
`The "${fieldName}" field references to a table "${prop.name}" with complex primary key, which is not supported`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
return [prop.name, naming.referenceColumnName()];
|
|
110
|
+
}
|
|
111
|
+
createAdapterError(
|
|
112
|
+
`Cannot normalize "${fieldName}" field name into path for "${metadata2.className} entity."`
|
|
113
|
+
);
|
|
114
|
+
};
|
|
115
|
+
const normalizeInput = (metadata2, input) => {
|
|
116
|
+
const fields = {};
|
|
117
|
+
Object.entries(input).forEach(([key, value]) => {
|
|
118
|
+
if (typeof value === "object" && value["$in"]) {
|
|
119
|
+
(0, import_dset.dset)(fields, key, value["$in"]);
|
|
120
|
+
} else {
|
|
121
|
+
const path = getFieldPath(metadata2, key);
|
|
122
|
+
(0, import_dset.dset)(fields, path, value);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
return fields;
|
|
126
|
+
};
|
|
127
|
+
const normalizeOutput = (metadata2, output, select) => {
|
|
128
|
+
output = (0, import_core.serialize)(output);
|
|
129
|
+
const result = {};
|
|
130
|
+
Object.entries(output).map(([key, value]) => ({
|
|
131
|
+
path: getReferencedPropertyName(
|
|
132
|
+
metadata2,
|
|
133
|
+
getPropertyMetadata(metadata2, key)
|
|
134
|
+
),
|
|
135
|
+
value
|
|
136
|
+
})).filter(({ path }) => select ? select.includes(path) : true).forEach(({ path, value }) => (0, import_dset.dset)(result, path, value));
|
|
137
|
+
return result;
|
|
138
|
+
};
|
|
139
|
+
function createWhereClause(path, value, op, target = {}) {
|
|
140
|
+
(0, import_dset.dset)(target, op == null || op === "eq" ? path : path.concat(op), value);
|
|
141
|
+
return target;
|
|
142
|
+
}
|
|
143
|
+
function createWhereInClause(fieldName, path, value, target) {
|
|
144
|
+
if (!Array.isArray(value)) {
|
|
145
|
+
createAdapterError(
|
|
146
|
+
`The value for the field "${fieldName}" must be an array when using the $in operator.`
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
return createWhereClause(path, value, "$in", target);
|
|
150
|
+
}
|
|
151
|
+
const normalizeWhereClauses = (metadata2, where) => {
|
|
152
|
+
if (!where) {
|
|
153
|
+
return {};
|
|
154
|
+
}
|
|
155
|
+
if (where.length === 1) {
|
|
156
|
+
const [w] = where;
|
|
157
|
+
if (!w) {
|
|
158
|
+
return {};
|
|
159
|
+
}
|
|
160
|
+
const path = getFieldPath(metadata2, w.field, true);
|
|
161
|
+
switch (w.operator) {
|
|
162
|
+
case "in":
|
|
163
|
+
return createWhereInClause(w.field, path, w.value);
|
|
164
|
+
case "contains":
|
|
165
|
+
return createWhereClause(path, `%${w.value}%`, "$like");
|
|
166
|
+
case "starts_with":
|
|
167
|
+
return createWhereClause(path, `${w.value}%`, "$like");
|
|
168
|
+
case "ends_with":
|
|
169
|
+
return createWhereClause(path, `%${w.value}`, "$like");
|
|
170
|
+
// The next 5 case statemets are _expected_ to fall through so we can simplify and reuse the same logic for these operators
|
|
171
|
+
case "gt":
|
|
172
|
+
case "gte":
|
|
173
|
+
case "lt":
|
|
174
|
+
case "lte":
|
|
175
|
+
case "ne":
|
|
176
|
+
return createWhereClause(path, w.value, `$${w.operator}`);
|
|
177
|
+
default:
|
|
178
|
+
return createWhereClause(path, w.value);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const result = {};
|
|
182
|
+
where.filter(({ connector }) => !connector || connector === "AND").forEach(({ field, operator, value }, index) => {
|
|
183
|
+
const path = ["$and", index].concat(
|
|
184
|
+
getFieldPath(metadata2, field, true)
|
|
185
|
+
);
|
|
186
|
+
if (operator === "in") {
|
|
187
|
+
return createWhereInClause(field, path, value, result);
|
|
188
|
+
}
|
|
189
|
+
return createWhereClause(path, value, "eq", result);
|
|
190
|
+
});
|
|
191
|
+
where.filter(({ connector }) => connector === "OR").forEach(({ field, value }, index) => {
|
|
192
|
+
const path = ["$and", index].concat(
|
|
193
|
+
getFieldPath(metadata2, field, true)
|
|
194
|
+
);
|
|
195
|
+
return createWhereClause(path, value, "eq", result);
|
|
196
|
+
});
|
|
197
|
+
return result;
|
|
198
|
+
};
|
|
199
|
+
return {
|
|
200
|
+
getEntityMetadata,
|
|
201
|
+
normalizeEntityName,
|
|
202
|
+
getFieldPath,
|
|
203
|
+
normalizeInput,
|
|
204
|
+
normalizeOutput,
|
|
205
|
+
normalizeWhereClauses
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// src/adapter.ts
|
|
210
|
+
function mikroOrmAdapter(orm) {
|
|
211
|
+
const {
|
|
212
|
+
getEntityMetadata,
|
|
213
|
+
getFieldPath,
|
|
214
|
+
normalizeInput,
|
|
215
|
+
normalizeOutput,
|
|
216
|
+
normalizeWhereClauses
|
|
217
|
+
} = createAdapterUtils(orm);
|
|
218
|
+
const adapter = (options = {}) => ({
|
|
219
|
+
id: "mikro-orm",
|
|
220
|
+
async create({ model, data, select }) {
|
|
221
|
+
const metadata = getEntityMetadata(model);
|
|
222
|
+
const input = normalizeInput(metadata, data);
|
|
223
|
+
if (options.advanced?.generateId !== false) {
|
|
224
|
+
input.id = typeof options.advanced?.generateId === "function" ? options.advanced.generateId({ model }) : (0, import_better_auth2.generateId)();
|
|
225
|
+
}
|
|
226
|
+
const entity = orm.em.create(metadata.class, input);
|
|
227
|
+
await orm.em.persistAndFlush(entity);
|
|
228
|
+
return normalizeOutput(metadata, entity, select);
|
|
229
|
+
},
|
|
230
|
+
async count({ model, where }) {
|
|
231
|
+
const metadata = getEntityMetadata(model);
|
|
232
|
+
return orm.em.count(
|
|
233
|
+
metadata.class,
|
|
234
|
+
normalizeWhereClauses(metadata, where)
|
|
235
|
+
);
|
|
236
|
+
},
|
|
237
|
+
async findOne({ model, where, select }) {
|
|
238
|
+
const metadata = getEntityMetadata(model);
|
|
239
|
+
const entity = await orm.em.findOne(
|
|
240
|
+
metadata.class,
|
|
241
|
+
normalizeWhereClauses(metadata, where)
|
|
242
|
+
);
|
|
243
|
+
if (!entity) {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
return normalizeOutput(metadata, entity, select);
|
|
247
|
+
},
|
|
248
|
+
async findMany({ model, where, limit, offset, sortBy }) {
|
|
249
|
+
const metadata = getEntityMetadata(model);
|
|
250
|
+
const options2 = {
|
|
251
|
+
limit,
|
|
252
|
+
offset
|
|
253
|
+
};
|
|
254
|
+
if (sortBy) {
|
|
255
|
+
const path = getFieldPath(metadata, sortBy.field);
|
|
256
|
+
(0, import_dset2.dset)(options2, ["orderBy", ...path], sortBy.direction);
|
|
257
|
+
}
|
|
258
|
+
const rows = await orm.em.find(
|
|
259
|
+
metadata.class,
|
|
260
|
+
normalizeWhereClauses(metadata, where),
|
|
261
|
+
options2
|
|
262
|
+
);
|
|
263
|
+
return rows.map((row) => normalizeOutput(metadata, row));
|
|
264
|
+
},
|
|
265
|
+
async update({ model, where, update }) {
|
|
266
|
+
const metadata = getEntityMetadata(model);
|
|
267
|
+
const entity = await orm.em.findOne(
|
|
268
|
+
metadata.class,
|
|
269
|
+
normalizeWhereClauses(metadata, where)
|
|
270
|
+
);
|
|
271
|
+
if (!entity) {
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
orm.em.assign(entity, normalizeInput(metadata, update));
|
|
275
|
+
await orm.em.flush();
|
|
276
|
+
return normalizeOutput(metadata, entity);
|
|
277
|
+
},
|
|
278
|
+
async updateMany({ model, where, update }) {
|
|
279
|
+
const metadata = getEntityMetadata(model);
|
|
280
|
+
const affected = await orm.em.nativeUpdate(
|
|
281
|
+
metadata.class,
|
|
282
|
+
normalizeWhereClauses(metadata, where),
|
|
283
|
+
normalizeInput(metadata, update)
|
|
284
|
+
);
|
|
285
|
+
orm.em.clear();
|
|
286
|
+
return affected;
|
|
287
|
+
},
|
|
288
|
+
async delete({ model, where }) {
|
|
289
|
+
const metadata = getEntityMetadata(model);
|
|
290
|
+
const entity = await orm.em.findOne(
|
|
291
|
+
metadata.class,
|
|
292
|
+
normalizeWhereClauses(metadata, where)
|
|
293
|
+
);
|
|
294
|
+
if (entity) {
|
|
295
|
+
await orm.em.removeAndFlush(entity);
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
async deleteMany({ model, where }) {
|
|
299
|
+
const metadata = getEntityMetadata(model);
|
|
300
|
+
const affected = await orm.em.nativeDelete(
|
|
301
|
+
metadata.class,
|
|
302
|
+
normalizeWhereClauses(metadata, where)
|
|
303
|
+
);
|
|
304
|
+
orm.em.clear();
|
|
305
|
+
return affected;
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
return adapter;
|
|
309
|
+
}
|
|
310
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
311
|
+
0 && (module.exports = {
|
|
312
|
+
mikroOrmAdapter
|
|
313
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { MikroORM } from '@mikro-orm/core';
|
|
2
|
+
import { BetterAuthOptions, Adapter } from 'better-auth';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates Mikro ORM adapter for Better Auth.
|
|
6
|
+
*
|
|
7
|
+
* Current limitations:
|
|
8
|
+
* * No m:m and 1:m and embedded references support
|
|
9
|
+
* * No complex primary key support
|
|
10
|
+
*
|
|
11
|
+
* @param orm - Instance of Mikro ORM returned from `MikroORM.init` or `MikroORM.initSync` methods
|
|
12
|
+
*/
|
|
13
|
+
declare function mikroOrmAdapter(orm: MikroORM): (options?: BetterAuthOptions) => Adapter;
|
|
14
|
+
|
|
15
|
+
export { mikroOrmAdapter };
|
package/lib/adapter.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { MikroORM } from '@mikro-orm/core';
|
|
2
|
+
import { BetterAuthOptions, Adapter } from 'better-auth';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates Mikro ORM adapter for Better Auth.
|
|
6
|
+
*
|
|
7
|
+
* Current limitations:
|
|
8
|
+
* * No m:m and 1:m and embedded references support
|
|
9
|
+
* * No complex primary key support
|
|
10
|
+
*
|
|
11
|
+
* @param orm - Instance of Mikro ORM returned from `MikroORM.init` or `MikroORM.initSync` methods
|
|
12
|
+
*/
|
|
13
|
+
declare function mikroOrmAdapter(orm: MikroORM): (options?: BetterAuthOptions) => Adapter;
|
|
14
|
+
|
|
15
|
+
export { mikroOrmAdapter };
|
package/lib/adapter.js
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
// src/adapter.ts
|
|
2
|
+
import { generateId } from "better-auth";
|
|
3
|
+
import { dset as dset2 } from "dset";
|
|
4
|
+
|
|
5
|
+
// src/utils/adapterUtils.ts
|
|
6
|
+
import { ReferenceKind, serialize } from "@mikro-orm/core";
|
|
7
|
+
import { dset } from "dset";
|
|
8
|
+
|
|
9
|
+
// src/utils/createAdapterError.ts
|
|
10
|
+
import { BetterAuthError } from "better-auth";
|
|
11
|
+
function createAdapterError(message) {
|
|
12
|
+
throw new BetterAuthError(`[Mikro ORM Adapter] ${message}`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// src/utils/adapterUtils.ts
|
|
16
|
+
var ownReferences = [ReferenceKind.SCALAR, ReferenceKind.ONE_TO_MANY];
|
|
17
|
+
function createAdapterUtils(orm) {
|
|
18
|
+
const naming = orm.config.getNamingStrategy();
|
|
19
|
+
const metadata = orm.getMetadata();
|
|
20
|
+
const normalizeEntityName = (name) => naming.getEntityName(naming.classToTableName(name));
|
|
21
|
+
const getEntityMetadata = (entityName) => {
|
|
22
|
+
entityName = normalizeEntityName(entityName);
|
|
23
|
+
if (!metadata.has(entityName)) {
|
|
24
|
+
createAdapterError(
|
|
25
|
+
`Cannot find metadata for "${entityName}" entity. Make sure it defined and listed in your Mikro ORM config.`
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return metadata.get(entityName);
|
|
29
|
+
};
|
|
30
|
+
function getPropertyMetadata(metadata2, fieldName) {
|
|
31
|
+
const prop = metadata2.props.find((prop2) => {
|
|
32
|
+
if (ownReferences.includes(prop2.kind) && prop2.name === fieldName) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
if (prop2.kind === ReferenceKind.MANY_TO_ONE && (prop2.name === fieldName || prop2.fieldNames.includes(naming.propertyToColumnName(fieldName)))) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
if (prop2.kind === ReferenceKind.MANY_TO_MANY && (prop2.name === fieldName || prop2.fieldNames.includes(naming.propertyToColumnName(fieldName)))) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
});
|
|
43
|
+
if (!prop) {
|
|
44
|
+
createAdapterError(
|
|
45
|
+
`Can't find property "${fieldName}" on entity "${metadata2.className}".`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
return prop;
|
|
49
|
+
}
|
|
50
|
+
function getReferencedColumnName(entityName, prop) {
|
|
51
|
+
if (ownReferences.includes(prop.kind)) {
|
|
52
|
+
return prop.name;
|
|
53
|
+
}
|
|
54
|
+
if (prop.kind === ReferenceKind.MANY_TO_ONE) {
|
|
55
|
+
return naming.joinColumnName(prop.name);
|
|
56
|
+
}
|
|
57
|
+
if (prop.kind === ReferenceKind.MANY_TO_MANY) {
|
|
58
|
+
return prop.name;
|
|
59
|
+
}
|
|
60
|
+
createAdapterError(
|
|
61
|
+
`Reference kind ${prop.kind} is not supported. Defined in "${entityName}" entity for "${prop.name}" field.`
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
const getReferencedPropertyName = (metadata2, prop) => naming.columnNameToProperty(
|
|
65
|
+
getReferencedColumnName(metadata2.className, prop)
|
|
66
|
+
);
|
|
67
|
+
const getFieldPath = (metadata2, fieldName, throwOnShadowProps = false) => {
|
|
68
|
+
const prop = getPropertyMetadata(metadata2, fieldName);
|
|
69
|
+
if (prop.persist === false && throwOnShadowProps) {
|
|
70
|
+
createAdapterError(
|
|
71
|
+
`Cannot serialize "${fieldName}" into path, because it cannot be persisted in "${metadata2.tableName}" table.`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
if (prop.kind === ReferenceKind.SCALAR) {
|
|
75
|
+
return [prop.name];
|
|
76
|
+
}
|
|
77
|
+
if (prop.kind === ReferenceKind.MANY_TO_ONE) {
|
|
78
|
+
if (prop.referencedPKs.length > 1) {
|
|
79
|
+
createAdapterError(
|
|
80
|
+
`The "${fieldName}" field references to a table "${prop.name}" with complex primary key, which is not supported`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return [prop.name, naming.referenceColumnName()];
|
|
84
|
+
}
|
|
85
|
+
createAdapterError(
|
|
86
|
+
`Cannot normalize "${fieldName}" field name into path for "${metadata2.className} entity."`
|
|
87
|
+
);
|
|
88
|
+
};
|
|
89
|
+
const normalizeInput = (metadata2, input) => {
|
|
90
|
+
const fields = {};
|
|
91
|
+
Object.entries(input).forEach(([key, value]) => {
|
|
92
|
+
if (typeof value === "object" && value["$in"]) {
|
|
93
|
+
dset(fields, key, value["$in"]);
|
|
94
|
+
} else {
|
|
95
|
+
const path = getFieldPath(metadata2, key);
|
|
96
|
+
dset(fields, path, value);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
return fields;
|
|
100
|
+
};
|
|
101
|
+
const normalizeOutput = (metadata2, output, select) => {
|
|
102
|
+
output = serialize(output);
|
|
103
|
+
const result = {};
|
|
104
|
+
Object.entries(output).map(([key, value]) => ({
|
|
105
|
+
path: getReferencedPropertyName(
|
|
106
|
+
metadata2,
|
|
107
|
+
getPropertyMetadata(metadata2, key)
|
|
108
|
+
),
|
|
109
|
+
value
|
|
110
|
+
})).filter(({ path }) => select ? select.includes(path) : true).forEach(({ path, value }) => dset(result, path, value));
|
|
111
|
+
return result;
|
|
112
|
+
};
|
|
113
|
+
function createWhereClause(path, value, op, target = {}) {
|
|
114
|
+
dset(target, op == null || op === "eq" ? path : path.concat(op), value);
|
|
115
|
+
return target;
|
|
116
|
+
}
|
|
117
|
+
function createWhereInClause(fieldName, path, value, target) {
|
|
118
|
+
if (!Array.isArray(value)) {
|
|
119
|
+
createAdapterError(
|
|
120
|
+
`The value for the field "${fieldName}" must be an array when using the $in operator.`
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
return createWhereClause(path, value, "$in", target);
|
|
124
|
+
}
|
|
125
|
+
const normalizeWhereClauses = (metadata2, where) => {
|
|
126
|
+
if (!where) {
|
|
127
|
+
return {};
|
|
128
|
+
}
|
|
129
|
+
if (where.length === 1) {
|
|
130
|
+
const [w] = where;
|
|
131
|
+
if (!w) {
|
|
132
|
+
return {};
|
|
133
|
+
}
|
|
134
|
+
const path = getFieldPath(metadata2, w.field, true);
|
|
135
|
+
switch (w.operator) {
|
|
136
|
+
case "in":
|
|
137
|
+
return createWhereInClause(w.field, path, w.value);
|
|
138
|
+
case "contains":
|
|
139
|
+
return createWhereClause(path, `%${w.value}%`, "$like");
|
|
140
|
+
case "starts_with":
|
|
141
|
+
return createWhereClause(path, `${w.value}%`, "$like");
|
|
142
|
+
case "ends_with":
|
|
143
|
+
return createWhereClause(path, `%${w.value}`, "$like");
|
|
144
|
+
// The next 5 case statemets are _expected_ to fall through so we can simplify and reuse the same logic for these operators
|
|
145
|
+
case "gt":
|
|
146
|
+
case "gte":
|
|
147
|
+
case "lt":
|
|
148
|
+
case "lte":
|
|
149
|
+
case "ne":
|
|
150
|
+
return createWhereClause(path, w.value, `$${w.operator}`);
|
|
151
|
+
default:
|
|
152
|
+
return createWhereClause(path, w.value);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const result = {};
|
|
156
|
+
where.filter(({ connector }) => !connector || connector === "AND").forEach(({ field, operator, value }, index) => {
|
|
157
|
+
const path = ["$and", index].concat(
|
|
158
|
+
getFieldPath(metadata2, field, true)
|
|
159
|
+
);
|
|
160
|
+
if (operator === "in") {
|
|
161
|
+
return createWhereInClause(field, path, value, result);
|
|
162
|
+
}
|
|
163
|
+
return createWhereClause(path, value, "eq", result);
|
|
164
|
+
});
|
|
165
|
+
where.filter(({ connector }) => connector === "OR").forEach(({ field, value }, index) => {
|
|
166
|
+
const path = ["$and", index].concat(
|
|
167
|
+
getFieldPath(metadata2, field, true)
|
|
168
|
+
);
|
|
169
|
+
return createWhereClause(path, value, "eq", result);
|
|
170
|
+
});
|
|
171
|
+
return result;
|
|
172
|
+
};
|
|
173
|
+
return {
|
|
174
|
+
getEntityMetadata,
|
|
175
|
+
normalizeEntityName,
|
|
176
|
+
getFieldPath,
|
|
177
|
+
normalizeInput,
|
|
178
|
+
normalizeOutput,
|
|
179
|
+
normalizeWhereClauses
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// src/adapter.ts
|
|
184
|
+
function mikroOrmAdapter(orm) {
|
|
185
|
+
const {
|
|
186
|
+
getEntityMetadata,
|
|
187
|
+
getFieldPath,
|
|
188
|
+
normalizeInput,
|
|
189
|
+
normalizeOutput,
|
|
190
|
+
normalizeWhereClauses
|
|
191
|
+
} = createAdapterUtils(orm);
|
|
192
|
+
const adapter = (options = {}) => ({
|
|
193
|
+
id: "mikro-orm",
|
|
194
|
+
async create({ model, data, select }) {
|
|
195
|
+
const metadata = getEntityMetadata(model);
|
|
196
|
+
const input = normalizeInput(metadata, data);
|
|
197
|
+
if (options.advanced?.generateId !== false) {
|
|
198
|
+
input.id = typeof options.advanced?.generateId === "function" ? options.advanced.generateId({ model }) : generateId();
|
|
199
|
+
}
|
|
200
|
+
const entity = orm.em.create(metadata.class, input);
|
|
201
|
+
await orm.em.persistAndFlush(entity);
|
|
202
|
+
return normalizeOutput(metadata, entity, select);
|
|
203
|
+
},
|
|
204
|
+
async count({ model, where }) {
|
|
205
|
+
const metadata = getEntityMetadata(model);
|
|
206
|
+
return orm.em.count(
|
|
207
|
+
metadata.class,
|
|
208
|
+
normalizeWhereClauses(metadata, where)
|
|
209
|
+
);
|
|
210
|
+
},
|
|
211
|
+
async findOne({ model, where, select }) {
|
|
212
|
+
const metadata = getEntityMetadata(model);
|
|
213
|
+
const entity = await orm.em.findOne(
|
|
214
|
+
metadata.class,
|
|
215
|
+
normalizeWhereClauses(metadata, where)
|
|
216
|
+
);
|
|
217
|
+
if (!entity) {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
return normalizeOutput(metadata, entity, select);
|
|
221
|
+
},
|
|
222
|
+
async findMany({ model, where, limit, offset, sortBy }) {
|
|
223
|
+
const metadata = getEntityMetadata(model);
|
|
224
|
+
const options2 = {
|
|
225
|
+
limit,
|
|
226
|
+
offset
|
|
227
|
+
};
|
|
228
|
+
if (sortBy) {
|
|
229
|
+
const path = getFieldPath(metadata, sortBy.field);
|
|
230
|
+
dset2(options2, ["orderBy", ...path], sortBy.direction);
|
|
231
|
+
}
|
|
232
|
+
const rows = await orm.em.find(
|
|
233
|
+
metadata.class,
|
|
234
|
+
normalizeWhereClauses(metadata, where),
|
|
235
|
+
options2
|
|
236
|
+
);
|
|
237
|
+
return rows.map((row) => normalizeOutput(metadata, row));
|
|
238
|
+
},
|
|
239
|
+
async update({ model, where, update }) {
|
|
240
|
+
const metadata = getEntityMetadata(model);
|
|
241
|
+
const entity = await orm.em.findOne(
|
|
242
|
+
metadata.class,
|
|
243
|
+
normalizeWhereClauses(metadata, where)
|
|
244
|
+
);
|
|
245
|
+
if (!entity) {
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
orm.em.assign(entity, normalizeInput(metadata, update));
|
|
249
|
+
await orm.em.flush();
|
|
250
|
+
return normalizeOutput(metadata, entity);
|
|
251
|
+
},
|
|
252
|
+
async updateMany({ model, where, update }) {
|
|
253
|
+
const metadata = getEntityMetadata(model);
|
|
254
|
+
const affected = await orm.em.nativeUpdate(
|
|
255
|
+
metadata.class,
|
|
256
|
+
normalizeWhereClauses(metadata, where),
|
|
257
|
+
normalizeInput(metadata, update)
|
|
258
|
+
);
|
|
259
|
+
orm.em.clear();
|
|
260
|
+
return affected;
|
|
261
|
+
},
|
|
262
|
+
async delete({ model, where }) {
|
|
263
|
+
const metadata = getEntityMetadata(model);
|
|
264
|
+
const entity = await orm.em.findOne(
|
|
265
|
+
metadata.class,
|
|
266
|
+
normalizeWhereClauses(metadata, where)
|
|
267
|
+
);
|
|
268
|
+
if (entity) {
|
|
269
|
+
await orm.em.removeAndFlush(entity);
|
|
270
|
+
}
|
|
271
|
+
},
|
|
272
|
+
async deleteMany({ model, where }) {
|
|
273
|
+
const metadata = getEntityMetadata(model);
|
|
274
|
+
const affected = await orm.em.nativeDelete(
|
|
275
|
+
metadata.class,
|
|
276
|
+
normalizeWhereClauses(metadata, where)
|
|
277
|
+
);
|
|
278
|
+
orm.em.clear();
|
|
279
|
+
return affected;
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
return adapter;
|
|
283
|
+
}
|
|
284
|
+
export {
|
|
285
|
+
mikroOrmAdapter
|
|
286
|
+
};
|
package/license
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present Nick K.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
{
|
|
2
|
+
"private": false,
|
|
3
|
+
"type": "module",
|
|
4
|
+
"name": "@forklaunch/better-auth-mikro-orm-fork",
|
|
5
|
+
"version": "0.3.0",
|
|
6
|
+
"description": "Mikro ORM Adapter for Better Auth",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"auth",
|
|
9
|
+
"better-auth",
|
|
10
|
+
"db",
|
|
11
|
+
"database",
|
|
12
|
+
"adapter",
|
|
13
|
+
"better-auth-adapter",
|
|
14
|
+
"mikro-orm"
|
|
15
|
+
],
|
|
16
|
+
"author": "Nick K.",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+ssh://git@github.com/octet-stream/better-auth-mikro-orm.git"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">= 18"
|
|
24
|
+
},
|
|
25
|
+
"types": "./lib/adapter.d.cts",
|
|
26
|
+
"main": "./lib/adapter.cjs",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"node": {
|
|
30
|
+
"types": "./lib/adapter.d.cts",
|
|
31
|
+
"module-sync": "./lib/adapter.js",
|
|
32
|
+
"default": "./lib/adapter.cjs"
|
|
33
|
+
},
|
|
34
|
+
"import": {
|
|
35
|
+
"types": "./lib/adapter.d.ts",
|
|
36
|
+
"default": "./lib/adapter.js"
|
|
37
|
+
},
|
|
38
|
+
"default": {
|
|
39
|
+
"types": "./lib/adapter.d.cts",
|
|
40
|
+
"default": "./lib/adapter.cjs"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"./package.json": "./package.json"
|
|
44
|
+
},
|
|
45
|
+
"files": [
|
|
46
|
+
"lib"
|
|
47
|
+
],
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@biomejs/biome": "1.9.4",
|
|
50
|
+
"@changesets/changelog-github": "0.5.1",
|
|
51
|
+
"@changesets/cli": "2.29.2",
|
|
52
|
+
"@commitlint/cli": "19.8.0",
|
|
53
|
+
"@commitlint/config-conventional": "19.8.0",
|
|
54
|
+
"@commitlint/types": "19.8.0",
|
|
55
|
+
"@faker-js/faker": "9.7.0",
|
|
56
|
+
"@mikro-orm/better-sqlite": "6.4.13",
|
|
57
|
+
"@mikro-orm/core": "6.4.13",
|
|
58
|
+
"@types/node": "22.14.1",
|
|
59
|
+
"@types/uuid": "10.0.0",
|
|
60
|
+
"@vitest/coverage-v8": "3.1.1",
|
|
61
|
+
"@vitest/ui": "3.1.1",
|
|
62
|
+
"better-auth": "1.2.7",
|
|
63
|
+
"del-cli": "6.0.0",
|
|
64
|
+
"husky": "9.1.7",
|
|
65
|
+
"is-in-ci": "1.0.0",
|
|
66
|
+
"tsup": "8.4.0",
|
|
67
|
+
"typescript": "5.8.3",
|
|
68
|
+
"uuid": "11.1.0",
|
|
69
|
+
"vitest": "3.1.1"
|
|
70
|
+
},
|
|
71
|
+
"dependencies": {
|
|
72
|
+
"dset": "3.1.4"
|
|
73
|
+
},
|
|
74
|
+
"peerDependencies": {
|
|
75
|
+
"@mikro-orm/core": "^6.0.0",
|
|
76
|
+
"better-auth": "^1.0.0"
|
|
77
|
+
},
|
|
78
|
+
"scripts": {
|
|
79
|
+
"build": "del-cli lib && tsup-node",
|
|
80
|
+
"test": "vitest run",
|
|
81
|
+
"test.watch": "vitest",
|
|
82
|
+
"test.ui": "vitest --ui",
|
|
83
|
+
"coverage": "vitest run --coverage",
|
|
84
|
+
"release": "pnpm build && changeset publish"
|
|
85
|
+
}
|
|
86
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# better-auth-mikro-orm
|
|
2
|
+
|
|
3
|
+
[Mikro ORM](https://mikro-orm.io/) adapter for [Better Auth](https://www.better-auth.com/)
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
pnpm:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add better-auth-mikro-orm
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
npm:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm i better-auth-mikro-orm
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
1. First you'll need to set up Mikro ORM and define the [core schema](https://www.better-auth.com/docs/concepts/database#core-schema) for Better Auth.
|
|
22
|
+
If you use any plugin - don't forget to check if they have any additional database schema definitions, then define entities you'll need for each plugin.
|
|
23
|
+
2. When you finished with the schema definition you can simply pass the result of `mikroOrmAdapter` call to the `database` option like this:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import {mikroOrmAdapter} from "better-auth-mikro-orm"
|
|
27
|
+
import {betterAuth} from "better-auth"
|
|
28
|
+
|
|
29
|
+
import {orm} from "./orm.js" // Your Mikro ORM instance
|
|
30
|
+
|
|
31
|
+
export const auth = betterAuth({
|
|
32
|
+
database: mikroOrmAdapter(orm),
|
|
33
|
+
|
|
34
|
+
// Don't forget to disable ID generator if it already managed by Mikro ORM:
|
|
35
|
+
advanced: {
|
|
36
|
+
generateId: false
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## API
|
|
42
|
+
|
|
43
|
+
### `mikroOrmAdapter(orm: MikroORM): AdapterInstance`
|
|
44
|
+
|
|
45
|
+
Creates Mikro ORM adapter instance. Note that this adapter **does not** manage database schema for you, so you can't use it with [`@better-auth/cli`](https://www.better-auth.com/docs/concepts/cli).
|
|
46
|
+
This means you'll have to manage database schema on your own.
|
|
47
|
+
Please refer to Better Auth and Mikro ORM documentation on the details.
|
|
48
|
+
|
|
49
|
+
Returns `AdapterInstance` function for Better Auth `database` option.
|
|
50
|
+
|
|
51
|
+
This function expects a single argument:
|
|
52
|
+
|
|
53
|
+
* `orm` - An instance of `MikroORM` returned from `MikroORM.init` or `MikroORM.initSync` methods.
|