Skip to content

Commit

Permalink
feat: handle associations
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael.Taylor committed Dec 15, 2021
1 parent abab9fd commit a41dca9
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 54 deletions.
14 changes: 14 additions & 0 deletions migrations/20211215213314-qualification-units-junction.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('qualification_unit', {
unitId: Sequelize.INTEGER,
qualificationId: Sequelize.INTEGER,
});
},

down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('qualification_unit');
},
};
6 changes: 4 additions & 2 deletions src/models/co-benefits/co-benefits.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ const { Model } = Sequelize;
import { sequelize } from '../database';
import { Project } from '../projects';

import ModelTypes from './projects.modeltypes.cjs';
import ModelTypes from './co-benifets.modeltypes.cjs';

class CoBenefit extends Model {
static associate() {
CoBenefit.belongsTo(Project);
CoBenefit.belongsTo(Project, {
onDelete: 'CASCADE',
});
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/models/co-benefits/co-benifets.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ module.exports = {
},
benefit: Sequelize.STRING,
owner: Sequelize.STRING,
projectId: Sequelize.NUMBER,
projectId: {
type: Sequelize.NUMBER,
onDelete: 'CASCADE',
},
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
};
6 changes: 4 additions & 2 deletions src/models/locations/locations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ const { Model } = Sequelize;
import { sequelize } from '../database';
import { Project } from '../projects';

import ModelTypes from './projects.modeltypes.cjs';
import ModelTypes from './locations.modeltypes.cjs';

class ProjectLocation extends Model {
static associate() {
ProjectLocation.hasMany(Project);
ProjectLocation.belongsTo(Project, {
onDelete: 'CASCADE',
});
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/models/locations/locations.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ module.exports = {
countryRegion: Sequelize.STRING,
country: Sequelize.STRING,
owner: Sequelize.STRING,
projectId: Sequelize.NUMBER,
projectId: {
type: Sequelize.NUMBER,
onDelete: 'CASCADE',
},
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
};
24 changes: 6 additions & 18 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,12 @@ import ModelTypes from './projects.modeltypes.cjs';

class Project extends Model {
static associate() {
Project.hasMany(RelatedProject, {
onDelete: 'CASCADE',
});
Project.hasMany(Vintage, {
onDelete: 'CASCADE',
});
Project.hasMany(Qualification, {
onDelete: 'CASCADE',
});
Project.hasMany(Rating, {
onDelete: 'CASCADE',
});
Project.hasMany(CoBenefit, {
onDelete: 'CASCADE',
});
Project.hasOne(ProjectLocation, {
onDelete: 'CASCADE',
});
Project.hasMany(RelatedProject);
Project.hasMany(Vintage);
Project.hasMany(Qualification);
Project.hasMany(Rating);
Project.hasMany(CoBenefit);
Project.hasMany(ProjectLocation);
}

static findAllMySQLFts(queryStr) {
Expand Down
11 changes: 9 additions & 2 deletions src/models/qualifications/qualifications.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import { sequelize } from '../database';
import { Project } from '../projects';
import { Unit } from '../units';

import ModelTypes from './projects.modeltypes.cjs';
import ModelTypes from './qualifications.modeltypes.cjs';

class Qualification extends Model {
static associate() {
// When all qualifications are removed from
// all projects and units, remove completly,
// otherwise just dissaciate
Qualification.belongsTo(Project);
Qualification.hasMany(Unit);
//Qualification.hasMany(Unit);
// https://gist.github.com/elliette/20ddc4e827efd9d62bc98752e7a62610#some-important-addendums
Qualification.belongsToMany(Unit, {
through: 'qualification_unit',
});
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/models/ratings/ratings.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ const { Model } = Sequelize;
import { sequelize } from '../database';
import { Project } from '../projects/index';

import ModelTypes from './projects.modeltypes.cjs';
import ModelTypes from './ratings.modeltypes.cjs';

class Rating extends Model {
static associate() {
Rating.belongsTo(Project);
Rating.belongsTo(Project, {
onDelete: 'CASCADE',
});
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/models/ratings/ratings.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ module.exports = {
rating: Sequelize.NUMBER,
link: Sequelize.STRING,
scale: Sequelize.STRING,
projectId: Sequelize.NUMBER,
projectId: {
type: Sequelize.NUMBER,
onDelete: 'CASCADE',
},
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
};
6 changes: 4 additions & 2 deletions src/models/related-projects/related-projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize } from '../database';

import ModelTypes from './projects.modeltypes.cjs';
import ModelTypes from './related-projects.modeltypes.cjs';

import { Project } from '../projects';

class RelatedProject extends Model {
static associate() {
RelatedProject.belongsTo(Project);
RelatedProject.belongsTo(Project, {
onDelete: 'CASCADE',
});
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/models/related-projects/related-projects.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ module.exports = {
relatedProjectType: Sequelize.STRING,
registry: Sequelize.STRING,
note: Sequelize.STRING,
projectId: Sequelize.NUMBER,
projectId: {
type: Sequelize.NUMBER,
onDelete: 'CASCADE',
},
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
};
2 changes: 1 addition & 1 deletion src/models/staging/staging.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize } from '../database';

import ModelTypes from './projects.modeltypes.cjs';
import ModelTypes from './staging.modeltypes.cjs';

class Staging extends Model {
static associate() {
Expand Down
12 changes: 8 additions & 4 deletions src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ import Sequelize from 'sequelize';
import { sequelize } from '../database';
import { Project, Qualification, Vintage } from '../../models';

import ModelTypes from './projects.modeltypes.cjs';
import ModelTypes from './units.modeltypes.cjs';

const { Model } = Sequelize;

class Unit extends Model {
static associate() {
Unit.belongsTo(Project);
Unit.hasMany(Qualification);
Unit.hasMany(Vintage);
Unit.hasOne(Vintage);

// https://gist.github.com/elliette/20ddc4e827efd9d62bc98752e7a62610#some-important-addendums
Unit.belongsToMany(Unit, {
through: 'qualification_unit',
});
}
}

Unit.init(ModelTypes, {
sequelize,
modelName: 'unit',
foreignKey: 'unitId',
});

export { Unit };
2 changes: 0 additions & 2 deletions src/models/units/units.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ module.exports = {
type: Sequelize.NUMBER,
primaryKey: true,
},
projectId: Sequelize.STRING,
buyer: Sequelize.STRING,
registry: Sequelize.STRING,
blockIdentifier: Sequelize.STRING,
identifier: Sequelize.STRING,
qualificationId: Sequelize.NUMBER,
unitType: Sequelize.STRING,
unitCount: Sequelize.NUMBER,
unitStatus: Sequelize.STRING,
Expand Down
10 changes: 0 additions & 10 deletions src/models/units/units.stub.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"registry": "registry 1",
"blockIdentifier": "bi 1",
"identifier": "i 1",
"qualificationId": 10,
"unitType": "regular",
"unitCount": 10,
"unitStatus": "draft",
Expand All @@ -16,7 +15,6 @@
"correspondingAdjustment": "1",
"unitTag": "tag1",
"vintageId": 1,
"projectId": 1,
"createdAt": "2022-11-05T14:41:00.000Z",
"updatedAt": "2022-11-05T14:41:00.000Z"
},
Expand All @@ -27,7 +25,6 @@
"registry": "registry 2",
"blockIdentifier": "bi 2",
"identifier": "i 2",
"qualificationId": 14,
"unitType": "regular",
"unitCount": 10,
"unitStatus": "draft",
Expand All @@ -37,7 +34,6 @@
"correspondingAdjustment": "1",
"unitTag": "tag2",
"vintageId": 1,
"projectId": 2,
"createdAt": "2020-11-05T14:41:00.000Z",
"updatedAt": "2020-11-05T14:41:00.000Z"
},
Expand All @@ -48,7 +44,6 @@
"registry": "registry 3",
"blockIdentifier": "bi 3",
"identifier": "i 3",
"qualificationId": 14,
"unitType": "regular",
"unitCount": 14,
"unitStatus": "published",
Expand All @@ -58,7 +53,6 @@
"correspondingAdjustment": "1",
"unitTag": "tag3",
"vintageId": 1,
"projectId": 3,
"createdAt": "2021-11-05T14:41:00.000Z",
"updatedAt": "2021-11-05T14:41:00.000Z"
},
Expand All @@ -69,7 +63,6 @@
"registry": "registry 4",
"blockIdentifier": "bi 4",
"identifier": "i 4",
"qualificationId": 14,
"unitType": "regular",
"unitCount": 14,
"unitStatus": "published",
Expand All @@ -79,7 +72,6 @@
"correspondingAdjustment": "1",
"unitTag": "tag4",
"vintageId": 1,
"projectId": 1,
"createdAt": "2025-11-05T14:41:00.000Z",
"updatedAt": "2025-11-05T14:41:00.000Z"
},
Expand All @@ -90,7 +82,6 @@
"registry": "registry 5",
"blockIdentifier": "bi 5",
"identifier": "i 5",
"qualificationId": 14,
"unitType": "regular",
"unitCount": 9,
"unitStatus": "published",
Expand All @@ -100,7 +91,6 @@
"correspondingAdjustment": "1",
"unitTag": "tag4",
"vintageId": 1,
"projectId": 1,
"createdAt": "2030-11-05T14:41:00.000Z",
"updatedAt": "2030-11-05T14:41:00.000Z"
}
Expand Down
6 changes: 3 additions & 3 deletions src/models/vintages/vintages.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize } from '../database';
import { Project, Unit } from '../projects/index';
import { Project, Unit } from '../../models';

import ModelTypes from './projects.modeltypes.cjs';
import ModelTypes from './vintages.modeltypes.cjs';

class Vintage extends Model {
static associate() {
Vintage.belongsTo(Project);
Vintage.hasMany(Unit);
Vintage.belongsToMany(Unit);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/models/vintages/vintages.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module.exports = {
verificationApproach: Sequelize.STRING,
verificationDate: Sequelize.DATE,
verificationBody: Sequelize.STRING,
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
projectId: Sequelize.NUMBER,
unitId: Sequelize.NUMBER,
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
};

0 comments on commit a41dca9

Please sign in to comment.