Skip to content

Commit 6cf1240

Browse files
authored
fix: PouchStorageTable using incorrect $ne operator (#2011)
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)
1 parent 95a589c commit 6cf1240

3 files changed

Lines changed: 47 additions & 19 deletions

File tree

jest.config.base.cjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ module.exports = {
6363
),
6464
// Handle monaco worker files
6565
'\\.worker.*$': 'identity-obj-proxy',
66+
// Handle pouchdb modules
67+
'^pouchdb-browser$': path.join(
68+
__dirname,
69+
'./packages/mocks/src/pouchdb-browser.js'
70+
),
71+
'^pouchdb-find': 'identity-obj-proxy',
6672
// All packages except icons and jsapi-types use src code
6773
'^@deephaven/(?!icons|jsapi-types)(.*)$': path.join(
6874
__dirname,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { makePouchFilter } from './PouchStorageTable';
2+
3+
describe('PouchStorageTable - Filter Functions', () => {
4+
describe('makePouchFilter', () => {
5+
it.each([
6+
['eq', 'value', { $eq: 'value' }],
7+
['notEq', 'value', { $ne: 'value' }],
8+
['greaterThan', 'value', { $gt: 'value' }],
9+
['greaterThanOrEqualTo', 'value', { $gte: 'value' }],
10+
['lessThan', 'value', { $lt: 'value' }],
11+
['lessThanOrEqualTo', 'value', { $lte: 'value' }],
12+
['startsWith', 'val', { $regex: `/^val.*/` }],
13+
['contains', 'value', { $regex: '/value/' }],
14+
['inIgnoreCase', ['value1', 'value2'], { $regex: '/value1,value2/i' }],
15+
])(
16+
'should create a PouchFilter for %s operator',
17+
(type, value, expected) => {
18+
const filter = makePouchFilter(type, value);
19+
expect(filter).toEqual(expected);
20+
}
21+
);
22+
23+
it('should throw an error for unsupported filter types', () => {
24+
expect(() => makePouchFilter('unsupported', 'value')).toThrow(
25+
'Unsupported type: unsupported'
26+
);
27+
});
28+
});
29+
});

packages/pouch-storage/src/PouchStorageTable.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,27 @@ export type PouchDBSort = Array<
3535
string | { [propName: string]: 'asc' | 'desc' }
3636
>;
3737

38-
// We may want to use `PouchDB.Find.ConditionOperators` instead of this, but
39-
// there are some mismatches in how we use this with the types.
40-
// https://github.com/deephaven/web-client-ui/issues/1505 to address this
41-
type PouchFilter = OnlyOneProp<{
42-
$eq: FilterValue | FilterValue[];
43-
$neq: FilterValue | FilterValue[];
44-
$gt: FilterValue | FilterValue[];
45-
$gte: FilterValue | FilterValue[];
46-
$lt: FilterValue | FilterValue[];
47-
$lte: FilterValue | FilterValue[];
48-
$regex: RegExp;
49-
}>;
50-
51-
function makePouchFilter(
38+
type PouchFilter = OnlyOneProp<
39+
Pick<
40+
PouchDB.Find.ConditionOperators,
41+
'$eq' | '$ne' | '$gt' | '$gte' | '$lt' | '$lte' | '$regex'
42+
>
43+
>;
44+
45+
export function makePouchFilter(
5246
type: string,
5347
value: FilterValue | FilterValue[]
5448
): PouchFilter {
5549
switch (type) {
5650
case FilterType.in:
5751
case FilterType.contains:
58-
return { $regex: new RegExp(`${value}`) };
52+
return { $regex: new RegExp(`${value}`).toString() };
5953
case FilterType.inIgnoreCase:
60-
return { $regex: new RegExp(`${value}`, 'i') };
54+
return { $regex: new RegExp(`${value}`, 'i').toString() };
6155
case FilterType.eq:
6256
return { $eq: value };
6357
case FilterType.notEq:
64-
// This should be `$ne` https://github.com/deephaven/web-client-ui/issues/1505
65-
return { $neq: value };
58+
return { $ne: value };
6659
case FilterType.greaterThan:
6760
return { $gt: value };
6861
case FilterType.greaterThanOrEqualTo:
@@ -72,7 +65,7 @@ function makePouchFilter(
7265
case FilterType.lessThanOrEqualTo:
7366
return { $lte: value };
7467
case FilterType.startsWith:
75-
return { $regex: new RegExp(`^(?${value}).*`) };
68+
return { $regex: new RegExp(`^${value}.*`).toString() };
7669
default:
7770
throw new Error(`Unsupported type: ${type}`);
7871
}

0 commit comments

Comments
 (0)