mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Wrapped collection's creation in transaction
refs https://github.com/TryGhost/Arch/issues/16 - This is needed to avoid stale data/race conditions when processing collection post updates
This commit is contained in:
parent
0880770d50
commit
3dc27f505c
2 changed files with 31 additions and 22 deletions
|
@ -22,7 +22,7 @@ const messages = {
|
||||||
};
|
};
|
||||||
|
|
||||||
interface SlugService {
|
interface SlugService {
|
||||||
generate(desired: string): Promise<string>;
|
generate(desired: string, options: {transaction: any}): Promise<string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
type CollectionsServiceDeps = {
|
type CollectionsServiceDeps = {
|
||||||
|
@ -190,7 +190,10 @@ export class CollectionsService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async createCollection(data: CollectionInputDTO): Promise<CollectionDTO> {
|
async createCollection(data: CollectionInputDTO): Promise<CollectionDTO> {
|
||||||
const slug = await this.slugService.generate(data.slug || data.title);
|
return await this.collectionsRepository.createTransaction(async (transaction) => {
|
||||||
|
const slug = await this.slugService.generate(data.slug || data.title, {
|
||||||
|
transaction: transaction
|
||||||
|
});
|
||||||
const collection = await Collection.create({
|
const collection = await Collection.create({
|
||||||
title: data.title,
|
title: data.title,
|
||||||
slug: slug,
|
slug: slug,
|
||||||
|
@ -203,7 +206,8 @@ export class CollectionsService {
|
||||||
|
|
||||||
if (collection.type === 'automatic' && collection.filter) {
|
if (collection.type === 'automatic' && collection.filter) {
|
||||||
const posts = await this.postsRepository.getAll({
|
const posts = await this.postsRepository.getAll({
|
||||||
filter: collection.filter
|
filter: collection.filter,
|
||||||
|
transaction: transaction
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const post of posts) {
|
for (const post of posts) {
|
||||||
|
@ -211,9 +215,12 @@ export class CollectionsService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.collectionsRepository.save(collection);
|
await this.collectionsRepository.save(collection, {
|
||||||
|
transaction: transaction
|
||||||
|
});
|
||||||
|
|
||||||
return this.toDTO(collection);
|
return this.toDTO(collection);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async addPostToCollection(collectionId: string, post: CollectionPostListItemDTO): Promise<CollectionDTO | null> {
|
async addPostToCollection(collectionId: string, post: CollectionPostListItemDTO): Promise<CollectionDTO | null> {
|
||||||
|
|
|
@ -19,8 +19,10 @@ class CollectionsServiceWrapper {
|
||||||
postsRepository: postsRepository,
|
postsRepository: postsRepository,
|
||||||
DomainEvents: DomainEvents,
|
DomainEvents: DomainEvents,
|
||||||
slugService: {
|
slugService: {
|
||||||
async generate(input) {
|
async generate(input, options) {
|
||||||
return models.Collection.generateSlug(models.Collection, input, {});
|
return models.Collection.generateSlug(models.Collection, input, {
|
||||||
|
transacting: options.transaction
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue