Skip to content

Commit 6d1a7dc

Browse files
authored
fix json/b nested fields casing (#421)
1 parent 05622b1 commit 6d1a7dc

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

.changeset/proud-webs-see.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@ts-safeql/eslint-plugin": patch
3+
"@ts-safeql/generate": patch
4+
---
5+
6+
fixed an issue where json/b fields weren't transformed when using `fieldTransform`

packages/eslint-plugin/src/rules/check-sql.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,6 +1878,33 @@ RuleTester.describe("check-sql", () => {
18781878
\`;
18791879
`,
18801880
},
1881+
{
1882+
name: "json/b: fieldTransform camel should apply to jsonb_build_object keys",
1883+
options: withConnection(connections.withTag, {
1884+
targets: [{ tag: "sql", fieldTransform: "camel" }],
1885+
}),
1886+
code: `sql<{ candidateLocations: { isSelected: boolean }[] | null }>\`
1887+
SELECT
1888+
jsonb_agg(
1889+
jsonb_build_object(
1890+
'is_selected', TRUE
1891+
)
1892+
) AS candidate_locations
1893+
\``,
1894+
},
1895+
{
1896+
name: "json/b: fieldTransform camel should apply to nested jsonb_build_object keys",
1897+
options: withConnection(connections.withTag, {
1898+
targets: [{ tag: "sql", fieldTransform: "camel" }],
1899+
}),
1900+
code: `sql<{ metadata: { outerKey: { innerKey: 'value' } } }>\`
1901+
SELECT
1902+
jsonb_build_object(
1903+
'outer_key',
1904+
jsonb_build_object('inner_key', 'value')
1905+
) AS metadata
1906+
\``,
1907+
},
18811908
],
18821909
invalid: [
18831910
{
@@ -1997,6 +2024,29 @@ RuleTester.describe("check-sql", () => {
19972024
`,
19982025
errors: [{ messageId: "missingTypeAnnotations" }],
19992026
},
2027+
{
2028+
name: "json/b: fieldTransform camel should enforce transformation on jsonb keys",
2029+
options: withConnection(connections.withTag, {
2030+
targets: [{ tag: "sql", fieldTransform: "camel" }],
2031+
}),
2032+
code: `sql<{ candidateLocations: { is_selected: boolean }[] | null }>\`
2033+
SELECT
2034+
jsonb_agg(
2035+
jsonb_build_object(
2036+
'is_selected', TRUE
2037+
)
2038+
) AS candidate_locations
2039+
\``,
2040+
output: `sql<{ candidateLocations: { isSelected: boolean }[] | null }>\`
2041+
SELECT
2042+
jsonb_agg(
2043+
jsonb_build_object(
2044+
'is_selected', TRUE
2045+
)
2046+
) AS candidate_locations
2047+
\``,
2048+
errors: [{ messageId: "incorrectTypeAnnotations" }],
2049+
},
20002050
],
20012051
});
20022052

packages/generate/src/ast-describe.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fmap, normalizeIndent } from "@ts-safeql/shared";
1+
import { fmap, normalizeIndent, toCase, IdentiferCase } from "@ts-safeql/shared";
22
import * as LibPgQueryAST from "@ts-safeql/sql-ast";
33
import {
44
isColumnStarRef,
@@ -26,6 +26,7 @@ type ASTDescriptionOptions = {
2626
pgTypes: PgTypesMap;
2727
pgEnums: PgEnumsMaps;
2828
pgFns: Map<string, { ts: string; pg: string }>;
29+
fieldTransform: IdentiferCase | undefined;
2930
};
3031

3132
type ASTDescriptionContext = ASTDescriptionOptions & {
@@ -571,6 +572,7 @@ function getDescribedSelectStmt({
571572
pgTypes: context.pgTypes,
572573
pgEnums: context.pgEnums,
573574
pgFns: context.pgFns,
575+
fieldTransform: context.fieldTransform,
574576
});
575577

576578
const firstColumn = subDescription.map.get(0);
@@ -936,7 +938,8 @@ function getDescribedJsonBuildObjectFunCall({
936938
return [unknownDescribedColumn];
937939
}
938940

939-
describedColumn.value.push([alias, resolveType({ context, nullable: false, type })]);
941+
const transformedKey = toCase(alias, context.fieldTransform);
942+
describedColumn.value.push([transformedKey, resolveType({ context, nullable: false, type })]);
940943
}
941944

942945
return [

packages/generate/src/generate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ async function generate(
271271
pgEnums: pgEnums,
272272
pgFns: functionsMap,
273273
typeExprMap: pgTypeExprMap,
274+
fieldTransform: params.fieldTransform,
274275
});
275276

276277
const columns = result.columns.map((col, position): ColumnAnalysisResult => {

0 commit comments

Comments
 (0)