From 54e61830d19d37d896e562b2d7883b35ee59f488 Mon Sep 17 00:00:00 2001 From: Akshat Jawne Date: Tue, 14 May 2024 07:38:09 -0600 Subject: [PATCH 1/6] modify PouchFilter type --- packages/pouch-storage/src/PouchStorageTable.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/pouch-storage/src/PouchStorageTable.ts b/packages/pouch-storage/src/PouchStorageTable.ts index eb6804103f..0adb32e70f 100644 --- a/packages/pouch-storage/src/PouchStorageTable.ts +++ b/packages/pouch-storage/src/PouchStorageTable.ts @@ -35,20 +35,19 @@ export type PouchDBSort = Array< string | { [propName: string]: 'asc' | 'desc' } >; -// We may want to use `PouchDB.Find.ConditionOperators` instead of this, but -// there are some mismatches in how we use this with the types. -// https://github.com/deephaven/web-client-ui/issues/1505 to address this type PouchFilter = OnlyOneProp<{ $eq: FilterValue | FilterValue[]; - $neq: FilterValue | FilterValue[]; + $ne: FilterValue | FilterValue[]; $gt: FilterValue | FilterValue[]; $gte: FilterValue | FilterValue[]; $lt: FilterValue | FilterValue[]; $lte: FilterValue | FilterValue[]; - $regex: RegExp; -}>; + $regex: RegExp | string; +}> & + Omit; -function makePouchFilter( +// eslint-disable-next-line import/prefer-default-export +export function makePouchFilter( type: string, value: FilterValue | FilterValue[] ): PouchFilter { @@ -61,8 +60,7 @@ function makePouchFilter( case FilterType.eq: return { $eq: value }; case FilterType.notEq: - // This should be `$ne` https://github.com/deephaven/web-client-ui/issues/1505 - return { $neq: value }; + return { $ne: value }; case FilterType.greaterThan: return { $gt: value }; case FilterType.greaterThanOrEqualTo: From 2c56ea1a3dff43cee2fab17611d880c077c47485 Mon Sep 17 00:00:00 2001 From: Akshat Jawne Date: Tue, 14 May 2024 07:51:22 -0600 Subject: [PATCH 2/6] remove unneccessary es lint disable --- packages/pouch-storage/src/PouchStorageTable.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/pouch-storage/src/PouchStorageTable.ts b/packages/pouch-storage/src/PouchStorageTable.ts index 0adb32e70f..d5709fe4ef 100644 --- a/packages/pouch-storage/src/PouchStorageTable.ts +++ b/packages/pouch-storage/src/PouchStorageTable.ts @@ -46,7 +46,6 @@ type PouchFilter = OnlyOneProp<{ }> & Omit; -// eslint-disable-next-line import/prefer-default-export export function makePouchFilter( type: string, value: FilterValue | FilterValue[] From ac9d15b9c1087cd5fbd45381fd09cf921fd9a00e Mon Sep 17 00:00:00 2001 From: Akshat Jawne Date: Tue, 14 May 2024 10:45:57 -0600 Subject: [PATCH 3/6] remove unneccessary intersection type --- packages/pouch-storage/src/PouchStorageTable.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/pouch-storage/src/PouchStorageTable.ts b/packages/pouch-storage/src/PouchStorageTable.ts index d5709fe4ef..fce3649b73 100644 --- a/packages/pouch-storage/src/PouchStorageTable.ts +++ b/packages/pouch-storage/src/PouchStorageTable.ts @@ -43,8 +43,7 @@ type PouchFilter = OnlyOneProp<{ $lt: FilterValue | FilterValue[]; $lte: FilterValue | FilterValue[]; $regex: RegExp | string; -}> & - Omit; +}>; export function makePouchFilter( type: string, From 53382bad0d24a67ba40bf355650a6115bcc918a9 Mon Sep 17 00:00:00 2001 From: Akshat Jawne Date: Tue, 14 May 2024 10:59:58 -0600 Subject: [PATCH 4/6] Add unit tests --- jest.config.base.cjs | 6 +++++ .../src/PouchStorageTable.test.ts | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 packages/pouch-storage/src/PouchStorageTable.test.ts diff --git a/jest.config.base.cjs b/jest.config.base.cjs index 891f961975..07be8cfdab 100644 --- a/jest.config.base.cjs +++ b/jest.config.base.cjs @@ -63,6 +63,12 @@ module.exports = { ), // Handle monaco worker files '\\.worker.*$': 'identity-obj-proxy', + // Handle pouchdb modules + '^pouchdb-browser$': path.join( + __dirname, + './packages/mocks/src/pouchdb-browser.js' + ), + '^pouchdb-find': 'identity-obj-proxy', // All packages except icons and jsapi-types use src code '^@deephaven/(?!icons|jsapi-types)(.*)$': path.join( __dirname, diff --git a/packages/pouch-storage/src/PouchStorageTable.test.ts b/packages/pouch-storage/src/PouchStorageTable.test.ts new file mode 100644 index 0000000000..e2ed2a3fa4 --- /dev/null +++ b/packages/pouch-storage/src/PouchStorageTable.test.ts @@ -0,0 +1,26 @@ +import { makePouchFilter } from './PouchStorageTable'; + +describe('PouchStorageTable - Filter Functions', () => { + describe('makePouchFilter', () => { + it.each([ + ['eq', 'value', { $eq: 'value' }], + ['notEq', 'value', { $ne: 'value' }], + ['greaterThan', 'value', { $gt: 'value' }], + ['greaterThanOrEqualTo', 'value', { $gte: 'value' }], + ['lessThan', 'value', { $lt: 'value' }], + ['lessThanOrEqualTo', 'value', { $lte: 'value' }], + ])( + 'should create a PouchFilter for %s operator', + (type, value, expected) => { + const filter = makePouchFilter(type, value); + expect(filter).toEqual(expected); + } + ); + + it('should throw an error for unsupported filter types', () => { + expect(() => makePouchFilter('unsupported', 'value')).toThrow( + 'Unsupported type: unsupported' + ); + }); + }); +}); From b668099d25154659cc4e8a59259a1b0df8e5a941 Mon Sep 17 00:00:00 2001 From: Akshat Jawne Date: Tue, 14 May 2024 13:40:51 -0600 Subject: [PATCH 5/6] Modify PouchFilter type to use PouchDB type --- .../pouch-storage/src/PouchStorageTable.ts | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/packages/pouch-storage/src/PouchStorageTable.ts b/packages/pouch-storage/src/PouchStorageTable.ts index fce3649b73..a445db6e87 100644 --- a/packages/pouch-storage/src/PouchStorageTable.ts +++ b/packages/pouch-storage/src/PouchStorageTable.ts @@ -35,15 +35,12 @@ export type PouchDBSort = Array< string | { [propName: string]: 'asc' | 'desc' } >; -type PouchFilter = OnlyOneProp<{ - $eq: FilterValue | FilterValue[]; - $ne: FilterValue | FilterValue[]; - $gt: FilterValue | FilterValue[]; - $gte: FilterValue | FilterValue[]; - $lt: FilterValue | FilterValue[]; - $lte: FilterValue | FilterValue[]; - $regex: RegExp | string; -}>; +type PouchFilter = OnlyOneProp< + Pick< + PouchDB.Find.ConditionOperators, + '$eq' | '$ne' | '$gt' | '$gte' | '$lt' | '$lte' | '$regex' + > +>; export function makePouchFilter( type: string, @@ -52,9 +49,9 @@ export function makePouchFilter( switch (type) { case FilterType.in: case FilterType.contains: - return { $regex: new RegExp(`${value}`) }; + return { $regex: new RegExp(`${value}`).toString() }; case FilterType.inIgnoreCase: - return { $regex: new RegExp(`${value}`, 'i') }; + return { $regex: new RegExp(`${value}`, 'i').toString() }; case FilterType.eq: return { $eq: value }; case FilterType.notEq: @@ -68,7 +65,7 @@ export function makePouchFilter( case FilterType.lessThanOrEqualTo: return { $lte: value }; case FilterType.startsWith: - return { $regex: new RegExp(`^(?${value}).*`) }; + return { $regex: new RegExp(`^(?${value}).*`).toString() }; default: throw new Error(`Unsupported type: ${type}`); } From 8a5850b60351008c2da350eae7f23d84b5f240d9 Mon Sep 17 00:00:00 2001 From: Akshat Jawne Date: Wed, 15 May 2024 07:30:54 -0600 Subject: [PATCH 6/6] add test cases for regex types --- packages/pouch-storage/src/PouchStorageTable.test.ts | 3 +++ packages/pouch-storage/src/PouchStorageTable.ts | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/pouch-storage/src/PouchStorageTable.test.ts b/packages/pouch-storage/src/PouchStorageTable.test.ts index e2ed2a3fa4..f6b5031ef3 100644 --- a/packages/pouch-storage/src/PouchStorageTable.test.ts +++ b/packages/pouch-storage/src/PouchStorageTable.test.ts @@ -9,6 +9,9 @@ describe('PouchStorageTable - Filter Functions', () => { ['greaterThanOrEqualTo', 'value', { $gte: 'value' }], ['lessThan', 'value', { $lt: 'value' }], ['lessThanOrEqualTo', 'value', { $lte: 'value' }], + ['startsWith', 'val', { $regex: `/^val.*/` }], + ['contains', 'value', { $regex: '/value/' }], + ['inIgnoreCase', ['value1', 'value2'], { $regex: '/value1,value2/i' }], ])( 'should create a PouchFilter for %s operator', (type, value, expected) => { diff --git a/packages/pouch-storage/src/PouchStorageTable.ts b/packages/pouch-storage/src/PouchStorageTable.ts index a445db6e87..5a174f9e8d 100644 --- a/packages/pouch-storage/src/PouchStorageTable.ts +++ b/packages/pouch-storage/src/PouchStorageTable.ts @@ -65,7 +65,7 @@ export function makePouchFilter( case FilterType.lessThanOrEqualTo: return { $lte: value }; case FilterType.startsWith: - return { $regex: new RegExp(`^(?${value}).*`).toString() }; + return { $regex: new RegExp(`^${value}.*`).toString() }; default: throw new Error(`Unsupported type: ${type}`); }