Skip to content

Commit

Permalink
feat(core): Implement negated string filter operators
Browse files Browse the repository at this point in the history
Closes #571
  • Loading branch information
michaelbromley committed Dec 11, 2020
1 parent 70cb932 commit 75b5b7a
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/admin-ui/src/lib/core/src/common/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2141,8 +2141,11 @@ export type ErrorResult = {

export type StringOperators = {
eq?: Maybe<Scalars['String']>;
notEq?: Maybe<Scalars['String']>;
contains?: Maybe<Scalars['String']>;
notContains?: Maybe<Scalars['String']>;
in?: Maybe<Array<Scalars['String']>>;
notIn?: Maybe<Array<Scalars['String']>>;
regex?: Maybe<Scalars['String']>;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1952,8 +1952,11 @@ export type ErrorResult = {

export type StringOperators = {
eq?: Maybe<Scalars['String']>;
notEq?: Maybe<Scalars['String']>;
contains?: Maybe<Scalars['String']>;
notContains?: Maybe<Scalars['String']>;
in?: Maybe<Array<Scalars['String']>>;
notIn?: Maybe<Array<Scalars['String']>>;
regex?: Maybe<Scalars['String']>;
};

Expand Down
3 changes: 3 additions & 0 deletions packages/common/src/generated-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,11 @@ export type ErrorResult = {

export type StringOperators = {
eq?: Maybe<Scalars['String']>;
notEq?: Maybe<Scalars['String']>;
contains?: Maybe<Scalars['String']>;
notContains?: Maybe<Scalars['String']>;
in?: Maybe<Array<Scalars['String']>>;
notIn?: Maybe<Array<Scalars['String']>>;
regex?: Maybe<Scalars['String']>;
};

Expand Down
3 changes: 3 additions & 0 deletions packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2109,8 +2109,11 @@ export type ErrorResult = {

export type StringOperators = {
eq?: Maybe<Scalars['String']>;
notEq?: Maybe<Scalars['String']>;
contains?: Maybe<Scalars['String']>;
notContains?: Maybe<Scalars['String']>;
in?: Maybe<Array<Scalars['String']>>;
notIn?: Maybe<Array<Scalars['String']>>;
regex?: Maybe<Scalars['String']>;
};

Expand Down
3 changes: 3 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1952,8 +1952,11 @@ export type ErrorResult = {

export type StringOperators = {
eq?: Maybe<Scalars['String']>;
notEq?: Maybe<Scalars['String']>;
contains?: Maybe<Scalars['String']>;
notContains?: Maybe<Scalars['String']>;
in?: Maybe<Array<Scalars['String']>>;
notIn?: Maybe<Array<Scalars['String']>>;
regex?: Maybe<Scalars['String']>;
};

Expand Down
3 changes: 3 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,11 @@ export type ErrorResult = {

export type StringOperators = {
eq?: Maybe<Scalars['String']>;
notEq?: Maybe<Scalars['String']>;
contains?: Maybe<Scalars['String']>;
notContains?: Maybe<Scalars['String']>;
in?: Maybe<Array<Scalars['String']>>;
notIn?: Maybe<Array<Scalars['String']>>;
regex?: Maybe<Scalars['String']>;
};

Expand Down
42 changes: 42 additions & 0 deletions packages/core/e2e/list-query-builder.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ describe('ListQueryBuilder', () => {
expect(getItemLabels(testEntities.items)).toEqual(['B']);
});

it('notEq', async () => {
const { testEntities } = await adminClient.query(GET_LIST, {
options: {
filter: {
label: {
notEq: 'B',
},
},
},
});

expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'D', 'E']);
});

it('contains', async () => {
const { testEntities } = await adminClient.query(GET_LIST, {
options: {
Expand All @@ -64,6 +78,20 @@ describe('ListQueryBuilder', () => {
expect(getItemLabels(testEntities.items)).toEqual(['C']);
});

it('notContains', async () => {
const { testEntities } = await adminClient.query(GET_LIST, {
options: {
filter: {
description: {
notContains: 'te',
},
},
},
});

expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
});

it('in', async () => {
const { testEntities } = await adminClient.query(GET_LIST, {
options: {
Expand All @@ -78,6 +106,20 @@ describe('ListQueryBuilder', () => {
expect(getItemLabels(testEntities.items)).toEqual(['A', 'C']);
});

it('notIn', async () => {
const { testEntities } = await adminClient.query(GET_LIST, {
options: {
filter: {
label: {
notIn: ['A', 'C'],
},
},
},
});

expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
});

describe('regex', () => {
it('simple substring', async () => {
const { testEntities } = await adminClient.query(GET_LIST, {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/api/schema/common/common-types.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,11 @@ interface ErrorResult {

input StringOperators {
eq: String
notEq: String
contains: String
notContains: String
in: [String!]
notIn: [String!]
regex: String
}

Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/common/types/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ export type FilterParameter<T extends VendureEntity> = {

export interface StringOperators {
eq?: string;
notEq?: string;
contains?: string;
notContains?: string;
in?: string[];
notIn?: string[];
regex?: string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,35 @@ function buildWhereCondition(
clause: `${fieldName} = :arg${argIndex}`,
parameters: { [`arg${argIndex}`]: convertDate(operand) },
};
case 'contains':
case 'notEq':
return {
clause: `${fieldName} != :arg${argIndex}`,
parameters: { [`arg${argIndex}`]: convertDate(operand) },
};
case 'contains': {
const LIKE = dbType === 'postgres' ? 'ILIKE' : 'LIKE';
return {
clause: `${fieldName} ${LIKE} :arg${argIndex}`,
parameters: { [`arg${argIndex}`]: `%${operand.trim()}%` },
};
}
case 'notContains': {
const LIKE = dbType === 'postgres' ? 'ILIKE' : 'LIKE';
return {
clause: `${fieldName} NOT ${LIKE} :arg${argIndex}`,
parameters: { [`arg${argIndex}`]: `%${operand.trim()}%` },
};
}
case 'in':
return {
clause: `${fieldName} IN (:...arg${argIndex})`,
parameters: { [`arg${argIndex}`]: operand },
};
case 'notIn':
return {
clause: `${fieldName} NOT IN (:...arg${argIndex})`,
parameters: { [`arg${argIndex}`]: operand },
};
case 'regex':
return {
clause: getRegexpClause(fieldName, argIndex, dbType),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1952,8 +1952,11 @@ export type ErrorResult = {

export type StringOperators = {
eq?: Maybe<Scalars['String']>;
notEq?: Maybe<Scalars['String']>;
contains?: Maybe<Scalars['String']>;
notContains?: Maybe<Scalars['String']>;
in?: Maybe<Array<Scalars['String']>>;
notIn?: Maybe<Array<Scalars['String']>>;
regex?: Maybe<Scalars['String']>;
};

Expand Down
2 changes: 1 addition & 1 deletion schema-admin.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion schema-shop.json

Large diffs are not rendered by default.

0 comments on commit 75b5b7a

Please sign in to comment.