From 92d26baec4a280783391efba8369c0aecfd936d6 Mon Sep 17 00:00:00 2001 From: egg Date: Tue, 18 Feb 2025 21:14:07 +0900 Subject: [PATCH] fix: move the queryBuilder to enable withDeleted #973 (#974) --- src/paginate.spec.ts | 15 +++++++++++++++ src/paginate.ts | 8 ++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/paginate.spec.ts b/src/paginate.spec.ts index 8ecf9f73..1714ac13 100644 --- a/src/paginate.spec.ts +++ b/src/paginate.spec.ts @@ -2382,6 +2382,21 @@ describe('paginate', () => { await catRepo.restore({ id: cats[0].id }) }) + it('should return all relation items even if deleted', async () => { + const config: PaginateConfig = { + sortableColumns: ['id'], + withDeleted: true, + relations: ['cat'], + } + const query: PaginateQuery = { + path: '', + } + await catRepo.softDelete({ id: cats[0].id }) + const result = await paginate(query, catHomeRepo, config) + expect(result.data[0].cat).not.toBeNull() + await catRepo.restore({ id: cats[0].id }) + }) + it('should return only undeleted items', async () => { const config: PaginateConfig = { sortableColumns: ['id'], diff --git a/src/paginate.ts b/src/paginate.ts index feb8b32f..008344e4 100644 --- a/src/paginate.ts +++ b/src/paginate.ts @@ -230,6 +230,10 @@ export async function paginate( } } + if (config.withDeleted) { + queryBuilder.withDeleted() + } + if (config.relations) { const relations = Array.isArray(config.relations) ? OrmUtils.propertyPathsToTruthyObject(config.relations) @@ -334,10 +338,6 @@ export async function paginate( queryBuilder.andWhere(`(${baseWhereStr})`) } - if (config.withDeleted) { - queryBuilder.withDeleted() - } - if (config.searchableColumns) { if (query.searchBy && !config.ignoreSearchByInQueryParam) { for (const column of query.searchBy) {