Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@balena/abstract-sql-compiler

Package Overview
Dependencies
Maintainers
4
Versions
467
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@balena/abstract-sql-compiler - npm Package Compare versions

Comparing version 7.5.0 to 7.6.0-model-based-optimizations-50dced9874a3c4649eddf65bd603d85c232116b5

4

CHANGELOG.md

@@ -7,2 +7,6 @@ # Change Log

## 7.6.0 - 2021-01-01
* Add the ability to optimize generated sql using model info [Pagan Gazzard]
## 7.5.0 - 2021-01-01

@@ -9,0 +13,0 @@

18

out/AbstractSQLCompiler.d.ts

@@ -233,12 +233,12 @@ export declare const enum Engines {

export declare const isResourceNode: (n: AbstractSqlType) => n is ResourceNode;
export declare function compileRule(abstractSQL: UpsertQueryNode, engine: Engines, noBinds: true): [string, string];
export declare function compileRule(abstractSQL: AbstractSqlQuery, engine: Engines, noBinds: true): string;
export declare function compileRule(abstractSQL: UpsertQueryNode, engine: Engines, noBinds?: false): [SqlResult, SqlResult];
export declare function compileRule(abstractSQL: SelectQueryNode | UnionQueryNode | InsertQueryNode | UpdateQueryNode | DeleteQueryNode, engine: Engines, noBinds?: false): SqlResult;
export declare function compileRule(abstractSQL: AbstractSqlQuery, engine: Engines, noBinds?: false): SqlResult | [SqlResult, SqlResult];
export declare function compileRule(abstractSQL: AbstractSqlQuery, engine: Engines, noBinds?: boolean): SqlResult | [SqlResult, SqlResult] | string;
export declare function compileRule(abstractSQL: UpsertQueryNode, engine: Engines, noBinds: true, abstractSqlModel?: AbstractSqlModel): [string, string];
export declare function compileRule(abstractSQL: AbstractSqlQuery, engine: Engines, noBinds: true, abstractSqlModel?: AbstractSqlModel): string;
export declare function compileRule(abstractSQL: UpsertQueryNode, engine: Engines, noBinds?: false, abstractSqlModel?: AbstractSqlModel): [SqlResult, SqlResult];
export declare function compileRule(abstractSQL: SelectQueryNode | UnionQueryNode | InsertQueryNode | UpdateQueryNode | DeleteQueryNode, engine: Engines, noBinds?: false, abstractSqlModel?: AbstractSqlModel): SqlResult;
export declare function compileRule(abstractSQL: AbstractSqlQuery, engine: Engines, noBinds?: false, abstractSqlModel?: AbstractSqlModel): SqlResult | [SqlResult, SqlResult];
export declare function compileRule(abstractSQL: AbstractSqlQuery, engine: Engines, noBinds?: boolean, abstractSqlModel?: AbstractSqlModel): SqlResult | [SqlResult, SqlResult] | string;
export declare const postgres: {
optimizeSchema: (abstractSqlModel: AbstractSqlModel) => AbstractSqlModel;
compileSchema: _.Function1<AbstractSqlModel, SqlModel>;
compileRule: (abstractSQL: AbstractSqlQuery) => SqlResult | [SqlResult, SqlResult];
compileRule: (abstractSQL: AbstractSqlQuery, abstractSqlModel?: AbstractSqlModel | undefined) => SqlResult | [SqlResult, SqlResult];
dataTypeValidate: (value: any, field: Pick<AbstractSqlField, 'dataType' | 'required'>) => Promise<any>;

@@ -251,3 +251,3 @@ getReferencedFields: (ruleBody: AbstractSqlQuery) => ReferencedFields;

compileSchema: _.Function1<AbstractSqlModel, SqlModel>;
compileRule: (abstractSQL: AbstractSqlQuery) => SqlResult | [SqlResult, SqlResult];
compileRule: (abstractSQL: AbstractSqlQuery, abstractSqlModel?: AbstractSqlModel | undefined) => SqlResult | [SqlResult, SqlResult];
dataTypeValidate: (value: any, field: Pick<AbstractSqlField, 'dataType' | 'required'>) => Promise<any>;

@@ -260,3 +260,3 @@ getReferencedFields: (ruleBody: AbstractSqlQuery) => ReferencedFields;

compileSchema: _.Function1<AbstractSqlModel, SqlModel>;
compileRule: (abstractSQL: AbstractSqlQuery) => SqlResult | [SqlResult, SqlResult];
compileRule: (abstractSQL: AbstractSqlQuery, abstractSqlModel?: AbstractSqlModel | undefined) => SqlResult | [SqlResult, SqlResult];
dataTypeValidate: (value: any, field: Pick<AbstractSqlField, 'dataType' | 'required'>) => Promise<any>;

@@ -263,0 +263,0 @@ getReferencedFields: (ruleBody: AbstractSqlQuery) => ReferencedFields;

@@ -190,4 +190,4 @@ "use strict";

};
function compileRule(abstractSQL, engine, noBinds = false) {
abstractSQL = AbstractSQLOptimiser_1.AbstractSQLOptimiser(abstractSQL, noBinds);
function compileRule(abstractSQL, engine, noBinds = false, abstractSqlModel) {
abstractSQL = AbstractSQLOptimiser_1.AbstractSQLOptimiser(abstractSQL, noBinds, abstractSqlModel);
return AbstractSQLRules2SQL_1.AbstractSQLRules2SQL(abstractSQL, engine, noBinds);

@@ -263,3 +263,3 @@ }

CREATE ${orReplaceStr}VIEW "${table.name}" AS (
${compileRule(definitionAbstractSql, engine, true).replace(/^/gm, ' ')}
${compileRule(definitionAbstractSql, engine, true, abstractSqlModel).replace(/^/gm, ' ')}
);`,

@@ -329,3 +329,3 @@ ],

const constraintName = check.name ? `CONSTRAINT "${check.name}" ` : '';
const sql = compileRule(check.abstractSql, engine, true);
const sql = compileRule(check.abstractSql, engine, true, abstractSqlModel);
createSqlElements.push(`\

@@ -427,3 +427,3 @@ ${comment}${constraintName}CHECK (${sql})`);

}
const { query: ruleSQL, bindings: ruleBindings } = compileRule(ruleBody, engine);
const { query: ruleSQL, bindings: ruleBindings } = compileRule(ruleBody, engine, undefined, abstractSqlModel);
let referencedFields;

@@ -456,3 +456,3 @@ try {

compileSchema: _.partial(compileSchema, _, engine, ifNotExists),
compileRule: (abstractSQL) => compileRule(abstractSQL, engine, false),
compileRule: (abstractSQL, abstractSqlModel) => compileRule(abstractSQL, engine, false, abstractSqlModel),
dataTypeValidate,

@@ -459,0 +459,0 @@ getReferencedFields,

@@ -1,2 +0,2 @@

import { AbstractSqlQuery } from './AbstractSQLCompiler';
export declare const AbstractSQLOptimiser: (abstractSQL: AbstractSqlQuery, $noBinds?: boolean) => AbstractSqlQuery;
import { AbstractSqlModel, AbstractSqlQuery } from './AbstractSQLCompiler';
export declare const AbstractSQLOptimiser: (abstractSQL: AbstractSqlQuery, $noBinds?: boolean, $abstractSqlModel?: AbstractSqlModel | undefined) => AbstractSqlQuery;

@@ -19,3 +19,5 @@ "use strict";

let helped = false;
let aliases = {};
let noBinds = false;
let abstractSqlModel;
const Helper = (fn) => {

@@ -360,2 +362,6 @@ return (...args) => {

checkArgs('From', args, 1);
const maybeAlias = args[0];
if (maybeAlias[0] === 'Alias' && maybeAlias[1][0] === 'Table') {
aliases[maybeAlias[2]] = maybeAlias[1][1];
}
return ['From', MaybeAlias(args[0], FromMatch)];

@@ -776,5 +782,24 @@ },

},
Exists: (args) => {
Exists: tryMatches(Helper((args) => {
var _a;
if (abstractSqlModel == null) {
return false;
}
checkArgs('Exists', args, 1);
const arg = getAbstractSqlQuery(args, 0);
switch (arg[0]) {
case 'ReferencedField':
const [, aliasName, fieldName] = arg;
const tableName = (_a = aliases[aliasName]) !== null && _a !== void 0 ? _a : aliasName;
const table = abstractSqlModel.tables[tableName];
const field = table === null || table === void 0 ? void 0 : table.fields.find((f) => f.fieldName === fieldName);
if ((field === null || field === void 0 ? void 0 : field.required) === true) {
return ['Boolean', true];
}
default:
return false;
}
}), (args) => {
checkArgs('Exists', args, 1);
const arg = getAbstractSqlQuery(args, 0);
const [type, ...rest] = arg;

@@ -788,3 +813,3 @@ switch (type) {

}
},
}),
NotExists: (args) => {

@@ -1040,6 +1065,8 @@ checkArgs('NotExists', args, 1);

};
const AbstractSQLOptimiser = (abstractSQL, $noBinds = false) => {
const AbstractSQLOptimiser = (abstractSQL, $noBinds = false, $abstractSqlModel) => {
noBinds = $noBinds;
abstractSqlModel = $abstractSqlModel;
do {
helped = false;
aliases = {};
const [type, ...rest] = abstractSQL;

@@ -1046,0 +1073,0 @@ switch (type) {

@@ -48,3 +48,3 @@ "use strict";

}
ruleBody = AbstractSQLOptimiser_1.AbstractSQLOptimiser(ruleBody, true);
ruleBody = AbstractSQLOptimiser_1.AbstractSQLOptimiser(ruleBody, true, abstractSqlModel);
const count = countFroms(ruleBody);

@@ -51,0 +51,0 @@ if (count === 1 &&

{
"name": "@balena/abstract-sql-compiler",
"version": "7.5.0",
"version": "7.6.0-model-based-optimizations-50dced9874a3c4649eddf65bd603d85c232116b5",
"description": "A translator for abstract sql into sql.",

@@ -5,0 +5,0 @@ "main": "out/AbstractSQLCompiler.js",

@@ -574,2 +574,3 @@ export const enum Engines {

noBinds: true,
abstractSqlModel?: AbstractSqlModel,
): [string, string];

@@ -580,2 +581,3 @@ export function compileRule(

noBinds: true,
abstractSqlModel?: AbstractSqlModel,
): string;

@@ -586,2 +588,3 @@ export function compileRule(

noBinds?: false,
abstractSqlModel?: AbstractSqlModel,
): [SqlResult, SqlResult];

@@ -597,2 +600,3 @@ export function compileRule(

noBinds?: false,
abstractSqlModel?: AbstractSqlModel,
): SqlResult;

@@ -603,2 +607,3 @@ export function compileRule(

noBinds?: false,
abstractSqlModel?: AbstractSqlModel,
): SqlResult | [SqlResult, SqlResult];

@@ -609,2 +614,3 @@ export function compileRule(

noBinds?: boolean,
abstractSqlModel?: AbstractSqlModel,
): SqlResult | [SqlResult, SqlResult] | string;

@@ -615,4 +621,5 @@ export function compileRule(

noBinds = false,
abstractSqlModel?: AbstractSqlModel,
): SqlResult | [SqlResult, SqlResult] | string | [string, string] {
abstractSQL = AbstractSQLOptimiser(abstractSQL, noBinds);
abstractSQL = AbstractSQLOptimiser(abstractSQL, noBinds, abstractSqlModel);
return AbstractSQLRules2SQL(abstractSQL, engine, noBinds);

@@ -710,6 +717,8 @@ }

CREATE ${orReplaceStr}VIEW "${table.name}" AS (
${compileRule(definitionAbstractSql as AbstractSqlQuery, engine, true).replace(
/^/gm,
' ',
)}
${compileRule(
definitionAbstractSql as AbstractSqlQuery,
engine,
true,
abstractSqlModel,
).replace(/^/gm, ' ')}
);`,

@@ -796,2 +805,3 @@ ],

true,
abstractSqlModel,
);

