@@ -22,7 +22,7 @@ import { stripAlias } from './kysely-utils';
2222
2323type Scope = {
2424 model ?: string ;
25- alias ?: string ;
25+ alias ?: OperationNode ;
2626 namesMapped ?: boolean ; // true means fields referring to this scope have their names already mapped
2727} ;
2828
@@ -120,7 +120,7 @@ export class QueryNameMapper extends OperationNodeTransformer {
120120 // map table name depending on how it is resolved
121121 let mappedTableName = node . table ?. table . identifier . name ;
122122 if ( mappedTableName ) {
123- if ( scope . alias === mappedTableName ) {
123+ if ( scope . alias && IdentifierNode . is ( scope . alias ) && scope . alias . name === mappedTableName ) {
124124 // table name is resolved to an alias, no mapping needed
125125 } else if ( scope . model === mappedTableName ) {
126126 // table name is resolved to a model, map the name as needed
@@ -222,7 +222,14 @@ export class QueryNameMapper extends OperationNodeTransformer {
222222 const origFieldName = this . extractFieldName ( selection . selection ) ;
223223 const fieldName = this . extractFieldName ( transformed ) ;
224224 if ( fieldName !== origFieldName ) {
225- selections . push ( SelectionNode . create ( this . wrapAlias ( transformed , origFieldName ) ) ) ;
225+ selections . push (
226+ SelectionNode . create (
227+ this . wrapAlias (
228+ transformed ,
229+ origFieldName ? IdentifierNode . create ( origFieldName ) : undefined ,
230+ ) ,
231+ ) ,
232+ ) ;
226233 } else {
227234 selections . push ( SelectionNode . create ( transformed ) ) ;
228235 }
@@ -241,7 +248,7 @@ export class QueryNameMapper extends OperationNodeTransformer {
241248 // if the field as a qualifier, the qualifier must match the scope's
242249 // alias if any, or model if no alias
243250 if ( scope . alias ) {
244- if ( scope . alias === qualifier ) {
251+ if ( scope . alias && IdentifierNode . is ( scope . alias ) && scope . alias . name === qualifier ) {
245252 // scope has an alias that matches the qualifier
246253 return scope ;
247254 } else {
@@ -295,8 +302,8 @@ export class QueryNameMapper extends OperationNodeTransformer {
295302 }
296303 }
297304
298- private wrapAlias < T extends OperationNode > ( node : T , alias : string | undefined ) {
299- return alias ? AliasNode . create ( node , IdentifierNode . create ( alias ) ) : node ;
305+ private wrapAlias < T extends OperationNode > ( node : T , alias : OperationNode | undefined ) {
306+ return alias ? AliasNode . create ( node , alias ) : node ;
300307 }
301308
302309 private processTableRef ( node : TableNode ) {
@@ -351,11 +358,11 @@ export class QueryNameMapper extends OperationNodeTransformer {
351358 // inner transformations will map column names
352359 const modelName = innerNode . table . identifier . name ;
353360 const mappedName = this . mapTableName ( modelName ) ;
354- const finalAlias = alias ?? ( mappedName !== modelName ? modelName : undefined ) ;
361+ const finalAlias = alias ?? ( mappedName !== modelName ? IdentifierNode . create ( modelName ) : undefined ) ;
355362 return {
356363 node : this . wrapAlias ( TableNode . create ( mappedName ) , finalAlias ) ,
357364 scope : {
358- alias : alias ?? modelName ,
365+ alias : alias ?? IdentifierNode . create ( modelName ) ,
359366 model : modelName ,
360367 namesMapped : ! this . hasMappedColumns ( modelName ) ,
361368 } ,
@@ -374,13 +381,13 @@ export class QueryNameMapper extends OperationNodeTransformer {
374381 }
375382 }
376383
377- private createSelectAllFields ( model : string , alias : string | undefined ) {
384+ private createSelectAllFields ( model : string , alias : OperationNode | undefined ) {
378385 const modelDef = requireModel ( this . schema , model ) ;
379386 return this . getModelFields ( modelDef ) . map ( ( fieldDef ) => {
380387 const columnName = this . mapFieldName ( model , fieldDef . name ) ;
381388 const columnRef = ReferenceNode . create (
382389 ColumnNode . create ( columnName ) ,
383- alias ? TableNode . create ( alias ) : undefined ,
390+ alias && IdentifierNode . is ( alias ) ? TableNode . create ( alias . name ) : undefined ,
384391 ) ;
385392 if ( columnName !== fieldDef . name ) {
386393 const aliased = AliasNode . create ( columnRef , IdentifierNode . create ( fieldDef . name ) ) ;
@@ -421,7 +428,7 @@ export class QueryNameMapper extends OperationNodeTransformer {
421428 alias = this . extractFieldName ( node ) ;
422429 }
423430 const result = super . transformNode ( node ) ;
424- return this . wrapAlias ( result , alias ) ;
431+ return this . wrapAlias ( result , alias ? IdentifierNode . create ( alias ) : undefined ) ;
425432 }
426433
427434 private processSelectAll ( node : SelectAllNode ) {
@@ -438,7 +445,9 @@ export class QueryNameMapper extends OperationNodeTransformer {
438445 return this . getModelFields ( modelDef ) . map ( ( fieldDef ) => {
439446 const columnName = this . mapFieldName ( modelDef . name , fieldDef . name ) ;
440447 const columnRef = ReferenceNode . create ( ColumnNode . create ( columnName ) ) ;
441- return columnName !== fieldDef . name ? this . wrapAlias ( columnRef , fieldDef . name ) : columnRef ;
448+ return columnName !== fieldDef . name
449+ ? this . wrapAlias ( columnRef , IdentifierNode . create ( fieldDef . name ) )
450+ : columnRef ;
442451 } ) ;
443452 }
444453
0 commit comments