@balena/odata-to-abstract-sql
Advanced tools
Comparing version 6.2.9-build-renovate-major-9--balenasbvr-types-86b247ac771c744c0a80140a1208c7520adff796-1 to 6.3.0-build-patch-delete-use-db-id-wo-translating-65b06761fca1b13e13ec99c4ed5394814feff4ef-1
@@ -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,37 @@ "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)) { | ||
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]); | ||
}; | ||
class Query { | ||
@@ -69,2 +104,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 +477,4 @@ const compiled = []; | ||
const subQuery = new Query(); | ||
subQuery.select.push(referencedIdField); | ||
subQuery.fromResource(this, resource); | ||
subQuery.addNestedFieldSelect(resource.idField, '$modifyid'); | ||
if (hasQueryOpts) { | ||
@@ -440,0 +481,0 @@ this.AddQueryOptions(resource, path, subQuery); |
{ | ||
"name": "@balena/odata-to-abstract-sql", | ||
"version": "6.2.9-build-renovate-major-9--balenasbvr-types-86b247ac771c744c0a80140a1208c7520adff796-1", | ||
"version": "6.3.0-build-patch-delete-use-db-id-wo-translating-65b06761fca1b13e13ec99c4ed5394814feff4ef-1", | ||
"description": "A consumer of the OData parser, written in OMeta", | ||
@@ -32,3 +32,3 @@ "type": "commonjs", | ||
"@balena/sbvr-parser": "^1.4.6", | ||
"@balena/sbvr-types": "^9.0.0", | ||
"@balena/sbvr-types": "^7.1.3", | ||
"@types/chai": "^4.3.18", | ||
@@ -58,4 +58,4 @@ "@types/chai-things": "0.0.38", | ||
"versionist": { | ||
"publishedAt": "2024-08-26T13:46:14.424Z" | ||
"publishedAt": "2024-08-26T16:42:16.123Z" | ||
} | ||
} |
@@ -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,50 @@ 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)) { | ||
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]); | ||
}; | ||
class Query { | ||
@@ -221,2 +272,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 +783,4 @@ compile(queryType: 'InsertQuery'): InsertQueryNode; | ||
const subQuery = new Query(); | ||
subQuery.select.push(referencedIdField); | ||
subQuery.fromResource(this, resource); | ||
subQuery.addNestedFieldSelect(resource.idField, '$modifyid'); | ||
if (hasQueryOpts) { | ||
@@ -728,0 +787,0 @@ this.AddQueryOptions(resource, path, subQuery); |
@@ -481,3 +481,3 @@ import { expect } from 'chai'; | ||
'SelectQuery', | ||
['Select', [['ReferencedField', 'pilot', 'id']]], | ||
['Select', [['Alias', ['ReferencedField', 'pilot', 'id'], '$modifyid']]], | ||
['From', ['Table', 'pilot']], | ||
@@ -546,3 +546,3 @@ [ | ||
}); | ||
return it('and updates', () => | ||
it('and updates', () => { | ||
expect(result[2]) | ||
@@ -578,7 +578,8 @@ .to.be.a.query.that.updates.fields( | ||
.from('pilot') | ||
.where(updateWhere)); | ||
.where(updateWhere); | ||
}); | ||
}), | ||
); | ||
return test('/pilot?$filter=' + odata, 'DELETE', (result) => | ||
test('/pilot?$filter=' + odata, 'DELETE', (result) => | ||
it('should delete from pilot where "' + odata + '"', () => { | ||
@@ -592,3 +593,6 @@ expect(result) | ||
'SelectQuery', | ||
['Select', [['ReferencedField', 'pilot', 'id']]], | ||
[ | ||
'Select', | ||
[['Alias', ['ReferencedField', 'pilot', 'id'], '$modifyid']], | ||
], | ||
['From', ['Table', 'pilot']], | ||
@@ -728,3 +732,6 @@ [ | ||
'SelectQuery', | ||
['Select', [['ReferencedField', 'pilot', 'id']]], | ||
[ | ||
'Select', | ||
[['Alias', ['ReferencedField', 'pilot', 'id'], '$modifyid']], | ||
], | ||
['From', ['Table', 'pilot']], | ||
@@ -752,3 +759,3 @@ ['Where', abstractsql], | ||
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 () { | ||
@@ -760,3 +767,3 @@ it('should be an upsert', () => | ||
}); | ||
return it('and updates', () => { | ||
it('and updates', () => { | ||
expect(result[2]) | ||
@@ -1521,3 +1528,3 @@ .to.be.a.query.that.updates.fields( | ||
'SelectQuery', | ||
['Select', [['ReferencedField', 'copilot', 'id']]], | ||
['Select', [['ReferencedField', 'copilot', '$modifyid']]], | ||
[ | ||
@@ -1535,2 +1542,7 @@ 'From', | ||
['Alias', ['Text', 'Junior'], 'rank'], | ||
[ | ||
'Alias', | ||
['ReferencedField', 'copilot', 'id'], | ||
'$modifyid', | ||
], | ||
], | ||
@@ -1559,1 +1571,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', '$modifyid']]], | ||
[ | ||
'From', | ||
[ | ||
'Alias', | ||
[ | ||
'SelectQuery', | ||
[ | ||
'Select', | ||
[ | ||
['Field', '*'], | ||
['Alias', ['Boolean', false], 'is blocked'], | ||
['Alias', ['Text', 'Junior'], 'rank'], | ||
[ | ||
'Alias', | ||
['ReferencedField', 'copilot', 'id'], | ||
'$modifyid', | ||
], | ||
], | ||
], | ||
['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'], '$modifyid']], | ||
], | ||
['From', ['Table', 'pilot']], | ||
@@ -56,3 +59,6 @@ ['Limit', ['Number', 5]], | ||
'SelectQuery', | ||
['Select', [['ReferencedField', 'pilot', 'id']]], | ||
[ | ||
'Select', | ||
[['Alias', ['ReferencedField', 'pilot', 'id'], '$modifyid']], | ||
], | ||
['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 too big to display
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
620539
7101