Skip to content

Commit

Permalink
feat: perfect associations, seed association data, eager loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael.Taylor committed Dec 16, 2021
1 parent 4dc71a6 commit 5c011a2
Show file tree
Hide file tree
Showing 26 changed files with 227 additions and 171 deletions.
4 changes: 1 addition & 3 deletions migrations/20211201194720-create-party.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('parties', {
Expand All @@ -17,9 +18,6 @@ module.exports = {
registry: {
type: Sequelize.STRING,
},
owner: {
type: Sequelize.STRING,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
Expand Down
2 changes: 2 additions & 0 deletions migrations/20211215213314-qualification-units-junction.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module.exports = {
await queryInterface.createTable('qualification_unit', {
unitId: Sequelize.INTEGER,
qualificationId: Sequelize.INTEGER,
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
});
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module.exports = {
await queryInterface.createTable('vintage_unit', {
unitId: Sequelize.INTEGER,
vintageId: Sequelize.INTEGER,
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
});
},

Expand Down
18 changes: 16 additions & 2 deletions seeders/20211209204301-add-projects.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
'use strict';
const ProjectStub = require('../src/models/projects/projects.stub.json');
const CoBenifetStub = require('../src/models/co-benefits/co-benefits.stub.json');
const RelatedProjectStub = require('../src/models/related-projects/related-projects.stub.json');
const QualificationStub = require('../src/models/qualifications/qualifications.stub.json');
const RatingsStub = require('../src/models/ratings/ratings.stub.json');
const VintagesStub = require('../src/models/vintages/vintages.stub.json');
const LocationsStub = require('../src/models/locations/locations.stub.json');

module.exports = {
up: async (queryInterface) =>
queryInterface.bulkInsert('projects', ProjectStub, {}),
up: async (queryInterface) => {
await queryInterface.bulkInsert('projects', ProjectStub, {});
await queryInterface.bulkInsert('coBenefits', CoBenifetStub, {});
await queryInterface.bulkInsert('relatedProjects', RelatedProjectStub, {});
await queryInterface.bulkInsert('qualifications', QualificationStub, {});
await queryInterface.bulkInsert('projectRatings', RatingsStub, {});
await queryInterface.bulkInsert('vintages', VintagesStub, {});
await queryInterface.bulkInsert('projectLocations', LocationsStub, {});
},

down: async (queryInterface) => {
await queryInterface.bulkDelete('projects');
},
Expand Down
20 changes: 16 additions & 4 deletions seeders/20211209205139-add-units.cjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
'use strict';
const UnitStub = require('../src/models/units/units.stub.json');

const JunctionStub = [
{
unitId: 1,
qualificationId: 1,
},
{
unitId: 1,
qualificationId: 2,
},
];
module.exports = {
up: async (queryInterface) =>
queryInterface.bulkInsert('Units', UnitStub, {}),
up: async (queryInterface) => {
await queryInterface.bulkInsert('units', UnitStub, {});
await queryInterface.bulkInsert('qualification_unit', JunctionStub, {});
},

down: async (queryInterface) => {
await queryInterface.bulkDelete('Units');
await queryInterface.bulkDelete('units');
},
};
8 changes: 7 additions & 1 deletion src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ export const findAll = async (req, res) => {

res.json(
await Unit.findAll({
include: [Qualification, Vintage],
include: [
{
model: Qualification,
as: 'qualification',
},
Vintage,
],
}),
);
};
Expand Down
15 changes: 10 additions & 5 deletions src/models/co-benefits/co-benefits.stub.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
[
{
"cobenefit": "TEST_COBENEFIT_1"
"benefit": "TEST_COBENEFIT_1",
"projectId": 1
},
{
"cobenefit": "TEST_COBENEFIT_2"
"benefit": "TEST_COBENEFIT_2",
"projectId": 1
},
{
"cobenefit": "TEST_COBENEFIT_3"
"benefit": "TEST_COBENEFIT_3",
"projectId": 1
},
{
"cobenefit": "TEST_COBENEFIT_4"
"benefit": "TEST_COBENEFIT_4",
"projectId": 1
},
{
"cobenefit": "TEST_COBENEFIT_5"
"benefit": "TEST_COBENEFIT_5",
"projectId": 1
}
]
6 changes: 3 additions & 3 deletions src/models/co-benefits/co-benifets.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ const Sequelize = require('sequelize');

module.exports = {
id: {
type: Sequelize.NUMBER,
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
benefit: Sequelize.STRING,
owner: Sequelize.STRING,
projectId: {
type: Sequelize.NUMBER,
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
},
createdAt: Sequelize.DATE,
Expand Down
6 changes: 3 additions & 3 deletions src/models/locations/locations.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ const Sequelize = require('sequelize');

module.exports = {
id: {
type: Sequelize.NUMBER,
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
countryRegion: Sequelize.STRING,
country: Sequelize.STRING,
owner: Sequelize.STRING,
projectId: {
type: Sequelize.NUMBER,
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
},
createdAt: Sequelize.DATE,
Expand Down
20 changes: 10 additions & 10 deletions src/models/locations/locations.stub.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
[
{
"in_country_region": true,
"host_country": "United States"
"countryRegion": "United States",
"projectId": 1
},
{
"in_country_region": true,
"host_country": "United States"
"countryRegion": "United States",
"projectId": 1
},
{
"in_country_region": false,
"host_country": "China"
"countryRegion": "China",
"projectId": 2
},
{
"in_country_region": false,
"host_country": "Russia"
"countryRegion": "Russia",
"projectId": 2
},
{
"in_country_region": true,
"host_country": "United States"
"countryRegion": "United States",
"projectId": 2
}
]
1 change: 0 additions & 1 deletion src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class Project extends Model {
validationApproach MATCH ":search" OR
projectTag MATCH ":search" OR
estimatedAnnualAverageEmissionReduction MATCH ":search" OR
owner MATCH ":search"
ORDER BY rank DESC`;

return sequelize.query(sql, {
Expand Down
4 changes: 2 additions & 2 deletions src/models/projects/projects.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ const Sequelize = require('sequelize');

module.exports = {
id: {
type: Sequelize.NUMBER,
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
warehouseProjectId: Sequelize.STRING,
projectID: Sequelize.STRING,
Expand All @@ -28,7 +29,6 @@ module.exports = {
validationDate: Sequelize.DATE,
projectTag: Sequelize.STRING,
estimatedAnnualAverageEmissionReduction: Sequelize.STRING,
owner: Sequelize.STRING,
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
};
2 changes: 1 addition & 1 deletion src/models/qualifications/qualifications.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Qualification extends Model {
// https://gist.github.com/elliette/20ddc4e827efd9d62bc98752e7a62610#some-important-addendums
Qualification.belongsToMany(Unit, {
through: 'qualification_unit',
as: 'qualification',
as: 'unit',
});
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/models/qualifications/qualifications.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ const Sequelize = require('sequelize');

module.exports = {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
qualificationId: Sequelize.NUMBER,
qualificationId: Sequelize.INTEGER,
qualificationLink: Sequelize.STRING,
projectId: Sequelize.NUMBER,
projectId: Sequelize.INTEGER,
type: Sequelize.STRING,
label: Sequelize.STRING,
creditingPeriodStartDate: Sequelize.DATE,
creditingPeriodEndDate: Sequelize.DATE,
unitId: Sequelize.NUMBER,
unitId: Sequelize.INTEGER,
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
};
21 changes: 2 additions & 19 deletions src/models/qualifications/qualifications.stub.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
[
{
"id": "1",
"projectId": 1,
"projectId": 2,
"type": "basic type",
"label": "qualification 1",
"creditingPeriodStartDate": "2011-10-05T14:48:00.000Z",
"creditingPeriodEndDate": "2022-10-05T14:48:00.000Z",
"validityStartDate": "2011-11-05T14:41:00.000Z",
"validityEndDate": "2022-11-05T14:41:00.000Z",
"unitQuantity": 1,
"unitId": 1,
"qualificationLink": "https://qualification.link/1"
},
{
Expand All @@ -18,45 +16,30 @@
"label": "qualification 2",
"creditingPeriodStartDate": "2011-10-05T14:48:00.000Z",
"creditingPeriodEndDate": "2022-10-05T14:48:00.000Z",
"validityStartDate": "2011-11-05T14:41:00.000Z",
"validityEndDate": "2022-11-05T14:41:00.000Z",
"unitQuantity": 1,
"qualificationLink": "https://qualification.link/2"
},
{
"id": "3",
"projectId": 1,
"type": "basic type",
"label": "qualification 3",
"creditingPeriodStartDate": "2010-10-05T14:48:00.000Z",
"creditingPeriodEndDate": "2022-10-05T14:48:00.000Z",
"validityStartDate": "2011-10-05T14:41:00.000Z",
"validityEndDate": "2021-11-05T14:41:00.000Z",
"unitQuantity": 10,
"qualificationLink": "https://qualification.link/3"
},
{
"id": "4",
"projectId": 1,
"type": "basic type",
"label": "qualification 4",
"creditingPeriodStartDate": "2011-10-05T14:48:00.000Z",
"creditingPeriodEndDate": "2022-10-05T14:48:00.000Z",
"validityStartDate": "2011-11-05T14:41:00.000Z",
"validityEndDate": "2022-11-05T14:41:00.000Z",
"unitQuantity": 1,
"qualificationLink": "https://qualification.link/4"
},
{
"id": "5",
"projectId": 1,
"type": "basic type",
"label": "qualification 5",
"creditingPeriodStartDate": "2010-10-05T14:48:00.000Z",
"creditingPeriodEndDate": "2021-10-05T14:48:00.000Z",
"validityStartDate": "2010-11-05T14:41:00.000Z",
"validityEndDate": "2021-11-05T14:41:00.000Z",
"unitQuantity": 5,
"qualificationLink": "https://qualification.link/5"
}
]
7 changes: 4 additions & 3 deletions src/models/ratings/ratings.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ const Sequelize = require('sequelize');

module.exports = {
id: {
type: Sequelize.NUMBER,
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
ratingType: Sequelize.STRING,
type: Sequelize.STRING,
rating: Sequelize.NUMBER,
link: Sequelize.STRING,
scale: Sequelize.STRING,
projectId: {
type: Sequelize.NUMBER,
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
},
createdAt: Sequelize.DATE,
Expand Down
Loading

0 comments on commit 5c011a2

Please sign in to comment.