Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: PouchStorageTable using incorrect $ne operator #2011

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 additions & 0 deletions packages/pouch-storage/src/PouchStorageTable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { makePouchFilter } from './PouchStorageTable';

describe('PouchStorageTable - Filter Functions', () => {
describe('makePouchFilter', () => {
it.each([
AkshatJawne marked this conversation as resolved.
Show resolved Hide resolved
['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'
);
});
});
});
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(
AkshatJawne marked this conversation as resolved.
Show resolved Hide resolved
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
Loading