@balena/odata-to-abstract-sql
Advanced tools
Comparing version 5.2.0-named-keys-1aa78f8bcec04b0434bf381e3beb7e4558ce2e3a to 5.2.0-named-keys-221412fc66f6b3906424f850c59e5fd002782c0c
@@ -7,6 +7,14 @@ # Change Log | ||
## 5.2.0 - 2020-07-17 | ||
## 5.2.0 - 2020-07-20 | ||
* Add support for specifying named keys [Pagan Gazzard] | ||
<details> | ||
<summary> Add support for specifying named keys [Pagan Gazzard] </summary> | ||
> ### odata-parser-2.1.0 - 2020-07-10 | ||
> | ||
> * Add support for specifying named keys [Pagan Gazzard] | ||
> | ||
</details> | ||
## 5.1.1 - 2020-07-10 | ||
@@ -13,0 +21,0 @@ |
@@ -411,9 +411,10 @@ "use strict"; | ||
PathKey(method, path, resource, bodyKeys) { | ||
if (path.key != null) { | ||
const { key } = path; | ||
if (key != null) { | ||
if (method === 'PUT' || method === 'PUT-INSERT' || method === 'POST') { | ||
if (isBindReference(path.key)) { | ||
addBodyKey(resource.resourceName, resource.idField, path.key, bodyKeys, this.extraBodyVars); | ||
if (isBindReference(key)) { | ||
addBodyKey(resource.resourceName, resource.idField, key, bodyKeys, this.extraBodyVars); | ||
} | ||
else { | ||
for (const [fieldName, bind] of Object.entries(path.key)) { | ||
for (const [fieldName, bind] of Object.entries(key)) { | ||
addBodyKey(resource.resourceName, fieldName, bind, bodyKeys, this.extraBodyVars); | ||
@@ -423,4 +424,4 @@ } | ||
} | ||
if (isBindReference(path.key)) { | ||
const key = this.Bind(path.key); | ||
if (isBindReference(key)) { | ||
const bind = this.Bind(key); | ||
const referencedField = [ | ||
@@ -431,13 +432,27 @@ 'ReferencedField', | ||
]; | ||
return [comparison.eq, referencedField, key]; | ||
return [comparison.eq, referencedField, bind]; | ||
} | ||
const namedKeys = Object.entries(path.key).map(([fieldName, bind]) => { | ||
const key = this.Bind(bind); | ||
const referencedField = [ | ||
'ReferencedField', | ||
resource.tableAlias, | ||
fieldName, | ||
]; | ||
return [comparison.eq, referencedField, key]; | ||
const fieldNames = Object.keys(key); | ||
const sqlFieldNames = fieldNames.map(exports.odataNameToSqlName).sort(); | ||
const fields = sqlFieldNames.map((fieldName) => { | ||
const resourceField = resource.fields.find((f) => f.fieldName === fieldName); | ||
if (resourceField == null) { | ||
throw new SyntaxError('Specified non-existent field for path key'); | ||
} | ||
return resourceField; | ||
}); | ||
if (!(fields.length === 1 && | ||
(fields[0].index === 'UNIQUE' || fields[0].index === 'PRIMARY KEY')) && | ||
!resource.indexes.some((index) => { | ||
return ((index.type === 'UNIQUE' || index.type === 'PRIMARY KEY') && | ||
sqlFieldNames.length === index.fields.length && | ||
_.isEqual(index.fields.slice().sort(), sqlFieldNames)); | ||
})) { | ||
throw new SyntaxError('Specified fields for path key that are not directly unique'); | ||
} | ||
const namedKeys = fieldNames.map((fieldName) => { | ||
const bind = this.Bind(key[fieldName]); | ||
const referencedField = this.ReferencedField(resource, fieldName); | ||
return [comparison.eq, referencedField, bind]; | ||
}); | ||
if (namedKeys.length === 1) { | ||
@@ -444,0 +459,0 @@ return namedKeys[0]; |
{ | ||
"name": "@balena/odata-to-abstract-sql", | ||
"version": "5.2.0-named-keys-1aa78f8bcec04b0434bf381e3beb7e4558ce2e3a", | ||
"version": "5.2.0-named-keys-221412fc66f6b3906424f850c59e5fd002782c0c", | ||
"description": "A consumer of the OData parser, written in OMeta", | ||
@@ -21,3 +21,3 @@ "main": "out/odata-to-abstract-sql.js", | ||
"@balena/abstract-sql-compiler": "^7.0.2", | ||
"@balena/odata-parser": "^2.1.0-named-keys-926615afa77b3c67db8429a03ac5437838fbf939", | ||
"@balena/odata-parser": "^2.1.0", | ||
"@types/lodash": "^4.14.157", | ||
@@ -24,0 +24,0 @@ "@types/memoizee": "^0.4.4", |
@@ -600,9 +600,10 @@ import * as _ from 'lodash'; | ||
): BooleanTypeNodes | void { | ||
if (path.key != null) { | ||
const { key } = path; | ||
if (key != null) { | ||
if (method === 'PUT' || method === 'PUT-INSERT' || method === 'POST') { | ||
if (isBindReference(path.key)) { | ||
if (isBindReference(key)) { | ||
addBodyKey( | ||
resource.resourceName, | ||
resource.idField, | ||
path.key, | ||
key, | ||
bodyKeys, | ||
@@ -612,3 +613,3 @@ this.extraBodyVars, | ||
} else { | ||
for (const [fieldName, bind] of Object.entries(path.key)) { | ||
for (const [fieldName, bind] of Object.entries(key)) { | ||
addBodyKey( | ||
@@ -624,4 +625,4 @@ resource.resourceName, | ||
} | ||
if (isBindReference(path.key)) { | ||
const key = this.Bind(path.key); | ||
if (isBindReference(key)) { | ||
const bind = this.Bind(key); | ||
const referencedField: ReferencedFieldNode = [ | ||
@@ -632,13 +633,42 @@ 'ReferencedField', | ||
]; | ||
return [comparison.eq, referencedField, key]; | ||
return [comparison.eq, referencedField, bind]; | ||
} | ||
const namedKeys = Object.entries(path.key).map( | ||
([fieldName, bind]): BooleanTypeNodes => { | ||
const key = this.Bind(bind); | ||
const referencedField: ReferencedFieldNode = [ | ||
'ReferencedField', | ||
resource.tableAlias!, | ||
const fieldNames = Object.keys(key); | ||
const sqlFieldNames = fieldNames.map(odataNameToSqlName).sort(); | ||
const fields = sqlFieldNames.map((fieldName) => { | ||
const resourceField = resource.fields.find( | ||
(f) => f.fieldName === fieldName, | ||
); | ||
if (resourceField == null) { | ||
throw new SyntaxError('Specified non-existent field for path key'); | ||
} | ||
return resourceField; | ||
}); | ||
if ( | ||
!( | ||
fields.length === 1 && | ||
(fields[0].index === 'UNIQUE' || fields[0].index === 'PRIMARY KEY') | ||
) && | ||
!resource.indexes.some((index) => { | ||
return ( | ||
(index.type === 'UNIQUE' || index.type === 'PRIMARY KEY') && | ||
sqlFieldNames.length === index.fields.length && | ||
_.isEqual(index.fields.slice().sort(), sqlFieldNames) | ||
); | ||
}) | ||
) { | ||
throw new SyntaxError( | ||
'Specified fields for path key that are not directly unique', | ||
); | ||
} | ||
const namedKeys = fieldNames.map( | ||
(fieldName): BooleanTypeNodes => { | ||
const bind = this.Bind(key[fieldName]); | ||
const referencedField: ReferencedFieldNode = this.ReferencedField( | ||
resource, | ||
fieldName, | ||
]; | ||
return [comparison.eq, referencedField, key]; | ||
); | ||
return [comparison.eq, referencedField, bind]; | ||
}, | ||
@@ -645,0 +675,0 @@ ); |
Sorry, the diff of this file is not supported yet
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
380936
2938
Updated@balena/odata-parser@^2.1.0