Skip to content

Commit

Permalink
Add typings for all abstract sql fragments, fixing where necessary
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
Page- committed Jun 12, 2024
1 parent fcab1e2 commit b84c335
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 56 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
31 changes: 26 additions & 5 deletions src/type-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
import type {
AnyTypeNodes,
EqualsNode,
LessThanNode,
LessThanOrEqualNode,
ReferencedFieldNode,
} from '@balena/abstract-sql-compiler';

export type NativeNames = Record<string, AnyTypeNodes>;
export type NativeProperties = Record<
string,
Record<string, (from: ReferencedFieldNode) => AnyTypeNodes>
>;
export type NativeFactTypes = Record<
string,
Record<
string,
(from: ReferencedFieldNode, to: ReferencedFieldNode) => AnyTypeNodes
>
>;

export interface DatabaseTypeFn {
(necessity: string, index: string): string;
castType: string;
Expand Down Expand Up @@ -48,20 +69,20 @@ const checkRequired = <T>(validateFn: (value: any) => T | Promise<T>) => {
};

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<string, NativeFactTypes[string]>;

export const validate = {
checkRequired,
Expand Down
2 changes: 1 addition & 1 deletion src/types/big-integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const types = {
export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;

export const nativeFactTypes = {
export const nativeFactTypes: TypeUtils.NativeFactTypes = {
Integer: TypeUtils.nativeFactTypeTemplates.comparison,
Real: TypeUtils.nativeFactTypeTemplates.comparison,
};
Expand Down
30 changes: 19 additions & 11 deletions src/types/color.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type {
BitwiseAndNode,
BitwiseShiftRightNode,
} from '@balena/abstract-sql-compiler';
import * as TypeUtils from '../type-utils';

export const types = {
Expand Down Expand Up @@ -25,23 +29,27 @@ type ColorObj = {
export type Types = TypeUtils.TsTypes<ColorObj, number | ColorObj>;
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],
],
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/types/concept-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const types = {
export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;

export const nativeFactTypes = {
export const nativeFactTypes: TypeUtils.NativeFactTypes = {
Integer: TypeUtils.nativeFactTypeTemplates.comparison,
Real: TypeUtils.nativeFactTypeTemplates.comparison,
};
Expand Down
12 changes: 8 additions & 4 deletions src/types/date-time.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type {
CurrentTimestampNode,
LessThanNode,
} from '@balena/abstract-sql-compiler';
import * as TypeUtils from '../type-utils';

export const types = {
Expand Down Expand Up @@ -29,15 +33,15 @@ export const fetchProcessing: TypeUtils.FetchProcessing<Types['Read']> = (
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<Types['Write'], DbWriteType> =
Expand Down
5 changes: 3 additions & 2 deletions src/types/date.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { LessThanNode } from '@balena/abstract-sql-compiler';
import * as TypeUtils from '../type-utils';

export const types = {
Expand Down Expand Up @@ -29,10 +30,10 @@ export const fetchProcessing: TypeUtils.FetchProcessing<Types['Read']> = (
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],
},
};

Expand Down
2 changes: 1 addition & 1 deletion src/types/foreign-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const types = {
export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;

export const nativeFactTypes = {
export const nativeFactTypes: TypeUtils.NativeFactTypes = {
Integer: TypeUtils.nativeFactTypeTemplates.comparison,
Real: TypeUtils.nativeFactTypeTemplates.comparison,
};
Expand Down
2 changes: 1 addition & 1 deletion src/types/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const types = {
export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;

export const nativeFactTypes = {
export const nativeFactTypes: TypeUtils.NativeFactTypes = {
Integer: TypeUtils.nativeFactTypeTemplates.comparison,
Real: TypeUtils.nativeFactTypeTemplates.comparison,
};
Expand Down
2 changes: 1 addition & 1 deletion src/types/real.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const types = {
export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;

export const nativeFactTypes = {
export const nativeFactTypes: TypeUtils.NativeFactTypes = {
Integer: TypeUtils.nativeFactTypeTemplates.comparison,
Real: TypeUtils.nativeFactTypeTemplates.comparison,
};
Expand Down
18 changes: 12 additions & 6 deletions src/types/text.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import type {
CharacterLengthNode,
StartsWithNode,
EndsWithNode,
ContainsNode,
} from '@balena/abstract-sql-compiler';
import * as TypeUtils from '../type-utils';

export const types = {
Expand All @@ -12,18 +18,18 @@ export const types = {
export type Types = TypeUtils.TsTypes<string, string>;
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],
},
};

Expand Down
23 changes: 7 additions & 16 deletions src/types/web-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as TypeUtils from '../type-utils';
import type {
CastNode,
ExtractJSONPathAsTextNode,
ReferencedFieldNode,
} from '@balena/abstract-sql-compiler';

export type WebResource = {
Expand Down Expand Up @@ -35,42 +34,34 @@ export const types = {
export type Types = TypeUtils.TsTypes<WebResource, WebResource>;
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',
Expand Down

0 comments on commit b84c335

Please sign in to comment.