@@ -287,6 +287,24 @@ analysisReports.hasMany(proteinVariants, {
287287 as : 'proteinVariants' , foreignKey : 'reportId' , onDelete : 'CASCADE' , constraints : true ,
288288} ) ;
289289
290+ const observedVariantAnnotations = require ( './reports/observedVariantAnnotations' ) ( sequelize , Sq ) ;
291+
292+ for ( const [ pivotValue , modelName ] of Object . entries ( KB_PIVOT_MAPPING ) ) {
293+ sequelize . models [ modelName ] . hasOne ( observedVariantAnnotations , {
294+ foreignKey : 'variantId' ,
295+ constraints : false ,
296+ scope : {
297+ [ KB_PIVOT_COLUMN ] : pivotValue ,
298+ } ,
299+ as : 'observedVariantAnnotation' ,
300+ } ) ;
301+ observedVariantAnnotations . belongsTo ( sequelize . models [ modelName ] , {
302+ foreignKey : 'variantId' ,
303+ constraints : false ,
304+ as : modelName ,
305+ } ) ;
306+ }
307+
290308// This adds the gene to variant relationships to the table which have a foreign key to the genes table
291309for ( const name of GENE_LINKED_VARIANT_MODELS ) {
292310 const variantModel = sequelize . models [ name ] ;
@@ -420,31 +438,15 @@ for (const [pivotValue, modelName] of Object.entries(KB_PIVOT_MAPPING)) {
420438 } ) ;
421439}
422440
423- // IMPORTANT: Must be defined after variant models so that the includes can be found
424- const observedVariantAnnotations = require ( './reports/observedVariantAnnotations' ) ( sequelize , Sq ) ;
441+ // IMPORTANT: Must add scope after variant models so that the includes can be found
442+ const extendedScope = {
443+ attributes : { exclude : [ 'id' , 'reportId' , 'variantId' , 'deletedAt' , 'updatedBy' ] } ,
444+ include : Object . values ( KB_PIVOT_MAPPING ) . map ( ( modelName ) => {
445+ return { model : sequelize . models [ modelName ] . scope ( 'public' ) , as : modelName } ;
446+ } ) ,
447+ } ;
425448
426- for ( const [ pivotValue , modelName ] of Object . entries ( KB_PIVOT_MAPPING ) ) {
427- sequelize . models [ modelName ] . hasOne ( observedVariantAnnotations , {
428- foreignKey : 'variantId' ,
429- constraints : false ,
430- scope : {
431- [ KB_PIVOT_COLUMN ] : pivotValue ,
432- } ,
433- as : 'observedVariantAnnotation' ,
434- } ) ;
435- observedVariantAnnotations . belongsTo ( sequelize . models [ modelName ] , {
436- foreignKey : 'variantId' ,
437- constraints : false ,
438- as : modelName ,
439- } ) ;
440- }
441-
442- observedVariantAnnotations . belongsTo ( analysisReports , {
443- as : 'report' , foreignKey : 'reportId' , targetKey : 'id' , onDelete : 'CASCADE' , constraints : true , foreignKeyConstraint : true ,
444- } ) ;
445- analysisReports . hasMany ( observedVariantAnnotations , {
446- as : 'observedVariantAnnotations' , foreignKey : 'reportId' , onDelete : 'CASCADE' , constraints : true , foreignKeyConstraint : true ,
447- } ) ;
449+ observedVariantAnnotations . addScope ( 'extended' , extendedScope ) ;
448450
449451// Presentation Data
450452const presentation = { } ;
0 commit comments