chore: Synchronizing refactor and small fixes from enterprise codebase (#3634)
* chore: Syncronizing refactor and small fixes from enterprise codebase * fix
This commit is contained in:
@@ -1,26 +1,26 @@
|
||||
import {
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
Column,
|
||||
PrimaryKey,
|
||||
IsUUID,
|
||||
DataType,
|
||||
Model,
|
||||
Default,
|
||||
} from "sequelize-typescript";
|
||||
import { FindOptions } from "sequelize";
|
||||
import { Model } from "sequelize-typescript";
|
||||
|
||||
class BaseModel extends Model {
|
||||
@IsUUID(4)
|
||||
@PrimaryKey
|
||||
@Default(DataType.UUIDV4)
|
||||
@Column(DataType.UUID)
|
||||
id: string;
|
||||
static async findAllInBatches<T extends BaseModel>(
|
||||
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;
|
||||
|
||||
@CreatedAt
|
||||
createdAt: Date;
|
||||
|
||||
@UpdatedAt
|
||||
updatedAt: Date;
|
||||
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 BaseModel;
|
||||
|
||||
Reference in New Issue
Block a user