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..f6b5031ef3 --- /dev/null +++ b/packages/pouch-storage/src/PouchStorageTable.test.ts @@ -0,0 +1,29 @@ +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' }], + ['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) => { + 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' + ); + }); + }); +}); diff --git a/packages/pouch-storage/src/PouchStorageTable.ts b/packages/pouch-storage/src/PouchStorageTable.ts index eb6804103f..5a174f9e8d 100644 --- a/packages/pouch-storage/src/PouchStorageTable.ts +++ b/packages/pouch-storage/src/PouchStorageTable.ts @@ -35,34 +35,27 @@ 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[]; - $gt: FilterValue | FilterValue[]; - $gte: FilterValue | FilterValue[]; - $lt: FilterValue | FilterValue[]; - $lte: FilterValue | FilterValue[]; - $regex: RegExp; -}>; - -function makePouchFilter( +type PouchFilter = OnlyOneProp< + Pick< + PouchDB.Find.ConditionOperators, + '$eq' | '$ne' | '$gt' | '$gte' | '$lt' | '$lte' | '$regex' + > +>; + +export function makePouchFilter( type: string, value: FilterValue | FilterValue[] ): PouchFilter { 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: - // 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: @@ -72,7 +65,7 @@ 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}`); }