diff --git a/src/paginate.spec.ts b/src/paginate.spec.ts index 4accbbe..f4c8bec 100644 --- a/src/paginate.spec.ts +++ b/src/paginate.spec.ts @@ -2061,6 +2061,62 @@ describe('paginate', () => { }) }) + it('should only select columns via query which are selected in config', async () => { + const config: PaginateConfig = { + select: ['id', 'home.id', 'home.pillows.id'], + relations: { home: { pillows: true } }, + sortableColumns: ['id', 'name'], + } + const query: PaginateQuery = { + path: '', + select: ['id', 'home.id', 'home.name'], + } + + const result = await paginate(query, catRepo, config) + + result.data.forEach((cat) => { + expect(cat.id).toBeDefined() + + if (cat.id === 1 || cat.id === 2) { + expect(cat.home.id).toBeDefined() + expect(cat.home.name).not.toBeDefined() + } else { + expect(cat.home).toBeNull() + } + }) + }) + + it('should return the specified nested relationship columns only', async () => { + const config: PaginateConfig = { + select: ['id', 'home.id', 'home.pillows.id'], + relations: { home: { pillows: true } }, + sortableColumns: ['id', 'name'], + } + const query: PaginateQuery = { + path: '', + } + + const result = await paginate(query, catRepo, config) + + result.data.forEach((cat) => { + expect(cat.id).toBeDefined() + expect(cat.name).not.toBeDefined() + + if (cat.id === 1 || cat.id === 2) { + expect(cat.home.id).toBeDefined() + expect(cat.home.name).not.toBeDefined() + expect(cat.home.countCat).not.toBeDefined() + + cat.home.pillows.forEach((pillow) => { + expect(pillow.id).toBeDefined() + expect(pillow.color).not.toBeDefined() + }) + } else { + expect(cat.home).toBeNull() + } + }) + }) + it('should return the right amount of results if a many to many relation is involved', async () => { const config: PaginateConfig = { sortableColumns: ['id'], diff --git a/src/paginate.ts b/src/paginate.ts index bef1aa9..1f34be4 100644 --- a/src/paginate.ts +++ b/src/paginate.ts @@ -183,11 +183,7 @@ export async function paginate( if (query.select?.includes(currentCol) ?? true) { const columnProperties = getPropertiesByColumnName(currentCol) const isRelation = checkIsRelation(queryBuilder, columnProperties.propertyPath) - const { isVirtualProperty } = extractVirtualProperty(queryBuilder, columnProperties) - if (hasColumnWithPropertyPath(queryBuilder, columnProperties) || isVirtualProperty) { - // here we can avoid to manually fix and add the query of virtual columns - cols.push(fixColumnAlias(columnProperties, queryBuilder.alias, isRelation)) - } + cols.push(fixColumnAlias(columnProperties, queryBuilder.alias, isRelation)) } return cols }, [])