This commit is contained in:
Tom Moor committed 2025-03-23 11:20:11 -04:00
1 parent 49a09a2840
commit a760b951d5
2 files changed
+21 -10

No files matched your search

+3 -1
View File
@@ -289,6 +289,7 @@ class Model<
},
callback: (results: Array<T>, query: FindOptions<T>) => Promise<void>
): Promise<number> {
let total = 0;
const mappedQuery = {
...query,
offset: query.offset ?? 0,
@@ -300,6 +301,7 @@ class Model<
do {
// @ts-expect-error this T
results = await this.findAll<T>(mappedQuery);
total += results.length;
await callback(results, mappedQuery);
mappedQuery.offset += mappedQuery.limit;
} while (
@@ -307,7 +309,7 @@ class Model<
(mappedQuery.totalLimit ?? Infinity) > mappedQuery.offset
);
return mappedQuery.offset;
return total;
}
/**
@@ -1,4 +1,5 @@
import Router from "koa-router";
import isEmpty from "lodash/isEmpty";
import isNil from "lodash/isNil";
import isNull from "lodash/isNull";
import isUndefined from "lodash/isUndefined";
@@ -180,7 +181,10 @@ router.post(
const { transaction } = ctx.state;
const notification = await Notification.findByPk(id, {
lock: transaction.LOCK.UPDATE,
lock: {
level: transaction.LOCK.UPDATE,
of: Notification,
},
rejectOnEmpty: true,
transaction,
});
@@ -229,14 +233,19 @@ router.post(
};
}
const total = await Notification.findAllInBatches(
{ where },
async (results) => {
await Promise.all(
results.map((notification) => notification.updateWithCtx(ctx, values))
);
}
);
let total = 0;
if (!isEmpty(values)) {
total = await Notification.findAllInBatches(
{ where },
async (results) => {
await Promise.all(
results.map((notification) =>
notification.updateWithCtx(ctx, values)
)
);
}
);
}
ctx.body = {
success: true,