Skip to content
9 changes: 5 additions & 4 deletions app/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ userMetadata.belongsTo(user, {

// Projects
const project = require('./project/project')(sequelize, Sq);
const projectVariantTextJoin = require('./project/projectVariantTextJoin')(sequelize, Sq);
const userProject = require('./project/userProject')(sequelize, Sq);
const reportProject = require('./project/reportProject')(sequelize, Sq);

Expand Down Expand Up @@ -610,14 +611,14 @@ const variantText = require('./variantText/variantText')(sequelize, Sq);
template.hasMany(variantText, {
as: 'variant_texts', foreignKey: 'templateId', onDelete: 'CASCADE', constraints: true,
});
project.hasMany(variantText, {
as: 'variant_texts', foreignKey: 'projectId', onDelete: 'CASCADE', constraints: true,
project.belongsToMany(variantText, {
as: 'variant_texts', through: {model: projectVariantTextJoin, unique: false}, foreignKey: 'projectId', onDelete: 'CASCADE', constraints: true,
});
variantText.belongsTo(template, {
as: 'template', foreignKey: 'templateId', targetKey: 'id', onDelete: 'CASCADE', constraints: true,
});
variantText.belongsTo(project, {
as: 'project', foreignKey: 'projectId', targetKey: 'id', onDelete: 'CASCADE', constraints: true,
variantText.belongsToMany(project, {
as: 'projects', through: {model: projectVariantTextJoin, unique: false}, foreignKey: 'variantTextId', onDelete: 'CASCADE', constraints: true,
});

// Template Appendix
Expand Down
5 changes: 4 additions & 1 deletion app/models/project/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ module.exports = (sequelize, Sq) => {
scopes: {
public: {
attributes: {
exclude: ['id', 'deletedAt', 'updatedBy'],
exclude: ['deletedAt', 'updatedBy'],
},
},
minimal: {
attributes: ['ident', 'name'],
},
Comment thread
bnguyen-bcgsc marked this conversation as resolved.
variantText: {
attributes: ['id', 'ident', 'name', 'description'],
},
},
});

Expand Down
53 changes: 53 additions & 0 deletions app/models/project/projectVariantTextJoin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const {DEFAULT_MAPPING_COLUMNS, DEFAULT_MAPPING_OPTIONS} = require('../base');

module.exports = (sequelize, Sq) => {
return sequelize.define(
'projectVariantTextJoin',
{
...DEFAULT_MAPPING_COLUMNS,
projectId: {
name: 'projectId',
field: 'project_id',
type: Sq.INTEGER,
unique: false,
allowNull: false,
references: {
model: 'projects',
key: 'id',
},
},
variantTextId: {
name: 'variantTextId',
field: 'variant_text_id',
type: Sq.INTEGER,
unique: false,
allowNull: false,
references: {
model: 'variant_texts',
key: 'id',
},
},
},
{
...DEFAULT_MAPPING_OPTIONS,
tableName: 'project_variant_text_join',
scopes: {
public: {
attributes: {
exclude: ['id', 'deletedAt'],
},
},
},
indexes: [
{
name: 'idx_project_id_join',
fields: ['project_id'],
},
{
name: 'idx_variant_text_id_join',
fields: ['variant_text_id'],
},
],
},
);
};
17 changes: 6 additions & 11 deletions app/models/variantText/variantText.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ module.exports = (sequelize, Sq) => {
'variantText',
{
...DEFAULT_COLUMNS,
projectId: {
type: Sq.INTEGER,
name: 'projectId',
field: 'project_id',
references: {
model: 'projects',
key: 'id',
},
},
templateId: {
type: Sq.INTEGER,
name: 'templateId',
Expand Down Expand Up @@ -59,13 +50,17 @@ module.exports = (sequelize, Sq) => {
},
include: [
{model: sequelize.models.template.scope('minimal'), as: 'template'},
{model: sequelize.models.project.scope('minimal'), as: 'project'},
{
model: sequelize.models.project.scope('minimal'),
as: 'projects',
through: {attributes: []},
},
],
},
extended: {
include: [
{model: sequelize.models.template, as: 'template'},
{model: sequelize.models.project, as: 'project'},
{model: sequelize.models.project, as: 'projects', through: {attributes: []}},
],
},
},
Expand Down
Loading
Loading