Skip to content

Commit

Permalink
feat: add EqualsAny as an operand type
Browse files Browse the repository at this point in the history
Update @balena/abstract-sql-compiler from 10.0.1 to 10.2.0
Update @balena/odata-parser from 3.1.0 to 4.1.0

Change-type: minor
  • Loading branch information
otaviojacobi committed Feb 11, 2025
1 parent 31da05d commit 4cbc758
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"author": "",
"license": "BSD",
"dependencies": {
"@balena/abstract-sql-compiler": "^10.0.1",
"@balena/odata-parser": "^3.1.0",
"@balena/abstract-sql-compiler": "^10.2.0",
"@balena/odata-parser": "^4.1.0",
"@types/lodash": "^4.17.10",
"@types/memoizee": "^0.4.11",
"@types/string-hash": "^1.1.3",
Expand Down
6 changes: 5 additions & 1 deletion src/odata-to-abstract-sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import type {
IsDistinctFromNode,
UnknownTypeNodes,
FromTypeNode,
EqualsAnyNode,
} from '@balena/abstract-sql-compiler';
import type {
ODataBinds,
Expand Down Expand Up @@ -136,6 +137,7 @@ export type ResourceFunction = (

const comparison = {
eq: 'IsNotDistinctFrom',
eqany: 'EqualsAny',
ne: 'IsDistinctFrom',
gt: 'GreaterThan',
ge: 'GreaterThanOrEqual',
Expand Down Expand Up @@ -1142,6 +1144,7 @@ export class OData2AbstractSQL {
const [type, ...rest] = match;
switch (type) {
case 'eq':
case 'eqany':
case 'ne':
case 'gt':
case 'ge':
Expand All @@ -1155,7 +1158,8 @@ export class OData2AbstractSQL {
| GreaterThanNode
| GreaterThanOrEqualNode
| LessThanNode
| LessThanOrEqualNode;
| LessThanOrEqualNode
| EqualsAnyNode;
}
case 'and':
case 'or':
Expand Down
34 changes: 31 additions & 3 deletions test/filterby.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const sqlOps = {
mul: 'Multiply',
div: 'Divide',
in: 'In',
eqany: 'EqualsAny',
};

const methodMaps = {
Expand All @@ -73,9 +74,7 @@ const createExpression = function (lhs, op, rhs) {
if (op === 'in') {
return {
odata: operandToOData(lhs) + ' ' + op + ' ' + operandToOData(rhs),
abstractsql: [sqlOps[op], operandToAbstractSQL(lhs)].concat(
operandToAbstractSQL(rhs),
),
abstractsql: [sqlOps['eqany'], operandToAbstractSQL(lhs), ['Bind', 0]],
};
}
if (rhs == null) {
Expand Down Expand Up @@ -148,6 +147,35 @@ const operandTest = (lhs, op, rhs) =>
);
});

test('/pilot?$filter=name in (null)', (result) => {
it('should be able to select with list with a single null', () => {
expect(result)
.to.be.a.query.that.selects(pilotFields)
.from('pilot')
.where(['EqualsAny', ['ReferencedField', 'pilot', 'name'], ['Bind', 0]]);
});
});

test(`/pilot?$filter=name eq 'test' or name in (null, 1) or name in (null) or startswith(name,'test1') or name in (1,2,3)`, (result) => {
it('should be able to select with multiple conditions', () => {
expect(result)
.to.be.a.query.that.selects(pilotFields)
.from('pilot')
.where([
'Or',
[
'IsNotDistinctFrom',
['ReferencedField', 'pilot', 'name'],
['Bind', 0],
],
['EqualsAny', ['ReferencedField', 'pilot', 'name'], ['Bind', 1]],
['EqualsAny', ['ReferencedField', 'pilot', 'name'], ['Bind', 2]],
['Startswith', ['ReferencedField', 'pilot', 'name'], ['Bind', 3]],
['EqualsAny', ['ReferencedField', 'pilot', 'name'], ['Bind', 4]],
]);
});
});

const navigatedOperandTest = (lhs, op, rhs) =>
run(function () {
const { odata, abstractsql } = createExpression(lhs, op, rhs);
Expand Down

0 comments on commit 4cbc758

Please sign in to comment.