Fixed saving collection card with 'latest' collection on sqlite

no issue

- added passthrough of `transaction` property when fetching post IDs otherwise SQLite will error with ` "Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?"`
This commit is contained in:
Kevin Ansfield 2023-09-25 11:49:10 +01:00
parent 6480cfb161
commit 562e8af26a
2 changed files with 12 additions and 7 deletions

View File

@ -108,7 +108,7 @@ type QueryOptions = {
interface PostsRepository {
getAll(options: QueryOptions): Promise<CollectionPost[]>;
getAllIds(): Promise<string[]>;
getAllIds(options?: {transaction: Knex.Transaction}): Promise<string[]>;
}
export class CollectionsService {
@ -129,7 +129,7 @@ export class CollectionsService {
this.slugService = deps.slugService;
}
private async toDTO(collection: Collection): Promise<CollectionDTO> {
private async toDTO(collection: Collection, options?: {transaction: Knex.Transaction}): Promise<CollectionDTO> {
const dto = {
id: collection.id,
title: collection.title,
@ -146,7 +146,7 @@ export class CollectionsService {
}))
};
if (collection.slug === 'latest') {
const allPostIds = await this.postsRepository.getAllIds();
const allPostIds = await this.postsRepository.getAllIds(options);
dto.posts = allPostIds.map((id, index) => ({
id,
sort_order: index
@ -574,7 +574,7 @@ export class CollectionsService {
if (!collection) {
return null;
}
return this.toDTO(collection);
return this.toDTO(collection, options);
}
async getBySlug(slug: string, options?: {transaction: Knex.Transaction}): Promise<CollectionDTO | null> {
@ -582,7 +582,7 @@ export class CollectionsService {
if (!collection) {
return null;
}
return this.toDTO(collection);
return this.toDTO(collection, options);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any

View File

@ -4,8 +4,13 @@ class PostsRepository {
this.moment = moment;
}
async getAllIds() {
const rows = await this.models.Post.query().select('id').where('type', 'post');
/**
* @param {Object} options
* @returns Promise<string[]>
*/
async getAllIds({transaction} = {}) {
const query = this.models.Post.query().select('id').where('type', 'post');
const rows = transaction ? await query.transacting(transaction) : await query;
return rows.map(row => row.id);
}