From 42eddb3aa5e6899da304be787b21dae9b38881f0 Mon Sep 17 00:00:00 2001 From: Naida Vatric Date: Tue, 21 Jan 2020 01:19:35 +0100 Subject: [PATCH] WIP Bulk create not working. --- app/crawler/savers/postgres.js | 13 ++++++++++++- app/helpers/db/priceHistory.js | 19 +++++++++++++++++++ app/helpers/db/realEstate.js | 4 +++- ... 20200121000524-add-priceHistory-table.js} | 8 ++++++-- app/models/priceHistory.js | 9 ++++++--- 5 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 app/helpers/db/priceHistory.js rename app/migrations/{20200120215830-add-priceHistory-table.js => 20200121000524-add-priceHistory-table.js} (83%) diff --git a/app/crawler/savers/postgres.js b/app/crawler/savers/postgres.js index 344e4ac..8c1553f 100644 --- a/app/crawler/savers/postgres.js +++ b/app/crawler/savers/postgres.js @@ -1,6 +1,7 @@ const moment = require("moment"); const { bulkUpsertRealEstates } = require("../../helpers/db/realEstate"); +const { bulkUpsertPriceHistory } = require("../../helpers/db/priceHistory"); class PostgresSaver { connect() { @@ -11,7 +12,17 @@ class PostgresSaver { async save(results) { const savedRecords = await bulkUpsertRealEstates(results); - + //Extruding data for price history table + const resultPrices = savedRecords.map(realEstate => { + return { + realEstateId: realEstate.dataValues.id, + price: realEstate.dataValues.price + }; + }); + const savedPrices = await bulkUpsertPriceHistory(resultPrices); + // + console.log("savedPrices", savedPrices); + // if (Array.isArray(savedRecords)) { const newRealEstates = []; const existingRealEstates = []; diff --git a/app/helpers/db/priceHistory.js b/app/helpers/db/priceHistory.js new file mode 100644 index 0000000..9d62be8 --- /dev/null +++ b/app/helpers/db/priceHistory.js @@ -0,0 +1,19 @@ +"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 + }); + } catch (e) { + console.log("Error bulk upserting priceHistory : ", e); + } +}; + +module.exports = { + bulkUpsertPriceHistory +}; diff --git a/app/helpers/db/realEstate.js b/app/helpers/db/realEstate.js index ebeb84c..cb7a2ef 100644 --- a/app/helpers/db/realEstate.js +++ b/app/helpers/db/realEstate.js @@ -63,7 +63,9 @@ const bulkUpsertRealEstates = async realEstateData => { "numberOfViewsAgency" ]; const order = [["updatedAt", "desc"]]; - + // + //console.log("realEstateData:", realEstateData); + // return await db.RealEstate.bulkCreate(realEstateData, { updateOnDuplicate: fieldsToUpdateIfDuplicate, returning: true, diff --git a/app/migrations/20200120215830-add-priceHistory-table.js b/app/migrations/20200121000524-add-priceHistory-table.js similarity index 83% rename from app/migrations/20200120215830-add-priceHistory-table.js rename to app/migrations/20200121000524-add-priceHistory-table.js index eb8f616..6d56f99 100644 --- a/app/migrations/20200120215830-add-priceHistory-table.js +++ b/app/migrations/20200121000524-add-priceHistory-table.js @@ -12,14 +12,18 @@ module.exports = { realEstateId: { type: Sequelize.BIGINT, allowNull: false, + unique: "uniquePriceRealEstate", references: { - model: "RealEstate", + model: "RealEstates", key: "id" }, onUpdate: "CASCADE", onDelete: "SET NULL" }, - price: Sequelize.REAL, + price: { + type: Sequelize.REAL, + unique: "uniquePriceRealEstate" + }, createdAt: { type: Sequelize.DATE, defaultValue: Sequelize.literal("NOW()") diff --git a/app/models/priceHistory.js b/app/models/priceHistory.js index ea9ddea..560a4fa 100644 --- a/app/models/priceHistory.js +++ b/app/models/priceHistory.js @@ -11,15 +11,18 @@ module.exports = (sequalize, DataTypes) => { realEstateId: { type: DataTypes.BIGINT, allowNull: false, - + unique: "uniquePriceRealEstate", references: { - model: "RealEstate", + model: "RealEstates", key: "id" }, onUpdate: "CASCADE", onDelete: "SET NULL" }, - price: DataTypes.REAL + price: { + type: DataTypes.REAL, + unique: "uniquePriceRealEstate" + } }); PriceHistory.associate = models => {