From bdbe84eace6c67b7509944f602bb80d0414bf76c Mon Sep 17 00:00:00 2001 From: Frantz Arty Date: Wed, 1 Dec 2021 08:43:54 -0500 Subject: [PATCH] feat: added models and migration scripts -Party -ProjectLocation -ProjectRatings -RelatedProject -Unit -Vintage --- .sequelizerc | 0 config/config.json | 20 ++++++ config/database.json | 23 +++++++ .../20211130200914-create-project-location.js | 30 ++++++++ .../20211201130904-create-project-ratings.js | 36 ++++++++++ migrations/20211201131101-create-party.js | 33 +++++++++ migrations/20211201131518-create-unit.js | 69 +++++++++++++++++++ migrations/20211201131734-create-vintage.js | 39 +++++++++++ .../20211201131902-create-related-project.js | 33 +++++++++ models/index.js | 37 ++++++++++ models/party.js | 25 +++++++ models/projectlocation.js | 24 +++++++ models/projectratings.js | 26 +++++++ models/relatedproject.js | 25 +++++++ models/unit.js | 37 ++++++++++ models/vintage.js | 27 ++++++++ 16 files changed, 484 insertions(+) create mode 100644 .sequelizerc create mode 100644 config/config.json create mode 100644 config/database.json create mode 100644 migrations/20211130200914-create-project-location.js create mode 100644 migrations/20211201130904-create-project-ratings.js create mode 100644 migrations/20211201131101-create-party.js create mode 100644 migrations/20211201131518-create-unit.js create mode 100644 migrations/20211201131734-create-vintage.js create mode 100644 migrations/20211201131902-create-related-project.js create mode 100644 models/index.js create mode 100644 models/party.js create mode 100644 models/projectlocation.js create mode 100644 models/projectratings.js create mode 100644 models/relatedproject.js create mode 100644 models/unit.js create mode 100644 models/vintage.js diff --git a/.sequelizerc b/.sequelizerc new file mode 100644 index 00000000..e69de29b diff --git a/config/config.json b/config/config.json new file mode 100644 index 00000000..746a092a --- /dev/null +++ b/config/config.json @@ -0,0 +1,20 @@ +{ + "development": { + "dialect": "sqlite", + "storage": "data.sqlite3" + }, + "test": { + "username": "root", + "password": null, + "database": "database_test", + "host": "127.0.0.1", + "dialect": "mysql" + }, + "production": { + "username": "root", + "password": null, + "database": "database_production", + "host": "127.0.0.1", + "dialect": "mysql" + } +} diff --git a/config/database.json b/config/database.json new file mode 100644 index 00000000..0f858c66 --- /dev/null +++ b/config/database.json @@ -0,0 +1,23 @@ +{ + "development": { + "username": "root", + "password": null, + "database": "database_development", + "host": "127.0.0.1", + "dialect": "mysql" + }, + "test": { + "username": "root", + "password": null, + "database": "database_test", + "host": "127.0.0.1", + "dialect": "mysql" + }, + "production": { + "username": "root", + "password": null, + "database": "database_production", + "host": "127.0.0.1", + "dialect": "mysql" + } +} diff --git a/migrations/20211130200914-create-project-location.js b/migrations/20211130200914-create-project-location.js new file mode 100644 index 00000000..a0026300 --- /dev/null +++ b/migrations/20211130200914-create-project-location.js @@ -0,0 +1,30 @@ +'use strict'; +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.createTable('ProjectLocations', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + countryRegion: { + type: Sequelize.STRING + }, + hostCountry: { + type: Sequelize.STRING + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: async (queryInterface, Sequelize) => { + await queryInterface.dropTable('ProjectLocations'); + } +}; \ No newline at end of file diff --git a/migrations/20211201130904-create-project-ratings.js b/migrations/20211201130904-create-project-ratings.js new file mode 100644 index 00000000..cfed88ee --- /dev/null +++ b/migrations/20211201130904-create-project-ratings.js @@ -0,0 +1,36 @@ +'use strict'; +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.createTable('ProjectRatings', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + ratingType: { + type: Sequelize.STRING + }, + rating: { + type: Sequelize.NUMBER + }, + link: { + type: Sequelize.STRING + }, + scale: { + type: Sequelize.STRING + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: async (queryInterface, Sequelize) => { + await queryInterface.dropTable('ProjectRatings'); + } +}; \ No newline at end of file diff --git a/migrations/20211201131101-create-party.js b/migrations/20211201131101-create-party.js new file mode 100644 index 00000000..0c70cb97 --- /dev/null +++ b/migrations/20211201131101-create-party.js @@ -0,0 +1,33 @@ +'use strict'; +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.createTable('Parties', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + name: { + type: Sequelize.STRING + }, + country: { + type: Sequelize.STRING + }, + registry: { + type: Sequelize.STRING + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: async (queryInterface, Sequelize) => { + await queryInterface.dropTable('Parties'); + } +}; \ No newline at end of file diff --git a/migrations/20211201131518-create-unit.js b/migrations/20211201131518-create-unit.js new file mode 100644 index 00000000..4833a379 --- /dev/null +++ b/migrations/20211201131518-create-unit.js @@ -0,0 +1,69 @@ +'use strict'; +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.createTable('Units', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + owner: { + type: Sequelize.STRING + }, + buyer: { + type: Sequelize.STRING + }, + registry: { + type: Sequelize.STRING + }, + blockIdentifier: { + type: Sequelize.STRING + }, + identifier: { + type: Sequelize.STRING + }, + qualificationId: { + type: Sequelize.NUMBER + }, + unitType: { + type: Sequelize.STRING + }, + unitCount: { + type: Sequelize.NUMBER + }, + unitStatus: { + type: Sequelize.STRING + }, + unitStatusDate: { + type: Sequelize.DATE + }, + transactionType: { + type: Sequelize.STRING + }, + unitIssuanceLocation: { + type: Sequelize.STRING + }, + unitLink: { + type: Sequelize.STRING + }, + correspondingAdjustment: { + type: Sequelize.STRING + }, + unitTag: { + type: Sequelize.STRING + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: async (queryInterface, Sequelize) => { + await queryInterface.dropTable('Units'); + } +}; \ No newline at end of file diff --git a/migrations/20211201131734-create-vintage.js b/migrations/20211201131734-create-vintage.js new file mode 100644 index 00000000..5212eae1 --- /dev/null +++ b/migrations/20211201131734-create-vintage.js @@ -0,0 +1,39 @@ +'use strict'; +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.createTable('Vintages', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + startDate: { + type: Sequelize.DATE + }, + endDate: { + type: Sequelize.DATE + }, + verificationApproach: { + type: Sequelize.STRING + }, + verificationDate: { + type: Sequelize.DATE + }, + verificationBody: { + type: Sequelize.STRING + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: async (queryInterface, Sequelize) => { + await queryInterface.dropTable('Vintages'); + } +}; \ No newline at end of file diff --git a/migrations/20211201131902-create-related-project.js b/migrations/20211201131902-create-related-project.js new file mode 100644 index 00000000..78abaa86 --- /dev/null +++ b/migrations/20211201131902-create-related-project.js @@ -0,0 +1,33 @@ +'use strict'; +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.createTable('RelatedProjects', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + relatedProjectType: { + type: Sequelize.STRING + }, + registry: { + type: Sequelize.STRING + }, + note: { + type: Sequelize.STRING + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: async (queryInterface, Sequelize) => { + await queryInterface.dropTable('RelatedProjects'); + } +}; \ No newline at end of file diff --git a/models/index.js b/models/index.js new file mode 100644 index 00000000..33f09e77 --- /dev/null +++ b/models/index.js @@ -0,0 +1,37 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const Sequelize = require('sequelize'); +const basename = path.basename(__filename); +const env = process.env.NODE_ENV || 'development'; +const config = require(__dirname + '/../config/config.json')[env]; +const db = {}; + +let sequelize; +if (config.use_env_variable) { + sequelize = new Sequelize(process.env[config.use_env_variable], config); +} else { + sequelize = new Sequelize(config.database, config.username, config.password, config); +} + +fs + .readdirSync(__dirname) + .filter(file => { + return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'); + }) + .forEach(file => { + const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes); + db[model.name] = model; + }); + +Object.keys(db).forEach(modelName => { + if (db[modelName].associate) { + db[modelName].associate(db); + } +}); + +db.sequelize = sequelize; +db.Sequelize = Sequelize; + +module.exports = db; diff --git a/models/party.js b/models/party.js new file mode 100644 index 00000000..6796bbea --- /dev/null +++ b/models/party.js @@ -0,0 +1,25 @@ +'use strict'; +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class Party 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 + } + }; + Party.init({ + name: DataTypes.STRING, + country: DataTypes.STRING, + registry: DataTypes.STRING + }, { + sequelize, + modelName: 'Party', + }); + return Party; +}; \ No newline at end of file diff --git a/models/projectlocation.js b/models/projectlocation.js new file mode 100644 index 00000000..1014eebc --- /dev/null +++ b/models/projectlocation.js @@ -0,0 +1,24 @@ +'use strict'; +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class ProjectLocation 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 + } + }; + ProjectLocation.init({ + countryRegion: DataTypes.STRING, + hostCountry: DataTypes.STRING + }, { + sequelize, + modelName: 'ProjectLocation', + }); + return ProjectLocation; +}; \ No newline at end of file diff --git a/models/projectratings.js b/models/projectratings.js new file mode 100644 index 00000000..3579a656 --- /dev/null +++ b/models/projectratings.js @@ -0,0 +1,26 @@ +'use strict'; +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class ProjectRatings 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 + } + }; + ProjectRatings.init({ + ratingType: DataTypes.STRING, + rating: DataTypes.NUMBER, + link: DataTypes.STRING, + scale: DataTypes.STRING + }, { + sequelize, + modelName: 'ProjectRatings', + }); + return ProjectRatings; +}; \ No newline at end of file diff --git a/models/relatedproject.js b/models/relatedproject.js new file mode 100644 index 00000000..7fe01e2a --- /dev/null +++ b/models/relatedproject.js @@ -0,0 +1,25 @@ +'use strict'; +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + 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.init({ + relatedProjectType: DataTypes.STRING, + registry: DataTypes.STRING, + note: DataTypes.STRING + }, { + sequelize, + modelName: 'RelatedProject', + }); + return RelatedProject; +}; \ No newline at end of file diff --git a/models/unit.js b/models/unit.js new file mode 100644 index 00000000..57c8ed10 --- /dev/null +++ b/models/unit.js @@ -0,0 +1,37 @@ +'use strict'; +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class Unit 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 + } + }; + Unit.init({ + owner: DataTypes.STRING, + buyer: DataTypes.STRING, + registry: DataTypes.STRING, + blockIdentifier: DataTypes.STRING, + identifier: DataTypes.STRING, + qualificationId: DataTypes.NUMBER, + unitType: DataTypes.STRING, + unitCount: DataTypes.NUMBER, + unitStatus: DataTypes.STRING, + unitStatusDate: DataTypes.DATE, + transactionType: DataTypes.STRING, + unitIssuanceLocation: DataTypes.STRING, + unitLink: DataTypes.STRING, + correspondingAdjustment: DataTypes.STRING, + unitTag: DataTypes.STRING + }, { + sequelize, + modelName: 'Unit', + }); + return Unit; +}; \ No newline at end of file diff --git a/models/vintage.js b/models/vintage.js new file mode 100644 index 00000000..4c299914 --- /dev/null +++ b/models/vintage.js @@ -0,0 +1,27 @@ +'use strict'; +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class Vintage 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 + } + }; + Vintage.init({ + startDate: DataTypes.DATE, + endDate: DataTypes.DATE, + verificationApproach: DataTypes.STRING, + verificationDate: DataTypes.DATE, + verificationBody: DataTypes.STRING + }, { + sequelize, + modelName: 'Vintage', + }); + return Vintage; +}; \ No newline at end of file