@@ -400,6 +400,45 @@ class ApiGateway {
400400 } )
401401 ) ;
402402
403+ app . get (
404+ `${ this . basePath } /v1/meta/model_name` ,
405+ userMiddlewares ,
406+ userAsyncHandler ( async ( req , res ) => {
407+ await this . metaModelName ( {
408+ context : req . context ,
409+ res : this . resToResultFn ( res ) ,
410+ } ) ;
411+ } )
412+ ) ;
413+
414+ app . get (
415+ `${ this . basePath } /v1/meta/:nameModel` ,
416+ userMiddlewares ,
417+ userAsyncHandler ( async ( req , res ) => {
418+ const { nameModel } = req . params ;
419+ await this . metaModelByName ( {
420+ context : req . context ,
421+ nameModel,
422+ res : this . resToResultFn ( res ) ,
423+ } ) ;
424+ } )
425+ ) ;
426+
427+ app . get (
428+ `${ this . basePath } /v1/meta/:nameModel/:field` ,
429+ userMiddlewares ,
430+ userAsyncHandler ( async ( req , res ) => {
431+ const { nameModel } = req . params ;
432+ const { field } = req . params ;
433+ await this . metaFieldModelByName ( {
434+ context : req . context ,
435+ nameModel,
436+ field,
437+ res : this . resToResultFn ( res ) ,
438+ } ) ;
439+ } )
440+ ) ;
441+
403442 app . post (
404443 `${ this . basePath } /v1/cubesql` ,
405444 userMiddlewares ,
@@ -577,35 +616,54 @@ class ApiGateway {
577616 } ) ) . filter ( cube => cube . config . measures ?. length || cube . config . dimensions ?. length || cube . config . segments ?. length ) ;
578617 }
579618
619+ private transformCubeConfig ( cube : any , cubeDefinitions : any ) {
620+ return {
621+ ...transformCube ( cube , cubeDefinitions ) ,
622+ measures : cube . measures ?. map ( measure => transformMeasure ( measure , cubeDefinitions ) ) ,
623+ dimensions : cube . dimensions ?. map ( dimension => transformDimension ( dimension , cubeDefinitions ) ) ,
624+ segments : cube . segments ?. map ( segment => transformSegment ( segment , cubeDefinitions ) ) ,
625+ joins : transformJoins ( cubeDefinitions [ cube . name ] ?. joins ) ,
626+ preAggregations : transformPreAggregations ( cubeDefinitions [ cube . name ] ?. preAggregations )
627+ } ;
628+ }
629+
630+ private extractModelNames ( cubes : any [ ] ) {
631+ return cubes . map ( cube => ( { name : cube . config ?. name } ) ) ;
632+ }
633+
634+ private async fetchMetaConfig ( context : RequestContext , includeCompiler : boolean ) {
635+ const compilerApi = await this . getCompilerApi ( context ) ;
636+ return compilerApi . metaConfig ( context , {
637+ requestId : context . requestId ,
638+ includeCompilerId : includeCompiler
639+ } ) ;
640+ }
641+
642+ private async fetchMetaConfigExtended ( context : ExtendedRequestContext ) {
643+ const compilerApi = await this . getCompilerApi ( context ) ;
644+ return compilerApi . metaConfigExtended ( context , {
645+ requestId : context . requestId
646+ } ) ;
647+ }
648+
580649 public async meta ( { context, res, includeCompilerId, onlyCompilerId } : {
581650 context : RequestContext ,
582651 res : MetaResponseResultFn ,
583652 includeCompilerId ?: boolean ,
584653 onlyCompilerId ?: boolean
585654 } ) {
586655 const requestStarted = new Date ( ) ;
587-
588656 try {
589657 await this . assertApiScope ( 'meta' , context . securityContext ) ;
590- const compilerApi = await this . getCompilerApi ( context ) ;
591- const metaConfig = await compilerApi . metaConfig ( context , {
592- requestId : context . requestId ,
593- includeCompilerId : includeCompilerId || onlyCompilerId
594- } ) ;
658+ const metaConfig = await this . fetchMetaConfig ( context , ! ! ( includeCompilerId || onlyCompilerId ) ) ;
595659 if ( onlyCompilerId ) {
596- const response : { cubes : any [ ] , compilerId ?: string } = {
597- cubes : [ ] ,
598- compilerId : metaConfig . compilerId
599- } ;
600- res ( response ) ;
660+ res ( { cubes : [ ] , compilerId : metaConfig . compilerId } ) ;
601661 return ;
602662 }
603663 const cubesConfig = includeCompilerId ? metaConfig . cubes : metaConfig ;
604- const cubes = this . filterVisibleItemsInMeta ( context , cubesConfig ) . map ( cube => cube . config ) ;
664+ const cubes = this . filterVisibleItemsInMeta ( context , cubesConfig ) . map ( ( cube : any ) => cube . config ) ;
605665 const response : { cubes : any [ ] , compilerId ?: string } = { cubes } ;
606- if ( includeCompilerId ) {
607- response . compilerId = metaConfig . compilerId ;
608- }
666+ if ( includeCompilerId ) response . compilerId = metaConfig . compilerId ;
609667 res ( response ) ;
610668 } catch ( e : any ) {
611669 this . handleError ( {
@@ -620,39 +678,74 @@ class ApiGateway {
620678
621679 public async metaExtended ( { context, res } : { context : ExtendedRequestContext , res : ResponseResultFn } ) {
622680 const requestStarted = new Date ( ) ;
681+ try {
682+ await this . assertApiScope ( 'meta' , context . securityContext ) ;
683+ const { metaConfig, cubeDefinitions } = await this . fetchMetaConfigExtended ( context ) ;
684+ const cubes = this . filterVisibleItemsInMeta ( context , metaConfig )
685+ . map ( ( meta : any ) => meta . config )
686+ . map ( ( cube : any ) => this . transformCubeConfig ( cube , cubeDefinitions ) ) ;
687+ res ( { cubes } ) ;
688+ } catch ( e : any ) {
689+ this . handleError ( { e, context, res, requestStarted } ) ;
690+ }
691+ }
623692
693+ public async metaModelName ( { context, res } : { context : ExtendedRequestContext , res : ResponseResultFn } ) {
694+ const requestStarted = new Date ( ) ;
624695 try {
625696 await this . assertApiScope ( 'meta' , context . securityContext ) ;
626- const compilerApi = await this . getCompilerApi ( context ) ;
627- const metaConfigExtended = await compilerApi . metaConfigExtended ( context , {
628- requestId : context . requestId ,
629- } ) ;
630- const { metaConfig, cubeDefinitions } = metaConfigExtended ;
697+ const { metaConfig } = await this . fetchMetaConfigExtended ( context ) ;
698+ const cubes = this . extractModelNames ( metaConfig ) ;
699+ res ( { cubes } ) ;
700+ } catch ( e : any ) {
701+ this . handleError ( { e, context, res, requestStarted } ) ;
702+ }
703+ }
631704
632- const cubes = this . filterVisibleItemsInMeta ( context , metaConfig )
633- . map ( ( meta ) => meta . config )
634- . map ( ( cube ) => ( {
635- ...transformCube ( cube , cubeDefinitions ) ,
636- measures : cube . measures ?. map ( ( measure ) => ( {
637- ...transformMeasure ( measure , cubeDefinitions ) ,
638- } ) ) ,
639- dimensions : cube . dimensions ?. map ( ( dimension ) => ( {
640- ...transformDimension ( dimension , cubeDefinitions ) ,
641- } ) ) ,
642- segments : cube . segments ?. map ( ( segment ) => ( {
643- ...transformSegment ( segment , cubeDefinitions ) ,
644- } ) ) ,
645- joins : transformJoins ( cubeDefinitions [ cube . name ] ?. joins ) ,
646- preAggregations : transformPreAggregations ( cubeDefinitions [ cube . name ] ?. preAggregations ) ,
647- } ) ) ;
705+ private async getModelCubes ( context : ExtendedRequestContext , nameModel : string ) {
706+ const { metaConfig, cubeDefinitions } = await this . fetchMetaConfigExtended ( context ) ;
707+ return this . filterVisibleItemsInMeta ( context , metaConfig )
708+ . map ( ( meta : any ) => meta . config )
709+ . map ( ( cube : any ) => this . transformCubeConfig ( cube , cubeDefinitions ) )
710+ . filter ( ( cube : any ) => cube . name === nameModel ) ;
711+ }
712+
713+ public async metaModelByName ( { context, nameModel, res } : { context : ExtendedRequestContext , nameModel : string , res : ResponseResultFn } ) {
714+ const requestStarted = new Date ( ) ;
715+ try {
716+ await this . assertApiScope ( 'meta' , context . securityContext ) ;
717+ const cubes = await this . getModelCubes ( context , nameModel ) ;
718+ if ( ! cubes . length ) {
719+ res ( { error : `Model ${ nameModel } not found` } ) ;
720+ return ;
721+ }
648722 res ( { cubes } ) ;
649723 } catch ( e : any ) {
650- this . handleError ( {
651- e,
652- context,
653- res,
654- requestStarted,
655- } ) ;
724+ this . handleError ( { e, context, res, requestStarted } ) ;
725+ }
726+ }
727+
728+ public async metaFieldModelByName ( { context, nameModel, field, res } : { context : ExtendedRequestContext , nameModel : string , field ?: string , res : ResponseResultFn } ) {
729+ const requestStarted = new Date ( ) ;
730+ try {
731+ await this . assertApiScope ( 'meta' , context . securityContext ) ;
732+ const cubes = await this . getModelCubes ( context , nameModel ) ;
733+ if ( ! cubes . length ) {
734+ res ( { error : `Model ${ nameModel } not found` } ) ;
735+ return ;
736+ }
737+ if ( field ) {
738+ const cube = cubes [ 0 ] ;
739+ if ( ! ( field in cube ) ) {
740+ res ( { error : `Field ${ field } not found in model ${ nameModel } ` } ) ;
741+ return ;
742+ }
743+ res ( { value : cube [ field ] } ) ;
744+ return ;
745+ }
746+ res ( { cubes } ) ;
747+ } catch ( e : any ) {
748+ this . handleError ( { e, context, res, requestStarted } ) ;
656749 }
657750 }
658751
0 commit comments