Skip to content

Commit 8f0cbb5

Browse files
committed
⬆️ improvement: ensure correct datatypes in distinction filters
1 parent fa793f8 commit 8f0cbb5

File tree

6 files changed

+122
-19
lines changed

6 files changed

+122
-19
lines changed

lib/abilityBuilder.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { and, eq, or } from "drizzle-orm";
2+
import { createDistinctValuesFromSQLType } from "./helpers/sqlTypes/distinctValuesFromSQLType";
23
import type {
34
GenericDrizzleDbTypeConstraints,
45
QueryConditionObject,
@@ -145,9 +146,18 @@ export const createAbilityBuilder = <
145146
);
146147
}
147148

149+
// we want a filter that excludes everything
150+
const distinctValues = createDistinctValuesFromSQLType(
151+
primaryKeyField.getSQLType(),
152+
);
153+
154+
// when the user has no permission for anything, ensure returns nothing
148155
conditionsPerEntityAndAction = [
149156
{
150-
where: and(eq(primaryKeyField, "1"), eq(primaryKeyField, "2")),
157+
where: and(
158+
eq(primaryKeyField, distinctValues.value1),
159+
eq(primaryKeyField, distinctValues.value2),
160+
),
151161
},
152162
];
153163
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { type PossibleSQLType, UnknownTypeRumbleError } from "./types";
2+
3+
export function createDistinctValuesFromSQLType(sqlType: PossibleSQLType): {
4+
value1: any;
5+
value2: any;
6+
} {
7+
if (
8+
["serial", "int", "integer", "tinyint", "smallint", "mediumint"].includes(
9+
sqlType,
10+
)
11+
) {
12+
return {
13+
value1: 1,
14+
value2: 2,
15+
};
16+
}
17+
18+
if (["real", "decimal", "double", "float"].includes(sqlType)) {
19+
return {
20+
value1: 1.1,
21+
value2: 2.2,
22+
};
23+
}
24+
25+
if (["string", "text", "varchar", "char", "text(256)"].includes(sqlType)) {
26+
return {
27+
value1: "a",
28+
value2: "b",
29+
};
30+
}
31+
32+
if (["uuid"].includes(sqlType)) {
33+
return {
34+
value1: "fba31870-5528-42d7-b27e-2e5ee657aea5",
35+
value2: "fc65db81-c2d1-483d-8a25-a30e2cf6e02d",
36+
};
37+
}
38+
39+
if (["boolean"].includes(sqlType)) {
40+
return {
41+
value1: true,
42+
value2: false,
43+
};
44+
}
45+
46+
if (["timestamp", "datetime"].includes(sqlType)) {
47+
return {
48+
value1: new Date(2022, 1, 1),
49+
value2: new Date(2022, 1, 2),
50+
};
51+
}
52+
53+
if (["date"].includes(sqlType)) {
54+
return {
55+
value1: new Date(2022, 1, 1),
56+
value2: new Date(2022, 1, 2),
57+
};
58+
}
59+
60+
if (["json"].includes(sqlType)) {
61+
return {
62+
value1: { a: 1 },
63+
value2: { b: 2 },
64+
};
65+
}
66+
67+
throw UnknownTypeRumbleError(sqlType, "Distinct");
68+
}

lib/helpers/mapSQLTypeToTSType.ts renamed to lib/helpers/sqlTypes/mapSQLTypeToTSType.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { SchemaBuilderType } from "../schemaBuilder";
2-
import type { GenericDrizzleDbTypeConstraints } from "../types/genericDrizzleDbType";
3-
import { RumbleError } from "../types/rumbleError";
1+
import type { SchemaBuilderType } from "../../schemaBuilder";
2+
import type { GenericDrizzleDbTypeConstraints } from "../../types/genericDrizzleDbType";
3+
import { type PossibleSQLType, UnknownTypeRumbleError } from "./types";
44

55
export function mapSQLTypeToGraphQLType<
66
UserContext extends Record<string, any>,
@@ -13,20 +13,12 @@ export function mapSQLTypeToGraphQLType<
1313
RequestEvent,
1414
Action
1515
>,
16-
>(sqlType: string) {
16+
>(sqlType: PossibleSQLType) {
1717
type ReturnType = Parameters<
1818
Parameters<Parameters<SchemaBuilder["queryField"]>[1]>[0]["field"]
1919
>[0]["type"];
2020

2121
let ret: ReturnType | undefined = undefined;
22-
// Int
23-
// Float
24-
// String
25-
// ID
26-
// Boolean
27-
// DateTime
28-
// Date
29-
// JSON
3022

3123
if (
3224
["serial", "int", "integer", "tinyint", "smallint", "mediumint"].includes(
@@ -36,7 +28,7 @@ export function mapSQLTypeToGraphQLType<
3628
ret = "Int";
3729
}
3830

39-
if (["real", "decimal", "real", "double", "float"].includes(sqlType)) {
31+
if (["real", "decimal", "double", "float"].includes(sqlType)) {
4032
ret = "Float";
4133
}
4234

@@ -68,7 +60,5 @@ export function mapSQLTypeToGraphQLType<
6860
return ret;
6961
}
7062

71-
throw new RumbleError(
72-
`RumbleError: Unknown SQL type '${sqlType}'. Please open an issue so it can be added.`,
73-
);
63+
throw UnknownTypeRumbleError(sqlType, "SQL to GQL");
7464
}

lib/helpers/sqlTypes/types.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { RumbleError } from "../../types/rumbleError";
2+
3+
export const possibleSQLTypesStrings = [
4+
"serial",
5+
"int",
6+
"integer",
7+
"tinyint",
8+
"smallint",
9+
"mediumint",
10+
"real",
11+
"decimal",
12+
"double",
13+
"float",
14+
"string",
15+
"text",
16+
"varchar",
17+
"char",
18+
"text(256)",
19+
"uuid",
20+
"boolean",
21+
"date",
22+
"datetime",
23+
"timestamp",
24+
"json",
25+
] as const;
26+
27+
export type PossibleSQLType = (typeof possibleSQLTypesStrings)[number];
28+
29+
export const UnknownTypeRumbleError = (
30+
sqlType: string,
31+
additionalInfo?: string,
32+
) =>
33+
new RumbleError(
34+
`RumbleError: Unknown SQL type '${sqlType}'. Please open an issue (https://github.com/m1212e/rumble/issues) so it can be added. (${additionalInfo})`,
35+
);

lib/object.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mapSQLTypeToGraphQLType } from "./helpers/mapSQLTypeToTSType";
1+
import { mapSQLTypeToGraphQLType } from "./helpers/sqlTypes/mapSQLTypeToTSType";
22
import { type MakePubSubInstanceType, createPubSubInstance } from "./pubsub";
33
import type { SchemaBuilderType } from "./schemaBuilder";
44
import type { GenericDrizzleDbTypeConstraints } from "./types/genericDrizzleDbType";

lib/whereArg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { and, eq } from "drizzle-orm";
22
import { capitalizeFirstLetter } from "./helpers/capitalize";
3-
import { mapSQLTypeToGraphQLType } from "./helpers/mapSQLTypeToTSType";
3+
import { mapSQLTypeToGraphQLType } from "./helpers/sqlTypes/mapSQLTypeToTSType";
44
import type { SchemaBuilderType } from "./schemaBuilder";
55
import type { GenericDrizzleDbTypeConstraints } from "./types/genericDrizzleDbType";
66
import { RumbleError } from "./types/rumbleError";

0 commit comments

Comments
 (0)