@forklaunch/better-auth-mikro-orm-fork 0.4.101 → 0.4.103

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
@@ -114,6 +114,12 @@ function createAdapterUtils(orm) {
114
114
  `Cannot normalize "${fieldName}" field name into path for "${metadata2.className}" entity.`
115
115
  );
116
116
  };
117
+ const normalizePropertyValue = (property, value) => {
118
+ if (!property.targetMeta || property.kind === import_core.ReferenceKind.SCALAR || property.kind === import_core.ReferenceKind.EMBEDDED) {
119
+ return value;
120
+ }
121
+ return orm.em.getReference(property.targetMeta.class, value);
122
+ };
117
123
  const normalizeInput = (metadata2, input) => {
118
124
  const fields = {};
119
125
  Object.entries(input).forEach(([key, value]) => {
@@ -126,7 +132,7 @@ function createAdapterUtils(orm) {
126
132
  });
127
133
  return fields;
128
134
  };
129
- const normalizeOutput = (metadata2, output, select) => {
135
+ const normalizeOutput = (metadata2, output) => {
130
136
  output = (0, import_core.serialize)(output);
131
137
  const result = {};
132
138
  Object.entries(output).map(([key, value]) => ({
@@ -135,7 +141,7 @@ function createAdapterUtils(orm) {
135
141
  getPropertyMetadata(metadata2, key)
136
142
  ),
137
143
  value
138
- })).filter(({ path }) => select ? select.includes(path) : true).forEach(({ path, value }) => (0, import_dset.dset)(result, path, value));
144
+ })).forEach(({ path, value }) => (0, import_dset.dset)(result, path, value));
139
145
  return result;
140
146
  };
