Skip to content

Commit

Permalink
feat: relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeen committed Dec 8, 2021
1 parent 6ba5853 commit 0cd24ce
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/models/co-benefits/co-benefits.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize } from '../database';
import { Project } from '../projects';

class CoBenefit extends Model {
/**
Expand All @@ -10,7 +11,7 @@ class CoBenefit extends Model {
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
CoBenefit.belongsTo(Project);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/models/locations/locations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize } from '../database';
import { Project } from '../projects';

class ProjectLocation extends Model {
/**
Expand All @@ -10,7 +11,7 @@ class ProjectLocation extends Model {
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
ProjectLocation.belongsTo(Project);
}
}

Expand Down
16 changes: 14 additions & 2 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@ import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize } from '../database';

import { RelatedProject } from './../related-projects';
import { Vintage } from './../vintages';
import { Qualification } from './../qualifications';
import { ProjectLocation } from './../locations';
import { Rating } from './../ratings';
import { CoBenefit } from './../co-benefits';

class Project extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
Project.hasMany(RelatedProject);
Project.hasMany(Vintage);
Project.hasMany(Qualification);
Project.hasMany(Rating);
Project.hasMany(CoBenefit);
Project.hasMany(ProjectLocation);
}
}

Expand Down Expand Up @@ -46,7 +58,7 @@ Project.init(
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
},
{ sequelize, modelName: 'Projects' },
{ sequelize, modelName: 'Projects', foreignKey: 'projectId' },
);

export { Project };
7 changes: 5 additions & 2 deletions src/models/qualifications/qualifications.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize } from '../database';
import { Project } from '../projects';
import { Unit } from '../units';

class Qualification extends Model {
/**
Expand All @@ -10,7 +12,8 @@ class Qualification extends Model {
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
Qualification.belongsTo(Project);
Qualification.hasMany(Unit);
}
}

Expand Down Expand Up @@ -55,7 +58,7 @@ Qualification.init(
type: Sequelize.DATE,
},
},
{ sequelize, modelName: 'Qualifications' },
{ sequelize, modelName: 'Qualifications', foreignKey: 'qualificationId' },
);

export { Qualification };
3 changes: 2 additions & 1 deletion src/models/ratings/ratings.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize } from '../database';
import { Project } from '../projects/index';

class Rating extends Model {
/**
Expand All @@ -10,7 +11,7 @@ class Rating extends Model {
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
Rating.belongsTo(Project);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/models/related-projects/related-projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize } from '../database';

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

class RelatedProject extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
RelatedProject.belongsTo(Project);
}
}

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

class Unit extends Model {
/**
Expand All @@ -10,7 +11,7 @@ class Unit extends Model {
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
Unit.belongsTo(Project);
}
}

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

class Vintage extends Model {
/**
Expand All @@ -10,7 +11,7 @@ class Vintage extends Model {
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
Vintage.belongsTo(Project);
}
}

Expand Down

0 comments on commit 0cd24ce

Please sign in to comment.