Skip to content

Commit

Permalink
feat(distinct): update types and exports (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegDO authored Dec 13, 2023
1 parent 9f1f696 commit b1e8c00
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 39 deletions.
39 changes: 12 additions & 27 deletions __tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 }],
});

Expand All @@ -399,21 +396,15 @@ 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 }],
});

expect(result.getQuery()).to.not.include('DISTINCT');
});

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 }],
});

Expand Down Expand Up @@ -1237,7 +1228,7 @@ describe('services/typeorm-json-query', () => {
// @ts-ignore
query: { attributes },
},
{ distinctType: DistinctType.ALL },
{ distinctType: 'all' },
)
.toQuery()
.getQuery();
Expand All @@ -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();
Expand Down Expand Up @@ -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, [
Expand All @@ -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 },
Expand All @@ -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 },
Expand Down Expand Up @@ -1460,7 +1445,7 @@ describe('services/typeorm-json-query', () => {
emptyInstance['applySelectAttributes'].call(
{
applyDistinctSelectToQuery: applyDistinctSelectToQueryStub,
options: { distinctType: DistinctType.POSTGRES },
options: { distinctType: 'postgres' },
},
qb,
[
Expand All @@ -1479,7 +1464,7 @@ describe('services/typeorm-json-query', () => {
emptyInstance['applySelectAttributes'].call(
{
applyDistinctSelectToQuery: applyDistinctSelectToQueryStub,
options: { distinctType: DistinctType.DISABLED },
options: { distinctType: 'disabled' },
},
qb,
[
Expand All @@ -1498,7 +1483,7 @@ describe('services/typeorm-json-query', () => {
emptyInstance['applySelectAttributes'].call(
{
applyDistinctSelectToQuery: applyDistinctSelectToQueryStub,
options: { distinctType: DistinctType.DISABLED },
options: { distinctType: 'disabled' },
},
qb,
[
Expand Down
26 changes: 14 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pick<IJsonQueryAttribute, 'isDistinct'>> {
name: string;
}
Expand All @@ -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
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -126,7 +128,7 @@ class TypeormJsonQuery<TEntity = ObjectLiteral> {
isDisableOrderBy: false,
isDisableGroupBy: false,
isDisablePagination: false,
distinctType: DistinctType.POSTGRES,
distinctType: 'postgres',
isLateralJoins: true,
};

Expand Down Expand Up @@ -249,7 +251,7 @@ class TypeormJsonQuery<TEntity = ObjectLiteral> {
[fieldName]: {
name: fieldName,
isDistinct:
distinctType === DistinctType.DISABLED || typeof field === 'string'
distinctType === 'disabled' || typeof field === 'string'
? false
: Boolean(field?.isDistinct),
},
Expand Down Expand Up @@ -1079,7 +1081,7 @@ class TypeormJsonQuery<TEntity = ObjectLiteral> {
const { distinctType } = this.options;

// Validate distinct type
if (!distinctType || distinctType === DistinctType.DISABLED) {
if (!distinctType || distinctType === 'disabled') {
throw new Error('Invalid json query: distinct type.');
}

Expand Down Expand Up @@ -1108,7 +1110,7 @@ class TypeormJsonQuery<TEntity = ObjectLiteral> {
return;
}

if (distinctType === DistinctType.ALL) {
if (distinctType === 'all') {
query.distinct(true);

return;
Expand All @@ -1131,7 +1133,7 @@ class TypeormJsonQuery<TEntity = ObjectLiteral> {
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);
}

Expand Down

0 comments on commit b1e8c00

Please sign in to comment.