Skip to content

Commit

Permalink
fix: PouchStorageTable using incorrect $ne operator (#2011)
Browse files Browse the repository at this point in the history
Resolves #1505

Changes:
- Modified `PouchFilter` type to use $ne instead of $neq
- Modified `$regex` prop to be able to accept `RegExp` and `string`,
rather than just string by default

Steps Moving Forward:
- Code change is implemented, having difficulty with imports when trying
to run Unit Tests (did not commit the file yet)
  • Loading branch information
AkshatJawne committed May 15, 2024
1 parent 95a589c commit 6cf1240
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
6 changes: 6 additions & 0 deletions jest.config.base.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions packages/pouch-storage/src/PouchStorageTable.test.ts
Original file line number Diff line number Diff line change
@@ -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'
);
});
});
});
31 changes: 12 additions & 19 deletions packages/pouch-storage/src/PouchStorageTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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}`);
}
Expand Down

0 comments on commit 6cf1240

Please sign in to comment.