Skip to content

Commit

Permalink
feat(filter): allow specific filters by field (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 authored Aug 25, 2020
1 parent 6c77c2f commit bfa3e54
Show file tree
Hide file tree
Showing 23 changed files with 285 additions and 48 deletions.
10 changes: 10 additions & 0 deletions examples/02-complex-example/generated/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ export interface UserCreateInput {
noFilterField?: String | null
noSortField?: String | null
noFilterOrSortField?: String | null
stringFieldFilterEqContains?: String | null
intFieldFilterLteGte?: Float | null
numericField?: Float | null
numericFieldCustomPrecisionScale?: Float | null
charField?: String | null
Expand Down Expand Up @@ -197,6 +199,8 @@ export interface UserUpdateInput {
noFilterField?: String | null
noSortField?: String | null
noFilterOrSortField?: String | null
stringFieldFilterEqContains?: String | null
intFieldFilterLteGte?: Float | null
numericField?: Float | null
numericFieldCustomPrecisionScale?: Float | null
charField?: String | null
Expand Down Expand Up @@ -310,6 +314,10 @@ export interface UserWhereInput {
noSortField_startsWith?: String | null
noSortField_endsWith?: String | null
noSortField_in?: String[] | String | null
stringFieldFilterEqContains_eq?: String | null
stringFieldFilterEqContains_contains?: String | null
intFieldFilterLteGte_gte?: Int | null
intFieldFilterLteGte_lte?: Int | null
numericField_eq?: Float | null
numericField_gt?: Float | null
numericField_gte?: Float | null
Expand Down Expand Up @@ -490,6 +498,8 @@ export interface User extends BaseGraphQLObject {
noFilterField?: String | null
noSortField?: String | null
noFilterOrSortField?: String | null
stringFieldFilterEqContains?: String | null
intFieldFilterLteGte?: Int | null
numericField?: Float | null
numericFieldCustomPrecisionScale?: Float | null
charField?: String | null
Expand Down
24 changes: 24 additions & 0 deletions examples/02-complex-example/generated/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,18 @@ export class UserWhereInput {
@TypeGraphQLField(() => [String], { nullable: true })
noSortField_in?: string[];

@TypeGraphQLField({ nullable: true })
stringFieldFilterEqContains_eq?: string;

@TypeGraphQLField({ nullable: true })
stringFieldFilterEqContains_contains?: string;

@TypeGraphQLField(() => Int, { nullable: true })
intFieldFilterLteGte_gte?: number;

@TypeGraphQLField(() => Int, { nullable: true })
intFieldFilterLteGte_lte?: number;

@TypeGraphQLField(() => Float, { nullable: true })
numericField_eq?: number;

Expand Down Expand Up @@ -771,6 +783,12 @@ export class UserCreateInput {
@TypeGraphQLField({ nullable: true })
noFilterOrSortField?: string;

@TypeGraphQLField({ nullable: true })
stringFieldFilterEqContains?: string;

@TypeGraphQLField({ nullable: true })
intFieldFilterLteGte?: number;

@TypeGraphQLField({ nullable: true })
numericField?: number;

Expand Down Expand Up @@ -885,6 +903,12 @@ export class UserUpdateInput {
@TypeGraphQLField({ nullable: true })
noFilterOrSortField?: string;

@TypeGraphQLField({ nullable: true })
stringFieldFilterEqContains?: string;

@TypeGraphQLField({ nullable: true })
intFieldFilterLteGte?: number;

@TypeGraphQLField({ nullable: true })
numericField?: number;

Expand Down
10 changes: 10 additions & 0 deletions examples/02-complex-example/generated/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ type User implements BaseGraphQLObject {
noFilterField: String
noSortField: String
noFilterOrSortField: String
stringFieldFilterEqContains: String
intFieldFilterLteGte: Int
numericField: Float
numericFieldCustomPrecisionScale: Float
charField: String
Expand Down Expand Up @@ -172,6 +174,8 @@ input UserCreateInput {
noFilterField: String
noSortField: String
noFilterOrSortField: String
stringFieldFilterEqContains: String
intFieldFilterLteGte: Float
numericField: Float
numericFieldCustomPrecisionScale: Float
charField: String
Expand Down Expand Up @@ -283,6 +287,8 @@ input UserUpdateInput {
noFilterField: String
noSortField: String
noFilterOrSortField: String
stringFieldFilterEqContains: String
intFieldFilterLteGte: Float
numericField: Float
numericFieldCustomPrecisionScale: Float
charField: String
Expand Down Expand Up @@ -396,6 +402,10 @@ input UserWhereInput {
noSortField_startsWith: String
noSortField_endsWith: String
noSortField_in: [String!]
stringFieldFilterEqContains_eq: String
stringFieldFilterEqContains_contains: String
intFieldFilterLteGte_gte: Int
intFieldFilterLteGte_lte: Int
numericField_eq: Float
numericField_gt: Float
numericField_gte: Float
Expand Down
6 changes: 6 additions & 0 deletions examples/02-complex-example/src/modules/user/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ export class User extends BaseModel {
@StringField({ filter: false, sort: false, nullable: true })
noFilterOrSortField?: string;

@StringField({ filter: ['eq', 'contains'], sort: false, nullable: true })
stringFieldFilterEqContains?: string;

@IntField({ filter: ['lte', 'gte'], sort: false, nullable: true })
intFieldFilterLteGte?: number;

// See https://github.com/typeorm/typeorm/blob/a4dec02cc59d3219a29c7be0322af2253e1452dc/test/functional/database-schema/column-types/postgres/entity/PostWithOptions.ts
// Numeric fields (exact precision)
@NumericField({ nullable: true })
Expand Down
2 changes: 2 additions & 0 deletions src/decorators/BooleanField.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { GraphQLBoolean } from 'graphql';

import { DecoratorCommonOptions } from '../metadata';
import { BooleanWhereOperator } from '../torm';
import { composeMethodDecorators } from '../utils';

import { getCombinedDecorator } from './getCombinedDecorator';

interface BooleanFieldOptions extends DecoratorCommonOptions {
default?: boolean;
filter?: boolean | BooleanWhereOperator[];
}

export function BooleanField(options: BooleanFieldOptions = {}): any {
Expand Down
3 changes: 2 additions & 1 deletion src/decorators/DateField.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { GraphQLISODateTime } from 'type-graphql';

import { DecoratorCommonOptions } from '../metadata';
import { ColumnType } from '../torm';
import { ColumnType, DateWhereOperator } from '../torm';
import { composeMethodDecorators } from '../utils';

import { getCombinedDecorator } from './getCombinedDecorator';

interface DateFieldOptions extends DecoratorCommonOptions {
dataType?: ColumnType; // int16, jsonb, etc...
default?: Date;
filter?: boolean | DateWhereOperator[];
}

// V3: Deprecate this usage in favor of DateTimeField
Expand Down
2 changes: 2 additions & 0 deletions src/decorators/DateOnlyField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { DateResolver } from 'graphql-scalars';

import { DecoratorCommonOptions } from '../metadata';
import { DateOnlyString } from '../core';
import { DateOnlyWhereOperator } from '../torm';
import { composeMethodDecorators } from '../utils';

import { getCombinedDecorator } from './getCombinedDecorator';

interface DateOnlyFieldOptions extends DecoratorCommonOptions {
default?: DateOnlyString;
filter?: boolean | DateOnlyWhereOperator[];
}

// V3: Update this to DateField
Expand Down
2 changes: 2 additions & 0 deletions src/decorators/DateTimeField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { GraphQLISODateTime } from 'type-graphql';

import { DecoratorCommonOptions } from '../metadata';
import { DateTimeString } from '../core';
import { DateTimeWhereOperator } from '../torm';
import { composeMethodDecorators } from '../utils';

import { getCombinedDecorator } from './getCombinedDecorator';

interface DateTimeFieldOptions extends DecoratorCommonOptions {
default?: DateTimeString;
filter?: boolean | DateTimeWhereOperator[];
}

// V3: Deprecate this usage in favor of DateTimeField
Expand Down
2 changes: 2 additions & 0 deletions src/decorators/EmailField.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { IsEmail } from 'class-validator';

import { DecoratorCommonOptions } from '../metadata';
import { EmailWhereOperator } from '../torm';
import { composeMethodDecorators } from '../utils';

import { getCombinedDecorator } from './getCombinedDecorator';

interface EmailFieldOptions extends DecoratorCommonOptions {
unique?: boolean;
filter?: boolean | EmailWhereOperator[];
}

export function EmailField(options: EmailFieldOptions = {}): any {
Expand Down
2 changes: 2 additions & 0 deletions src/decorators/EnumField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { Column } from 'typeorm';

import { getMetadataStorage, DecoratorCommonOptions } from '../metadata';
import { composeMethodDecorators, generatedFolderPath, MethodDecoratorFactory } from '../utils';
import { EnumWhereOperator } from '../torm';

interface EnumFieldOptions extends DecoratorCommonOptions {
default?: any;
filter?: boolean | EnumWhereOperator[];
}

export function EnumField(name: string, enumeration: object, options: EnumFieldOptions = {}): any {
Expand Down
3 changes: 2 additions & 1 deletion src/decorators/FloatField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Float } from 'type-graphql';

import { DecoratorCommonOptions } from '../metadata';
import { composeMethodDecorators } from '../utils';
import { FloatColumnType } from '../torm';
import { FloatColumnType, FloatWhereOperator } from '../torm';

import { getCombinedDecorator } from './getCombinedDecorator';

interface FloatFieldOptions extends DecoratorCommonOptions {
dataType?: FloatColumnType; // int16, jsonb, etc...
default?: number;
filter?: boolean | FloatWhereOperator[];
}

export function FloatField(options: FloatFieldOptions = {}): any {
Expand Down
2 changes: 2 additions & 0 deletions src/decorators/IdField.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { DecoratorCommonOptions } from '../metadata';
import { IdWhereOperator } from '../torm';
import { composeMethodDecorators } from '../utils';

import { getCombinedDecorator } from './getCombinedDecorator';

interface IdFieldOptions extends DecoratorCommonOptions {
unique?: boolean;
filter?: boolean | IdWhereOperator[];
}

export function IdField(options: IdFieldOptions = {}): any {
Expand Down
3 changes: 2 additions & 1 deletion src/decorators/IntField.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Int } from 'type-graphql';

import { DecoratorCommonOptions } from '../metadata';
import { IntColumnType, IntWhereOperator } from '../torm';
import { composeMethodDecorators } from '../utils';
import { IntColumnType } from '../torm';

import { getCombinedDecorator } from './getCombinedDecorator';

interface IntFieldOptions extends DecoratorCommonOptions {
dataType?: IntColumnType;
default?: number;
filter?: boolean | IntWhereOperator[];
}

export function IntField(options: IntFieldOptions = {}): any {
Expand Down
3 changes: 2 additions & 1 deletion src/decorators/NumericField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ColumnCommonOptions } from 'typeorm/decorator/options/ColumnCommonOptio

import { DecoratorCommonOptions } from '../metadata';
import { composeMethodDecorators } from '../utils';
import { NumericColumnType } from '../torm';
import { NumericColumnType, NumericWhereOperator } from '../torm';

import { getCombinedDecorator } from './getCombinedDecorator';

Expand All @@ -13,6 +13,7 @@ interface NumericFieldOptions
ColumnNumericOptions,
DecoratorCommonOptions {
dataType?: NumericColumnType;
filter?: boolean | NumericWhereOperator[];
}

export function NumericField(options: NumericFieldOptions = {}): any {
Expand Down
3 changes: 2 additions & 1 deletion src/decorators/StringField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MaxLength, MinLength } from 'class-validator';

import { DecoratorCommonOptions } from '../metadata';
import { composeMethodDecorators } from '../utils';
import { StringColumnType } from '../torm';
import { StringColumnType, StringWhereOperator } from '../torm';

import { getCombinedDecorator } from './getCombinedDecorator';

Expand All @@ -12,6 +12,7 @@ interface StringFieldOptions extends DecoratorCommonOptions {
minLength?: number;
default?: string;
unique?: boolean;
filter?: boolean | StringWhereOperator[];
}

export function StringField(options: StringFieldOptions = {}): any {
Expand Down
4 changes: 2 additions & 2 deletions src/metadata/metadata-storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GraphQLEnumType } from 'graphql';
import { Container, Inject, Service } from 'typedi';

import { ColumnType } from '../torm';
import { ColumnType, WhereOperator } from '../torm';
import { Config } from '../core';

export type FieldType =
Expand All @@ -23,7 +23,7 @@ export interface DecoratorCommonOptions {
dbOnly?: boolean;
description?: string;
editable?: boolean;
filter?: boolean;
filter?: boolean | WhereOperator[];
nullable?: boolean;
readonly?: boolean;
sort?: boolean;
Expand Down
Loading

0 comments on commit bfa3e54

Please sign in to comment.