Wired up events to CollectionsBookshelfRepository

refs https://github.com/TryGhost/Arch/issues/95

This handles actually dispatching the events
This commit is contained in:
Fabien "egg" O'Carroll 2023-09-22 15:16:56 +07:00 committed by Fabien 'egg' O'Carroll
parent 673b06119b
commit c6f2c985f1
2 changed files with 11 additions and 2 deletions

View File

@ -12,9 +12,12 @@ const {default: ObjectID} = require('bson-objectid');
module.exports = class BookshelfCollectionsRepository {
#model;
#relationModel;
constructor(model, relationModel) {
/** @type {import('@tryghost/domain-events')} */
#DomainEvents;
constructor(model, relationModel, DomainEvents) {
this.#model = model;
this.#relationModel = relationModel;
this.#DomainEvents = DomainEvents;
}
async createTransaction(cb) {
@ -241,6 +244,12 @@ module.exports = class BookshelfCollectionsRepository {
if (collectionPostRelationsToDeleteIds.length > 0) {
await this.#relationModel.query().delete().whereIn('id', collectionPostRelationsToDeleteIds).transacting(options.transaction);
}
options.transaction.executionPromise.then(() => {
for (const event of collection.events) {
this.#DomainEvents.dispatch(event);
}
});
}
}
};

View File

@ -12,7 +12,7 @@ class CollectionsServiceWrapper {
const DomainEvents = require('@tryghost/domain-events');
const postsRepository = require('./PostsRepository').getInstance();
const models = require('../../models');
const collectionsRepositoryInMemory = new BookshelfCollectionsRepository(models.Collection, models.CollectionPost);
const collectionsRepositoryInMemory = new BookshelfCollectionsRepository(models.Collection, models.CollectionPost, DomainEvents);
const collectionsService = new CollectionsService({
collectionsRepository: collectionsRepositoryInMemory,