Skip to content

Commit

Permalink
feat: add local test mirror db and safe db mirror utility
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jan 13, 2022
1 parent ea69454 commit 2973b9f
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 98 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"release": "./node_modules/.bin/standard-version && git push --tags",
"postinstall": "npm run requirements-check",
"resetDb": "rm -f ./data.sqlite3 && npx sequelize-cli db:migrate --env local && npx sequelize-cli db:seed:all --debug --env local",
"resetTestDb": "rm -f ./test.sqlite3 && npx sequelize-cli db:migrate --env test && npx sequelize-cli db:seed:all --debug --env test",
"resetTestDb": "rm -f ./test.sqlite3 && npx sequelize-cli db:migrate --env test && npx sequelize-cli db:seed:all --debug --env test && rm -f ./testMirror.sqlite3 && npx sequelize-cli db:migrate --env mirrorTest",
"resetMirrorDb": "npx sequelize-cli db:drop --env mirror && npx sequelize-cli db:create --env mirror && npx sequelize-cli db:migrate --env mirror --debug"
},
"dependencies": {
Expand Down
4 changes: 4 additions & 0 deletions src/config/config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ module.exports = {
dialect: 'sqlite',
storage: './test.sqlite3',
},
mirrorTest: {
dialect: 'sqlite',
storage: './testMirror.sqlite3',
},
mirror: {
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
Expand Down
16 changes: 9 additions & 7 deletions src/models/co-benefits/co-benefits.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 { CoBenefitMirror } from './co-benefits.model.mirror';
import { sequelize } from '../database';
import { sequelize, safeMirrorDbHandler } from '../database';
import { Project } from '../projects';
import ModelTypes from './co-benifets.modeltypes.cjs';

Expand All @@ -15,26 +15,28 @@ class CoBenefit extends Model {
foreignKey: 'projectId',
});

if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
CoBenefitMirror.belongsTo(Project, {
onDelete: 'CASCADE',
targetKey: 'warehouseProjectId',
foreignKey: 'projectId',
});
}
});
}

static async create(values, options) {
if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
CoBenefitMirror.create(values, options);
}
});

return super.create(values, options);
}

static async destroy(values) {
if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
CoBenefitMirror.destroy(values);
}
});

return super.destroy(values);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/models/co-benefits/co-benefits.model.mirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import Sequelize from 'sequelize';
const { Model } = Sequelize;

import { sequelizeMirror } from '../database';
import { sequelizeMirror, safeMirrorDbHandler } from '../database';
import ModelTypes from './co-benifets.modeltypes.cjs';

class CoBenefitMirror extends Model {}

if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
CoBenefitMirror.init(ModelTypes, {
sequelize: sequelizeMirror,
modelName: 'coBenefit',
});
}
});

export { CoBenefitMirror };
16 changes: 15 additions & 1 deletion src/models/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,18 @@ dotenv.config();

// possible values: local, test
export const sequelize = new Sequelize(config[process.env.NODE_ENV]);
export const sequelizeMirror = new Sequelize(config['mirror']);

const mirrorConfig = process.env.NODE_ENV === 'local' ? 'mirror' : 'mirrorTest';
export const sequelizeMirror = new Sequelize(config[mirrorConfig]);

export const safeMirrorDbHandler = (callback) => {
sequelizeMirror
.authenticate()
.then(() => {
console.log('Mirror DB connected');
callback();
})
.catch((err) => {
console.log('Mirror DB not connected');
});
};
16 changes: 9 additions & 7 deletions src/models/locations/locations.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 { sequelize, safeMirrorDbHandler } from '../database';
import { Project } from '../projects';

import ModelTypes from './locations.modeltypes.cjs';
Expand All @@ -17,26 +17,28 @@ class ProjectLocation extends Model {
foreignKey: 'projectId',
});

if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
ProjectLocationMirror.belongsTo(Project, {
onDelete: 'CASCADE',
targetKey: 'warehouseProjectId',
foreignKey: 'projectId',
});
}
});
}

static async create(values, options) {
if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
ProjectLocationMirror.create(values, options);
}
});

return super.create(values, options);
}

static async destroy(values) {
if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
ProjectLocationMirror.destroy(values);
}
});

return super.destroy(values);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/models/locations/locations.model.mirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import Sequelize from 'sequelize';
const { Model } = Sequelize;

import { sequelizeMirror } from '../database';
import { sequelizeMirror, safeMirrorDbHandler } from '../database';
import ModelTypes from './locations.modeltypes.cjs';

