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

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
@@ -222,28 +222,30 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => (0, impo
222
222
  } = createAdapterUtils(orm);
223
223
  return {
224
224
  async create({ model, data, select }) {
225
- const em = orm.em.fork();
226
225
  const metadata = getEntityMetadata(model);
227
226
  const input = normalizeInput(metadata, data);
228
- if (options.advanced?.generateId === false && !options.advanced?.database) {
227
+ if (options.advanced?.generateId === false && !options.advanced?.database || options.advanced?.database?.generateId === false) {
229
228
  Reflect.deleteProperty(input, "id");
230
229
  }
231
- const entity = em.create(metadata.class, input);
232
- await em.persistAndFlush(entity);
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
+ }
233
237
  return normalizeOutput(metadata, entity, select);
234
238
  },
235
239
  async count({ model, where }) {
236
- const em = orm.em.fork();
237
240
  const metadata = getEntityMetadata(model);
238
- return em.count(
241
+ return orm.em.count(
239
242
  metadata.class,
240
243
  normalizeWhereClauses(metadata, where)
241
244
  );
242
245
  },
243
246
  async findOne({ model, where, select }) {
244
- const em = orm.em.fork();
245
247
  const metadata = getEntityMetadata(model);
246
- const entity = await em.findOne(
248
+ const entity = await orm.em.findOne(
247
249
  metadata.class,
248
250
  normalizeWhereClauses(metadata, where)
249
251
  );
@@ -253,7 +255,6 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => (0, impo
253
255
  return normalizeOutput(metadata, entity, select);
254
256
  },
255
257
  async findMany({ model, where, limit, offset, sortBy }) {
256
- const em = orm.em.fork();
257
258
  const metadata = getEntityMetadata(model);
258
259
  const options2 = {
259
260
  limit,
@@ -263,7 +264,7 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => (0, impo
263
264
  const path = getFieldPath(metadata, sortBy.field);
264
265
  (0, import_dset2.dset)(options2, ["orderBy", ...path], sortBy.direction);
265
266
  }
266
- const rows = await em.find(
267
+ const rows = await orm.em.find(
267
268
  metadata.class,
268
269
  normalizeWhereClauses(metadata, where),
269
270
  options2
@@ -271,33 +272,36 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => (0, impo
271
272
  return rows.map((row) => normalizeOutput(metadata, row));
272
273
  },
273
274
  async update({ model, where, update }) {
274
- const em = orm.em.fork();
275
275
  const metadata = getEntityMetadata(model);
276
- const entity = await em.findOne(
276
+ const entity = await orm.em.findOne(
277
277
  metadata.class,
278
278
  normalizeWhereClauses(metadata, where)
279
279
  );
280
280
  if (!entity) {
281
281
  return null;
282
282
  }
283
- em.assign(entity, normalizeInput(metadata, update));
284
- await em.flush();
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
+ }
285
290
  return normalizeOutput(metadata, entity);
286
291
  },
287
292
  async updateMany({ model, where, update }) {
288
- const em = orm.em.fork();
289
293
  const metadata = getEntityMetadata(model);
290
- const affected = await em.nativeUpdate(
294
+ const affected = await orm.em.nativeUpdate(
291
295
  metadata.class,
292
296
  normalizeWhereClauses(metadata, where),
293
297
  normalizeInput(metadata, update)
294
298
  );
299
+ orm.em.clear();
295
300
  return affected;
296
301
  },
297
302
  async delete({ model, where }) {
298
- const em = orm.em.fork();
299
303
  const metadata = getEntityMetadata(model);
300
- const entity = await em.findOne(
304
+ const entity = await orm.em.findOne(
301
305
  metadata.class,
302
306
  normalizeWhereClauses(metadata, where),
303
307
  {
@@ -305,20 +309,19 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => (0, impo
305
309
  }
306
310
  );
307
311
  if (entity) {
308
- await em.removeAndFlush(entity);
312
+ await orm.em.removeAndFlush(entity);
309
313
  }
310
314
  },
311
315
  async deleteMany({ model, where }) {
312
- const em = orm.em.fork();
313
316
  const metadata = getEntityMetadata(model);
314
- const [rows, count] = await em.findAndCount(
317
+ const [rows, count] = await orm.em.findAndCount(
315
318
  metadata.class,
316
319
  normalizeWhereClauses(metadata, where),
317
320
  {
318
321
  fields: ["id"]
319
322
  }
320
323
  );
321
- await em.removeAndFlush(rows);
324
+ await orm.em.removeAndFlush(rows);
322
325
  return count;
323
326
  }
324
327
  };
package/lib/adapter.js CHANGED
@@ -196,28 +196,30 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => createAd
196
196
  } = createAdapterUtils(orm);
197
197
  return {
198
198
  async create({ model, data, select }) {
199
- const em = orm.em.fork();
200
199
  const metadata = getEntityMetadata(model);
201
200
  const input = normalizeInput(metadata, data);
202
- if (options.advanced?.generateId === false && !options.advanced?.database) {
201
+ if (options.advanced?.generateId === false && !options.advanced?.database || options.advanced?.database?.generateId === false) {
203
202
  Reflect.deleteProperty(input, "id");
204
203
  }
205
- const entity = em.create(metadata.class, input);
206
- await em.persistAndFlush(entity);
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
+ }
207
211
  return normalizeOutput(metadata, entity, select);
208
212
  },
209
213
  async count({ model, where }) {
210
- const em = orm.em.fork();
211
214
  const metadata = getEntityMetadata(model);
212
- return em.count(
215
+ return orm.em.count(
213
216
  metadata.class,
214
217
  normalizeWhereClauses(metadata, where)
215
218
  );
216
219
  },
217
220
  async findOne({ model, where, select }) {
218
- const em = orm.em.fork();
219
221
  const metadata = getEntityMetadata(model);
220
- const entity = await em.findOne(
222
+ const entity = await orm.em.findOne(
221
223
  metadata.class,
222
224
  normalizeWhereClauses(metadata, where)
223
225
  );
@@ -227,7 +229,6 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => createAd
227
229
  return normalizeOutput(metadata, entity, select);
228
230
  },
229
231
  async findMany({ model, where, limit, offset, sortBy }) {
230
- const em = orm.em.fork();
231
232
  const metadata = getEntityMetadata(model);
232
233
  const options2 = {
233
234
  limit,
@@ -237,7 +238,7 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => createAd
237
238
  const path = getFieldPath(metadata, sortBy.field);
238
239
  dset2(options2, ["orderBy", ...path], sortBy.direction);
239
240
  }
240
- const rows = await em.find(
241
+ const rows = await orm.em.find(
241
242
  metadata.class,
242
243
  normalizeWhereClauses(metadata, where),
243
244
  options2
@@ -245,33 +246,36 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => createAd
245
246
  return rows.map((row) => normalizeOutput(metadata, row));
246
247
  },
247
248
  async update({ model, where, update }) {
248
- const em = orm.em.fork();
249
249
  const metadata = getEntityMetadata(model);
250
- const entity = await em.findOne(
250
+ const entity = await orm.em.findOne(
251
251
  metadata.class,
252
252
  normalizeWhereClauses(metadata, where)
253
253
  );
254
254
  if (!entity) {
255
255
  return null;
256
256
  }
257
- em.assign(entity, normalizeInput(metadata, update));
258
- await em.flush();
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
+ }
259
264
  return normalizeOutput(metadata, entity);
260
265
  },
261
266
  async updateMany({ model, where, update }) {
262
- const em = orm.em.fork();
263
267
  const metadata = getEntityMetadata(model);
264
- const affected = await em.nativeUpdate(
268
+ const affected = await orm.em.nativeUpdate(
265
269
  metadata.class,
266
270
  normalizeWhereClauses(metadata, where),
267
271
  normalizeInput(metadata, update)
268
272
  );
273
+ orm.em.clear();
269
274
  return affected;
270
275
  },
271
276
  async delete({ model, where }) {
272
- const em = orm.em.fork();
273
277
  const metadata = getEntityMetadata(model);
274
- const entity = await em.findOne(
278
+ const entity = await orm.em.findOne(
275
279
  metadata.class,
276
280
  normalizeWhereClauses(metadata, where),
277
281
  {
@@ -279,20 +283,19 @@ var mikroOrmAdapter = (orm, { debugLogs, supportsJSON = true } = {}) => createAd
279
283
  }
280
284
  );
281
285
  if (entity) {
282
- await em.removeAndFlush(entity);
286
+ await orm.em.removeAndFlush(entity);
283
287
  }
284
288
  },
285
289
  async deleteMany({ model, where }) {
286
- const em = orm.em.fork();
287
290
  const metadata = getEntityMetadata(model);
288
- const [rows, count] = await em.findAndCount(
291
+ const [rows, count] = await orm.em.findAndCount(
289
292
  metadata.class,
290
293
  normalizeWhereClauses(metadata, where),
291
294
  {
292
295
  fields: ["id"]
293
296
  }
294
297
  );
295
- await em.removeAndFlush(rows);
298
+ await orm.em.removeAndFlush(rows);
296
299
  return count;
297
300
  }
298
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.102",
6
6
  "description": "Mikro ORM Adapter for Better Auth",
7
7
  "keywords": [
8
8
  "auth",