@balena/odata-to-abstract-sql
Advanced tools
Comparing version 5.3.2 to 5.4.0-official-table-definitions-e7956b90d4f5bfbbd57f288f9367db3876a8b216
@@ -7,4 +7,5 @@ # Change Log | ||
## 5.3.2 - 2020-12-18 | ||
## 5.4.0 - 2020-12-18 | ||
* Add support for official abstract-sql table definitions [Pagan Gazzard] | ||
* Avoid trying to rewrite binds if there are none to rewrite [Pagan Gazzard] | ||
@@ -11,0 +12,0 @@ |
import * as _ from 'lodash'; | ||
import * as memoize from 'memoizee'; | ||
import type { AbstractSqlQuery, AbstractSqlModel, AbstractSqlTable, DurationNode, AbstractSqlType, SelectNode, FromNode, WhereNode, OrderByNode, LimitNode, OffsetNode, NumberTypeNodes, FieldsNode, ValuesNode, ReferencedFieldNode, BooleanTypeNodes, UnionQueryNode, SelectQueryNode, BindNode, TableNode } from '@balena/abstract-sql-compiler'; | ||
import type { AbstractSqlQuery, AbstractSqlModel, AbstractSqlTable, DurationNode, AbstractSqlType, SelectNode, FromNode, WhereNode, OrderByNode, LimitNode, OffsetNode, NumberTypeNodes, FieldsNode, ValuesNode, ReferencedFieldNode, BooleanTypeNodes, SelectQueryNode, BindNode, TableNode, Definition as ModernDefinition, ResourceNode, UnionQueryNode } from '@balena/abstract-sql-compiler'; | ||
import type { ODataBinds, ODataQuery, SupportedMethod, ExpandPropertyPath, ResourceOptions, OrderByOption, OrderByPropertyPath, FilterOption, BindReference } from '@balena/odata-parser'; | ||
export type { ODataBinds, ODataQuery, SupportedMethod }; | ||
export declare type ResourceNode = ['Resource', string]; | ||
declare module '@balena/abstract-sql-compiler' { | ||
interface AbstractSqlTable { | ||
definition?: Definition; | ||
} | ||
interface FromTypeNode { | ||
ResourceNode: ResourceNode; | ||
} | ||
} | ||
export interface Definition { | ||
interface LegacyDefinition { | ||
extraBinds: ODataBinds; | ||
abstractSqlQuery: SelectQueryNode | UnionQueryNode | ResourceNode | TableNode; | ||
} | ||
interface Resource extends AbstractSqlTable { | ||
export declare type Definition = ModernDefinition | LegacyDefinition; | ||
interface Resource extends Omit<AbstractSqlTable, 'definition'> { | ||
tableAlias?: string; | ||
@@ -44,3 +36,3 @@ definition?: Definition; | ||
export declare const odataNameToSqlName: ((odataName: string) => string) & memoize.Memoized<(odataName: string) => string>; | ||
export declare const rewriteBinds: (definition: NonNullable<Resource['definition']>, existingBinds: ODataBinds, inc?: number) => void; | ||
export declare const rewriteBinds: (definition: ModernDefinition, existingBinds: ODataBinds, inc?: number) => void; | ||
export declare const isBindReference: (maybeBind: { | ||
@@ -119,3 +111,3 @@ [key: string]: unknown; | ||
Synonym(sqlName: string): string; | ||
rewriteDefinition(definition: Definition, extraBindVars: ODataBinds, bindVarsLength: number): Definition; | ||
rewriteDefinition(definition: Definition, extraBindVars: ODataBinds, bindVarsLength: number): ModernDefinition; | ||
} |
@@ -7,2 +7,11 @@ "use strict"; | ||
const randomstring = require("randomstring"); | ||
const convertToModernDefinition = (definition) => { | ||
if ('abstractSql' in definition) { | ||
return definition; | ||
} | ||
return { | ||
binds: definition.extraBinds, | ||
abstractSql: definition.abstractSqlQuery, | ||
}; | ||
}; | ||
const comparison = { | ||
@@ -58,7 +67,3 @@ eq: 'IsNotDistinctFrom', | ||
const definition = odataToAbstractSql.rewriteDefinition(resource.definition, args.extraBindVars, args.bindVarsLength); | ||
this.from.push([ | ||
'Alias', | ||
definition.abstractSqlQuery, | ||
resource.tableAlias, | ||
]); | ||
this.from.push(['Alias', definition.abstractSql, resource.tableAlias]); | ||
} | ||
@@ -105,8 +110,8 @@ else if (resource.name !== resource.tableAlias) { | ||
const rewriteBinds = (definition, existingBinds, inc = 0) => { | ||
const { extraBinds } = definition; | ||
if (extraBinds.length === 0) { | ||
const { binds } = definition; | ||
if (binds == null || binds.length === 0) { | ||
return; | ||
} | ||
inc += existingBinds.length; | ||
modifyAbstractSql('Bind', definition.abstractSqlQuery, (bind) => { | ||
modifyAbstractSql('Bind', definition.abstractSql, (bind) => { | ||
if (typeof bind[1] === 'number') { | ||
@@ -116,3 +121,3 @@ bind[1] += inc; | ||
}); | ||
existingBinds.push(...extraBinds); | ||
existingBinds.push(...binds); | ||
}; | ||
@@ -336,18 +341,20 @@ exports.rewriteBinds = rewriteBinds; | ||
unionResource.definition = { | ||
extraBinds: [], | ||
abstractSqlQuery: bindVarSelectQuery, | ||
binds: [], | ||
abstractSql: bindVarSelectQuery, | ||
}; | ||
} | ||
else { | ||
unionResource.definition = { ...unionResource.definition }; | ||
if (unionResource.definition.abstractSqlQuery[0] !== 'SelectQuery') { | ||
unionResource.definition = { | ||
...convertToModernDefinition(unionResource.definition), | ||
}; | ||
if (unionResource.definition.abstractSql[0] !== 'SelectQuery') { | ||
throw new Error('Only select query definitions supported for inserts'); | ||
} | ||
const isTable = (part) => part[0] === 'Table' && part[1] === unionResource.name; | ||
if (isTable(unionResource.definition.abstractSqlQuery)) { | ||
unionResource.definition.abstractSqlQuery = bindVarSelectQuery; | ||
if (isTable(unionResource.definition.abstractSql)) { | ||
unionResource.definition.abstractSql = bindVarSelectQuery; | ||
} | ||
else { | ||
let found = false; | ||
unionResource.definition.abstractSqlQuery = unionResource.definition.abstractSqlQuery.map((part) => { | ||
unionResource.definition.abstractSql = unionResource.definition.abstractSql.map((part) => { | ||
if (part[0] === 'From') { | ||
@@ -1143,5 +1150,5 @@ if (isTable(part[1])) { | ||
rewriteDefinition(definition, extraBindVars, bindVarsLength) { | ||
const rewrittenDefinition = _.cloneDeep(definition); | ||
const rewrittenDefinition = _.cloneDeep(convertToModernDefinition(definition)); | ||
exports.rewriteBinds(rewrittenDefinition, extraBindVars, bindVarsLength); | ||
modifyAbstractSql('Resource', rewrittenDefinition.abstractSqlQuery, (resource) => { | ||
modifyAbstractSql('Resource', rewrittenDefinition.abstractSql, (resource) => { | ||
const resourceName = resource[1]; | ||
@@ -1154,3 +1161,3 @@ const referencedResource = this.clientModel.tables[resourceName]; | ||
const subDefinition = this.rewriteDefinition(referencedResource.definition, extraBindVars, bindVarsLength); | ||
resource.splice(0, resource.length, ...subDefinition.abstractSqlQuery); | ||
resource.splice(0, resource.length, ...subDefinition.abstractSql); | ||
} | ||
@@ -1157,0 +1164,0 @@ else if (referencedResource.fields.some((field) => field.computed != null)) { |
{ | ||
"name": "@balena/odata-to-abstract-sql", | ||
"version": "5.3.2", | ||
"version": "5.4.0-official-table-definitions-e7956b90d4f5bfbbd57f288f9367db3876a8b216", | ||
"description": "A consumer of the OData parser, written in OMeta", | ||
@@ -5,0 +5,0 @@ "main": "out/odata-to-abstract-sql.js", |
@@ -23,3 +23,2 @@ import * as _ from 'lodash'; | ||
BooleanTypeNodes, | ||
UnionQueryNode, | ||
SelectQueryNode, | ||
@@ -31,2 +30,5 @@ InNode, | ||
TableNode, | ||
Definition as ModernDefinition, | ||
ResourceNode, | ||
UnionQueryNode, | ||
} from '@balena/abstract-sql-compiler'; | ||
@@ -46,19 +48,20 @@ import type { | ||
export type ResourceNode = ['Resource', string]; | ||
declare module '@balena/abstract-sql-compiler' { | ||
interface AbstractSqlTable { | ||
definition?: Definition; | ||
} | ||
interface FromTypeNode { | ||
ResourceNode: ResourceNode; | ||
} | ||
} | ||
export interface Definition { | ||
interface LegacyDefinition { | ||
extraBinds: ODataBinds; | ||
abstractSqlQuery: SelectQueryNode | UnionQueryNode | ResourceNode | TableNode; | ||
} | ||
export type Definition = ModernDefinition | LegacyDefinition; | ||
const convertToModernDefinition = ( | ||
definition: Definition, | ||
): ModernDefinition => { | ||
if ('abstractSql' in definition) { | ||
return definition; | ||
} | ||
return { | ||
binds: definition.extraBinds, | ||
abstractSql: definition.abstractSqlQuery, | ||
}; | ||
}; | ||
interface Resource extends AbstractSqlTable { | ||
interface Resource extends Omit<AbstractSqlTable, 'definition'> { | ||
tableAlias?: string; | ||
@@ -150,7 +153,3 @@ definition?: Definition; | ||
); | ||
this.from.push([ | ||
'Alias', | ||
definition.abstractSqlQuery, | ||
resource.tableAlias, | ||
]); | ||
this.from.push(['Alias', definition.abstractSql, resource.tableAlias]); | ||
} else if (resource.name !== resource.tableAlias) { | ||
@@ -209,8 +208,8 @@ this.from.push(['Alias', ['Table', resource.name], resource.tableAlias]); | ||
export const rewriteBinds = ( | ||
definition: NonNullable<Resource['definition']>, | ||
definition: ModernDefinition, | ||
existingBinds: ODataBinds, | ||
inc: number = 0, | ||
): void => { | ||
const { extraBinds } = definition; | ||
if (extraBinds.length === 0) { | ||
const { binds } = definition; | ||
if (binds == null || binds.length === 0) { | ||
return; | ||
@@ -221,3 +220,3 @@ } | ||
'Bind', | ||
definition.abstractSqlQuery as AbstractSqlQuery, | ||
definition.abstractSql as AbstractSqlQuery, | ||
(bind: BindNode) => { | ||
@@ -229,3 +228,3 @@ if (typeof bind[1] === 'number') { | ||
); | ||
existingBinds.push(...extraBinds); | ||
existingBinds.push(...binds); | ||
}; | ||
@@ -509,8 +508,10 @@ | ||
unionResource.definition = { | ||
extraBinds: [], | ||
abstractSqlQuery: bindVarSelectQuery, | ||
binds: [], | ||
abstractSql: bindVarSelectQuery, | ||
}; | ||
} else { | ||
unionResource.definition = { ...unionResource.definition }; | ||
if (unionResource.definition.abstractSqlQuery[0] !== 'SelectQuery') { | ||
unionResource.definition = { | ||
...convertToModernDefinition(unionResource.definition), | ||
}; | ||
if (unionResource.definition.abstractSql[0] !== 'SelectQuery') { | ||
throw new Error( | ||
@@ -524,7 +525,7 @@ 'Only select query definitions supported for inserts', | ||
if (isTable(unionResource.definition.abstractSqlQuery)) { | ||
unionResource.definition.abstractSqlQuery = bindVarSelectQuery; | ||
if (isTable(unionResource.definition.abstractSql)) { | ||
unionResource.definition.abstractSql = bindVarSelectQuery; | ||
} else { | ||
let found = false; | ||
unionResource.definition.abstractSqlQuery = unionResource.definition.abstractSqlQuery.map( | ||
unionResource.definition.abstractSql = unionResource.definition.abstractSql.map( | ||
(part) => { | ||
@@ -1467,8 +1468,10 @@ if (part[0] === 'From') { | ||
bindVarsLength: number, | ||
): Definition { | ||
const rewrittenDefinition = _.cloneDeep(definition); | ||
): ModernDefinition { | ||
const rewrittenDefinition = _.cloneDeep( | ||
convertToModernDefinition(definition), | ||
); | ||
rewriteBinds(rewrittenDefinition, extraBindVars, bindVarsLength); | ||
modifyAbstractSql( | ||
'Resource', | ||
rewrittenDefinition.abstractSqlQuery as AbstractSqlQuery, | ||
rewrittenDefinition.abstractSql as AbstractSqlQuery, | ||
(resource: ResourceNode) => { | ||
@@ -1489,3 +1492,3 @@ const resourceName = resource[1]; | ||
resource.length, | ||
...(subDefinition.abstractSqlQuery as AbstractSqlType[]), | ||
...(subDefinition.abstractSql as AbstractSqlType[]), | ||
); | ||
@@ -1492,0 +1495,0 @@ } else if ( |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
387248
2954
2