class ProjectLocationMirror extends Model {}

if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
ProjectLocationMirror.init(ModelTypes, {
sequelize: sequelizeMirror,
modelName: 'projectLocation',
});
}
});

export { ProjectLocationMirror };
16 changes: 9 additions & 7 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Sequelize from 'sequelize';
import rxjs from 'rxjs';
const { Model } = Sequelize;

import { sequelize } from '../database';
import { sequelize, safeMirrorDbHandler } from '../database';

import {
RelatedProject,
Expand All @@ -28,19 +28,20 @@ class Project extends Model {
Project.hasMany(CoBenefit);
Project.hasMany(RelatedProject);

if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
ProjectMirror.hasMany(ProjectLocation);
ProjectMirror.hasMany(Qualification);
ProjectMirror.hasMany(Vintage);
ProjectMirror.hasMany(CoBenefit);
ProjectMirror.hasMany(RelatedProject);
}
});
}

static async create(values, options) {
if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
ProjectMirror.create(values, options);
}
});

const createResult = await super.create(values, options);

const { orgUid } = values;
Expand All @@ -51,9 +52,10 @@ class Project extends Model {
}

static async destroy(values) {
if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
ProjectMirror.destroy(values);
}
});

const record = await super.findOne(values.where);
const { orgUid } = record.dataValues;

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

import { sequelizeMirror } from '../database';
import { sequelizeMirror, safeMirrorDbHandler } from '../database';
import ModelTypes from './projects.modeltypes.cjs';

class ProjectMirror extends Model {}

if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
ProjectMirror.init(ModelTypes, {
sequelize: sequelizeMirror,
modelName: 'project',
foreignKey: 'projectId',
});
}
});

export { ProjectMirror };
23 changes: 18 additions & 5 deletions src/models/qualifications/qualifications.model.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize } from '../database';
import { sequelize, safeMirrorDbHandler } from '../database';
import { Project } from '../projects';
import { Unit } from '../units';

Expand All @@ -23,19 +23,32 @@ class Qualification extends Model {
through: 'qualification_unit',
as: 'unit',
});

safeMirrorDbHandler(() => {
QualificationMirror.belongsTo(Project, {
targetKey: 'warehouseProjectId',
foreignKey: 'projectId',
});

QualificationMirror.belongsToMany(Unit, {
through: 'qualification_unit',
as: 'unit',
});
});
}

static async create(values, options) {
if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
QualificationMirror.create(values, options);
}
});

return super.create(values, options);
}

static async destroy(values) {
if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
QualificationMirror.destroy(values);
}
});
return super.destroy(values);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/models/qualifications/qualifications.model.mirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import Sequelize from 'sequelize';
const { Model } = Sequelize;

import { sequelizeMirror } from '../database';
import { sequelizeMirror, safeMirrorDbHandler } from '../database';
import ModelTypes from './qualifications.modeltypes.cjs';

class QualificationMirror extends Model {}

if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
QualificationMirror.init(ModelTypes, {
sequelize: sequelizeMirror,
modelName: 'qualification',
foreignKey: 'qualificationId',
});
}
});

export { QualificationMirror };
16 changes: 9 additions & 7 deletions src/models/ratings/ratings.model.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize } from '../database';
import { sequelize, safeMirrorDbHandler } from '../database';
import { Project } from '../projects/index';

import ModelTypes from './ratings.modeltypes.cjs';
Expand All @@ -15,26 +15,28 @@ class Rating extends Model {
foreignKey: 'projectId',
});

if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
RatingMirror.belongsTo(Project, {
onDelete: 'CASCADE',
targetKey: 'warehouseProjectId',
foreignKey: 'projectId',
});
}
});
}

static async create(values, options) {
if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
RatingMirror.create(values, options);
}
});

return super.create(values, options);
}

static async destroy(values) {
if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
RatingMirror.destroy(values);
}
});

return super.destroy(values);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/models/ratings/ratings.model.mirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import Sequelize from 'sequelize';
const { Model } = Sequelize;

import { sequelizeMirror } from '../database';
import { sequelizeMirror, safeMirrorDbHandler } from '../database';
import ModelTypes from './ratings.modeltypes.cjs';

class RatingMirror extends Model {}

if (process.env.DB_USE_MIRROR === 'true') {
safeMirrorDbHandler(() => {
RatingMirror.init(ModelTypes, {
sequelize: sequelizeMirror,
modelName: 'projectRating',
});
}
});

export { RatingMirror };
Loading

0 comments on commit 2973b9f

Please sign in to comment.