New Replace type utility (#7476)

* fix: Replace type

* trigger ci
This commit is contained in:
Apoorv Mishra
2024-08-30 10:02:38 +05:30
committed by GitHub
parent d090316065
commit 5374784df6
4 changed files with 16 additions and 11 deletions

View File

@@ -44,7 +44,7 @@ async function teamPermanentDeleter(team: Team) {
where: {
teamId,
},
limit: 100,
batchLimit: 100,
},
async (attachments, options) => {
await sequelize.transaction(async (transaction) => {
@@ -72,7 +72,7 @@ async function teamPermanentDeleter(team: Team) {
where: {
teamId,
},
limit: 100,
batchLimit: 100,
},
async (users) => {
await sequelize.transaction(async (transaction) => {

View File

@@ -5,6 +5,7 @@ import isObject from "lodash/isObject";
import pick from "lodash/pick";
import { FindOptions, NonAttribute } from "sequelize";
import { Model as SequelizeModel } from "sequelize-typescript";
import { Replace } from "@server/types";
class Model<
TModelAttributes extends {} = any,
@@ -17,14 +18,14 @@ class Model<
* @param callback The function to call for each batch of results
*/
static async findAllInBatches<T extends Model>(
query: FindOptions<T>,
query: Replace<FindOptions<T>, "limit", "batchLimit">,
callback: (results: Array<T>, query: FindOptions<T>) => Promise<void>
) {
if (!query.offset) {
query.offset = 0;
}
if (!query.limit) {
query.limit = 10;
if (!query.batchLimit) {
query.batchLimit = 10;
}
let results;
@@ -32,8 +33,8 @@ class Model<
// @ts-expect-error this T
results = await this.findAll<T>(query);
await callback(results, query);
query.offset += query.limit;
} while (results.length >= query.limit);
query.offset += query.batchLimit;
} while (results.length >= query.batchLimit);
}
/**

View File

@@ -299,7 +299,7 @@ export default class WebsocketsProcessor {
await GroupUser.findAllInBatches<GroupUser>(
{
where: { groupId: event.modelId },
limit: 100,
batchLimit: 100,
},
async (groupUsers) => {
for (const groupUser of groupUsers) {
@@ -478,7 +478,7 @@ export default class WebsocketsProcessor {
where: {
groupId: event.modelId,
},
limit: 100,
batchLimit: 100,
},
async (groupMemberships) => {
for (const groupMembership of groupMemberships) {
@@ -532,7 +532,7 @@ export default class WebsocketsProcessor {
where: {
groupId: event.modelId,
},
limit: 100,
batchLimit: 100,
},
async (groupMemberships) => {
for (const groupMembership of groupMemberships) {
@@ -591,7 +591,7 @@ export default class WebsocketsProcessor {
required: true,
},
],
limit: 100,
batchLimit: 100,
},
async (groupUsers) => {
for (const groupMembership of groupMemberships) {

View File

@@ -540,3 +540,7 @@ export type UnfurlSignature = (
) => Promise<Unfurl | void>;
export type UninstallSignature = (integration: Integration) => Promise<void>;
export type Replace<T, K extends keyof T, N extends string> = {
[P in keyof T as P extends K ? N : P]: T[P extends K ? K : P];
};