From 48434c107a02bed96b9c280eea5afc51ace5a0be Mon Sep 17 00:00:00 2001 From: Ido Kahlon Date: Thu, 1 Feb 2024 12:39:55 +0200 Subject: [PATCH 1/4] handle shorthand filter --- .../src/converters/filter_transformer.spec.ts | 26 ++++++++++++++++++- .../src/converters/filter_transformer.ts | 21 ++++++++++----- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/libs/velo-external-db-core/src/converters/filter_transformer.spec.ts b/libs/velo-external-db-core/src/converters/filter_transformer.spec.ts index f2eb4eb47..d8fecd3c5 100644 --- a/libs/velo-external-db-core/src/converters/filter_transformer.spec.ts +++ b/libs/velo-external-db-core/src/converters/filter_transformer.spec.ts @@ -193,6 +193,28 @@ describe('Filter Transformer', () => { value: ctx.fieldValue }) }) + + test('correctly transform short syntax filter with object value', () => { + const filter = { + [ctx.fieldName]: ctx.objectValue + } + expect(env.FilterTransformer.transform(filter)).toEqual({ + fieldName: ctx.fieldName, + operator: env.FilterTransformer.wixOperatorToAdapterOperator('$eq'), + value: ctx.objectValue + }) + }) + + test('correctly transform short syntax filter with encrypted value', () => { + const filter = { + [ctx.fieldName]: { $encrypted: ctx.fieldValue } + } + expect(env.FilterTransformer.transform(filter)).toEqual({ + fieldName: ctx.fieldName, + operator: env.FilterTransformer.wixOperatorToAdapterOperator('$eq'), + value: { $encrypted: ctx.fieldValue } + }) + }) }) interface Enviorment { @@ -211,7 +233,8 @@ describe('Filter Transformer', () => { fieldValue: Uninitialized, operator: Uninitialized, fieldListValue: Uninitialized, - veloDate: Uninitialized + veloDate: Uninitialized, + objectValue: Uninitialized } beforeEach(() => { @@ -223,6 +246,7 @@ describe('Filter Transformer', () => { ctx.operator = gen.randomOperator() as WixDataMultiFieldOperators | WixDataSingleFieldOperators ctx.fieldListValue = [chance.word(), chance.word(), chance.word(), chance.word(), chance.word()] ctx.veloDate = genCommon.veloDate() + ctx.objectValue = genCommon.randomObject() }) }) diff --git a/libs/velo-external-db-core/src/converters/filter_transformer.ts b/libs/velo-external-db-core/src/converters/filter_transformer.ts index e49dcb594..93d26b473 100644 --- a/libs/velo-external-db-core/src/converters/filter_transformer.ts +++ b/libs/velo-external-db-core/src/converters/filter_transformer.ts @@ -118,16 +118,25 @@ export default class FilterTransformer implements IFilterTransformer { } valueAndOperatorFromFilter(filter: any, fieldName: string) { - // there are two options: - // { fieldName: { operator: value } } - // or - // { fieldName: value } const filterValue = filter[fieldName] - const operator = isObject(filterValue) ? Object.keys(filterValue)[0] : '$eq' - const value = isObject(filterValue) ? filterValue[operator] : filterValue + if (isObject(filterValue) && this.isOperator(Object.keys(filterValue)[0])) { + // Object.keys(filterValue)[0] is an operator + const operator = Object.keys(filterValue)[0] + const value = filterValue[operator] + return { + operator, + value + } + } + const operator = '$eq' + const value = filterValue return { operator, value } } + + private isOperator(operator: string) { + return operator.startsWith('$') && operator !== '$encrypted' + } } From 9ab31492491c8095ec267a33c6445b4e8a6d52ee Mon Sep 17 00:00:00 2001 From: Ido Kahlon Date: Thu, 1 Feb 2024 15:04:14 +0200 Subject: [PATCH 2/4] bump to 2.0.1 --- libs/velo-external-db-core/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/velo-external-db-core/package.json b/libs/velo-external-db-core/package.json index 87af1f2eb..e935ba0d8 100644 --- a/libs/velo-external-db-core/package.json +++ b/libs/velo-external-db-core/package.json @@ -1,5 +1,5 @@ { "name": "@wix-velo/velo-external-db-core", - "version": "2.0.0", + "version": "2.0.1", "type": "commonjs" } From 94b984030b4992e28c4d2f9fb03b2ac9f83f19c9 Mon Sep 17 00:00:00 2001 From: Ido Kahlon Date: Thu, 1 Feb 2024 15:05:44 +0200 Subject: [PATCH 3/4] remove comment --- libs/velo-external-db-core/src/converters/filter_transformer.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/libs/velo-external-db-core/src/converters/filter_transformer.ts b/libs/velo-external-db-core/src/converters/filter_transformer.ts index 93d26b473..5e98c0058 100644 --- a/libs/velo-external-db-core/src/converters/filter_transformer.ts +++ b/libs/velo-external-db-core/src/converters/filter_transformer.ts @@ -120,7 +120,6 @@ export default class FilterTransformer implements IFilterTransformer { valueAndOperatorFromFilter(filter: any, fieldName: string) { const filterValue = filter[fieldName] if (isObject(filterValue) && this.isOperator(Object.keys(filterValue)[0])) { - // Object.keys(filterValue)[0] is an operator const operator = Object.keys(filterValue)[0] const value = filterValue[operator] return { From a69b5bee06ec0b580ac22dbb95c96483d2df97b7 Mon Sep 17 00:00:00 2001 From: Ido Kahlon Date: Thu, 1 Feb 2024 15:08:32 +0200 Subject: [PATCH 4/4] add comma --- .../src/converters/filter_transformer.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/velo-external-db-core/src/converters/filter_transformer.spec.ts b/libs/velo-external-db-core/src/converters/filter_transformer.spec.ts index d8fecd3c5..4b7b406d4 100644 --- a/libs/velo-external-db-core/src/converters/filter_transformer.spec.ts +++ b/libs/velo-external-db-core/src/converters/filter_transformer.spec.ts @@ -234,7 +234,7 @@ describe('Filter Transformer', () => { operator: Uninitialized, fieldListValue: Uninitialized, veloDate: Uninitialized, - objectValue: Uninitialized + objectValue: Uninitialized, } beforeEach(() => {