diff --git a/package.json b/package.json index 9cb367f..4c0b2af 100644 --- a/package.json +++ b/package.json @@ -20,20 +20,20 @@ "sha.js": "^2.4.11" }, "devDependencies": { - "@balena/abstract-sql-compiler": "^9.0.5", - "@balena/lint": "^8.0.0", + "@balena/abstract-sql-compiler": "^9.2.0", + "@balena/lint": "^8.0.2", "@types/bcrypt": "^5.0.2", - "@types/chai": "^4.3.12", + "@types/chai": "^4.3.16", "@types/chai-datetime": "^0.0.39", "@types/mocha": "^10.0.6", "@types/sha.js": "^2.4.4", "chai": "^4.4.1", "chai-datetime": "^1.8.0", - "husky": "^9.0.0", - "lint-staged": "^15.2.2", - "mocha": "^10.3.0", + "husky": "^9.0.11", + "lint-staged": "^15.2.6", + "mocha": "^10.4.0", "ts-node": "^10.9.2", - "typescript": "^5.4.2" + "typescript": "^5.4.5" }, "lint-staged": { "*.ts": [ diff --git a/src/type-utils.ts b/src/type-utils.ts index 471b86c..15a062a 100644 --- a/src/type-utils.ts +++ b/src/type-utils.ts @@ -1,3 +1,24 @@ +import type { + AnyTypeNodes, + EqualsNode, + LessThanNode, + LessThanOrEqualNode, + ReferencedFieldNode, +} from '@balena/abstract-sql-compiler'; + +export type NativeNames = Record; +export type NativeProperties = Record< + string, + Record AnyTypeNodes> +>; +export type NativeFactTypes = Record< + string, + Record< + string, + (from: ReferencedFieldNode, to: ReferencedFieldNode) => AnyTypeNodes + > +>; + export interface DatabaseTypeFn { (necessity: string, index: string): string; castType: string; @@ -48,20 +69,20 @@ const checkRequired = (validateFn: (value: any) => T | Promise) => { }; const equality = { - 'is equal to': (from: string, to: string) => ['Equals', from, to], -}; + 'is equal to': (from, to): EqualsNode => ['Equals', from, to], +} satisfies NativeFactTypes[string]; export const nativeFactTypeTemplates = { equality, comparison: { - 'is less than': (from: string, to: string) => ['LessThan', from, to], - 'is less than or equal to': (from: string, to: string) => [ + 'is less than': (from, to): LessThanNode => ['LessThan', from, to], + 'is less than or equal to': (from, to): LessThanOrEqualNode => [ 'LessThanOrEqual', from, to, ], ...equality, }, -}; +} satisfies Record; export const validate = { checkRequired, diff --git a/src/types/big-integer.ts b/src/types/big-integer.ts index 5d970e5..9e7dd57 100644 --- a/src/types/big-integer.ts +++ b/src/types/big-integer.ts @@ -12,7 +12,7 @@ export const types = { export type Types = TypeUtils.TsTypes; type DbWriteType = number; -export const nativeFactTypes = { +export const nativeFactTypes: TypeUtils.NativeFactTypes = { Integer: TypeUtils.nativeFactTypeTemplates.comparison, Real: TypeUtils.nativeFactTypeTemplates.comparison, }; diff --git a/src/types/color.ts b/src/types/color.ts index dc23998..942538d 100644 --- a/src/types/color.ts +++ b/src/types/color.ts @@ -1,3 +1,7 @@ +import type { + BitwiseAndNode, + BitwiseShiftRightNode, +} from '@balena/abstract-sql-compiler'; import * as TypeUtils from '../type-utils'; export const types = { @@ -25,23 +29,27 @@ type ColorObj = { export type Types = TypeUtils.TsTypes; type DbWriteType = number; -export const nativeProperties = { +export const nativeProperties: TypeUtils.NativeProperties = { has: { - 'Red Component': (from: string) => [ + 'Red Component': (from): BitwiseAndNode => [ 'BitwiseAnd', - ['BitwiseShiftRight', from, 16], - 255, + ['BitwiseShiftRight', from, ['Number', 16]], + ['Number', 255], ], - 'Green Component': (from: string) => [ + 'Green Component': (from): BitwiseAndNode => [ 'BitwiseAnd', - ['BitwiseShiftRight', from, 8], - 255, + ['BitwiseShiftRight', from, ['Number', 8]], + ['Number', 255], ], - 'Blue Component': (from: string) => ['BitwiseShiftRight', from, 255], - 'Alpha Component': (from: string) => [ + 'Blue Component': (from): BitwiseShiftRightNode => [ + 'BitwiseShiftRight', + from, + ['Number', 255], + ], + 'Alpha Component': (from): BitwiseAndNode => [ 'BitwiseAnd', - ['BitwiseShiftRight', from, 24], - 255, + ['BitwiseShiftRight', from, ['Number', 24]], + ['Number', 255], ], }, }; diff --git a/src/types/concept-type.ts b/src/types/concept-type.ts index cab40ca..e28155d 100644 --- a/src/types/concept-type.ts +++ b/src/types/concept-type.ts @@ -12,7 +12,7 @@ export const types = { export type Types = TypeUtils.TsTypes; type DbWriteType = number; -export const nativeFactTypes = { +export const nativeFactTypes: TypeUtils.NativeFactTypes = { Integer: TypeUtils.nativeFactTypeTemplates.comparison, Real: TypeUtils.nativeFactTypeTemplates.comparison, }; diff --git a/src/types/date-time.ts b/src/types/date-time.ts index c378d1a..79e875f 100644 --- a/src/types/date-time.ts +++ b/src/types/date-time.ts @@ -1,3 +1,7 @@ +import type { + CurrentTimestampNode, + LessThanNode, +} from '@balena/abstract-sql-compiler'; import * as TypeUtils from '../type-utils'; export const types = { @@ -29,15 +33,15 @@ export const fetchProcessing: TypeUtils.FetchProcessing = ( return date.toISOString(); }; -export const nativeFactTypes = { +export const nativeFactTypes: TypeUtils.NativeFactTypes = { 'Date Time': { ...TypeUtils.nativeFactTypeTemplates.equality, - 'is before': (from: string, to: string) => ['LessThan', from, to], + 'is before': (from, to): LessThanNode => ['LessThan', from, to], }, }; -export const nativeNames = { - 'Current Time': ['Now'], +export const nativeNames: TypeUtils.NativeNames = { + 'Current Time': ['CurrentTimestamp'] satisfies CurrentTimestampNode, }; export const validate: TypeUtils.Validate = diff --git a/src/types/date.ts b/src/types/date.ts index b407d02..9daff9e 100644 --- a/src/types/date.ts +++ b/src/types/date.ts @@ -1,3 +1,4 @@ +import type { LessThanNode } from '@balena/abstract-sql-compiler'; import * as TypeUtils from '../type-utils'; export const types = { @@ -29,10 +30,10 @@ export const fetchProcessing: TypeUtils.FetchProcessing = ( return date.toISOString(); }; -export const nativeFactTypes = { +export const nativeFactTypes: TypeUtils.NativeFactTypes = { Date: { ...TypeUtils.nativeFactTypeTemplates.equality, - 'is before': (from: string, to: string) => ['LessThan', from, to], + 'is before': (from, to): LessThanNode => ['LessThan', from, to], }, }; diff --git a/src/types/foreign-key.ts b/src/types/foreign-key.ts index cab40ca..e28155d 100644 --- a/src/types/foreign-key.ts +++ b/src/types/foreign-key.ts @@ -12,7 +12,7 @@ export const types = { export type Types = TypeUtils.TsTypes; type DbWriteType = number; -export const nativeFactTypes = { +export const nativeFactTypes: TypeUtils.NativeFactTypes = { Integer: TypeUtils.nativeFactTypeTemplates.comparison, Real: TypeUtils.nativeFactTypeTemplates.comparison, }; diff --git a/src/types/integer.ts b/src/types/integer.ts index 0dbfabb..322f8a2 100644 --- a/src/types/integer.ts +++ b/src/types/integer.ts @@ -12,7 +12,7 @@ export const types = { export type Types = TypeUtils.TsTypes; type DbWriteType = number; -export const nativeFactTypes = { +export const nativeFactTypes: TypeUtils.NativeFactTypes = { Integer: TypeUtils.nativeFactTypeTemplates.comparison, Real: TypeUtils.nativeFactTypeTemplates.comparison, }; diff --git a/src/types/real.ts b/src/types/real.ts index ae308e2..4f211f2 100644 --- a/src/types/real.ts +++ b/src/types/real.ts @@ -12,7 +12,7 @@ export const types = { export type Types = TypeUtils.TsTypes; type DbWriteType = number; -export const nativeFactTypes = { +export const nativeFactTypes: TypeUtils.NativeFactTypes = { Integer: TypeUtils.nativeFactTypeTemplates.comparison, Real: TypeUtils.nativeFactTypeTemplates.comparison, }; diff --git a/src/types/text.ts b/src/types/text.ts index 0abc572..f3736dc 100644 --- a/src/types/text.ts +++ b/src/types/text.ts @@ -1,3 +1,9 @@ +import type { + CharacterLengthNode, + StartsWithNode, + EndsWithNode, + ContainsNode, +} from '@balena/abstract-sql-compiler'; import * as TypeUtils from '../type-utils'; export const types = { @@ -12,18 +18,18 @@ export const types = { export type Types = TypeUtils.TsTypes; type DbWriteType = string; -export const nativeProperties = { +export const nativeProperties: TypeUtils.NativeProperties = { has: { - Length: (from: string) => ['CharacterLength', from], + Length: (from): CharacterLengthNode => ['CharacterLength', from], }, }; -export const nativeFactTypes = { +export const nativeFactTypes: TypeUtils.NativeFactTypes = { Text: { ...TypeUtils.nativeFactTypeTemplates.equality, - 'starts with': (from: string, to: string) => ['Startswith', from, to], - 'ends with': (from: string, to: string) => ['Endswith', from, to], - contains: (from: string, to: string) => ['Contains', from, to], + 'starts with': (from, to): StartsWithNode => ['StartsWith', from, to], + 'ends with': (from, to): EndsWithNode => ['EndsWith', from, to], + contains: (from, to): ContainsNode => ['Contains', from, to], }, }; diff --git a/src/types/web-resource.ts b/src/types/web-resource.ts index 6f75ed6..2c7dc4d 100644 --- a/src/types/web-resource.ts +++ b/src/types/web-resource.ts @@ -2,7 +2,6 @@ import * as TypeUtils from '../type-utils'; import type { CastNode, ExtractJSONPathAsTextNode, - ReferencedFieldNode, } from '@balena/abstract-sql-compiler'; export type WebResource = { @@ -35,42 +34,34 @@ export const types = { export type Types = TypeUtils.TsTypes; type DbWriteType = string; -export const nativeProperties = { +export const nativeProperties: TypeUtils.NativeProperties = { has: { - Filename: ( - referencedField: ReferencedFieldNode, - ): ExtractJSONPathAsTextNode => [ + Filename: (referencedField): ExtractJSONPathAsTextNode => [ 'ExtractJSONPathAsText', referencedField, ['TextArray', ['EmbeddedText', 'filename']], ], - HRef: (referencedField: ReferencedFieldNode): ExtractJSONPathAsTextNode => [ + HRef: (referencedField): ExtractJSONPathAsTextNode => [ 'ExtractJSONPathAsText', referencedField, ['TextArray', ['EmbeddedText', 'href']], ], - 'Content Type': ( - referencedField: ReferencedFieldNode, - ): ExtractJSONPathAsTextNode => [ + 'Content Type': (referencedField): ExtractJSONPathAsTextNode => [ 'ExtractJSONPathAsText', referencedField, ['TextArray', ['EmbeddedText', 'content_type']], ], - 'Content Disposition': ( - referencedField: ReferencedFieldNode, - ): ExtractJSONPathAsTextNode => [ + 'Content Disposition': (referencedField): ExtractJSONPathAsTextNode => [ 'ExtractJSONPathAsText', referencedField, ['TextArray', ['EmbeddedText', 'content_disposition']], ], - Checksum: ( - referencedField: ReferencedFieldNode, - ): ExtractJSONPathAsTextNode => [ + Checksum: (referencedField): ExtractJSONPathAsTextNode => [ 'ExtractJSONPathAsText', referencedField, ['TextArray', ['EmbeddedText', 'checksum']], ], - Size: (referencedField: ReferencedFieldNode): CastNode => [ + Size: (referencedField): CastNode => [ 'Cast', [ 'ExtractJSONPathAsText',