保存arrayData属于表

时间:2019-05-25 15:35:17

标签: node.js sequelize.js

我正在使用"sequelize-cli": "^5.4.0"node v10.15.3

我有三个数组,它们的数据来自API。

我的结构看起来-类似-如下:

var faker = require('faker');

const companyData = []
const ratingData = []
const issuerData = []

for (let index = 0; index < 20; index++) {
    issuerData.push({
        name: faker.company.companyName()
    })
    companyData.push({
        name: faker.company.companyName(),
        symbol: faker.name.findName()
    })
    ratingData.push({
        action: faker.random.arrayElement(['Buy', 'Sell', 'Neutral']),
        rating_date: new Date()
    })
}

const data = [issuerData, companyData, ratingData]
console.log(data);

我很容易保存公司/发行人数据,如下所示:

    Company.bulkCreate(dataArray, {
        fields: [
            "name",
            "symbol"
        ],
        updateOnDuplicate: ["name"]
    }).then((iss) => {
        return iss;
    }).catch((error) => console.log(error));

我的三个表如下所示:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Company = sequelize.define('Company', {
    name: DataTypes.STRING,
    symbol: DataTypes.STRING
  }, {});
  Company.associate = function(models) {
    Company.hasMany(models.Rating);
  };
  return Company;
};

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Issuer = sequelize.define('Issuer', {
    name: DataTypes.STRING
  }, {});
  Issuer.associate = function (models) {
    Issuer.hasMany(models.Rating);
  };
  return Issuer;
};

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Rating = sequelize.define('Rating', {
    action: DataTypes.STRING,
    rating_date: DataTypes.STRING
  }, {});
  Rating.associate = function(models) {
    Rating.belongsTo(models.Issuer);
    Rating.belongsTo(models.Company);
  };
  return Rating;
};

有什么建议可以与合适的发行人和公司保存评级?

感谢您的答复!

0 个答案:

没有答案