Skip to content

Commit

Permalink
close transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerpadilla committed Apr 18, 2023
1 parent 4727036 commit 62f360f
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/nukak-express/src/querierMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Router as expressRouter, type Request } from 'express';
import { getQuerier } from 'nukak';
import { getEntities, getMeta } from 'nukak/entity';
import type { EntityMeta, Query, Type } from 'nukak/type';
import type { EntityMeta, FieldValue, Query, Type } from 'nukak/type';
import { kebabCase } from 'nukak/util';
import { parseQuery } from './query.util.js';

Expand Down Expand Up @@ -144,14 +144,15 @@ export function buildQuerierRouter<E>(entity: Type<E>, opts: ExtraOptions) {

router.delete('/', async (req, res, next) => {
const querier = await getQuerier();
let ids: FieldValue<E>[] = [];
let count = 0;
try {
await querier.beginTransaction();
const founds = await querier.findMany(entity, req.query, [meta.id]);
if (!founds.length) {
return res.json({ data: [], count: 0 });
if (founds.length) {
ids = founds.map((found) => found[meta.id]);
count = await querier.deleteMany(entity, { $filter: { [meta.id]: ids } }, { softDelete: !!req.query.softDelete });
}
const ids = founds.map((found) => found[meta.id]);
const count = await querier.deleteMany(entity, { $filter: { [meta.id]: ids } }, { softDelete: !!req.query.softDelete });
await querier.commitTransaction();
res.json({ data: ids, count });
} catch (err: any) {
Expand Down

0 comments on commit 62f360f

Please sign in to comment.