@@ -922,2 +932,4 @@ createSqlElements.push(`\

engine,
undefined,
abstractSqlModel,
) as SqlResult;

@@ -954,4 +966,6 @@ let referencedFields: ReferencedFields | undefined;

compileSchema: _.partial(compileSchema, _, engine, ifNotExists),
compileRule: (abstractSQL: AbstractSqlQuery) =>
compileRule(abstractSQL, engine, false),
compileRule: (
abstractSQL: AbstractSqlQuery,
abstractSqlModel?: AbstractSqlModel,
) => compileRule(abstractSQL, engine, false, abstractSqlModel),
dataTypeValidate,

@@ -958,0 +972,0 @@ getReferencedFields,

@@ -5,6 +5,10 @@ import * as _ from 'lodash';

import {
AbstractSqlModel,
AbstractSqlQuery,
AbstractSqlType,
AliasNode,
DurationNode,
ReferencedFieldNode,
ReplaceNode,
TableNode,
} from './AbstractSQLCompiler';

@@ -39,3 +43,5 @@ import * as AbstractSQLRules2SQL from './AbstractSQLRules2SQL';

let helped = false;
let aliases: { [alias: string]: string } = {};
let noBinds = false;
let abstractSqlModel: AbstractSqlModel | undefined;
const Helper = <F extends (...args: any[]) => any>(fn: F) => {

@@ -441,2 +447,6 @@ return (...args: Parameters<F>): ReturnType<F> => {

checkArgs('From', args, 1);
const maybeAlias = args[0] as AliasNode<TableNode>;
if (maybeAlias[0] === 'Alias' && maybeAlias[1][0] === 'Table') {
aliases[maybeAlias[2]] = maybeAlias[1][1];
}
return ['From', MaybeAlias(args[0] as AbstractSqlQuery, FromMatch)];

@@ -904,14 +914,35 @@ },

},
Exists: (args) => {
checkArgs('Exists', args, 1);
const arg = getAbstractSqlQuery(args, 0);
const [type, ...rest] = arg;
switch (type) {
case 'SelectQuery':
case 'UnionQuery':
return ['Exists', typeRules[type](rest)];
default:
return ['Exists', AnyValue(arg)];
}
},
Exists: tryMatches(
Helper<OptimisationMatchFn>((args) => {
if (abstractSqlModel == null) {
return false;
}
checkArgs('Exists', args, 1);
const arg = getAbstractSqlQuery(args, 0);
switch (arg[0]) {
case 'ReferencedField':
const [, aliasName, fieldName] = arg as ReferencedFieldNode;
const tableName = aliases[aliasName] ?? aliasName;
const table = abstractSqlModel.tables[tableName];
const field = table?.fields.find((f) => f.fieldName === fieldName);
if (field?.required === true) {
return ['Boolean', true] as AbstractSqlQuery;
}
default:
return false;
}
}),
(args) => {
checkArgs('Exists', args, 1);
const arg = getAbstractSqlQuery(args, 0);
const [type, ...rest] = arg;
switch (type) {
case 'SelectQuery':
case 'UnionQuery':
return ['Exists', typeRules[type](rest)];
default:
return ['Exists', AnyValue(arg)];
}
},
),
NotExists: (args) => {

@@ -1225,6 +1256,9 @@ checkArgs('NotExists', args, 1);

$noBinds = false,
$abstractSqlModel?: AbstractSqlModel,
): AbstractSqlQuery => {
noBinds = $noBinds;
abstractSqlModel = $abstractSqlModel;
do {
helped = false;
aliases = {};
const [type, ...rest] = abstractSQL;

@@ -1231,0 +1265,0 @@ switch (type) {

@@ -63,3 +63,3 @@ export const enum Engines {

// Optimize the rule body, this also normalizes it making the check constraint check easier
ruleBody = AbstractSQLOptimiser(ruleBody, true);
ruleBody = AbstractSQLOptimiser(ruleBody, true, abstractSqlModel);

@@ -66,0 +66,0 @@ const count = countFroms(ruleBody);

@@ -79,3 +79,3 @@ import * as AbstractSQLCompiler from '../..';

CONSTRAINT "test$hkEwz3pzAqalNu6crijhhdWJ0ffUvqRGK8rMkQbViPg=" CHECK (0 < "id"
AND "id" IS NOT NULL)
AND \"id\" IS NOT NULL)
);`,

@@ -85,2 +85,75 @@ ]);

