Skip to content

Commit

Permalink
Merge pull request #255 from balena-io-modules/update-dependencies
Browse files Browse the repository at this point in the history
Update node to 20, make sbvr-types a peer dependency, update dependencies & tests to match
  • Loading branch information
flowzone-app[bot] authored Oct 7, 2024
2 parents 0d128f8 + 28c15f3 commit 51695da
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 85 deletions.
37 changes: 20 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@balena/abstract-sql-compiler",
"version": "9.2.0",
"description": "A translator for abstract sql into sql.",
"type": "commonjs",
"main": "out/AbstractSQLCompiler.js",
"types": "out/AbstractSQLCompiler.d.ts",
"scripts": {
Expand All @@ -16,27 +17,29 @@
"repository": "https://github.com/balena-io-modules/abstract-sql-compiler.git",
"author": "",
"dependencies": {
"@balena/sbvr-types": "^7.0.1",
"lodash": "^4.17.21"
},
"peerDependencies": {
"@balena/sbvr-types": "^7.1.0, ^8.0.0, ^9.0.2"
},
"devDependencies": {
"@balena/lf-to-abstract-sql": "^5.0.0",
"@balena/lint": "^8.0.0",
"@balena/odata-parser": "^3.0.0",
"@balena/odata-to-abstract-sql": "^6.0.1",
"@balena/sbvr-parser": "^1.4.3",
"@balena/lf-to-abstract-sql": "^5.0.2",
"@balena/lint": "^8.2.8",
"@balena/odata-parser": "^3.1.0",
"@balena/odata-to-abstract-sql": "^6.4.0",
"@balena/sbvr-parser": "^1.4.6",
"@types/chai": "^4.3.4",
"@types/common-tags": "^1.8.1",
"@types/lodash": "^4.14.192",
"@types/mocha": "^10.0.1",
"@types/node": "^20.0.0",
"@types/common-tags": "^1.8.4",
"@types/lodash": "^4.17.10",
"@types/mocha": "^10.0.8",
"@types/node": "^20.16.10",
"chai": "^4.3.7",
"common-tags": "^1.8.2",
"husky": "^9.0.0",
"lint-staged": "^15.0.0",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^5.4.3"
"husky": "^9.1.6",
"lint-staged": "^15.2.10",
"mocha": "^10.7.3",
"ts-node": "^10.9.2",
"typescript": "^5.6.2"
},
"lint-staged": {
"*.js": [
Expand All @@ -57,8 +60,8 @@
]
},
"engines": {
"node": ">=16.13.0",
"npm": ">=8.1.0"
"node": ">=20.14.0",
"npm": ">=10.7.0"
},
"versionist": {
"publishedAt": "2024-06-12T13:14:08.116Z"
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractSQLCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AbstractSQLRules2SQL } from './AbstractSQLRules2SQL';
export { Binding, SqlResult } from './AbstractSQLRules2SQL';
import type { SbvrType } from '@balena/sbvr-types';
import sbvrTypes from '@balena/sbvr-types';
import * as _ from 'lodash';
import _ from 'lodash';
import { optimizeSchema, generateRuleSlug } from './AbstractSQLSchemaOptimiser';
import type {
ReferencedFields,
Expand Down
23 changes: 4 additions & 19 deletions src/AbstractSQLOptimiser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as _ from 'lodash';
import _ from 'lodash';

import type { Dictionary } from 'lodash';
import type {
Expand Down Expand Up @@ -323,7 +323,7 @@ const Field: MetaMatchFn<FieldNode | ReferencedFieldNode> = (args) => {
};

const AnyNotNullValue = (args: any): boolean => {
return args != null && (args as any) !== 'Null' && args[0] !== 'Null';
return args != null && args !== 'Null' && args[0] !== 'Null';
};

const FieldOp =
Expand Down Expand Up @@ -603,15 +603,7 @@ const typeRules = {
case 'RightJoin':
case 'FullJoin':
case 'CrossJoin':
tables.push(
typeRules[type](rest) as
| FromNode
| InnerJoinNode
| LeftJoinNode
| RightJoinNode
| FullJoinNode
| CrossJoinNode,
);
tables.push(typeRules[type](rest));
break;
case 'Where':
case 'GroupBy':
Expand Down Expand Up @@ -1375,14 +1367,7 @@ const typeRules = {
switch (valuesType) {
case 'SelectQuery':
case 'UnionQuery':
values = [
[
'Values',
typeRules[valuesType](valuesRest) as
| SelectQueryNode
| UnionQueryNode,
],
];
values = [['Values', typeRules[valuesType](valuesRest)]];
break;
default:
values = [['Values', valuesArray.map(Value)] as ValuesNode];
Expand Down
12 changes: 6 additions & 6 deletions src/AbstractSQLRules2SQL.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as _ from 'lodash';
import _ from 'lodash';

import sbvrTypes from '@balena/sbvr-types';

Expand Down Expand Up @@ -38,7 +38,7 @@ type MatchFn = (args: AbstractSqlType[], indent: string) => string;
let fieldOrderings: Binding[] = [];
let fieldOrderingsLookup: Dictionary<number> = {};
let engine: Engines = Engines.postgres;
let noBinds: boolean = false;
let noBinds = false;

export const comparisons = {
Equals: ' = ',
Expand Down Expand Up @@ -669,7 +669,7 @@ const typeRules: Dictionary<MatchFn> = {
SelectQuery: (args, indent) => {
const tables: string[] = [];
const joins: string[] = [];
let select: string = '';
let select = '';
const groups = {
Where: '',
GroupBy: '',
Expand Down Expand Up @@ -869,7 +869,7 @@ const typeRules: Dictionary<MatchFn> = {
checkArgs('Cast', args, 2);
const value = AnyValue(getAbstractSqlQuery(args, 0), indent);
const typeName = args[1] as keyof typeof sbvrTypes;
if (!sbvrTypes[typeName] || !sbvrTypes[typeName].types[engine]) {
if (!sbvrTypes[typeName]?.types[engine]) {
throw new SyntaxError(`Invalid cast type: ${typeName}`);
}
let type: string;
Expand Down Expand Up @@ -1451,7 +1451,7 @@ const typeRules: Dictionary<MatchFn> = {
const tables: string[] = [];
let fields: string[] = [];
let values: string[] = [];
let where: string = '';
let where = '';
for (const arg of args) {
if (!isAbstractSqlQuery(arg)) {
throw new SyntaxError('`UpdateQuery` args must all be arrays');
Expand Down Expand Up @@ -1510,7 +1510,7 @@ const typeRules: Dictionary<MatchFn> = {
},
DeleteQuery: (args, indent) => {
const tables: string[] = [];
let where: string = '';
let where = '';
for (const arg of args) {
if (!isAbstractSqlQuery(arg)) {
throw new SyntaxError('`DeleteQuery` args must all be arrays');
Expand Down
6 changes: 3 additions & 3 deletions src/AbstractSQLSchemaOptimiser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const enum Engines {
import { AbstractSQLOptimiser } from './AbstractSQLOptimiser';
export { Binding, SqlResult } from './AbstractSQLRules2SQL';
import sbvrTypes from '@balena/sbvr-types';
import * as _ from 'lodash';
import _ from 'lodash';
import type {
AbstractSqlModel,
AbstractSqlQuery,
Expand Down Expand Up @@ -44,7 +44,7 @@ export const generateRuleSlug = (

export const optimizeSchema = (
abstractSqlModel: AbstractSqlModel,
createCheckConstraints: boolean = true,
createCheckConstraints = true,
): AbstractSqlModel => {
abstractSqlModel.rules = abstractSqlModel.rules
.map((rule): AbstractSqlQuery | undefined => {
Expand Down Expand Up @@ -126,7 +126,7 @@ export const optimizeSchema = (
);
if (table) {
table.checks ??= [];
table.checks!.push({
table.checks.push({
description: ruleSE,
name: generateRuleSlug(tableName, ruleBody),
abstractSql: whereNode,
Expand Down
4 changes: 2 additions & 2 deletions src/referenced-fields.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as _ from 'lodash';
import _ from 'lodash';
import type {
AbstractSqlQuery,
AbstractSqlType,
Expand Down Expand Up @@ -270,7 +270,7 @@ export const getRuleReferencedFields: EngineInstance['getRuleReferencedFields']
_.isEqual(ruleBody[2], ['Number', 0]) &&
isSelectQueryNode(ruleBody[1])
) {
const select = ruleBody[1].find(isSelectNode) as SelectNode;
const select = ruleBody[1].find(isSelectNode)!;
select[1] = [];
$getRuleReferencedFields(referencedFields, ruleBody[1], IsSafe.Delete);
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/abstract-sql/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as AbstractSQLCompiler from '../..';

import { expect } from 'chai';
import * as _ from 'lodash';
import _ from 'lodash';

const bindingsTest = function (actualBindings, expectedBindings) {
if (expectedBindings == null) {
Expand Down
18 changes: 9 additions & 9 deletions test/odata/expand.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
aliasPlaneFields,
aliasPilotCanFlyPlaneFields,
} from './fields';
import * as _ from 'lodash';
import _ from 'lodash';

const postgresAgg = (field) => 'COALESCE(JSON_AGG(' + field + "), '[]')";
const mysqlAgg = (field) => "'[' || group_concat(" + field + ", ',') || ']'";
Expand Down Expand Up @@ -436,17 +436,17 @@ SELECT (
SELECT ${aliasLicenceFields.join(', ')}
FROM "licence" AS "pilot.licence"
WHERE "pilot"."licence" = "pilot.licence"."id"
LIMIT 10
LIMIT ?
) AS "pilot.licence"
) AS "licence", ${remainingPilotFields}
FROM "pilot"`,
);
});
};
const url = '/pilot?$expand=licence($top=10)';
test.postgres(url, testFunc(postgresAgg));
test.mysql.skip(url, testFunc(mysqlAgg));
test.websql.skip(url, testFunc(websqlAgg));
test.postgres(url, 'GET', [['Bind', 0]], testFunc(postgresAgg));
test.mysql.skip(url, 'GET', [['Bind', 0]], testFunc(mysqlAgg));
test.websql.skip(url, 'GET', [['Bind', 0]], testFunc(websqlAgg));
})();

(function () {
Expand Down Expand Up @@ -493,17 +493,17 @@ SELECT (
SELECT ${aliasLicenceFields.join(', ')}
FROM "licence" AS "pilot.licence"
WHERE "pilot"."licence" = "pilot.licence"."id"
OFFSET 10
OFFSET ?
) AS "pilot.licence"
) AS "licence", ${remainingPilotFields}
FROM "pilot"`,
);
});
};
const url = '/pilot?$expand=licence($skip=10)';
test.postgres(url, testFunc(postgresAgg));
test.mysql.skip(url, testFunc(mysqlAgg));
test.websql.skip(url, testFunc(websqlAgg));
test.postgres(url, 'GET', [['Bind', 0]], testFunc(postgresAgg));
test.mysql.skip(url, 'GET', [['Bind', 0]], testFunc(mysqlAgg));
test.websql.skip(url, 'GET', [['Bind', 0]], testFunc(websqlAgg));
})();

(function () {
Expand Down
14 changes: 7 additions & 7 deletions test/odata/filterby.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
import { expect } from 'chai';
import test, { clientModel } from './test';
import * as _ from 'lodash';
import _ from 'lodash';
import { odataNameToSqlName } from '@balena/odata-to-abstract-sql';
import { pilotFields, teamFields, aliasPilotCanFlyPlaneFields } from './fields';

Expand Down Expand Up @@ -69,7 +69,7 @@ let parseOperandFactory = function (defaultResource) {
typeof operand === 'boolean' ||
typeof operand === 'number' ||
_.isDate(operand) ||
(typeof operand === 'string' && operand.charAt(0) === "'")
(typeof operand === 'string' && operand.startsWith("'"))
) {
return [['Bind', bindNo++]];
}
Expand All @@ -95,7 +95,7 @@ let parseOperandFactory = function (defaultResource) {
if (operand === 'null') {
return 'NULL';
}
if (operand.charAt(0) === "'") {
if (operand.startsWith("'")) {
return '?';
}
const fieldParts = operand.split('/');
Expand Down Expand Up @@ -636,7 +636,7 @@ WHERE EXISTS (
};
const updateWhere = `\
WHERE "pilot"."id" IN ((
SELECT "pilot"."id"
SELECT "pilot"."id" AS "$modifyid"
FROM "pilot",
"pilot-can fly-plane" AS "pilot.pilot-can fly-plane",
"plane" AS "pilot.pilot-can fly-plane.plane"
Expand Down Expand Up @@ -731,7 +731,7 @@ ${updateWhere}`,
`\
DELETE FROM "pilot"
WHERE "pilot"."id" IN ((
SELECT "pilot"."id"
SELECT "pilot"."id" AS "$modifyid"
FROM "pilot",
"pilot-can fly-plane" AS "pilot.pilot-can fly-plane",
"plane" AS "pilot.pilot-can fly-plane.plane"
Expand Down Expand Up @@ -808,7 +808,7 @@ UPDATE "pilot"
SET "name" = ?
WHERE ("pilot"."id") IS NOT NULL AND ("pilot"."id") = (?)
AND "pilot"."id" IN ((
SELECT "pilot"."id"
SELECT "pilot"."id" AS "$modifyid"
FROM "pilot"
WHERE ${sql}
))`,
Expand Down Expand Up @@ -865,7 +865,7 @@ SET "created at" = DEFAULT,
"was trained by-pilot" = DEFAULT
WHERE ("pilot"."id") IS NOT NULL AND ("pilot"."id") = (?)
AND "pilot"."id" IN ((
SELECT "pilot"."id"
SELECT "pilot"."id" AS "$modifyid"
FROM "pilot"
WHERE ${sql}
))`,
Expand Down
Loading

0 comments on commit 51695da

Please sign in to comment.