diff --git a/__tests__/index-test.ts b/__tests__/index-test.ts index d93739f..4feff96 100644 --- a/__tests__/index-test.ts +++ b/__tests__/index-test.ts @@ -13,7 +13,7 @@ import type { Brackets } from 'typeorm'; import { SelectQueryBuilder } from 'typeorm'; import TestEntity from '@__mocks__/entities/test-entity'; import TypeormMock from '@__mocks__/typeorm'; -import TypeormJsonQuery, { DistinctType } from '@src/index'; +import TypeormJsonQuery from '@src/index'; describe('services/typeorm-json-query', () => { const sandbox = sinon.createSandbox(); @@ -386,10 +386,7 @@ describe('services/typeorm-json-query', () => { }); it('should correctly build select params when distinct attributes is exist for all distinct type', () => { - const result = TypeormJsonQuery.init( - { queryBuilder }, - { distinctType: DistinctType.ALL }, - ).toQuery({ + const result = TypeormJsonQuery.init({ queryBuilder }, { distinctType: 'all' }).toQuery({ attributes: [{ name: 'id' }, { name: 'param', isDistinct: true }], }); @@ -399,10 +396,7 @@ describe('services/typeorm-json-query', () => { }); it('should correctly build select and ignore provided distinct when distinct option is disabled', () => { - const result = TypeormJsonQuery.init( - { queryBuilder }, - { distinctType: DistinctType.DISABLED }, - ).toQuery({ + const result = TypeormJsonQuery.init({ queryBuilder }, { distinctType: 'disabled' }).toQuery({ attributes: [{ name: 'id' }, { name: 'param', isDistinct: true }], }); @@ -410,10 +404,7 @@ describe('services/typeorm-json-query', () => { }); it('should correctly build select and ignore provided distinct when distinct option is disabled', () => { - const result = TypeormJsonQuery.init( - { queryBuilder }, - { distinctType: DistinctType.DISABLED }, - ).toQuery({ + const result = TypeormJsonQuery.init({ queryBuilder }, { distinctType: 'disabled' }).toQuery({ attributes: [{ name: 'id' }, { name: 'param', isDistinct: true }], }); @@ -1237,7 +1228,7 @@ describe('services/typeorm-json-query', () => { // @ts-ignore query: { attributes }, }, - { distinctType: DistinctType.ALL }, + { distinctType: 'all' }, ) .toQuery() .getQuery(); @@ -1254,7 +1245,7 @@ describe('services/typeorm-json-query', () => { queryBuilder, query: { attributes: [{ name: 'id' }, { name: 'param', isDistinct: true }] }, }, - { distinctType: DistinctType.DISABLED }, + { distinctType: 'disabled' }, ) .toQuery() .getQuery(); @@ -1394,10 +1385,7 @@ describe('services/typeorm-json-query', () => { it('should throw error on apply select with distinct if distinct disabled', () => { const qb = repository.createQueryBuilder(); - const instance = TypeormJsonQuery.init( - { queryBuilder }, - { distinctType: DistinctType.DISABLED }, - ); + const instance = TypeormJsonQuery.init({ queryBuilder }, { distinctType: 'disabled' }); expect(() => instance['applyDistinctSelectToQuery'](qb, [ @@ -1409,7 +1397,7 @@ describe('services/typeorm-json-query', () => { it('should correctly apply select with all distinct', () => { const qb = repository.createQueryBuilder(); - const instance = TypeormJsonQuery.init({ queryBuilder }, { distinctType: DistinctType.ALL }); + const instance = TypeormJsonQuery.init({ queryBuilder }, { distinctType: 'all' }); instance['applyDistinctSelectToQuery'](qb, [ { name: 'id', isDistinct: false }, @@ -1424,10 +1412,7 @@ describe('services/typeorm-json-query', () => { it('should correctly apply select with postgres distinct', () => { const qb = repository.createQueryBuilder(); - const instance = TypeormJsonQuery.init( - { queryBuilder }, - { distinctType: DistinctType.POSTGRES }, - ); + const instance = TypeormJsonQuery.init({ queryBuilder }, { distinctType: 'postgres' }); instance['applyDistinctSelectToQuery'](qb, [ { name: 'id', isDistinct: false }, @@ -1460,7 +1445,7 @@ describe('services/typeorm-json-query', () => { emptyInstance['applySelectAttributes'].call( { applyDistinctSelectToQuery: applyDistinctSelectToQueryStub, - options: { distinctType: DistinctType.POSTGRES }, + options: { distinctType: 'postgres' }, }, qb, [ @@ -1479,7 +1464,7 @@ describe('services/typeorm-json-query', () => { emptyInstance['applySelectAttributes'].call( { applyDistinctSelectToQuery: applyDistinctSelectToQueryStub, - options: { distinctType: DistinctType.DISABLED }, + options: { distinctType: 'disabled' }, }, qb, [ @@ -1498,7 +1483,7 @@ describe('services/typeorm-json-query', () => { emptyInstance['applySelectAttributes'].call( { applyDistinctSelectToQuery: applyDistinctSelectToQueryStub, - options: { distinctType: DistinctType.DISABLED }, + options: { distinctType: 'disabled' }, }, qb, [ diff --git a/src/index.ts b/src/index.ts index 0df9046..bd757a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,12 +29,6 @@ export interface ITypeormRelationOptions { isDisabled?: boolean; } -export enum DistinctType { - DISABLED = 'disabled', - POSTGRES = 'postgres', // Apply postrges distinct on - ALL = 'all', // Apply any RDB distinct -} - export interface IAttribute extends Required> { name: string; } @@ -50,7 +44,7 @@ export interface ITypeormJsonQueryOptions { maxDeepWhere: number; defaultRelationPageSize: number; defaultRelationMaxPageSize: number; - distinctType: DistinctType; // Default - disabled + distinctType: TDistinct; // Default - postgres /** * E.g.: ['*'] - disable select relations (only join) or ['relation', { name: 'some-relation', isSelect: false, isLateral: true }] * NOTE: by default DISABLE select provided relations @@ -82,6 +76,14 @@ export interface IJsonRelationResult { isSelect?: boolean; } +/** + * Distinct types + * @description Supported distinct types: + * POSTGRES: distinct on + * ALL: any RDB distinct + */ +export type TDistinct = 'disabled' | 'postgres' | 'all'; + type TOrderExpressions = { field: string; expression: IJsonQueryOrderField['expression']; @@ -126,7 +128,7 @@ class TypeormJsonQuery { isDisableOrderBy: false, isDisableGroupBy: false, isDisablePagination: false, - distinctType: DistinctType.POSTGRES, + distinctType: 'postgres', isLateralJoins: true, }; @@ -249,7 +251,7 @@ class TypeormJsonQuery { [fieldName]: { name: fieldName, isDistinct: - distinctType === DistinctType.DISABLED || typeof field === 'string' + distinctType === 'disabled' || typeof field === 'string' ? false : Boolean(field?.isDistinct), }, @@ -1079,7 +1081,7 @@ class TypeormJsonQuery { const { distinctType } = this.options; // Validate distinct type - if (!distinctType || distinctType === DistinctType.DISABLED) { + if (!distinctType || distinctType === 'disabled') { throw new Error('Invalid json query: distinct type.'); } @@ -1108,7 +1110,7 @@ class TypeormJsonQuery { return; } - if (distinctType === DistinctType.ALL) { + if (distinctType === 'all') { query.distinct(true); return; @@ -1131,7 +1133,7 @@ class TypeormJsonQuery { const { distinctType } = this.options; // Build distinct only if enabled distinct and distinct attributes exists - if (distinctType !== DistinctType.DISABLED && attributes.some(({ isDistinct }) => isDistinct)) { + if (distinctType !== 'disabled' && attributes.some(({ isDistinct }) => isDistinct)) { return this.applyDistinctSelectToQuery(query, attributes); }