fix: move blocks logic

This commit is contained in:
hexastack
2024-11-15 15:53:35 +01:00
parent a864816c34
commit e0a7a783f9
7 changed files with 238 additions and 31 deletions

View File

@@ -38,10 +38,12 @@ export type DeleteResult = {
export enum EHook {
preCreate = 'preCreate',
preUpdate = 'preUpdate',
preUpdateMany = 'preUpdateMany',
preDelete = 'preDelete',
preValidate = 'preValidate',
postCreate = 'postCreate',
postUpdate = 'postUpdate',
postUpdateMany = 'postUpdateMany',
postDelete = 'postDelete',
postValidate = 'postValidate',
}
@@ -157,6 +159,19 @@ export abstract class BaseRepository<
);
});
hooks?.updateMany.pre.execute(async function () {
const query = this as Query<D, D, unknown, T, 'updateMany'>;
const criteria = query.getFilter();
const updates = query.getUpdate();
await repository.preUpdateMany(query, criteria, updates);
repository.emitter.emit(
repository.getEventName(EHook.preUpdateMany),
criteria,
updates?.['$set'],
);
});
hooks?.findOneAndUpdate.post.execute(async function (
updated: HydratedDocument<T>,
) {
@@ -375,6 +390,14 @@ export abstract class BaseRepository<
// Nothing ...
}
async preUpdateMany(
_query: Query<D, D, unknown, T, 'updateMany'>,
_criteria: TFilterQuery<T>,
_updates: UpdateWithAggregationPipeline | UpdateQuery<D>,
) {
// Nothing ...
}
async postUpdate(
_query: Query<D, D, unknown, T, 'findOneAndUpdate'>,
_updated: T,

View File

@@ -17,7 +17,7 @@ enum LifecycleOperation {
// InsertMany = 'insertMany',
// Update = 'update',
// UpdateOne = 'updateOne',
// UpdateMany = 'updateMany',
UpdateMany = 'updateMany',
}
type PreHook = (...args: any[]) => void;
@@ -69,7 +69,7 @@ export class LifecycleHookManager {
// insertMany: ['pre'],
// update: ['pre', 'post'],
// updateOne: ['pre', 'post'],
// updateMany: ['pre', 'post'],
updateMany: ['pre', 'post'],
};
const lifecycleHooks: LifecycleHooks = {} as LifecycleHooks;