@balena/abstract-sql-compiler
Advanced tools
Comparing version
@@ -7,4 +7,5 @@ # Change Log | ||
## 9.1.0 - 2024-01-05 | ||
## 9.1.0 - 2024-03-21 | ||
* Update TypeScript to 5.4.3 [Thodoris Greasidis] | ||
* Export the generateRuleHashAcronym helper [Thodoris Greasidis] | ||
@@ -14,2 +15,10 @@ * Add support for unique indexes with NOT DISTINCT NULLS [Thodoris Greasidis] | ||
## 9.0.6 - 2024-03-21 | ||
* Update TypeScript to 5.4.3 [Thodoris Greasidis] | ||
## 9.0.5 - 2024-03-05 | ||
* Update `@balena/sbvr-types` [Harald Fischer] | ||
## 9.0.4 - 2024-01-02 | ||
@@ -16,0 +25,0 @@ |
@@ -275,3 +275,3 @@ export declare const enum Engines { | ||
description?: string; | ||
nulls?: string; | ||
distinctNulls?: boolean; | ||
predicate?: BooleanTypeNodes; | ||
@@ -369,3 +369,3 @@ } | ||
} | ||
export declare const isAliasNode: <T>(n: AbstractSqlType | AliasNode<T>) => n is AliasNode<T>; | ||
export declare const isAliasNode: <T>(n: AliasNode<T> | AbstractSqlType) => n is AliasNode<T>; | ||
export declare const isFromNode: (n: AbstractSqlType) => n is FromNode; | ||
@@ -385,3 +385,3 @@ export declare const isTableNode: (n: AbstractSqlType) => n is TableNode; | ||
optimizeSchema: (abstractSqlModel: AbstractSqlModel, createCheckConstraints?: boolean) => AbstractSqlModel; | ||
generateRuleHashAcronym: (tableName: string, ruleBody: AbstractSqlType) => string; | ||
generateRuleSlug: (tableName: string, ruleBody: AbstractSqlType) => string; | ||
compileSchema: (abstractSqlModel: AbstractSqlModel) => SqlModel; | ||
@@ -396,3 +396,3 @@ compileRule: (abstractSQL: AbstractSqlQuery) => SqlResult | [SqlResult, SqlResult]; | ||
optimizeSchema: (abstractSqlModel: AbstractSqlModel, createCheckConstraints?: boolean) => AbstractSqlModel; | ||
generateRuleHashAcronym: (tableName: string, ruleBody: AbstractSqlType) => string; | ||
generateRuleSlug: (tableName: string, ruleBody: AbstractSqlType) => string; | ||
compileSchema: (abstractSqlModel: AbstractSqlModel) => SqlModel; | ||
@@ -407,3 +407,3 @@ compileRule: (abstractSQL: AbstractSqlQuery) => SqlResult | [SqlResult, SqlResult]; | ||
optimizeSchema: (abstractSqlModel: AbstractSqlModel, createCheckConstraints?: boolean) => AbstractSqlModel; | ||
generateRuleHashAcronym: (tableName: string, ruleBody: AbstractSqlType) => string; | ||
generateRuleSlug: (tableName: string, ruleBody: AbstractSqlType) => string; | ||
compileSchema: (abstractSqlModel: AbstractSqlModel) => SqlModel; | ||
@@ -410,0 +410,0 @@ compileRule: (abstractSQL: AbstractSqlQuery) => SqlResult | [SqlResult, SqlResult]; |
@@ -226,3 +226,9 @@ "use strict"; | ||
for (const index of table.indexes) { | ||
const nullsSql = index.nulls != null ? ` NULLS ${index.nulls}` : ''; | ||
let nullsSql = ''; | ||
if (index.distinctNulls != null) { | ||
nullsSql = | ||
index.distinctNulls === false | ||
? ` NULLS NOT DISTINCT` | ||
: ` NULLS DISTINCT`; | ||
} | ||
if (index.predicate == null) { | ||
@@ -236,3 +242,3 @@ createSqlElements.push(index.type + nullsSql + '("' + index.fields.join('", "') + '")'); | ||
const comment = index.description | ||
? `-- ${index.description.split(/\r?\n/).join('\n-- ')}\n` | ||
? `-- ${index.description.replaceAll(/\r?\n/g, '\n-- ')}\n` | ||
: ''; | ||
@@ -386,3 +392,3 @@ const whereSql = compileRule(index.predicate, engine, true); | ||
optimizeSchema: AbstractSQLSchemaOptimiser_1.optimizeSchema, | ||
generateRuleHashAcronym: AbstractSQLSchemaOptimiser_1.generateRuleHashAcronym, | ||
generateRuleSlug: AbstractSQLSchemaOptimiser_1.generateRuleSlug, | ||
compileSchema: (abstractSqlModel) => compileSchema(abstractSqlModel, engine, ifNotExists), | ||
@@ -389,0 +395,0 @@ compileRule: (abstractSQL) => compileRule(abstractSQL, engine, false), |
@@ -8,3 +8,3 @@ export declare const enum Engines { | ||
import { AbstractSqlModel, AbstractSqlType } from './AbstractSQLCompiler'; | ||
export declare const generateRuleHashAcronym: (tableName: string, ruleBody: AbstractSqlType) => string; | ||
export declare const generateRuleSlug: (tableName: string, ruleBody: AbstractSqlType) => string; | ||
export declare const optimizeSchema: (abstractSqlModel: AbstractSqlModel, createCheckConstraints?: boolean) => AbstractSqlModel; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.optimizeSchema = exports.generateRuleHashAcronym = exports.Engines = void 0; | ||
exports.optimizeSchema = exports.generateRuleSlug = exports.Engines = void 0; | ||
var Engines; | ||
@@ -28,7 +28,7 @@ (function (Engines) { | ||
}; | ||
const generateRuleHashAcronym = (tableName, ruleBody) => { | ||
const generateRuleSlug = (tableName, ruleBody) => { | ||
const sha = sbvr_types_1.default.SHA.validateSync(`${tableName}$${JSON.stringify(ruleBody)}`).replace(/^\$sha256\$/, ''); | ||
return `${tableName.slice(0, 30)}$${sha}`.slice(0, 63); | ||
}; | ||
exports.generateRuleHashAcronym = generateRuleHashAcronym; | ||
exports.generateRuleSlug = generateRuleSlug; | ||
const optimizeSchema = (abstractSqlModel, createCheckConstraints = true) => { | ||
@@ -96,3 +96,3 @@ abstractSqlModel.rules = abstractSqlModel.rules | ||
description: ruleSE, | ||
name: (0, exports.generateRuleHashAcronym)(tableName, ruleBody), | ||
name: (0, exports.generateRuleSlug)(tableName, ruleBody), | ||
abstractSql: whereNode, | ||
@@ -99,0 +99,0 @@ }); |
{ | ||
"name": "@balena/abstract-sql-compiler", | ||
"version": "9.1.0-build-partial-unique-index-555a2f627402e1dca0fc32611e708d460f6ff6b2-1", | ||
"version": "9.1.0-build-partial-unique-index-694b370a6cf006d4cc16a3d8396c1a90f5d7d91b-1", | ||
"description": "A translator for abstract sql into sql.", | ||
@@ -19,3 +19,3 @@ "main": "out/AbstractSQLCompiler.js", | ||
"dependencies": { | ||
"@balena/sbvr-types": "^6.0.0", | ||
"@balena/sbvr-types": "^7.0.1", | ||
"lodash": "^4.17.21" | ||
@@ -40,3 +40,3 @@ }, | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.0.4" | ||
"typescript": "^5.4.3" | ||
}, | ||
@@ -66,4 +66,4 @@ "lint-staged": { | ||
"versionist": { | ||
"publishedAt": "2024-01-05T16:42:59.831Z" | ||
"publishedAt": "2024-03-21T17:23:24.104Z" | ||
} | ||
} |
@@ -14,9 +14,7 @@ export const enum Engines { | ||
export { Binding, SqlResult } from './AbstractSQLRules2SQL'; | ||
import type { SbvrType } from '@balena/sbvr-types'; | ||
import sbvrTypes from '@balena/sbvr-types'; | ||
import * as _ from 'lodash'; | ||
import { optimizeSchema, generateRuleSlug } from './AbstractSQLSchemaOptimiser'; | ||
import { | ||
optimizeSchema, | ||
generateRuleHashAcronym, | ||
} from './AbstractSQLSchemaOptimiser'; | ||
import { | ||
getReferencedFields, | ||
@@ -459,4 +457,5 @@ getRuleReferencedFields, | ||
name?: string; | ||
/** For rules converted to partial unique indexes this holds the actual rule expression */ | ||
description?: string; | ||
nulls?: string; | ||
distinctNulls?: boolean; | ||
predicate?: BooleanTypeNodes; | ||
@@ -587,3 +586,6 @@ } | ||
const { dataType, required = false } = field; | ||
const validateFn = validateTypes[dataType as keyof typeof sbvrTypes]; | ||
// Without the `: SbvrType['validate']` widening TS complains that | ||
// "none of those signatures are compatible with each other". | ||
const validateFn: SbvrType['validate'] = | ||
validateTypes[dataType as keyof typeof sbvrTypes]; | ||
if (validateFn != null) { | ||
@@ -884,3 +886,9 @@ return validateFn(value, required); | ||
for (const index of table.indexes) { | ||
const nullsSql = index.nulls != null ? ` NULLS ${index.nulls}` : ''; | ||
let nullsSql = ''; | ||
if (index.distinctNulls != null) { | ||
nullsSql = | ||
index.distinctNulls === false | ||
? ` NULLS NOT DISTINCT` | ||
: ` NULLS DISTINCT`; | ||
} | ||
// Non-partial indexes are added directly to the CREATE TABLE statement | ||
@@ -897,3 +905,3 @@ if (index.predicate == null) { | ||
const comment = index.description | ||
? `-- ${index.description.split(/\r?\n/).join('\n-- ')}\n` | ||
? `-- ${index.description.replaceAll(/\r?\n/g, '\n-- ')}\n` | ||
: ''; | ||
@@ -1086,3 +1094,3 @@ const whereSql = compileRule( | ||
optimizeSchema, | ||
generateRuleHashAcronym, | ||
generateRuleSlug, | ||
compileSchema: (abstractSqlModel: AbstractSqlModel) => | ||
@@ -1089,0 +1097,0 @@ compileSchema(abstractSqlModel, engine, ifNotExists), |
@@ -35,3 +35,3 @@ export const enum Engines { | ||
export const generateRuleHashAcronym = ( | ||
export const generateRuleSlug = ( | ||
tableName: string, | ||
@@ -133,3 +133,3 @@ ruleBody: AbstractSqlType, | ||
description: ruleSE, | ||
name: generateRuleHashAcronym(tableName, ruleBody), | ||
name: generateRuleSlug(tableName, ruleBody), | ||
abstractSql: whereNode, | ||
@@ -136,0 +136,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
841175
0.23%17824
0.08%+ Added
- Removed
Updated