Compare commits
4 Commits
sliders-fo
...
price-hist
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c6f0e039a5 | ||
|
|
8d3f001678 | ||
|
|
42eddb3aa5 | ||
|
|
0a181f742f |
@@ -1,6 +1,7 @@
|
|||||||
const moment = require("moment");
|
const moment = require("moment");
|
||||||
|
|
||||||
const { bulkUpsertRealEstates } = require("../../helpers/db/realEstate");
|
const { bulkUpsertRealEstates } = require("../../helpers/db/realEstate");
|
||||||
|
const { bulkUpsertPriceHistory } = require("../../helpers/db/priceHistory");
|
||||||
|
|
||||||
class PostgresSaver {
|
class PostgresSaver {
|
||||||
connect() {
|
connect() {
|
||||||
@@ -11,6 +12,21 @@ class PostgresSaver {
|
|||||||
|
|
||||||
async save(results) {
|
async save(results) {
|
||||||
const savedRecords = await bulkUpsertRealEstates(results);
|
const savedRecords = await bulkUpsertRealEstates(results);
|
||||||
|
//Extruding data for price history table
|
||||||
|
const resultPrices = savedRecords.map(realEstate => {
|
||||||
|
//Null values canot be recognized by ignore duplicates in sequalize
|
||||||
|
//Value price = 0 indicates 'cijena na upit'
|
||||||
|
const priceTmp =
|
||||||
|
realEstate.dataValues.price === null ? 0 : realEstate.dataValues.price;
|
||||||
|
|
||||||
|
return {
|
||||||
|
realEstateId: realEstate.dataValues.id,
|
||||||
|
price: priceTmp,
|
||||||
|
createdAt: realEstate.dataValues.createdAt,
|
||||||
|
updatedAt: realEstate.dataValues.updatedAt
|
||||||
|
};
|
||||||
|
});
|
||||||
|
const savedPrices = await bulkUpsertPriceHistory(resultPrices);
|
||||||
|
|
||||||
if (Array.isArray(savedRecords)) {
|
if (Array.isArray(savedRecords)) {
|
||||||
const newRealEstates = [];
|
const newRealEstates = [];
|
||||||
|
|||||||
20
app/helpers/db/priceHistory.js
Normal file
20
app/helpers/db/priceHistory.js
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
"use strict";
|
||||||
|
const db = require("../../models/index");
|
||||||
|
const sequelize = require("sequelize");
|
||||||
|
|
||||||
|
const bulkUpsertPriceHistory = async priceHistoryData => {
|
||||||
|
try {
|
||||||
|
const order = [["realEstateId", "desc"]];
|
||||||
|
|
||||||
|
return await db.PriceHistory.bulkCreate(priceHistoryData, {
|
||||||
|
order,
|
||||||
|
ignoreDuplicates: true
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
console.log("Error bulk upserting priceHistory : ", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
bulkUpsertPriceHistory
|
||||||
|
};
|
||||||
42
app/migrations/20200121000524-add-priceHistory-table.js
Normal file
42
app/migrations/20200121000524-add-priceHistory-table.js
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
"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,
|
||||||
|
unique: "uniquePriceRealEstate",
|
||||||
|
references: {
|
||||||
|
model: "RealEstates",
|
||||||
|
key: "id"
|
||||||
|
},
|
||||||
|
onUpdate: "CASCADE",
|
||||||
|
onDelete: "SET NULL"
|
||||||
|
},
|
||||||
|
price: {
|
||||||
|
type: Sequelize.REAL,
|
||||||
|
unique: "uniquePriceRealEstate"
|
||||||
|
},
|
||||||
|
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", {});
|
||||||
|
}
|
||||||
|
};
|
||||||
10
app/migrations/20200121094500-add-constraint-priceHistory.js
Normal file
10
app/migrations/20200121094500-add-constraint-priceHistory.js
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
"use strict";
|
||||||
|
module.exports = {
|
||||||
|
up: (queryInterface, Sequelize) =>
|
||||||
|
queryInterface.addConstraint("PriceHistory", ["realEstateId", "price"], {
|
||||||
|
type: "unique",
|
||||||
|
name: "uniquePriceRealEstate"
|
||||||
|
}),
|
||||||
|
down: queryInterface =>
|
||||||
|
queryInterface.removeConstraint("PriceHistory", "uniquePriceRealEstate")
|
||||||
|
};
|
||||||
44
app/models/priceHistory.js
Normal file
44
app/models/priceHistory.js
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
"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,
|
||||||
|
unique: "uniquePriceRealEstate",
|
||||||
|
references: {
|
||||||
|
model: "RealEstates",
|
||||||
|
key: "id"
|
||||||
|
},
|
||||||
|
onUpdate: "CASCADE",
|
||||||
|
onDelete: "SET NULL"
|
||||||
|
},
|
||||||
|
price: {
|
||||||
|
type: DataTypes.REAL,
|
||||||
|
unique: "uniquePriceRealEstate"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
freezeTableName: true
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
PriceHistory.associate = models => {
|
||||||
|
PriceHistory.hasMany(models.RealEstate, {
|
||||||
|
foreignKey: "id",
|
||||||
|
sourceKey: "realEstateId",
|
||||||
|
targetKey: "id",
|
||||||
|
as: "realEstates"
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return PriceHistory;
|
||||||
|
};
|
||||||
34
app/seeders/20200121150443-initial-price-history.js
Normal file
34
app/seeders/20200121150443-initial-price-history.js
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
const { RealEstate } = require("../models");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
async up(queryInterface, Sequelize) {
|
||||||
|
//Reading initial data from RealEstate table in db
|
||||||
|
const realEstateInitialData = await RealEstate.findAll();
|
||||||
|
//Extruding data for table PriceHistory
|
||||||
|
const priceHistoryInitialData = realEstateInitialData.map(realEstate => {
|
||||||
|
//Null values canot be recognized by ignore duplicates in sequalize
|
||||||
|
//Value price = 0 indicates 'cijena na upit'
|
||||||
|
const priceTmp =
|
||||||
|
realEstate.dataValues.price === null ? 0 : realEstate.dataValues.price;
|
||||||
|
|
||||||
|
return {
|
||||||
|
realEstateId: realEstate.dataValues.id,
|
||||||
|
price: priceTmp,
|
||||||
|
createdAt: realEstate.dataValues.createdAt,
|
||||||
|
updatedAt: realEstate.dataValues.updatedAt
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return queryInterface.bulkInsert(
|
||||||
|
"PriceHistory",
|
||||||
|
priceHistoryInitialData,
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
async down(queryInterface, Sequelize) {
|
||||||
|
return queryInterface.bulkDelete("PriceHistory", null, {});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
"start": "node ./index.js",
|
"start": "node ./index.js",
|
||||||
"start-mon": "nodemon ./index.js",
|
"start-mon": "nodemon ./index.js",
|
||||||
"migrate": "cd app && npx sequelize db:migrate",
|
"migrate": "cd app && npx sequelize db:migrate",
|
||||||
|
"seed": "cd app && npx sequelize db:seed:all",
|
||||||
"setup": "docker build -t marketalerts . && docker run -e POSTGRES_USER=docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=marketalerts --name pg_marketalerts -d -p 5432:5432 marketalerts && sleep 10 && npm run migrate",
|
"setup": "docker build -t marketalerts . && docker run -e POSTGRES_USER=docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=marketalerts --name pg_marketalerts -d -p 5432:5432 marketalerts && sleep 10 && npm run migrate",
|
||||||
"docker-start": "docker start pg_marketalerts",
|
"docker-start": "docker start pg_marketalerts",
|
||||||
"docker-stop": "docker stop pg_marketalerts",
|
"docker-stop": "docker stop pg_marketalerts",
|
||||||
|
|||||||
Reference in New Issue
Block a user