From 0a181f742f9e4478ffefe4a778108ea5cb95d0cd Mon Sep 17 00:00:00 2001 From: Naida Vatric Date: Mon, 20 Jan 2020 23:09:02 +0100 Subject: [PATCH] WIP Added model and migration for new table priceHistory. --- .../20200120215830-add-priceHistory-table.js | 38 +++++++++++++++++++ app/models/priceHistory.js | 35 +++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 app/migrations/20200120215830-add-priceHistory-table.js create mode 100644 app/models/priceHistory.js diff --git a/app/migrations/20200120215830-add-priceHistory-table.js b/app/migrations/20200120215830-add-priceHistory-table.js new file mode 100644 index 0000000..eb8f616 --- /dev/null +++ b/app/migrations/20200120215830-add-priceHistory-table.js @@ -0,0 +1,38 @@ +"use strict"; + +module.exports = { + up: (queryInterface, Sequelize) => { + const tableFields = { + id: { + type: Sequelize.BIGINT, + autoIncrement: true, + allowNull: false, + primaryKey: true + }, + realEstateId: { + type: Sequelize.BIGINT, + allowNull: false, + references: { + model: "RealEstate", + key: "id" + }, + onUpdate: "CASCADE", + onDelete: "SET NULL" + }, + price: Sequelize.REAL, + createdAt: { + type: Sequelize.DATE, + defaultValue: Sequelize.literal("NOW()") + }, + updatedAt: { + type: Sequelize.DATE, + defaultValue: Sequelize.literal("NOW()") + } + }; + return queryInterface.createTable("PriceHistory", tableFields); + }, + + down: queryInterface => { + return queryInterface.dropTable("PriceHistory", {}); + } +}; diff --git a/app/models/priceHistory.js b/app/models/priceHistory.js new file mode 100644 index 0000000..ea9ddea --- /dev/null +++ b/app/models/priceHistory.js @@ -0,0 +1,35 @@ +"use strict"; + +module.exports = (sequalize, DataTypes) => { + const PriceHistory = sequalize.define("PriceHistory", { + id: { + type: DataTypes.BIGINT, + autoIncrement: true, + primaryKey: true, + allowNull: false + }, + realEstateId: { + type: DataTypes.BIGINT, + allowNull: false, + + references: { + model: "RealEstate", + key: "id" + }, + onUpdate: "CASCADE", + onDelete: "SET NULL" + }, + price: DataTypes.REAL + }); + + PriceHistory.associate = models => { + PriceHistory.hasMany(models.RealEstate, { + foreignKey: "id", + sourceKey: "realEstateId", + targetKey: "id", + as: "realEstates" + }); + }; + + return PriceHistory; +};