it('should optimize null checks for a required field', () => {
expect(
generateSchema({
synonyms: {},
relationships: {},
tables: {
test: {
name: 'test',
resourceName: 'test',
idField: 'id',
fields: [
{
fieldName: 'id',
dataType: 'Integer',
required: true,
index: 'PRIMARY KEY',
},
],
indexes: [],
primitive: false,
},
},
rules: [
[
'Rule',
[
'Body',
[
'Not',
[
'Exists',
[
'SelectQuery',
['Select', []],
['From', ['test', 'test.0']],
[
'Where',
[
'Not',
[
'And',
[
'LessThan',
['Integer', 0],
['ReferencedField', 'test.0', 'id'],
],
['Exists', ['ReferencedField', 'test.0', 'id']],
],
],
],
],
],
],
] as AbstractSQLCompiler.AbstractSqlQuery,
[
'StructuredEnglish',
'It is necessary that each test has an id that is greater than 0.',
],
],
],
}),
)
.to.have.property('createSchema')
.that.deep.equals([
`\
CREATE TABLE IF NOT EXISTS "test" (
"id" INTEGER NOT NULL PRIMARY KEY
, -- It is necessary that each test has an id that is greater than 0.
CONSTRAINT "test$TIITyGYLwuTGGJjwAk8awbiE/hnw6y8rue+hQ8Pp7as=" CHECK (0 < "id")
);`,
]);
});
it('should correctly shorten a converted check rule with a long name', () => {

@@ -100,2 +173,3 @@ expect(

dataType: 'Integer',
required: true,
index: 'PRIMARY KEY',

@@ -169,8 +243,7 @@ },

CREATE TABLE IF NOT EXISTS "test_table_with_very_very_long_name" (
"id" INTEGER NULL PRIMARY KEY
"id" INTEGER NOT NULL PRIMARY KEY
, -- It is necessary that each test_table_with_very_very_long_name has an id that is greater than 0.
CONSTRAINT "test_table_with_very_very_long$9z+XEkP4EI1mhDQ8SiLulo2NLmenGY1C" CHECK (0 < "id"
AND "id" IS NOT NULL)
CONSTRAINT "test_table_with_very_very_long$/rDs8gDAB2Zoc7woBPozVMLKpx9jNTNa" CHECK (0 < "id")
);`,
]);
});

@@ -301,4 +301,3 @@ const typeVocab = require('fs').readFileSync(

"pilot-can fly-plane" AS "pilot.1-can fly-plane.0"
WHERE "pilot.1"."name" IS NOT NULL
AND "pilot.1-can fly-plane.0"."pilot" = "pilot.1"."id"
WHERE "pilot.1-can fly-plane.0"."pilot" = "pilot.1"."id"
AND "pilot.1-can fly-plane.0"."can fly-plane" = "plane.0"."id"

@@ -318,3 +317,2 @@ ) >= 3

0 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -352,4 +350,3 @@ ) AS "result";`,

) >= 2
OR 5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL)
OR 5 < "pilot.0"."years of experience")
)

