* fix: type server models * fix: make ParanoidModel generic * fix: ApiKey * fix: Attachment * fix: AuthenticationProvider * fix: Backlink * fix: Collection * fix: Comment * fix: Document * fix: FileOperation * fix: Group * fix: GroupPermission * fix: GroupUser * fix: Integration * fix: IntegrationAuthentication * fix: Notification * fix: Pin * fix: Revision * fix: SearchQuery * fix: Share * fix: Star * fix: Subscription * fix: TypeError * fix: Imports * fix: Team * fix: TeamDomain * fix: User * fix: UserAuthentication * fix: UserPermission * fix: View * fix: WebhookDelivery * fix: WebhookSubscription * Remove type duplication --------- Co-authored-by: Tom Moor <tom.moor@gmail.com>
37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
/* eslint-disable @typescript-eslint/ban-types */
|
|
import { FindOptions } from "sequelize";
|
|
import { Model as SequelizeModel } from "sequelize-typescript";
|
|
|
|
class Model<
|
|
TModelAttributes extends {} = any,
|
|
TCreationAttributes extends {} = TModelAttributes
|
|
> extends SequelizeModel<TModelAttributes, TCreationAttributes> {
|
|
/**
|
|
* Find all models in batches, calling the callback function for each batch.
|
|
*
|
|
* @param query The query options.
|
|
* @param callback The function to call for each batch of results
|
|
*/
|
|
static async findAllInBatches<T extends Model>(
|
|
query: FindOptions<T>,
|
|
callback: (results: Array<T>, query: FindOptions<T>) => Promise<void>
|
|
) {
|
|
if (!query.offset) {
|
|
query.offset = 0;
|
|
}
|
|
if (!query.limit) {
|
|
query.limit = 10;
|
|
}
|
|
let results;
|
|
|
|
do {
|
|
// @ts-expect-error this T
|
|
results = await this.findAll<T>(query);
|
|
await callback(results, query);
|
|
query.offset += query.limit;
|
|
} while (results.length >= query.limit);
|
|
}
|
|
}
|
|
|
|
export default Model;
|