@forklaunch/better-auth-mikro-orm-fork 0.3.0 → 0.4.21

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 CHANGED
@@ -25,7 +25,7 @@ __export(src_exports, {
25
25
  module.exports = __toCommonJS(src_exports);
26
26
 
27
27
  // src/adapter.ts
28
- var import_better_auth2 = require("better-auth");
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 = [import_core.ReferenceKind.SCALAR, import_core.ReferenceKind.ONE_TO_MANY];
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) => naming.columnNameToProperty(
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["$in"]) {
119
- (0, import_dset.dset)(fields, key, value["$in"]);
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,128 @@ function createAdapterUtils(orm) {
207
205
  }
208
206
 
209
207
  // 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);
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?.generateId === false) && !options.advanced?.database) {
228
+ Reflect.deleteProperty(input, "id");
229
+ }
230
+ const entity = orm.em.create(metadata.class, input);
231
+ try {
232
+ await orm.em.persistAndFlush(entity);
233
+ } catch (error) {
234
+ await orm.em.removeAndFlush(entity);
235
+ throw error;
236
+ }
237
+ return normalizeOutput(metadata, entity, select);
238
+ },
239
+ async count({ model, where }) {
240
+ const metadata = getEntityMetadata(model);
241
+ return orm.em.count(
242
+ metadata.class,
243
+ normalizeWhereClauses(metadata, where)
244
+ );
245
+ },
246
+ async findOne({ model, where, select }) {
247
+ const metadata = getEntityMetadata(model);
248
+ const entity = await orm.em.findOne(
249
+ metadata.class,
250
+ normalizeWhereClauses(metadata, where)
251
+ );
252
+ if (!entity) {
253
+ return null;
254
+ }
255
+ return normalizeOutput(metadata, entity, select);
256
+ },
257
+ async findMany({ model, where, limit, offset, sortBy }) {
258
+ const metadata = getEntityMetadata(model);
259
+ const options2 = {
260
+ limit,
261
+ offset
262
+ };
263
+ if (sortBy) {
264
+ const path = getFieldPath(metadata, sortBy.field);
265
+ (0, import_dset2.dset)(options2, ["orderBy", ...path], sortBy.direction);
266
+ }
267
+ const rows = await orm.em.find(
268
+ metadata.class,
269
+ normalizeWhereClauses(metadata, where),
270
+ options2
271
+ );
272
+ return rows.map((row) => normalizeOutput(metadata, row));
273
+ },
274
+ async update({ model, where, update }) {
275
+ const metadata = getEntityMetadata(model);
276
+ const entity = await orm.em.findOne(
277
+ metadata.class,
278
+ normalizeWhereClauses(metadata, where)
279
+ );
280
+ if (!entity) {
281
+ return null;
282
+ }
283
+ orm.em.assign(entity, normalizeInput(metadata, update));
284
+ try {
285
+ await orm.em.flush();
286
+ } catch (error) {
287
+ await orm.em.removeAndFlush(entity);
288
+ throw error;
289
+ }
290
+ return normalizeOutput(metadata, entity);
291
+ },
292
+ async updateMany({ model, where, update }) {
293
+ const metadata = getEntityMetadata(model);
294
+ const affected = await orm.em.nativeUpdate(
295
+ metadata.class,
296
+ normalizeWhereClauses(metadata, where),
297
+ normalizeInput(metadata, update)
298
+ );
299
+ orm.em.clear();
300
+ return affected;
301
+ },
302
+ async delete({ model, where }) {
303
+ const metadata = getEntityMetadata(model);
304
+ const entity = await orm.em.findOne(
305
+ metadata.class,
306
+ normalizeWhereClauses(metadata, where),
307
+ {
308
+ fields: ["id"]
309
+ }
310
+ );
311
+ if (entity) {
312
+ await orm.em.removeAndFlush(entity);
313
+ }
314
+ },
315
+ async deleteMany({ model, where }) {
316
+ const metadata = getEntityMetadata(model);
317
+ const [rows, count] = await orm.em.findAndCount(
318
+ metadata.class,
319
+ normalizeWhereClauses(metadata, where),
320
+ {
321
+ fields: ["id"]
322
+ }
323
+ );
324
+ await orm.em.removeAndFlush(rows);
325
+ return count;
296
326
  }
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
- }
327
+ };
328
+ }
329
+ });
310
330
  // Annotate the CommonJS export names for ESM import in node:
