WIP Added model, migration and bulk upsert fnc.
This commit is contained in:
@@ -14,9 +14,16 @@ class PostgresSaver {
|
|||||||
const savedRecords = await bulkUpsertRealEstates(results);
|
const savedRecords = await bulkUpsertRealEstates(results);
|
||||||
//Extruding data for price history table
|
//Extruding data for price history table
|
||||||
const resultPrices = savedRecords.map(realEstate => {
|
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 {
|
return {
|
||||||
realEstateId: realEstate.dataValues.id,
|
realEstateId: realEstate.dataValues.id,
|
||||||
price: realEstate.dataValues.price
|
price: priceTmp,
|
||||||
|
createdAt: realEstate.dataValues.createdAt,
|
||||||
|
updatedAt: realEstate.dataValues.updatedAt
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
const savedPrices = await bulkUpsertPriceHistory(resultPrices);
|
const savedPrices = await bulkUpsertPriceHistory(resultPrices);
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ const bulkUpsertPriceHistory = async priceHistoryData => {
|
|||||||
const order = [["realEstateId", "desc"]];
|
const order = [["realEstateId", "desc"]];
|
||||||
|
|
||||||
return await db.PriceHistory.bulkCreate(priceHistoryData, {
|
return await db.PriceHistory.bulkCreate(priceHistoryData, {
|
||||||
order
|
order,
|
||||||
|
ignoreDuplicates: true
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("Error bulk upserting priceHistory : ", e);
|
console.log("Error bulk upserting priceHistory : ", e);
|
||||||
|
|||||||
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")
|
||||||
|
};
|
||||||
@@ -1,29 +1,35 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
module.exports = (sequalize, DataTypes) => {
|
module.exports = (sequalize, DataTypes) => {
|
||||||
const PriceHistory = sequalize.define("PriceHistory", {
|
const PriceHistory = sequalize.define(
|
||||||
id: {
|
"PriceHistory",
|
||||||
type: DataTypes.BIGINT,
|
{
|
||||||
autoIncrement: true,
|
id: {
|
||||||
primaryKey: true,
|
type: DataTypes.BIGINT,
|
||||||
allowNull: false
|
autoIncrement: true,
|
||||||
},
|
primaryKey: true,
|
||||||
realEstateId: {
|
allowNull: false
|
||||||
type: DataTypes.BIGINT,
|
|
||||||
allowNull: false,
|
|
||||||
unique: "uniquePriceRealEstate",
|
|
||||||
references: {
|
|
||||||
model: "RealEstates",
|
|
||||||
key: "id"
|
|
||||||
},
|
},
|
||||||
onUpdate: "CASCADE",
|
realEstateId: {
|
||||||
onDelete: "SET NULL"
|
type: DataTypes.BIGINT,
|
||||||
|
allowNull: false,
|
||||||
|
unique: "uniquePriceRealEstate",
|
||||||
|
references: {
|
||||||
|
model: "RealEstates",
|
||||||
|
key: "id"
|
||||||
|
},
|
||||||
|
onUpdate: "CASCADE",
|
||||||
|
onDelete: "SET NULL"
|
||||||
|
},
|
||||||
|
price: {
|
||||||
|
type: DataTypes.REAL,
|
||||||
|
unique: "uniquePriceRealEstate"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
price: {
|
{
|
||||||
type: DataTypes.REAL,
|
freezeTableName: true
|
||||||
unique: "uniquePriceRealEstate"
|
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
|
|
||||||
PriceHistory.associate = models => {
|
PriceHistory.associate = models => {
|
||||||
PriceHistory.hasMany(models.RealEstate, {
|
PriceHistory.hasMany(models.RealEstate, {
|
||||||
|
|||||||
19
app/seeders/20200121150443-initial-price-history.js
Normal file
19
app/seeders/20200121150443-initial-price-history.js
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
up: (queryInterface, Sequelize) => {
|
||||||
|
// ??
|
||||||
|
const realEstateInitialData = await queryInterface.Sequelize.findall ();
|
||||||
|
const priceHistoryInitialData = realEstateInitialData.map ();
|
||||||
|
|
||||||
|
return queryInterface.bulkInsert(
|
||||||
|
"PriceHistory",
|
||||||
|
priceHistoryInitialData,
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
down: (queryInterface, Sequelize) => {
|
||||||
|
return queryInterface.bulkDelete("PriceHistory", null, {});
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user