Skip to content

Commit 5c7d7e7

Browse files
authored
Use column names instead of accessors for ts queries (#4627)
# Description of Changes When creating sql queries, we were using the column accessor name, rather than the name used in the database. This should fix that. # Expected complexity level and risk 1. # Testing This has some unit tests.
1 parent a16c6fe commit 5c7d7e7

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

crates/bindings-typescript/src/lib/query.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ function createRowExpr<TableDef extends TypedTableDef>(
348348
columnBuilder.typeBuilder.algebraicType as InferSpacetimeTypeOfColumn<
349349
TableDef,
350350
typeof columnName
351-
>
351+
>,
352+
columnBuilder.columnMetadata.name
352353
);
353354
row[columnName] = Object.freeze(column);
354355
}
@@ -438,7 +439,10 @@ export class ColumnExpression<
438439
ColumnName extends ColumnNames<TableDef>,
439440
> {
440441
readonly type = 'column' as const;
442+
// This is the column accessor
441443
readonly column: ColumnName;
444+
// The name of the column in the database.
445+
readonly columnName: string;
442446
readonly table: TableDef['sourceName'];
443447
// phantom: actual runtime value is undefined
444448
readonly tsValueType?: RowType<TableDef>[ColumnName];
@@ -447,10 +451,12 @@ export class ColumnExpression<
447451
constructor(
448452
table: TableDef['sourceName'],
449453
column: ColumnName,
450-
spacetimeType: InferSpacetimeTypeOfColumn<TableDef, ColumnName>
454+
spacetimeType: InferSpacetimeTypeOfColumn<TableDef, ColumnName>,
455+
columnName?: string
451456
) {
452457
this.table = table;
453458
this.column = column;
459+
this.columnName = columnName || column;
454460
this.spacetimeType = spacetimeType;
455461
}
456462

@@ -838,7 +844,7 @@ function valueExprToSql<Table extends TypedTableDef>(
838844
return literalValueToSql(expr.value);
839845
}
840846
const table = tableAlias ?? expr.table;
841-
return `${quoteIdentifier(table)}.${quoteIdentifier(expr.column)}`;
847+
return `${quoteIdentifier(table)}.${quoteIdentifier(expr.columnName)}`;
842848
}
843849

844850
function literalValueToSql(value: unknown): string {

crates/bindings-typescript/tests/query.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,20 @@ const ordersTable = table(
5454
}
5555
);
5656

57+
const renamedColumnsTable = table(
58+
{
59+
name: 'renamed_columns',
60+
},
61+
{
62+
displayName: t.string().name('display_name'),
63+
ageYears: t.u32().name('age_years'),
64+
}
65+
);
66+
5767
const schemaDef = tablesToSchema(new ModuleContext(), {
5868
person: personTable,
5969
orders: ordersTable,
70+
renamedColumns: renamedColumnsTable,
6071
});
6172

6273
describe('Timestamp thing', () => {
@@ -347,4 +358,17 @@ describe('TableScan.toSql', () => {
347358
const sql = toSql(qb.person);
348359
expect(sql).toBe('SELECT * FROM "person"');
349360
});
361+
362+
it('uses DB column names for accessors with explicit DB names', () => {
363+
const qb = makeQueryBuilder(schemaDef);
364+
const sql = toSql(
365+
qb.renamedColumns
366+
.where(row => row.displayName.eq('Alice').and(row.ageYears.gt(30)))
367+
.build()
368+
);
369+
370+
expect(sql).toBe(
371+
`SELECT * FROM "renamed_columns" WHERE ("renamed_columns"."display_name" = 'Alice') AND ("renamed_columns"."age_years" > 30)`
372+
);
373+
});
350374
});

0 commit comments

Comments
 (0)