311
331
  0 && (module.exports = {
312
332
  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 { BetterAuthOptions, Adapter } from 'better-auth';
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 function mikroOrmAdapter(orm: MikroORM): (options?: BetterAuthOptions) => Adapter;
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 { BetterAuthOptions, Adapter } from 'better-auth';
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 function mikroOrmAdapter(orm: MikroORM): (options?: BetterAuthOptions) => Adapter;
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 { generateId } from "better-auth";
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 = [ReferenceKind.SCALAR, ReferenceKind.ONE_TO_MANY];
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) => naming.columnNameToProperty(
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["$in"]) {
93
- dset(fields, key, value["$in"]);
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,128 @@ function createAdapterUtils(orm) {
181
179
  }
182
180
 
183
181
  // 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);
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?.generateId === false) && !options.advanced?.database) {
202
+ Reflect.deleteProperty(input, "id");
203
+ }
204
+ const entity = orm.em.create(metadata.class, input);
205
+ try {
206
+ await orm.em.persistAndFlush(entity);
207
+ } catch (error) {
208
+ await orm.em.removeAndFlush(entity);
209
+ throw error;
210
+ }
211
+ return normalizeOutput(metadata, entity, select);
212
+ },
213
+ async count({ model, where }) {
214
+ const metadata = getEntityMetadata(model);
215
+ return orm.em.count(
216
+ metadata.class,
217
+ normalizeWhereClauses(metadata, where)
218
+ );
219
+ },
220
+ async findOne({ model, where, select }) {
221
+ const metadata = getEntityMetadata(model);
222
+ const entity = await orm.em.findOne(
223
+ metadata.class,
224
+ normalizeWhereClauses(metadata, where)
225
+ );
226
+ if (!entity) {
227
+ return null;
228
+ }
229
+ return normalizeOutput(metadata, entity, select);
230
+ },
231
+ async findMany({ model, where, limit, offset, sortBy }) {
232
+ const metadata = getEntityMetadata(model);
233
+ const options2 = {
234
+ limit,
235
+ offset
236
+ };
237
+ if (sortBy) {
238
+ const path = getFieldPath(metadata, sortBy.field);
239
+ dset2(options2, ["orderBy", ...path], sortBy.direction);
240
+ }
241
+ const rows = await orm.em.find(
242
+ metadata.class,
243
+ normalizeWhereClauses(metadata, where),
244
+ options2
245
+ );
246
+ return rows.map((row) => normalizeOutput(metadata, row));
247
+ },
248
+ async update({ model, where, update }) {
249
+ const metadata = getEntityMetadata(model);
250
+ const entity = await orm.em.findOne(
251
+ metadata.class,
252
+ normalizeWhereClauses(metadata, where)
253
+ );
254
+ if (!entity) {
255
+ return null;
256
+ }
257
+ orm.em.assign(entity, normalizeInput(metadata, update));
258
+ try {
259
+ await orm.em.flush();
260
+ } catch (error) {
261
+ await orm.em.removeAndFlush(entity);
262
+ throw error;
263
+ }
264
+ return normalizeOutput(metadata, entity);
265
+ },
266
+ async updateMany({ model, where, update }) {
267
+ const metadata = getEntityMetadata(model);
268
+ const affected = await orm.em.nativeUpdate(
269
+ metadata.class,
270
+ normalizeWhereClauses(metadata, where),
271
+ normalizeInput(metadata, update)
272
+ );
273
+ orm.em.clear();
274
+ return affected;
275
+ },
276
+ async delete({ model, where }) {
277
+ const metadata = getEntityMetadata(model);
278
+ const entity = await orm.em.findOne(
279
+ metadata.class,
280
+ normalizeWhereClauses(metadata, where),
281
+ {
282
+ fields: ["id"]
283
+ }
284
+ );
285
+ if (entity) {
286
+ await orm.em.removeAndFlush(entity);
287
+ }
288
+ },
289
+ async deleteMany({ model, where }) {
290
+ const metadata = getEntityMetadata(model);
291
+ const [rows, count] = await orm.em.findAndCount(
292
+ metadata.class,
293
+ normalizeWhereClauses(metadata, where),
294
+ {
295
+ fields: ["id"]
296
+ }
297
+ );
298
+ await orm.em.removeAndFlush(rows);
299
+ return count;
270
300
  }
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
- }
301
+ };
302
+ }
303
+ });
284
304
  export {
285
305
  mikroOrmAdapter
286
306
  };
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.3.0",
5
+ "version": "0.4.21",
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": "Nick K.",
16
+ "author": "Nick K., 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.7",
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
  }