@@ -16,6 +16,7 @@ import {
1616 OperationNodeTransformer ,
1717 PrimitiveValueListNode ,
1818 type QueryId ,
19+ RawNode ,
1920 ReferenceNode ,
2021 ReturningNode ,
2122 SelectAllNode ,
@@ -763,6 +764,21 @@ export class QueryNameMapper extends OperationNodeTransformer {
763764 if ( mappedValue ) {
764765 return mappedValue ;
765766 }
767+ } else if (
768+ this . isOperationNode ( value ) &&
769+ ValueNode . is ( value ) &&
770+ Array . isArray ( value . value ) &&
771+ value . value . every ( ( v ) => typeof v === 'string' )
772+ ) {
773+ const everyMappedValueExists = value . value . every ( ( v ) => enumValueMapping [ v ] ) ;
774+ if ( everyMappedValueExists ) {
775+ return ValueNode . create ( value . value . map ( ( v ) => enumValueMapping [ v ] ) ) ;
776+ }
777+ } else if ( Array . isArray ( value ) && value . every ( ( v ) => typeof v === 'string' ) ) {
778+ const everyMappedValueExists = value . every ( ( v ) => enumValueMapping [ v ] ) ;
779+ if ( everyMappedValueExists ) {
780+ return value . map ( ( v ) => enumValueMapping [ v ] ) ;
781+ }
766782 }
767783
768784 return value ;
@@ -789,23 +805,39 @@ export class QueryNameMapper extends OperationNodeTransformer {
789805 return selection ;
790806 }
791807
808+ const shouldUseArray = fieldDef . array ;
809+
792810 const eb = expressionBuilder ( ) ;
793- const caseBuilder = eb . case ( ) ;
811+ const caseNode = shouldUseArray ? ColumnNode . create ( 't' ) : node ;
794812 let caseWhen : CaseWhenBuilder < any , any , any , any > | undefined ;
813+
795814 for ( const [ key , value ] of Object . entries ( enumValueMapping ) ) {
796815 if ( ! caseWhen ) {
797- caseWhen = caseBuilder . when ( new ExpressionWrapper ( node ) , '=' , value ) . then ( key ) ;
816+ caseWhen = eb . case ( ) . when ( new ExpressionWrapper ( caseNode ) , '=' , value ) . then ( key ) ;
798817 } else {
799- caseWhen = caseWhen . when ( new ExpressionWrapper ( node ) , '=' , value ) . then ( key ) ;
818+ caseWhen = caseWhen . when ( new ExpressionWrapper ( caseNode ) , '=' , value ) . then ( key ) ;
800819 }
801820 }
802821
803822 // the explicit cast to "text" is needed to address postgres's case-when type inference issue
804- const finalExpr = caseWhen ! . else ( this . dialect . castText ( new ExpressionWrapper ( node ) ) ) . end ( ) ;
823+ const caseExpr = caseWhen ! . else ( this . dialect . castText ( new ExpressionWrapper ( caseNode ) ) ) . end ( ) ;
824+
825+ if ( ! shouldUseArray ) {
826+ if ( aliasName ) {
827+ return caseExpr . as ( aliasName ) . toOperationNode ( ) as SelectionNodeChild ;
828+ } else {
829+ return caseExpr . toOperationNode ( ) as SelectionNodeChild ;
830+ }
831+ }
832+
833+ const arrayExpr = new ExpressionWrapper (
834+ RawNode . create ( [ 'ARRAY(SELECT ' , ' FROM unnest( ' , ' ) AS t)' ] , [ caseExpr . toOperationNode ( ) , node ] ) ,
835+ ) ;
836+
805837 if ( aliasName ) {
806- return finalExpr . as ( aliasName ) . toOperationNode ( ) as SelectionNodeChild ;
838+ return arrayExpr . as ( aliasName ) . toOperationNode ( ) as SelectionNodeChild ;
807839 } else {
808- return finalExpr . toOperationNode ( ) as SelectionNodeChild ;
840+ return arrayExpr . toOperationNode ( ) as SelectionNodeChild ;
809841 }
810842 }
811843
0 commit comments