Browse Source

feat(core): Add `inList` op to enable filtering on custom field lists

Closes #1332
Michael Bromley 4 years ago
parent
commit
94da850360

+ 25 - 0
packages/core/src/api/schema/common/common-types.graphql

@@ -125,6 +125,31 @@ input DateOperators {
     between: DateRange
 }
 
+"Operators for filtering on a list of String fields"
+input StringListOperators {
+    inList: String!
+}
+
+"Operators for filtering on a list of Number fields"
+input NumberListOperators {
+    inList: Float!
+}
+
+"Operators for filtering on a list of Boolean fields"
+input BooleanListOperators {
+    inList: Boolean!
+}
+
+"Operators for filtering on a list of ID fields"
+input IDListOperators {
+    inList: ID!
+}
+
+"Operators for filtering on a list of Date fields"
+input DateListOperators {
+    inList: DateTime!
+}
+
 """
 Used to construct boolean expressions for filtering search results
 by FacetValue ID. Examples:

+ 4 - 0
packages/core/src/common/types/common-types.ts

@@ -147,6 +147,10 @@ export interface DateOperators {
     between?: DateRange;
 }
 
+export interface ListOperators {
+    inList?: string | number | boolean | Date;
+}
+
 export type PaymentMetadata = {
     [prop: string]: any;
 } & {

+ 6 - 2
packages/core/src/service/helpers/list-query-builder/parse-filter-params.ts

@@ -8,6 +8,7 @@ import {
     BooleanOperators,
     DateOperators,
     FilterParameter,
+    ListOperators,
     NullOptionals,
     NumberOperators,
     StringOperators,
@@ -22,7 +23,7 @@ export interface WhereCondition {
     parameters: { [param: string]: string | number };
 }
 
-type AllOperators = StringOperators & BooleanOperators & NumberOperators & DateOperators;
+type AllOperators = StringOperators & BooleanOperators & NumberOperators & DateOperators & ListOperators;
 type Operator = { [K in keyof AllOperators]-?: K }[keyof AllOperators];
 
 export function parseFilterParams<T extends VendureEntity>(
@@ -95,11 +96,14 @@ function buildWhereCondition(
                 clause: `${fieldName} != :arg${argIndex}`,
                 parameters: { [`arg${argIndex}`]: convertDate(operand) },
             };
+        case 'inList':
         case 'contains': {
             const LIKE = dbType === 'postgres' ? 'ILIKE' : 'LIKE';
             return {
                 clause: `${fieldName} ${LIKE} :arg${argIndex}`,
-                parameters: { [`arg${argIndex}`]: `%${operand.trim()}%` },
+                parameters: {
+                    [`arg${argIndex}`]: `%${typeof operand === 'string' ? operand.trim() : operand}%`,
+                },
             };
         }
         case 'notContains': {