@@ -373,3 +370,2 @@ ) AS "result";`,

5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -398,3 +394,2 @@ ) AS "result";`,

5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -504,3 +499,2 @@ ) AS "result";`,

AND 5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -524,3 +518,2 @@ ) AS "result";`,

5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -635,3 +628,2 @@ ) AS "result";`,

5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -660,3 +652,2 @@ ) AS "result";`,

5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -688,3 +679,2 @@ ) AS "result";`,

5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -713,3 +703,2 @@ ) AS "result";`,

5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -739,7 +728,5 @@ ) AS "result";`,

OR 10 < LENGTH("pilot.0"."name")
AND LENGTH("pilot.0"."name") IS NOT NULL
AND "pilot.0"."name" IS NOT NULL)
AND LENGTH("pilot.0"."name") IS NOT NULL)
AND NOT (
5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -767,7 +754,5 @@ ) AS "result";`,

AND 10 < LENGTH("pilot.0"."name")
AND LENGTH("pilot.0"."name") IS NOT NULL
AND "pilot.0"."name" IS NOT NULL)
AND LENGTH("pilot.0"."name") IS NOT NULL)
AND NOT (
5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -795,7 +780,5 @@ ) AS "result";`,

AND 10 < LENGTH("pilot.0"."name")
AND LENGTH("pilot.0"."name") IS NOT NULL
AND "pilot.0"."name" IS NOT NULL)
AND LENGTH("pilot.0"."name") IS NOT NULL)
AND NOT (
5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -823,7 +806,5 @@ ) AS "result";`,

