@@ -51,21 +51,39 @@ export abstract class BaseSchemaFormatter {
5151 tableNames : TableName [ ] ,
5252 schemaContext : SchemaContext = { }
5353 ) : SchemaFile [ ] {
54- const schemaForTables = this . scaffoldingSchema . generateForTables (
54+ const tableSchemas = this . scaffoldingSchema . generateForTables (
5555 tableNames . map ( ( n ) => this . scaffoldingSchema . resolveTableName ( n ) )
5656 ) ;
5757
58- return schemaForTables . map ( ( tableSchema ) => ( {
59- fileName : `${ tableSchema . cube } .${ this . fileExtension ( ) } ` ,
60- content : this . renderFile ( this . schemaDescriptorForTable ( tableSchema , schemaContext ) ) ,
61- } ) ) ;
58+ return this . generateFilesByTableSchemas ( tableSchemas , schemaContext ) ;
6259 }
6360
6461 public generateFilesByCubeDescriptors (
6562 cubeDescriptors : CubeDescriptor [ ] ,
6663 schemaContext : SchemaContext = { }
6764 ) : SchemaFile [ ] {
68- return this . schemaForTablesByCubeDescriptors ( cubeDescriptors ) . map ( ( tableSchema ) => ( {
65+ return this . generateFilesByTableSchemas ( this . tableSchemasByCubeDescriptors ( cubeDescriptors ) , schemaContext ) ;
66+ }
67+
68+ protected generateFilesByTableSchemas ( tableSchemas : TableSchema [ ] , schemaContext : SchemaContext = { } ) : SchemaFile [ ] {
69+ const cubeToDimensionNamesMap = new Map (
70+ tableSchemas . map ( tableSchema => [ tableSchema . cube , tableSchema . dimensions . map ( d => d . name ) ] )
71+ ) ;
72+
73+ tableSchemas = tableSchemas . map ( ( tableSchema ) => {
74+ const updatedJoins = tableSchema . joins . map ( ( join ) => ( {
75+ ...join ,
76+ thisTableColumnIncludedAsDimension : ! ! cubeToDimensionNamesMap . get ( tableSchema . cube ) ?. includes ( join . thisTableColumn ) ,
77+ columnToJoinIncludedAsDimension : ! ! cubeToDimensionNamesMap . get ( join . cubeToJoin ) ?. includes ( join . columnToJoin )
78+ } ) ) ;
79+
80+ return {
81+ ...tableSchema ,
82+ joins : updatedJoins
83+ } ;
84+ } ) ;
85+
86+ return tableSchemas . map ( ( tableSchema ) => ( {
6987 fileName : `${ tableSchema . cube } .${ this . fileExtension ( ) } ` ,
7088 content : this . renderFile ( this . schemaDescriptorForTable ( tableSchema , schemaContext ) ) ,
7189 } ) ) ;
@@ -106,7 +124,7 @@ export abstract class BaseSchemaFormatter {
106124 return ! ! name . match ( / ^ [ a - z 0 - 9 _ ] + $ / ) ;
107125 }
108126
109- public schemaDescriptorForTable ( tableSchema : TableSchema , schemaContext : SchemaContext = { } ) {
127+ protected schemaDescriptorForTable ( tableSchema : TableSchema , schemaContext : SchemaContext = { } ) {
110128 let table = `${
111129 tableSchema . schema ?. length ? `${ this . escapeName ( tableSchema . schema ) } .` : ''
112130 } ${ this . escapeName ( tableSchema . table ) } `;
@@ -130,23 +148,39 @@ export abstract class BaseSchemaFormatter {
130148 sql : `SELECT * FROM ${ table } ` ,
131149 } ;
132150
133- return {
134- cube : tableSchema . cube ,
135- ...sqlOption ,
136- ...dataSourceProp ,
151+ // Try to use dimension refs if possible
152+ // Source and target columns must be included in the respective cubes as dimensions
153+ // {CUBE.dimension_name} = {other_cube.other_dimension_name}
154+ // instead of
155+ // {CUBE}.dimension_name = {other_cube}.other_dimension_name
156+ const joins = tableSchema . joins
157+ . map ( ( j ) => {
158+ const thisTableColumnRef = j . thisTableColumnIncludedAsDimension
159+ ? this . cubeReference ( `CUBE.${ j . thisTableColumn } ` )
160+ : `${ this . cubeReference ( 'CUBE' ) } .${ this . escapeName (
161+ j . thisTableColumn
162+ ) } `;
163+ const columnToJoinRef = j . columnToJoinIncludedAsDimension
164+ ? this . cubeReference ( `${ j . cubeToJoin } .${ j . columnToJoin } ` )
165+ : `${ this . cubeReference ( j . cubeToJoin ) } .${ this . escapeName ( j . columnToJoin ) } ` ;
137166
138- joins : tableSchema . joins
139- . map ( ( j ) => ( {
167+ return ( {
140168 [ j . cubeToJoin ] : {
141- sql : `${ this . cubeReference ( 'CUBE' ) } .${ this . escapeName (
142- j . thisTableColumn
143- ) } = ${ this . cubeReference ( j . cubeToJoin ) } .${ this . escapeName ( j . columnToJoin ) } `,
169+ sql : `${ thisTableColumnRef } = ${ columnToJoinRef } ` ,
144170 relationship : this . options . snakeCase
145171 ? ( JOIN_RELATIONSHIP_MAP [ j . relationship ] ?? j . relationship )
146172 : j . relationship ,
147173 } ,
148- } ) )
149- . reduce ( ( a , b ) => ( { ...a , ...b } ) , { } ) ,
174+ } ) ;
175+ } )
176+ . reduce ( ( a , b ) => ( { ...a , ...b } ) , { } ) ;
177+
178+ return {
179+ cube : tableSchema . cube ,
180+ ...sqlOption ,
181+ ...dataSourceProp ,
182+
183+ joins,
150184 dimensions : tableSchema . dimensions . sort ( ( a ) => ( a . isPrimaryKey ? - 1 : 0 ) )
151185 . map ( ( m ) => ( {
152186 [ this . memberName ( m ) ] : {
@@ -189,7 +223,7 @@ export abstract class BaseSchemaFormatter {
189223 } ;
190224 }
191225
192- protected schemaForTablesByCubeDescriptors ( cubeDescriptors : CubeDescriptor [ ] ) {
226+ protected tableSchemasByCubeDescriptors ( cubeDescriptors : CubeDescriptor [ ] ) {
193227 const tableNames = cubeDescriptors . map ( ( { tableName } ) => tableName ) ;
194228 const generatedSchemaForTables = this . scaffoldingSchema . generateForTables (
195229 tableNames . map ( ( n ) => this . scaffoldingSchema . resolveTableName ( n ) )
0 commit comments