141
147
  function createWhereClause(path, value, op, target = {}) {
@@ -222,28 +228,30 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => (0, impo
222
228
  } = createAdapterUtils(orm);
223
229
  return {
224
230
  async create({ model, data, select }) {
225
- const em = orm.em.fork();
226
231
  const metadata = getEntityMetadata(model);
227
232
  const input = normalizeInput(metadata, data);
228
- if (options.advanced?.generateId === false && !options.advanced?.database) {
233
+ if (options.advanced?.generateId === false && !options.advanced?.database || options.advanced?.database?.generateId === false) {
229
234
  Reflect.deleteProperty(input, "id");
230
235
  }
231
- const entity = em.create(metadata.class, input);
232
- await em.persistAndFlush(entity);
236
+ const entity = orm.em.create(metadata.class, input);
237
+ try {
238
+ await orm.em.persistAndFlush(entity);
239
+ } catch (error) {
240
+ await orm.em.removeAndFlush(entity);
241
+ throw error;
242
+ }
233
243
  return normalizeOutput(metadata, entity, select);
234
244
  },
235
245
  async count({ model, where }) {
236
- const em = orm.em.fork();
237
246
  const metadata = getEntityMetadata(model);
238
- return em.count(
247
+ return orm.em.count(
239
248
  metadata.class,
240
249
  normalizeWhereClauses(metadata, where)
241
250
  );
242
251
  },
243
252
  async findOne({ model, where, select }) {
244
- const em = orm.em.fork();
245
253
  const metadata = getEntityMetadata(model);
246
- const entity = await em.findOne(
254
+ const entity = await orm.em.findOne(
247
255
  metadata.class,
248
256
  normalizeWhereClauses(metadata, where)
249
257
  );
@@ -253,7 +261,6 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => (0, impo
253
261
  return normalizeOutput(metadata, entity, select);
254
262
  },
255
263
  async findMany({ model, where, limit, offset, sortBy }) {
256
- const em = orm.em.fork();
257
264
  const metadata = getEntityMetadata(model);
258
265
  const options2 = {
259
266
  limit,
@@ -263,7 +270,7 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => (0, impo
263
270
  const path = getFieldPath(metadata, sortBy.field);
264
271
  (0, import_dset2.dset)(options2, ["orderBy", ...path], sortBy.direction);
265
272
  }
266
- const rows = await em.find(
273
+ const rows = await orm.em.find(
267
274
  metadata.class,
268
275
  normalizeWhereClauses(metadata, where),
269
276
  options2
@@ -271,33 +278,34 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => (0, impo
271
278
  return rows.map((row) => normalizeOutput(metadata, row));
272
279
  },
273
280
  async update({ model, where, update }) {
274
- const em = orm.em.fork();
275
281
  const metadata = getEntityMetadata(model);
276
- const entity = await em.findOne(
282
+ const entity = await orm.em.findOne(
277
283
  metadata.class,
278
284
  normalizeWhereClauses(metadata, where)
279
285
  );
280
286
  if (!entity) {
281
287
  return null;
282
288
  }
283
- em.assign(entity, normalizeInput(metadata, update));
284
- await em.flush();
289
+ orm.em.assign(entity, normalizeInput(metadata, update));
290
+ try {
291
+ await orm.em.flush();
292
+ } catch (error) {
293
+ await orm.em.removeAndFlush(entity);
294
+ throw error;
295
+ }
285
296
  return normalizeOutput(metadata, entity);
286
297
  },
287
298
  async updateMany({ model, where, update }) {
288
- const em = orm.em.fork();
289
299
  const metadata = getEntityMetadata(model);
290
- const affected = await em.nativeUpdate(
300
+ return orm.em.nativeUpdate(
291
301
  metadata.class,
292
302
  normalizeWhereClauses(metadata, where),
293
303
  normalizeInput(metadata, update)
294
304
  );
295
- return affected;
296
305
  },
297
306
  async delete({ model, where }) {
298
- const em = orm.em.fork();
299
307
  const metadata = getEntityMetadata(model);
300
- const entity = await em.findOne(
308
+ const entity = await orm.em.findOne(
301
309
  metadata.class,
302
310
  normalizeWhereClauses(metadata, where),
303
311
  {
@@ -305,21 +313,15 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => (0, impo
305
313
  }
306
314
  );
307
315
  if (entity) {
308
- await em.removeAndFlush(entity);
316
+ await orm.em.removeAndFlush(entity);
309
317
  }
310
318
  },
311
319
  async deleteMany({ model, where }) {
312
- const em = orm.em.fork();
313
320
  const metadata = getEntityMetadata(model);
314
- const [rows, count] = await em.findAndCount(
321
+ return orm.em.nativeDelete(
315
322
  metadata.class,
316
- normalizeWhereClauses(metadata, where),
317
- {
318
- fields: ["id"]
319
- }
323
+ normalizeWhereClauses(metadata, where)
320
324
  );
321
- await em.removeAndFlush(rows);
322
- return count;
323
325
  }
324
326
  };
325
327
  }
package/lib/adapter.js CHANGED
@@ -88,6 +88,12 @@ function createAdapterUtils(orm) {
88
88
  `Cannot normalize "${fieldName}" field name into path for "${metadata2.className}" entity.`
89
89
  );
90
90
  };
91
+ const normalizePropertyValue = (property, value) => {
92
+ if (!property.targetMeta || property.kind === ReferenceKind.SCALAR || property.kind === ReferenceKind.EMBEDDED) {
93
+ return value;
94
+ }
95
+ return orm.em.getReference(property.targetMeta.class, value);
96
+ };
91
97
  const normalizeInput = (metadata2, input) => {
92
98
  const fields = {};
93
99
  Object.entries(input).forEach(([key, value]) => {
@@ -100,7 +106,7 @@ function createAdapterUtils(orm) {
100
106
  });
101
107
  return fields;
102
108
  };
103
- const normalizeOutput = (metadata2, output, select) => {
109
+ const normalizeOutput = (metadata2, output) => {
104
110
  output = serialize(output);
105
111
  const result = {};
106
112
  Object.entries(output).map(([key, value]) => ({
@@ -109,7 +115,7 @@ function createAdapterUtils(orm) {
109
115
  getPropertyMetadata(metadata2, key)
110
116
  ),
111
117
  value
112
- })).filter(({ path }) => select ? select.includes(path) : true).forEach(({ path, value }) => dset(result, path, value));
118
+ })).forEach(({ path, value }) => dset(result, path, value));
113
119
  return result;
114
120
  };
115
121
  function createWhereClause(path, value, op, target = {}) {
@@ -196,28 +202,30 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => createAd
196
202
  } = createAdapterUtils(orm);
197
203
  return {
198
204
  async create({ model, data, select }) {
199
- const em = orm.em.fork();
200
205
  const metadata = getEntityMetadata(model);
201
206
  const input = normalizeInput(metadata, data);
202
- if (options.advanced?.generateId === false && !options.advanced?.database) {
207
+ if (options.advanced?.generateId === false && !options.advanced?.database || options.advanced?.database?.generateId === false) {
203
208
  Reflect.deleteProperty(input, "id");
204
209
  }
205
- const entity = em.create(metadata.class, input);
206
- await em.persistAndFlush(entity);
210
+ const entity = orm.em.create(metadata.class, input);
211
+ try {
212
+ await orm.em.persistAndFlush(entity);
213
+ } catch (error) {
214
+ await orm.em.removeAndFlush(entity);
215
+ throw error;
216
+ }
207
217
  return normalizeOutput(metadata, entity, select);
208
218
  },
209
219
  async count({ model, where }) {
210
- const em = orm.em.fork();
211
220
  const metadata = getEntityMetadata(model);
212
- return em.count(
221
+ return orm.em.count(
213
222
  metadata.class,
214
223
  normalizeWhereClauses(metadata, where)
215
224
  );
216
225
  },
217
226
  async findOne({ model, where, select }) {
218
- const em = orm.em.fork();
219
227
  const metadata = getEntityMetadata(model);
220
- const entity = await em.findOne(
228
+ const entity = await orm.em.findOne(
221
229
  metadata.class,
222
230
  normalizeWhereClauses(metadata, where)
223
231
  );
@@ -227,7 +235,6 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => createAd
227
235
  return normalizeOutput(metadata, entity, select);
228
236
  },
229
237
  async findMany({ model, where, limit, offset, sortBy }) {
230
- const em = orm.em.fork();
231
238
  const metadata = getEntityMetadata(model);
232
239
  const options2 = {
233
240
  limit,
@@ -237,7 +244,7 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => createAd
237
244
  const path = getFieldPath(metadata, sortBy.field);
238
245
  dset2(options2, ["orderBy", ...path], sortBy.direction);
239
246
  }
240
- const rows = await em.find(
247
+ const rows = await orm.em.find(
241
248
  metadata.class,
242
249
  normalizeWhereClauses(metadata, where),
243
250
  options2
@@ -245,33 +252,34 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => createAd
245
252
  return rows.map((row) => normalizeOutput(metadata, row));
246
253
  },
247
254
  async update({ model, where, update }) {
248
- const em = orm.em.fork();
249
255
  const metadata = getEntityMetadata(model);
250
- const entity = await em.findOne(
256
+ const entity = await orm.em.findOne(
251
257
  metadata.class,
252
258
  normalizeWhereClauses(metadata, where)
253
259
  );
254
260
  if (!entity) {
255
261
  return null;
256
262
  }
257
- em.assign(entity, normalizeInput(metadata, update));
258
- await em.flush();
263
+ orm.em.assign(entity, normalizeInput(metadata, update));
264
+ try {
265
+ await orm.em.flush();
266
+ } catch (error) {
267
+ await orm.em.removeAndFlush(entity);
268
+ throw error;
269
+ }
259
270
  return normalizeOutput(metadata, entity);
260
271
  },
261
272
  async updateMany({ model, where, update }) {
262
- const em = orm.em.fork();
263
273
  const metadata = getEntityMetadata(model);
264
- const affected = await em.nativeUpdate(
274
+ return orm.em.nativeUpdate(
265
275
  metadata.class,
266
276
  normalizeWhereClauses(metadata, where),
267
277
  normalizeInput(metadata, update)
268
278
  );
269
- return affected;
270
279
  },
271
280
  async delete({ model, where }) {
272
- const em = orm.em.fork();
273
281
  const metadata = getEntityMetadata(model);
274
- const entity = await em.findOne(
282
+ const entity = await orm.em.findOne(
275
283
  metadata.class,
276
284
  normalizeWhereClauses(metadata, where),
277
285
  {
@@ -279,21 +287,15 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => createAd
279
287
  }
280
288
  );
281
289
  if (entity) {
282
- await em.removeAndFlush(entity);
290
+ await orm.em.removeAndFlush(entity);
283
291
  }
284
292
  },
285
293
  async deleteMany({ model, where }) {
286
- const em = orm.em.fork();
287
294
  const metadata = getEntityMetadata(model);
288
- const [rows, count] = await em.findAndCount(
295
+ return orm.em.nativeDelete(
289
296
  metadata.class,
290
- normalizeWhereClauses(metadata, where),
291
- {
292
- fields: ["id"]
293
- }
297
+ normalizeWhereClauses(metadata, where)
294
298
  );
295
- await em.removeAndFlush(rows);
296
- return count;
297
299
  }
298
300
  };
299
301
  }
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.4.101",
5
+ "version": "0.4.103",
6
6
  "description": "Mikro ORM Adapter for Better Auth",
7
7
  "keywords": [
8
8
  "auth",
@@ -61,6 +61,7 @@
61
61
  "@vitest/ui": "3.1.1",
62
62
  "better-auth": "1.2.9",
63
63
  "del-cli": "6.0.0",
64
+ "es-toolkit": "1.39.7",
64
65
  "husky": "9.1.7",
65
66
  "is-in-ci": "1.0.0",
66
67
  "tsup": "8.4.0",
package/readme.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  [Mikro ORM](https://mikro-orm.io/) adapter for [Better Auth](https://www.better-auth.com/)
4
4
 
5
+
6
+ [![CI](https://github.com/octet-stream/better-auth-mikro-orm/actions/workflows/ci.yaml/badge.svg)](https://github.com/octet-stream/better-auth-mikro-orm/actions/workflows/ci.yaml)
7
+ [![codecov](https://codecov.io/gh/octet-stream/better-auth-mikro-orm/graph/badge.svg?token=xcVndkC8mL)](https://codecov.io/gh/octet-stream/better-auth-mikro-orm)
8
+
5
9
  ## Installation
6
10
 
7
11
  pnpm: