@forklaunch/better-auth-mikro-orm-fork 0.3.0 → 0.4.1
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 +125 -115
- package/lib/adapter.d.cts +25 -3
- package/lib/adapter.d.ts +25 -3
- package/lib/adapter.js +125 -115
- package/package.json +4 -3
package/lib/adapter.cjs
CHANGED
|
@@ -25,7 +25,7 @@ __export(src_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(src_exports);
|
|
26
26
|
|
|
27
27
|
// src/adapter.ts
|
|
28
|
-
var
|
|
28
|
+
var import_adapters = require("better-auth/adapters");
|
|
29
29
|
var import_dset2 = require("dset");
|
|
30
30
|
|
|
31
31
|
// src/utils/adapterUtils.ts
|
|
@@ -39,7 +39,11 @@ function createAdapterError(message) {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
// src/utils/adapterUtils.ts
|
|
42
|
-
var ownReferences = [
|
|
42
|
+
var ownReferences = [
|
|
43
|
+
import_core.ReferenceKind.SCALAR,
|
|
44
|
+
import_core.ReferenceKind.ONE_TO_MANY,
|
|
45
|
+
import_core.ReferenceKind.EMBEDDED
|
|
46
|
+
];
|
|
43
47
|
function createAdapterUtils(orm) {
|
|
44
48
|
const naming = orm.config.getNamingStrategy();
|
|
45
49
|
const metadata = orm.getMetadata();
|
|
@@ -78,7 +82,7 @@ function createAdapterUtils(orm) {
|
|
|
78
82
|
return prop.name;
|
|
79
83
|
}
|
|
80
84
|
if (prop.kind === import_core.ReferenceKind.MANY_TO_ONE) {
|
|
81
|
-
return naming.joinColumnName(prop.name);
|
|
85
|
+
return naming.columnNameToProperty(naming.joinColumnName(prop.name));
|
|
82
86
|
}
|
|
83
87
|
if (prop.kind === import_core.ReferenceKind.MANY_TO_MANY) {
|
|
84
88
|
return prop.name;
|
|
@@ -87,9 +91,7 @@ function createAdapterUtils(orm) {
|
|
|
87
91
|
`Reference kind ${prop.kind} is not supported. Defined in "${entityName}" entity for "${prop.name}" field.`
|
|
88
92
|
);
|
|
89
93
|
}
|
|
90
|
-
const getReferencedPropertyName = (metadata2, prop) =>
|
|
91
|
-
getReferencedColumnName(metadata2.className, prop)
|
|
92
|
-
);
|
|
94
|
+
const getReferencedPropertyName = (metadata2, prop) => getReferencedColumnName(metadata2.className, prop);
|
|
93
95
|
const getFieldPath = (metadata2, fieldName, throwOnShadowProps = false) => {
|
|
94
96
|
const prop = getPropertyMetadata(metadata2, fieldName);
|
|
95
97
|
if (prop.persist === false && throwOnShadowProps) {
|
|
@@ -97,7 +99,7 @@ function createAdapterUtils(orm) {
|
|
|
97
99
|
`Cannot serialize "${fieldName}" into path, because it cannot be persisted in "${metadata2.tableName}" table.`
|
|
98
100
|
);
|
|
99
101
|
}
|
|
100
|
-
if (prop.kind === import_core.ReferenceKind.SCALAR) {
|
|
102
|
+
if (prop.kind === import_core.ReferenceKind.SCALAR || prop.kind === import_core.ReferenceKind.EMBEDDED) {
|
|
101
103
|
return [prop.name];
|
|
102
104
|
}
|
|
103
105
|
if (prop.kind === import_core.ReferenceKind.MANY_TO_ONE) {
|
|
@@ -109,14 +111,14 @@ function createAdapterUtils(orm) {
|
|
|
109
111
|
return [prop.name, naming.referenceColumnName()];
|
|
110
112
|
}
|
|
111
113
|
createAdapterError(
|
|
112
|
-
`Cannot normalize "${fieldName}" field name into path for "${metadata2.className} entity
|
|
114
|
+
`Cannot normalize "${fieldName}" field name into path for "${metadata2.className}" entity.`
|
|
113
115
|
);
|
|
114
116
|
};
|
|
115
117
|
const normalizeInput = (metadata2, input) => {
|
|
116
118
|
const fields = {};
|
|
117
119
|
Object.entries(input).forEach(([key, value]) => {
|
|
118
|
-
if (typeof value === "object" && value
|
|
119
|
-
(0, import_dset.dset)(fields, key, value
|
|
120
|
+
if (typeof value === "object" && value.$in) {
|
|
121
|
+
(0, import_dset.dset)(fields, key, value.$in);
|
|
120
122
|
} else {
|
|
121
123
|
const path = getFieldPath(metadata2, key);
|
|
122
124
|
(0, import_dset.dset)(fields, path, value);
|
|
@@ -180,18 +182,14 @@ function createAdapterUtils(orm) {
|
|
|
180
182
|
}
|
|
181
183
|
const result = {};
|
|
182
184
|
where.filter(({ connector }) => !connector || connector === "AND").forEach(({ field, operator, value }, index) => {
|
|
183
|
-
const path = ["$and", index].concat(
|
|
184
|
-
getFieldPath(metadata2, field, true)
|
|
185
|
-
);
|
|
185
|
+
const path = ["$and", index].concat(getFieldPath(metadata2, field, true));
|
|
186
186
|
if (operator === "in") {
|
|
187
187
|
return createWhereInClause(field, path, value, result);
|
|
188
188
|
}
|
|
189
189
|
return createWhereClause(path, value, "eq", result);
|
|
190
190
|
});
|
|
191
191
|
where.filter(({ connector }) => connector === "OR").forEach(({ field, value }, index) => {
|
|
192
|
-
const path = ["$and", index].concat(
|
|
193
|
-
getFieldPath(metadata2, field, true)
|
|
194
|
-
);
|
|
192
|
+
const path = ["$and", index].concat(getFieldPath(metadata2, field, true));
|
|
195
193
|
return createWhereClause(path, value, "eq", result);
|
|
196
194
|
});
|
|
197
195
|
return result;
|
|
@@ -207,106 +205,118 @@ function createAdapterUtils(orm) {
|
|
|
207
205
|
}
|
|
208
206
|
|
|
209
207
|
// src/adapter.ts
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
const
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
)
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
208
|
+
var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => (0, import_adapters.createAdapter)({
|
|
209
|
+
config: {
|
|
210
|
+
debugLogs,
|
|
211
|
+
supportsJSON,
|
|
212
|
+
adapterId: "mikro-orm-adapter",
|
|
213
|
+
adapterName: "Mikro ORM Adapter"
|
|
214
|
+
},
|
|
215
|
+
adapter({ options }) {
|
|
216
|
+
const {
|
|
217
|
+
getEntityMetadata,
|
|
218
|
+
getFieldPath,
|
|
219
|
+
normalizeInput,
|
|
220
|
+
normalizeOutput,
|
|
221
|
+
normalizeWhereClauses
|
|
222
|
+
} = createAdapterUtils(orm);
|
|
223
|
+
return {
|
|
224
|
+
async create({ model, data, select }) {
|
|
225
|
+
const metadata = getEntityMetadata(model);
|
|
226
|
+
const input = normalizeInput(metadata, data);
|
|
227
|
+
if (options.advanced?.generateId === false && !options.advanced?.database) {
|
|
228
|
+
Reflect.deleteProperty(input, "id");
|
|
229
|
+
}
|
|
230
|
+
const entity = orm.em.create(metadata.class, input);
|
|
231
|
+
await orm.em.persistAndFlush(entity);
|
|
232
|
+
return normalizeOutput(metadata, entity, select);
|
|
233
|
+
},
|
|
234
|
+
async count({ model, where }) {
|
|
235
|
+
const metadata = getEntityMetadata(model);
|
|
236
|
+
return orm.em.count(
|
|
237
|
+
metadata.class,
|
|
238
|
+
normalizeWhereClauses(metadata, where)
|
|
239
|
+
);
|
|
240
|
+
},
|
|
241
|
+
async findOne({ model, where, select }) {
|
|
242
|
+
const metadata = getEntityMetadata(model);
|
|
243
|
+
const entity = await orm.em.findOne(
|
|
244
|
+
metadata.class,
|
|
245
|
+
normalizeWhereClauses(metadata, where)
|
|
246
|
+
);
|
|
247
|
+
if (!entity) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
return normalizeOutput(metadata, entity, select);
|
|
251
|
+
},
|
|
252
|
+
async findMany({ model, where, limit, offset, sortBy }) {
|
|
253
|
+
const metadata = getEntityMetadata(model);
|
|
254
|
+
const options2 = {
|
|
255
|
+
limit,
|
|
256
|
+
offset
|
|
257
|
+
};
|
|
258
|
+
if (sortBy) {
|
|
259
|
+
const path = getFieldPath(metadata, sortBy.field);
|
|
260
|
+
(0, import_dset2.dset)(options2, ["orderBy", ...path], sortBy.direction);
|
|
261
|
+
}
|
|
262
|
+
const rows = await orm.em.find(
|
|
263
|
+
metadata.class,
|
|
264
|
+
normalizeWhereClauses(metadata, where),
|
|
265
|
+
options2
|
|
266
|
+
);
|
|
267
|
+
return rows.map((row) => normalizeOutput(metadata, row));
|
|
268
|
+
},
|
|
269
|
+
async update({ model, where, update }) {
|
|
270
|
+
const metadata = getEntityMetadata(model);
|
|
271
|
+
const entity = await orm.em.findOne(
|
|
272
|
+
metadata.class,
|
|
273
|
+
normalizeWhereClauses(metadata, where)
|
|
274
|
+
);
|
|
275
|
+
if (!entity) {
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
orm.em.assign(entity, normalizeInput(metadata, update));
|
|
279
|
+
await orm.em.flush();
|
|
280
|
+
return normalizeOutput(metadata, entity);
|
|
281
|
+
},
|
|
282
|
+
async updateMany({ model, where, update }) {
|
|
283
|
+
const metadata = getEntityMetadata(model);
|
|
284
|
+
const affected = await orm.em.nativeUpdate(
|
|
285
|
+
metadata.class,
|
|
286
|
+
normalizeWhereClauses(metadata, where),
|
|
287
|
+
normalizeInput(metadata, update)
|
|
288
|
+
);
|
|
289
|
+
orm.em.clear();
|
|
290
|
+
return affected;
|
|
291
|
+
},
|
|
292
|
+
async delete({ model, where }) {
|
|
293
|
+
const metadata = getEntityMetadata(model);
|
|
294
|
+
const entity = await orm.em.findOne(
|
|
295
|
+
metadata.class,
|
|
296
|
+
normalizeWhereClauses(metadata, where),
|
|
297
|
+
{
|
|
298
|
+
fields: ["id"]
|
|
299
|
+
}
|
|
300
|
+
);
|
|
301
|
+
if (entity) {
|
|
302
|
+
await orm.em.removeAndFlush(entity);
|
|
303
|
+
}
|
|
304
|
+
},
|
|
305
|
+
async deleteMany({ model, where }) {
|
|
306
|
+
const metadata = getEntityMetadata(model);
|
|
307
|
+
const [rows, count] = await orm.em.findAndCount(
|
|
308
|
+
metadata.class,
|
|
309
|
+
normalizeWhereClauses(metadata, where),
|
|
310
|
+
{
|
|
311
|
+
fields: ["id"]
|
|
312
|
+
}
|
|
313
|
+
);
|
|
314
|
+
await orm.em.removeAndFlush(rows);
|
|
315
|
+
return count;
|
|
296
316
|
}
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
|
|
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
|
-
}
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
});
|
|
310
320
|
// Annotate the CommonJS export names for ESM import in node:
|
|
311
321
|
0 && (module.exports = {
|
|
312
322
|
mikroOrmAdapter
|
package/lib/adapter.d.cts
CHANGED
|
@@ -1,15 +1,37 @@
|
|
|
1
|
+
import * as better_auth from 'better-auth';
|
|
1
2
|
import { MikroORM } from '@mikro-orm/core';
|
|
2
|
-
import {
|
|
3
|
+
import { AdapterDebugLogs } from 'better-auth/adapters';
|
|
3
4
|
|
|
5
|
+
interface MikroOrmAdapterConfig {
|
|
6
|
+
/**
|
|
7
|
+
* Enable debug logs.
|
|
8
|
+
*
|
|
9
|
+
* @default false
|
|
10
|
+
*/
|
|
11
|
+
debugLogs?: AdapterDebugLogs;
|
|
12
|
+
/**
|
|
13
|
+
* Indicates whether or not JSON is supported by target database.
|
|
14
|
+
*
|
|
15
|
+
* This option is enabled by default, because Mikro ORM supports JSON serialization/deserialization via [JsonType](https://mikro-orm.io/docs/custom-types#jsontype).
|
|
16
|
+
* See documentation for more info: https://mikro-orm.io/docs/json-properties
|
|
17
|
+
*
|
|
18
|
+
* If disabled, Better Auth will handle these transformations for you.
|
|
19
|
+
*
|
|
20
|
+
* @default true
|
|
21
|
+
*/
|
|
22
|
+
supportsJSON?: boolean;
|
|
23
|
+
}
|
|
4
24
|
/**
|
|
5
25
|
* Creates Mikro ORM adapter for Better Auth.
|
|
6
26
|
*
|
|
7
27
|
* Current limitations:
|
|
8
28
|
* * No m:m and 1:m and embedded references support
|
|
9
29
|
* * No complex primary key support
|
|
30
|
+
* * No schema generation
|
|
10
31
|
*
|
|
11
32
|
* @param orm - Instance of Mikro ORM returned from `MikroORM.init` or `MikroORM.initSync` methods
|
|
33
|
+
* @param config - Additional configuration for Mikro ORM adapter
|
|
12
34
|
*/
|
|
13
|
-
declare
|
|
35
|
+
declare const mikroOrmAdapter: (orm: MikroORM, { debugLogs, supportsJSON }?: MikroOrmAdapterConfig) => (options: better_auth.BetterAuthOptions) => better_auth.Adapter;
|
|
14
36
|
|
|
15
|
-
export { mikroOrmAdapter };
|
|
37
|
+
export { type MikroOrmAdapterConfig, mikroOrmAdapter };
|
package/lib/adapter.d.ts
CHANGED
|
@@ -1,15 +1,37 @@
|
|
|
1
|
+
import * as better_auth from 'better-auth';
|
|
1
2
|
import { MikroORM } from '@mikro-orm/core';
|
|
2
|
-
import {
|
|
3
|
+
import { AdapterDebugLogs } from 'better-auth/adapters';
|
|
3
4
|
|
|
5
|
+
interface MikroOrmAdapterConfig {
|
|
6
|
+
/**
|
|
7
|
+
* Enable debug logs.
|
|
8
|
+
*
|
|
9
|
+
* @default false
|
|
10
|
+
*/
|
|
11
|
+
debugLogs?: AdapterDebugLogs;
|
|
12
|
+
/**
|
|
13
|
+
* Indicates whether or not JSON is supported by target database.
|
|
14
|
+
*
|
|
15
|
+
* This option is enabled by default, because Mikro ORM supports JSON serialization/deserialization via [JsonType](https://mikro-orm.io/docs/custom-types#jsontype).
|
|
16
|
+
* See documentation for more info: https://mikro-orm.io/docs/json-properties
|
|
17
|
+
*
|
|
18
|
+
* If disabled, Better Auth will handle these transformations for you.
|
|
19
|
+
*
|
|
20
|
+
* @default true
|
|
21
|
+
*/
|
|
22
|
+
supportsJSON?: boolean;
|
|
23
|
+
}
|
|
4
24
|
/**
|
|
5
25
|
* Creates Mikro ORM adapter for Better Auth.
|
|
6
26
|
*
|
|
7
27
|
* Current limitations:
|
|
8
28
|
* * No m:m and 1:m and embedded references support
|
|
9
29
|
* * No complex primary key support
|
|
30
|
+
* * No schema generation
|
|
10
31
|
*
|
|
11
32
|
* @param orm - Instance of Mikro ORM returned from `MikroORM.init` or `MikroORM.initSync` methods
|
|
33
|
+
* @param config - Additional configuration for Mikro ORM adapter
|
|
12
34
|
*/
|
|
13
|
-
declare
|
|
35
|
+
declare const mikroOrmAdapter: (orm: MikroORM, { debugLogs, supportsJSON }?: MikroOrmAdapterConfig) => (options: better_auth.BetterAuthOptions) => better_auth.Adapter;
|
|
14
36
|
|
|
15
|
-
export { mikroOrmAdapter };
|
|
37
|
+
export { type MikroOrmAdapterConfig, mikroOrmAdapter };
|
package/lib/adapter.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/adapter.ts
|
|
2
|
-
import {
|
|
2
|
+
import { createAdapter } from "better-auth/adapters";
|
|
3
3
|
import { dset as dset2 } from "dset";
|
|
4
4
|
|
|
5
5
|
// src/utils/adapterUtils.ts
|
|
@@ -13,7 +13,11 @@ function createAdapterError(message) {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
// src/utils/adapterUtils.ts
|
|
16
|
-
var ownReferences = [
|
|
16
|
+
var ownReferences = [
|
|
17
|
+
ReferenceKind.SCALAR,
|
|
18
|
+
ReferenceKind.ONE_TO_MANY,
|
|
19
|
+
ReferenceKind.EMBEDDED
|
|
20
|
+
];
|
|
17
21
|
function createAdapterUtils(orm) {
|
|
18
22
|
const naming = orm.config.getNamingStrategy();
|
|
19
23
|
const metadata = orm.getMetadata();
|
|
@@ -52,7 +56,7 @@ function createAdapterUtils(orm) {
|
|
|
52
56
|
return prop.name;
|
|
53
57
|
}
|
|
54
58
|
if (prop.kind === ReferenceKind.MANY_TO_ONE) {
|
|
55
|
-
return naming.joinColumnName(prop.name);
|
|
59
|
+
return naming.columnNameToProperty(naming.joinColumnName(prop.name));
|
|
56
60
|
}
|
|
57
61
|
if (prop.kind === ReferenceKind.MANY_TO_MANY) {
|
|
58
62
|
return prop.name;
|
|
@@ -61,9 +65,7 @@ function createAdapterUtils(orm) {
|
|
|
61
65
|
`Reference kind ${prop.kind} is not supported. Defined in "${entityName}" entity for "${prop.name}" field.`
|
|
62
66
|
);
|
|
63
67
|
}
|
|
64
|
-
const getReferencedPropertyName = (metadata2, prop) =>
|
|
65
|
-
getReferencedColumnName(metadata2.className, prop)
|
|
66
|
-
);
|
|
68
|
+
const getReferencedPropertyName = (metadata2, prop) => getReferencedColumnName(metadata2.className, prop);
|
|
67
69
|
const getFieldPath = (metadata2, fieldName, throwOnShadowProps = false) => {
|
|
68
70
|
const prop = getPropertyMetadata(metadata2, fieldName);
|
|
69
71
|
if (prop.persist === false && throwOnShadowProps) {
|
|
@@ -71,7 +73,7 @@ function createAdapterUtils(orm) {
|
|
|
71
73
|
`Cannot serialize "${fieldName}" into path, because it cannot be persisted in "${metadata2.tableName}" table.`
|
|
72
74
|
);
|
|
73
75
|
}
|
|
74
|
-
if (prop.kind === ReferenceKind.SCALAR) {
|
|
76
|
+
if (prop.kind === ReferenceKind.SCALAR || prop.kind === ReferenceKind.EMBEDDED) {
|
|
75
77
|
return [prop.name];
|
|
76
78
|
}
|
|
77
79
|
if (prop.kind === ReferenceKind.MANY_TO_ONE) {
|
|
@@ -83,14 +85,14 @@ function createAdapterUtils(orm) {
|
|
|
83
85
|
return [prop.name, naming.referenceColumnName()];
|
|
84
86
|
}
|
|
85
87
|
createAdapterError(
|
|
86
|
-
`Cannot normalize "${fieldName}" field name into path for "${metadata2.className} entity
|
|
88
|
+
`Cannot normalize "${fieldName}" field name into path for "${metadata2.className}" entity.`
|
|
87
89
|
);
|
|
88
90
|
};
|
|
89
91
|
const normalizeInput = (metadata2, input) => {
|
|
90
92
|
const fields = {};
|
|
91
93
|
Object.entries(input).forEach(([key, value]) => {
|
|
92
|
-
if (typeof value === "object" && value
|
|
93
|
-
dset(fields, key, value
|
|
94
|
+
if (typeof value === "object" && value.$in) {
|
|
95
|
+
dset(fields, key, value.$in);
|
|
94
96
|
} else {
|
|
95
97
|
const path = getFieldPath(metadata2, key);
|
|
96
98
|
dset(fields, path, value);
|
|
@@ -154,18 +156,14 @@ function createAdapterUtils(orm) {
|
|
|
154
156
|
}
|
|
155
157
|
const result = {};
|
|
156
158
|
where.filter(({ connector }) => !connector || connector === "AND").forEach(({ field, operator, value }, index) => {
|
|
157
|
-
const path = ["$and", index].concat(
|
|
158
|
-
getFieldPath(metadata2, field, true)
|
|
159
|
-
);
|
|
159
|
+
const path = ["$and", index].concat(getFieldPath(metadata2, field, true));
|
|
160
160
|
if (operator === "in") {
|
|
161
161
|
return createWhereInClause(field, path, value, result);
|
|
162
162
|
}
|
|
163
163
|
return createWhereClause(path, value, "eq", result);
|
|
164
164
|
});
|
|
165
165
|
where.filter(({ connector }) => connector === "OR").forEach(({ field, value }, index) => {
|
|
166
|
-
const path = ["$and", index].concat(
|
|
167
|
-
getFieldPath(metadata2, field, true)
|
|
168
|
-
);
|
|
166
|
+
const path = ["$and", index].concat(getFieldPath(metadata2, field, true));
|
|
169
167
|
return createWhereClause(path, value, "eq", result);
|
|
170
168
|
});
|
|
171
169
|
return result;
|
|
@@ -181,106 +179,118 @@ function createAdapterUtils(orm) {
|
|
|
181
179
|
}
|
|
182
180
|
|
|
183
181
|
// src/adapter.ts
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
)
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
182
|
+
var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => createAdapter({
|
|
183
|
+
config: {
|
|
184
|
+
debugLogs,
|
|
185
|
+
supportsJSON,
|
|
186
|
+
adapterId: "mikro-orm-adapter",
|
|
187
|
+
adapterName: "Mikro ORM Adapter"
|
|
188
|
+
},
|
|
189
|
+
adapter({ options }) {
|
|
190
|
+
const {
|
|
191
|
+
getEntityMetadata,
|
|
192
|
+
getFieldPath,
|
|
193
|
+
normalizeInput,
|
|
194
|
+
normalizeOutput,
|
|
195
|
+
normalizeWhereClauses
|
|
196
|
+
} = createAdapterUtils(orm);
|
|
197
|
+
return {
|
|
198
|
+
async create({ model, data, select }) {
|
|
199
|
+
const metadata = getEntityMetadata(model);
|
|
200
|
+
const input = normalizeInput(metadata, data);
|
|
201
|
+
if (options.advanced?.generateId === false && !options.advanced?.database) {
|
|
202
|
+
Reflect.deleteProperty(input, "id");
|
|
203
|
+
}
|
|
204
|
+
const entity = orm.em.create(metadata.class, input);
|
|
205
|
+
await orm.em.persistAndFlush(entity);
|
|
206
|
+
return normalizeOutput(metadata, entity, select);
|
|
207
|
+
},
|
|
208
|
+
async count({ model, where }) {
|
|
209
|
+
const metadata = getEntityMetadata(model);
|
|
210
|
+
return orm.em.count(
|
|
211
|
+
metadata.class,
|
|
212
|
+
normalizeWhereClauses(metadata, where)
|
|
213
|
+
);
|
|
214
|
+
},
|
|
215
|
+
async findOne({ model, where, select }) {
|
|
216
|
+
const metadata = getEntityMetadata(model);
|
|
217
|
+
const entity = await orm.em.findOne(
|
|
218
|
+
metadata.class,
|
|
219
|
+
normalizeWhereClauses(metadata, where)
|
|
220
|
+
);
|
|
221
|
+
if (!entity) {
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
return normalizeOutput(metadata, entity, select);
|
|
225
|
+
},
|
|
226
|
+
async findMany({ model, where, limit, offset, sortBy }) {
|
|
227
|
+
const metadata = getEntityMetadata(model);
|
|
228
|
+
const options2 = {
|
|
229
|
+
limit,
|
|
230
|
+
offset
|
|
231
|
+
};
|
|
232
|
+
if (sortBy) {
|
|
233
|
+
const path = getFieldPath(metadata, sortBy.field);
|
|
234
|
+
dset2(options2, ["orderBy", ...path], sortBy.direction);
|
|
235
|
+
}
|
|
236
|
+
const rows = await orm.em.find(
|
|
237
|
+
metadata.class,
|
|
238
|
+
normalizeWhereClauses(metadata, where),
|
|
239
|
+
options2
|
|
240
|
+
);
|
|
241
|
+
return rows.map((row) => normalizeOutput(metadata, row));
|
|
242
|
+
},
|
|
243
|
+
async update({ model, where, update }) {
|
|
244
|
+
const metadata = getEntityMetadata(model);
|
|
245
|
+
const entity = await orm.em.findOne(
|
|
246
|
+
metadata.class,
|
|
247
|
+
normalizeWhereClauses(metadata, where)
|
|
248
|
+
);
|
|
249
|
+
if (!entity) {
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
orm.em.assign(entity, normalizeInput(metadata, update));
|
|
253
|
+
await orm.em.flush();
|
|
254
|
+
return normalizeOutput(metadata, entity);
|
|
255
|
+
},
|
|
256
|
+
async updateMany({ model, where, update }) {
|
|
257
|
+
const metadata = getEntityMetadata(model);
|
|
258
|
+
const affected = await orm.em.nativeUpdate(
|
|
259
|
+
metadata.class,
|
|
260
|
+
normalizeWhereClauses(metadata, where),
|
|
261
|
+
normalizeInput(metadata, update)
|
|
262
|
+
);
|
|
263
|
+
orm.em.clear();
|
|
264
|
+
return affected;
|
|
265
|
+
},
|
|
266
|
+
async delete({ model, where }) {
|
|
267
|
+
const metadata = getEntityMetadata(model);
|
|
268
|
+
const entity = await orm.em.findOne(
|
|
269
|
+
metadata.class,
|
|
270
|
+
normalizeWhereClauses(metadata, where),
|
|
271
|
+
{
|
|
272
|
+
fields: ["id"]
|
|
273
|
+
}
|
|
274
|
+
);
|
|
275
|
+
if (entity) {
|
|
276
|
+
await orm.em.removeAndFlush(entity);
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
async deleteMany({ model, where }) {
|
|
280
|
+
const metadata = getEntityMetadata(model);
|
|
281
|
+
const [rows, count] = await orm.em.findAndCount(
|
|
282
|
+
metadata.class,
|
|
283
|
+
normalizeWhereClauses(metadata, where),
|
|
284
|
+
{
|
|
285
|
+
fields: ["id"]
|
|
286
|
+
}
|
|
287
|
+
);
|
|
288
|
+
await orm.em.removeAndFlush(rows);
|
|
289
|
+
return count;
|
|
270
290
|
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
|
|
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
|
-
}
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
});
|
|
284
294
|
export {
|
|
285
295
|
mikroOrmAdapter
|
|
286
296
|
};
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"private": false,
|
|
3
3
|
"type": "module",
|
|
4
4
|
"name": "@forklaunch/better-auth-mikro-orm-fork",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.4.1",
|
|
6
6
|
"description": "Mikro ORM Adapter for Better Auth",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"auth",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"better-auth-adapter",
|
|
14
14
|
"mikro-orm"
|
|
15
15
|
],
|
|
16
|
-
"author": "
|
|
16
|
+
"author": "Forklift Technologies, Inc.",
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"repository": {
|
|
19
19
|
"type": "git",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"@types/uuid": "10.0.0",
|
|
60
60
|
"@vitest/coverage-v8": "3.1.1",
|
|
61
61
|
"@vitest/ui": "3.1.1",
|
|
62
|
-
"better-auth": "1.2.
|
|
62
|
+
"better-auth": "1.2.9",
|
|
63
63
|
"del-cli": "6.0.0",
|
|
64
64
|
"husky": "9.1.7",
|
|
65
65
|
"is-in-ci": "1.0.0",
|
|
@@ -81,6 +81,7 @@
|
|
|
81
81
|
"test.watch": "vitest",
|
|
82
82
|
"test.ui": "vitest --ui",
|
|
83
83
|
"coverage": "vitest run --coverage",
|
|
84
|
+
"coverage.ui": "vitest --coverage --ui",
|
|
84
85
|
"release": "pnpm build && changeset publish"
|
|
85
86
|
}
|
|
86
87
|
}
|