AND 10 < LENGTH("pilot.0"."name")
AND LENGTH("pilot.0"."name") IS NOT NULL
AND "pilot.0"."name" IS NOT NULL)
AND LENGTH("pilot.0"."name") IS NOT NULL)
AND NOT (
5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -857,3 +838,2 @@ ) AS "result";`,

5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -889,3 +869,2 @@ ) AS "result";`,

5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -910,4 +889,3 @@ ) AS "result";`,

OR 10 < LENGTH("pilot.0"."name")
AND LENGTH("pilot.0"."name") IS NOT NULL
AND "pilot.0"."name" IS NOT NULL)
AND LENGTH("pilot.0"."name") IS NOT NULL)
AND (

@@ -920,3 +898,2 @@ SELECT COUNT(*)

5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -940,3 +917,2 @@ ) AS "result";`,

AND LENGTH("pilot.0"."name") IS NOT NULL
AND "pilot.0"."name" IS NOT NULL
OR (

@@ -949,3 +925,2 @@ SELECT COUNT(*)

5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -981,3 +956,2 @@ ) AS "result";`,

5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -1013,3 +987,2 @@ ) AS "result";`,

5 < "pilot.0"."years of experience"
AND "pilot.0"."years of experience" IS NOT NULL
)

@@ -1016,0 +989,0 @@ ) AS "result";`,

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

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc