diff --git a/package.json b/package.json index 95689ac..edc38c5 100644 --- a/package.json +++ b/package.json @@ -2,10 +2,11 @@ "name": "@balena/odata-to-abstract-sql", "version": "6.2.7", "description": "A consumer of the OData parser, written in OMeta", + "type": "commonjs", "main": "out/odata-to-abstract-sql.js", "scripts": { "lint": "balena-lint -e js -e ts src test && tsc --noEmit && tsc --noEmit --project tsconfig.js.json", - "prettify": "balena-lint -e js -e ts --fix src test", + "lint-fix": "balena-lint -e js -e ts --fix src test", "pretest": "npm run prepare", "test": "mocha && npm run lint", "prepublish": "require-npm4-to-publish", @@ -15,31 +16,31 @@ "author": "", "license": "BSD", "dependencies": { - "@balena/abstract-sql-compiler": "^9.1.4", - "@balena/odata-parser": "^3.0.3", - "@types/lodash": "^4.14.202", + "@balena/abstract-sql-compiler": "^9.2.0", + "@balena/odata-parser": "^3.0.8", + "@types/lodash": "^4.17.7", "@types/memoizee": "^0.4.11", "@types/string-hash": "^1.1.3", "lodash": "^4.17.21", - "memoizee": "^0.4.15", + "memoizee": "^0.4.17", "string-hash": "^1.1.3" }, "devDependencies": { - "@balena/lf-to-abstract-sql": "^5.0.1", - "@balena/lint": "^8.0.0", - "@balena/sbvr-parser": "^1.4.3", - "@balena/sbvr-types": "^7.0.1", - "@types/chai": "^4.3.11", + "@balena/lf-to-abstract-sql": "^5.0.2", + "@balena/lint": "^8.2.7", + "@balena/sbvr-parser": "^1.4.6", + "@balena/sbvr-types": "^7.1.3", + "@types/chai": "^4.3.18", "@types/chai-things": "0.0.38", - "@types/mocha": "^10.0.6", - "chai": "^4.4.1", + "@types/mocha": "^10.0.7", + "chai": "^4.5.0", "chai-things": "~0.2.0", - "husky": "^9.0.0", - "lint-staged": "^15.2.0", - "mocha": "^10.2.0", + "husky": "^9.1.5", + "lint-staged": "^15.2.9", + "mocha": "^10.7.3", "require-npm4-to-publish": "^1.0.0", "ts-node": "^10.9.2", - "typescript": "^5.3.3" + "typescript": "^5.5.4" }, "mocha": { "reporter": "spec", diff --git a/src/odata-to-abstract-sql.ts b/src/odata-to-abstract-sql.ts index b772531..9988210 100644 --- a/src/odata-to-abstract-sql.ts +++ b/src/odata-to-abstract-sql.ts @@ -169,7 +169,7 @@ const containsQueryOption = (opts?: object): boolean => { return false; } for (const key in opts) { - if (key[0] === '$') { + if (key.startsWith('$')) { return true; } } @@ -274,7 +274,7 @@ const modifyAbstractSql = < export const rewriteBinds = ( definition: ModernDefinition, existingBinds: ODataBinds, - inc: number = 0, + inc = 0, ): void => { const { binds } = definition; if (binds == null || binds.length === 0) { @@ -338,7 +338,7 @@ export class OData2AbstractSQL { public extraBindVars = [] as unknown as ODataBinds; private resourceAliases: Dictionary = {}; public defaultResource: Resource | undefined; - public bindVarsLength: number = 0; + public bindVarsLength = 0; private checkAlias: (alias: string) => string; private alreadyComputedFields: AlreadyComputedFieldsLookup = {}; @@ -738,7 +738,7 @@ export class OData2AbstractSQL { path: ODataQuery, resource: AliasedResource, bodyKeys: string[], - ): BooleanTypeNodes | void { + ): BooleanTypeNodes | undefined { const { key } = path; if (key != null) { if (method === 'PUT' || method === 'PUT-INSERT' || method === 'POST') { @@ -938,7 +938,7 @@ export class OData2AbstractSQL { } resource[resourceMappingsProp] = resourceMappings; } - return resource[resourceMappingsProp]!; + return resource[resourceMappingsProp]; } ResolveRelationship(resource: string | Resource, relationship: string) { let resourceName; @@ -962,7 +962,7 @@ export class OData2AbstractSQL { .flatMap((sqlName) => this.Synonym(sqlName).split('-')) .value(); const relationshipMapping = _.get(resourceRelations, relationshipPath); - if (!relationshipMapping || !relationshipMapping.$) { + if (!relationshipMapping?.$) { throw new SyntaxError( `Could not resolve relationship mapping from '${resourceName}' to '${relationshipPath}'`, ); @@ -978,11 +978,7 @@ export class OData2AbstractSQL { let odataFieldNames: Array< Parameters >; - if ( - path.options && - path.options.$select && - path.options.$select.properties - ) { + if (path.options?.$select?.properties) { this.AddExtraFroms(query, resource, path.options.$select.properties); odataFieldNames = path.options.$select.properties.map((prop: any) => { const field = this.Property(prop) as { @@ -1016,7 +1012,7 @@ export class OData2AbstractSQL { alias: string = fieldName, // Setting this flag to true will ignore the lookup for alreadyComputedFields // and will compile the computed Field statement into the abstract SQL statement regardless - forceCompilingComputedField: boolean = false, + forceCompilingComputedField = false, ): | ReferencedFieldNode | AliasNode @@ -1527,7 +1523,7 @@ export class OData2AbstractSQL { if (expand.property) { this.Expands(expandResource, nestedExpandQuery, [expand.property]); } - if (expand.options && expand.options.$expand) { + if (expand.options?.$expand) { this.Expands( expandResource, nestedExpandQuery, @@ -1611,7 +1607,9 @@ export class OData2AbstractSQL { // TODO: try removing try { if (Array.isArray(match)) { - match.forEach((v) => this.AddExtraFroms(query, parentResource, v)); + match.forEach((v) => { + this.AddExtraFroms(query, parentResource, v); + }); } else { let nextProp = match; let prop; @@ -1619,8 +1617,7 @@ export class OData2AbstractSQL { // tslint:disable-next-line:no-conditional-assignment (prop = nextProp) && prop.name && - prop.property && - prop.property.name + prop.property?.name ) { nextProp = prop.property; const resourceAlias = this.resourceAliases[prop.name]; @@ -1634,7 +1631,7 @@ export class OData2AbstractSQL { ); } } - if (nextProp && nextProp.args) { + if (nextProp?.args) { this.AddExtraFroms(query, parentResource, prop.args); } } @@ -1693,7 +1690,7 @@ export class OData2AbstractSQL { resource: Resource, extraBindVars: ODataBinds, bindVarsLength: number, - bypassDefinition: boolean = false, + bypassDefinition = false, tableAlias?: string, isModifyOperation?: boolean, ): FromTypeNodes { diff --git a/test/chai-sql.js b/test/chai-sql.js index fcb762b..03123b1 100644 --- a/test/chai-sql.js +++ b/test/chai-sql.js @@ -82,7 +82,7 @@ chai.use(function ($chai, utils) { utils.addMethod(assertionPrototype, 'groupby', multiBodyClause('GroupBy')); utils.addMethod(assertionPrototype, 'where', bodyClause('Where')); utils.addMethod(assertionPrototype, 'limit', bodyClause('Limit')); - return utils.addMethod(assertionPrototype, 'offset', bodyClause('Offset')); + utils.addMethod(assertionPrototype, 'offset', bodyClause('Offset')); }); const generateClientModel = function (input) { @@ -160,7 +160,7 @@ export function operandToAbstractSQLFactory( if (operand === 'null') { return ['Null']; } - if (operand.charAt(0) === '(') { + if (operand.startsWith('(')) { return operand .slice(1, -1) .split(',') @@ -174,7 +174,7 @@ export function operandToAbstractSQLFactory( }); } - if (operand.charAt(0) === "'") { + if (operand.startsWith("'")) { binds.push([ 'Text', decodeURIComponent(operand.slice(1, operand.length - 1)), diff --git a/test/filterby.js b/test/filterby.js index a1791f8..600f81e 100644 --- a/test/filterby.js +++ b/test/filterby.js @@ -529,7 +529,9 @@ run(function () { ); test(`/pilot?$filter=${odata}`, 'POST', { name: 'Peter' }, (result) => - it(`should insert pilot where '${odata}'`, () => insertTest(result)), + it(`should insert pilot where '${odata}'`, () => { + insertTest(result); + }), ); test(`/pilot?$filter=${odata}`, 'PUT', { name: 'Peter' }, (result) => @@ -537,7 +539,9 @@ run(function () { it('should be an upsert', () => { expect(result).to.be.a.query.that.upserts; }); - it('that inserts', () => insertTest(result[1])); + it('that inserts', () => { + insertTest(result[1]); + }); return it('and updates', () => expect(result[2]) .to.be.a.query.that.updates.fields( @@ -725,9 +729,9 @@ run([['Number', 1]], function () { ]; 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); + }), ); test('/pilot(1)?$filter=' + odata, 'PATCH', { name }, (result) => @@ -744,7 +748,9 @@ run([['Number', 1]], function () { 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])); + it('that inserts', () => { + insertTest(result[1]); + }); return it('and updates', () => { expect(result[2]) .to.be.a.query.that.updates.fields(