WIP Bulk create not working.
This commit is contained in:
@@ -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,7 +12,17 @@ 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 => {
|
||||||
|
return {
|
||||||
|
realEstateId: realEstate.dataValues.id,
|
||||||
|
price: realEstate.dataValues.price
|
||||||
|
};
|
||||||
|
});
|
||||||
|
const savedPrices = await bulkUpsertPriceHistory(resultPrices);
|
||||||
|
//
|
||||||
|
console.log("savedPrices", savedPrices);
|
||||||
|
//
|
||||||
if (Array.isArray(savedRecords)) {
|
if (Array.isArray(savedRecords)) {
|
||||||
const newRealEstates = [];
|
const newRealEstates = [];
|
||||||
const existingRealEstates = [];
|
const existingRealEstates = [];
|
||||||
|
|||||||
19
app/helpers/db/priceHistory.js
Normal file
19
app/helpers/db/priceHistory.js
Normal file
@@ -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
|
||||||
|
};
|
||||||
@@ -63,7 +63,9 @@ const bulkUpsertRealEstates = async realEstateData => {
|
|||||||
"numberOfViewsAgency"
|
"numberOfViewsAgency"
|
||||||
];
|
];
|
||||||
const order = [["updatedAt", "desc"]];
|
const order = [["updatedAt", "desc"]];
|
||||||
|
//
|
||||||
|
//console.log("realEstateData:", realEstateData);
|
||||||
|
//
|
||||||
return await db.RealEstate.bulkCreate(realEstateData, {
|
return await db.RealEstate.bulkCreate(realEstateData, {
|
||||||
updateOnDuplicate: fieldsToUpdateIfDuplicate,
|
updateOnDuplicate: fieldsToUpdateIfDuplicate,
|
||||||
returning: true,
|
returning: true,
|
||||||
|
|||||||
@@ -12,14 +12,18 @@ module.exports = {
|
|||||||
realEstateId: {
|
realEstateId: {
|
||||||
type: Sequelize.BIGINT,
|
type: Sequelize.BIGINT,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
|
unique: "uniquePriceRealEstate",
|
||||||
references: {
|
references: {
|
||||||
model: "RealEstate",
|
model: "RealEstates",
|
||||||
key: "id"
|
key: "id"
|
||||||
},
|
},
|
||||||
onUpdate: "CASCADE",
|
onUpdate: "CASCADE",
|
||||||
onDelete: "SET NULL"
|
onDelete: "SET NULL"
|
||||||
},
|
},
|
||||||
price: Sequelize.REAL,
|
price: {
|
||||||
|
type: Sequelize.REAL,
|
||||||
|
unique: "uniquePriceRealEstate"
|
||||||
|
},
|
||||||
createdAt: {
|
createdAt: {
|
||||||
type: Sequelize.DATE,
|
type: Sequelize.DATE,
|
||||||
defaultValue: Sequelize.literal("NOW()")
|
defaultValue: Sequelize.literal("NOW()")
|
||||||
@@ -11,15 +11,18 @@ module.exports = (sequalize, DataTypes) => {
|
|||||||
realEstateId: {
|
realEstateId: {
|
||||||
type: DataTypes.BIGINT,
|
type: DataTypes.BIGINT,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
|
unique: "uniquePriceRealEstate",
|
||||||
references: {
|
references: {
|
||||||
model: "RealEstate",
|
model: "RealEstates",
|
||||||
key: "id"
|
key: "id"
|
||||||
},
|
},
|
||||||
onUpdate: "CASCADE",
|
onUpdate: "CASCADE",
|
||||||
onDelete: "SET NULL"
|
onDelete: "SET NULL"
|
||||||
},
|
},
|
||||||
price: DataTypes.REAL
|
price: {
|
||||||
|
type: DataTypes.REAL,
|
||||||
|
unique: "uniquePriceRealEstate"
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
PriceHistory.associate = models => {
|
PriceHistory.associate = models => {
|
||||||
|
|||||||
Reference in New Issue
Block a user