Skip to content

Commit 9a7732c

Browse files
committed
include observedCariantAnnotation in kbMatchedStatements; move the definition of observedVariantAnnotations before the variant models and add scope later
1 parent 1a14204 commit 9a7732c

3 files changed

Lines changed: 36 additions & 34 deletions

File tree

app/models/index.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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
291309
for (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
450452
const presentation = {};

app/models/reports/kbMatchedStatements.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,16 @@ module.exports = (sequelize, Sq) => {
122122
as: 'kbMatches',
123123
include: [
124124
...Object.values(KB_PIVOT_MAPPING).map((modelName) => {
125-
return {model: sequelize.models[modelName].scope('public'), as: modelName};
125+
return {
126+
model: sequelize.models[modelName].scope('public'),
127+
as: modelName,
128+
include: [
129+
{
130+
model: sequelize.models.observedVariantAnnotations,
131+
as: 'observedVariantAnnotation',
132+
}
133+
]
134+
};
126135
}),
127136
],
128137
attributes: {

app/models/reports/observedVariantAnnotations.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,10 @@ module.exports = (sequelize, Sq) => {
4848
scopes: {
4949
public: {
5050
attributes: {exclude: ['id', 'reportId', 'variantId', 'deletedAt', 'updatedBy']},
51-
include: Object.values(KB_PIVOT_MAPPING).map((modelName) => {
52-
return {model: sequelize.models[modelName].scope('public'), as: modelName};
53-
}),
5451
},
5552
minimal: {
5653
attributes: {exclude: ['id', 'reportId', 'variantId', 'deletedAt', 'updatedBy']},
5754
},
58-
extended: {
59-
attributes: {exclude: ['id', 'reportId', 'variantId', 'deletedAt', 'updatedBy']},
60-
include: Object.values(KB_PIVOT_MAPPING).map((modelName) => {
61-
return {model: sequelize.models[modelName].scope('extended'), as: modelName};
62-
}),
63-
},
6455
},
6556
hooks: {
6657
...DEFAULT_REPORT_OPTIONS.hooks,

0 commit comments

Comments
 (0)