@balena/odata-to-abstract-sql
Advanced tools
Comparing version 6.2.8-build-renovate-major-9--balenasbvr-types-cb6ecf0bc2bfe70e632ac40538e3b5029813b72d-1 to 6.3.0-build-patch-delete-use-db-id-wo-translating-f7e0ab275cf2b108cf8e5a81ac325e1c9f903b82-1
@@ -7,23 +7,6 @@ # Change Log | ||
## 6.2.8 - 2024-08-08 | ||
## 6.3.0 - 2024-08-26 | ||
* Support PUT/PATCH/DELETE requests on resources with a translated ID field [Thodoris Greasidis] | ||
<details> | ||
<summary> Update dependency @balena/sbvr-types to v9 [Self-hosted Renovate Bot] </summary> | ||
> ### sbvr-types-9.0.1 - 2024-08-05 | ||
> | ||
> * Update @balena/lint to v8.2.7 [Josh Bowling] | ||
> | ||
> ### sbvr-types-9.0.0 - 2024-08-02 | ||
> | ||
> * Update BigInteger and BigSerial TsTypes [Josh Bowling] | ||
> | ||
> ### sbvr-types-8.0.0 - 2024-06-12 | ||
> | ||
> * Improve BigInteger and BigSerial support [Josh Bowling] | ||
> | ||
</details> | ||
## 6.2.7 - 2024-04-23 | ||
@@ -30,0 +13,0 @@ |
@@ -44,2 +44,3 @@ import * as memoize from 'memoizee'; | ||
}, bypassDefinition?: boolean, isModifyOperation?: boolean): void; | ||
addNestedFieldSelect(fieldName: string, fieldNameAlias: string): void; | ||
compile(queryType: 'SelectQuery'): SelectQueryNode; | ||
@@ -46,0 +47,0 @@ compile(queryType: 'InsertQuery'): InsertQueryNode; |
@@ -51,2 +51,41 @@ "use strict"; | ||
}; | ||
const addNestedFieldSelect = (selectNode, fromNode, fieldName, fieldNameAlias) => { | ||
let aliasName; | ||
let tableOrSubqueryNode; | ||
if ((0, abstract_sql_compiler_1.isAliasNode)(fromNode)) { | ||
tableOrSubqueryNode = fromNode[1]; | ||
aliasName = fromNode[2]; | ||
} | ||
else { | ||
tableOrSubqueryNode = fromNode; | ||
if (!(0, abstract_sql_compiler_1.isTableNode)(tableOrSubqueryNode)) { | ||
throw new Error(''); | ||
} | ||
} | ||
if ((0, abstract_sql_compiler_1.isTableNode)(tableOrSubqueryNode)) { | ||
selectNode.push([ | ||
'Alias', | ||
['ReferencedField', aliasName ?? tableOrSubqueryNode[1], fieldName], | ||
fieldNameAlias, | ||
]); | ||
return; | ||
} | ||
if (!(0, abstract_sql_compiler_1.isSelectQueryNode)(tableOrSubqueryNode)) { | ||
throw new Error(`Adding a nested field select to a subquery containing a ${tableOrSubqueryNode[0]} is not supported`); | ||
} | ||
if (aliasName == null) { | ||
throw new Error('Found unaliased SelectQueryNode'); | ||
} | ||
const nestedSelectNode = tableOrSubqueryNode.find(abstract_sql_compiler_1.isSelectNode); | ||
if (nestedSelectNode == null) { | ||
throw new Error(`Cannot find SelectNode in subquery`); | ||
} | ||
const nestedFromNode = tableOrSubqueryNode.find(abstract_sql_compiler_1.isFromNode); | ||
if (nestedFromNode == null) { | ||
throw new Error(`Cannot find FromNode in subquery`); | ||
} | ||
addNestedFieldSelect(nestedSelectNode[1], nestedFromNode[1], fieldName, fieldNameAlias); | ||
selectNode.push(['ReferencedField', aliasName, fieldNameAlias]); | ||
return; | ||
}; | ||
class Query { | ||
@@ -69,2 +108,8 @@ constructor() { | ||
} | ||
addNestedFieldSelect(fieldName, fieldNameAlias) { | ||
if (this.from.length !== 1) { | ||
throw new Error(`Adding nested field SELECTs is only supported for queries with exactly 1 FROM clause. Found ${this.from.length}`); | ||
} | ||
addNestedFieldSelect(this.select, this.from[0], fieldName, fieldNameAlias); | ||
} | ||
compile(queryType) { | ||
@@ -436,4 +481,8 @@ const compiled = []; | ||
const subQuery = new Query(); | ||
subQuery.select.push(referencedIdField); | ||
subQuery.fromResource(this, resource); | ||
subQuery.addNestedFieldSelect(resource.idField, method === 'PATCH' | ||
? '$updateid' | ||
: method === 'DELETE' | ||
? '$deleteid' | ||
: '$upsertid'); | ||
if (hasQueryOpts) { | ||
@@ -440,0 +489,0 @@ this.AddQueryOptions(resource, path, subQuery); |
{ | ||
"name": "@balena/odata-to-abstract-sql", | ||
"version": "6.2.8-build-renovate-major-9--balenasbvr-types-cb6ecf0bc2bfe70e632ac40538e3b5029813b72d-1", | ||
"version": "6.3.0-build-patch-delete-use-db-id-wo-translating-f7e0ab275cf2b108cf8e5a81ac325e1c9f903b82-1", | ||
"description": "A consumer of the OData parser, written in OMeta", | ||
@@ -31,3 +31,3 @@ "main": "out/odata-to-abstract-sql.js", | ||
"@balena/sbvr-parser": "^1.4.3", | ||
"@balena/sbvr-types": "^9.0.0", | ||
"@balena/sbvr-types": "^7.0.1", | ||
"@types/chai": "^4.3.11", | ||
@@ -57,4 +57,4 @@ "@types/chai-things": "0.0.38", | ||
"versionist": { | ||
"publishedAt": "2024-08-08T08:47:31.410Z" | ||
"publishedAt": "2024-08-26T10:30:57.642Z" | ||
} | ||
} |
@@ -7,2 +7,4 @@ import * as _ from 'lodash'; | ||
isFromNode, | ||
isSelectNode, | ||
isSelectQueryNode, | ||
isTableNode, | ||
@@ -73,2 +75,3 @@ } from '@balena/abstract-sql-compiler'; | ||
UnknownTypeNodes, | ||
FromTypeNode, | ||
} from '@balena/abstract-sql-compiler'; | ||
@@ -181,2 +184,54 @@ import type { | ||
const addNestedFieldSelect = ( | ||
selectNode: SelectNode[1], | ||
fromNode: FromNode[1], | ||
fieldName: string, | ||
fieldNameAlias: string, | ||
) => { | ||
let aliasName: string | undefined; | ||
let tableOrSubqueryNode: FromTypeNode[keyof FromTypeNode]; | ||
if (isAliasNode(fromNode)) { | ||
tableOrSubqueryNode = fromNode[1]; | ||
aliasName = fromNode[2]; | ||
} else { | ||
tableOrSubqueryNode = fromNode; | ||
if (!isTableNode(tableOrSubqueryNode)) { | ||
throw new Error(''); | ||
} | ||
} | ||
if (isTableNode(tableOrSubqueryNode)) { | ||
selectNode.push([ | ||
'Alias', | ||
['ReferencedField', aliasName ?? tableOrSubqueryNode[1], fieldName], | ||
fieldNameAlias, | ||
]); | ||
return; | ||
} | ||
if (!isSelectQueryNode(tableOrSubqueryNode)) { | ||
throw new Error( | ||
`Adding a nested field select to a subquery containing a ${tableOrSubqueryNode[0]} is not supported`, | ||
); | ||
} | ||
if (aliasName == null) { | ||
// This should never happen but we are checking it to make TS happy. | ||
throw new Error('Found unaliased SelectQueryNode'); | ||
} | ||
const nestedSelectNode = tableOrSubqueryNode.find(isSelectNode); | ||
if (nestedSelectNode == null) { | ||
throw new Error(`Cannot find SelectNode in subquery`); | ||
} | ||
const nestedFromNode = tableOrSubqueryNode.find(isFromNode); | ||
if (nestedFromNode == null) { | ||
throw new Error(`Cannot find FromNode in subquery`); | ||
} | ||
addNestedFieldSelect( | ||
nestedSelectNode[1], | ||
nestedFromNode[1], | ||
fieldName, | ||
fieldNameAlias, | ||
); | ||
selectNode.push(['ReferencedField', aliasName, fieldNameAlias]); | ||
return; | ||
}; | ||
class Query { | ||
@@ -221,2 +276,10 @@ public select: Array< | ||
} | ||
addNestedFieldSelect(fieldName: string, fieldNameAlias: string): void { | ||
if (this.from.length !== 1) { | ||
throw new Error( | ||
`Adding nested field SELECTs is only supported for queries with exactly 1 FROM clause. Found ${this.from.length}`, | ||
); | ||
} | ||
addNestedFieldSelect(this.select, this.from[0], fieldName, fieldNameAlias); | ||
} | ||
compile(queryType: 'SelectQuery'): SelectQueryNode; | ||
@@ -724,4 +787,11 @@ compile(queryType: 'InsertQuery'): InsertQueryNode; | ||
const subQuery = new Query(); | ||
subQuery.select.push(referencedIdField); | ||
subQuery.fromResource(this, resource); | ||
subQuery.addNestedFieldSelect( | ||
resource.idField, | ||
method === 'PATCH' | ||
? '$updateid' | ||
: method === 'DELETE' | ||
? '$deleteid' | ||
: '$upsertid', | ||
); | ||
if (hasQueryOpts) { | ||
@@ -728,0 +798,0 @@ this.AddQueryOptions(resource, path, subQuery); |
@@ -65,2 +65,8 @@ import { expect } from 'chai'; | ||
const methodToWhereClauseIdMap = { | ||
PATCH: '$updateid', | ||
PUT: '$upsertid', | ||
DELETE: '$deleteid', | ||
}; | ||
const createExpression = function (lhs, op, rhs) { | ||
@@ -477,3 +483,6 @@ if (lhs === 'not') { | ||
}; | ||
const updateWhere = [ | ||
const getUpdateWhere = ( | ||
/** @type keyof typeof methodToWhereClauseIdMap */ method, | ||
) => [ | ||
'In', | ||
@@ -483,3 +492,12 @@ ['ReferencedField', 'pilot', 'id'], | ||
'SelectQuery', | ||
['Select', [['ReferencedField', 'pilot', 'id']]], | ||
[ | ||
'Select', | ||
[ | ||
[ | ||
'Alias', | ||
['ReferencedField', 'pilot', 'id'], | ||
methodToWhereClauseIdMap[method], | ||
], | ||
], | ||
], | ||
['From', ['Table', 'pilot']], | ||
@@ -530,3 +548,3 @@ [ | ||
.from('pilot') | ||
.where(updateWhere); | ||
.where(getUpdateWhere('PATCH')); | ||
}), | ||
@@ -536,3 +554,5 @@ ); | ||
test(`/pilot?$filter=${odata}`, 'POST', { name: 'Peter' }, (result) => | ||
it(`should insert pilot where '${odata}'`, () => insertTest(result)), | ||
it(`should insert pilot where '${odata}'`, () => { | ||
insertTest(result); | ||
}), | ||
); | ||
@@ -545,4 +565,6 @@ | ||
}); | ||
it('that inserts', () => insertTest(result[1])); | ||
return it('and updates', () => | ||
it('that inserts', () => { | ||
insertTest(result[1]); | ||
}); | ||
it('and updates', () => { | ||
expect(result[2]) | ||
@@ -578,7 +600,8 @@ .to.be.a.query.that.updates.fields( | ||
.from('pilot') | ||
.where(updateWhere)); | ||
.where(getUpdateWhere('PUT')); | ||
}); | ||
}), | ||
); | ||
return test('/pilot?$filter=' + odata, 'DELETE', (result) => | ||
test('/pilot?$filter=' + odata, 'DELETE', (result) => | ||
it('should delete from pilot where "' + odata + '"', () => { | ||
@@ -592,3 +615,6 @@ expect(result) | ||
'SelectQuery', | ||
['Select', [['ReferencedField', 'pilot', 'id']]], | ||
[ | ||
'Select', | ||
[['Alias', ['ReferencedField', 'pilot', 'id'], '$deleteid']], | ||
], | ||
['From', ['Table', 'pilot']], | ||
@@ -720,3 +746,6 @@ [ | ||
}; | ||
const updateWhere = [ | ||
const getUpdateWhere = ( | ||
/** @type keyof typeof methodToWhereClauseIdMap */ method, | ||
) => [ | ||
'And', | ||
@@ -729,3 +758,12 @@ ['IsNotDistinctFrom', ['ReferencedField', 'pilot', 'id'], ['Bind', 0]], | ||
'SelectQuery', | ||
['Select', [['ReferencedField', 'pilot', 'id']]], | ||
[ | ||
'Select', | ||
[ | ||
[ | ||
'Alias', | ||
['ReferencedField', 'pilot', 'id'], | ||
methodToWhereClauseIdMap[method], | ||
], | ||
], | ||
], | ||
['From', ['Table', 'pilot']], | ||
@@ -738,5 +776,5 @@ ['Where', abstractsql], | ||
test('/pilot(1)?$filter=' + odata, 'POST', { name }, (result) => | ||
it('should insert into pilot where "' + odata + '"', () => | ||
insertTest(result), | ||
), | ||
it('should insert into pilot where "' + odata + '"', () => { | ||
insertTest(result); | ||
}), | ||
); | ||
@@ -750,12 +788,14 @@ | ||
.from('pilot') | ||
.where(updateWhere); | ||
.where(getUpdateWhere('PATCH')); | ||
}), | ||
); | ||
return test('/pilot(1)?$filter=' + odata, 'PUT', { name }, (result) => | ||
test('/pilot(1)?$filter=' + odata, 'PUT', { name }, (result) => | ||
describe('should upsert the pilot with id 1', function () { | ||
it('should be an upsert', () => | ||
expect(result).to.be.a.query.that.upserts); | ||
it('that inserts', () => insertTest(result[1])); | ||
return it('and updates', () => { | ||
it('that inserts', () => { | ||
insertTest(result[1]); | ||
}); | ||
it('and updates', () => { | ||
expect(result[2]) | ||
@@ -791,3 +831,3 @@ .to.be.a.query.that.updates.fields( | ||
.from('pilot') | ||
.where(updateWhere); | ||
.where(getUpdateWhere('PUT')); | ||
}); | ||
@@ -1521,3 +1561,3 @@ }), | ||
'SelectQuery', | ||
['Select', [['ReferencedField', 'copilot', 'id']]], | ||
['Select', [['ReferencedField', 'copilot', '$updateid']]], | ||
[ | ||
@@ -1535,2 +1575,7 @@ 'From', | ||
['Alias', ['Text', 'Junior'], 'rank'], | ||
[ | ||
'Alias', | ||
['ReferencedField', 'copilot', 'id'], | ||
'$updateid', | ||
], | ||
], | ||
@@ -1559,1 +1604,57 @@ ], | ||
); | ||
test( | ||
`/copilot?$select=id,rank&$filter=rank eq 'major'`, | ||
'DELETE', | ||
{ assists__pilot: 1 }, | ||
(result) => | ||
it(`should DELETE copilot based on filtered computed field rank`, () => { | ||
expect(result).to.be.a.query.to.deep.equal([ | ||
'DeleteQuery', | ||
['From', ['Table', 'copilot']], | ||
[ | ||
'Where', | ||
[ | ||
'In', | ||
['ReferencedField', 'copilot', 'id'], | ||
[ | ||
'SelectQuery', | ||
['Select', [['ReferencedField', 'copilot', '$deleteid']]], | ||
[ | ||
'From', | ||
[ | ||
'Alias', | ||
[ | ||
'SelectQuery', | ||
[ | ||
'Select', | ||
[ | ||
['Field', '*'], | ||
['Alias', ['Boolean', false], 'is blocked'], | ||
['Alias', ['Text', 'Junior'], 'rank'], | ||
[ | ||
'Alias', | ||
['ReferencedField', 'copilot', 'id'], | ||
'$deleteid', | ||
], | ||
], | ||
], | ||
['From', ['Table', 'copilot']], | ||
], | ||
'copilot', | ||
], | ||
], | ||
[ | ||
'Where', | ||
[ | ||
'IsNotDistinctFrom', | ||
['ReferencedField', 'copilot', 'rank'], | ||
['Bind', 0], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]); | ||
}), | ||
); |
@@ -39,3 +39,6 @@ import { expect } from 'chai'; | ||
'SelectQuery', | ||
['Select', [['ReferencedField', 'pilot', 'id']]], | ||
[ | ||
'Select', | ||
[['Alias', ['ReferencedField', 'pilot', 'id'], '$updateid']], | ||
], | ||
['From', ['Table', 'pilot']], | ||
@@ -56,3 +59,6 @@ ['Limit', ['Number', 5]], | ||
'SelectQuery', | ||
['Select', [['ReferencedField', 'pilot', 'id']]], | ||
[ | ||
'Select', | ||
[['Alias', ['ReferencedField', 'pilot', 'id'], '$deleteid']], | ||
], | ||
['From', ['Table', 'pilot']], | ||
@@ -59,0 +65,0 @@ ['Limit', ['Number', 5]], |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
603631
27
7148