最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sequelize create model with object type - Stack Overflow

programmeradmin0浏览0评论

Is this possible to create model with sequelize to look like:

var User = sequelize.define('User', {
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING,
    facebook: {
      id: DataTypes.STRING,
      token: DataTypes.STRING,
      email: DataTypes.STRING,
      name: DataTypes.STRING
    },
})


Idea is: When i will get user data from DB i would like to see User: { facebook: { id, token, ... } }

Is this possible to create model with sequelize to look like:

var User = sequelize.define('User', {
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING,
    facebook: {
      id: DataTypes.STRING,
      token: DataTypes.STRING,
      email: DataTypes.STRING,
      name: DataTypes.STRING
    },
})


Idea is: When i will get user data from DB i would like to see User: { facebook: { id, token, ... } }

Share Improve this question asked Nov 3, 2017 at 9:27 Андрей ГузюкАндрей Гузюк 2,1646 gold badges31 silver badges56 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

No.

Either

  1. you make User.facebook a DataType.JSON field (which is supported by postgresql only)
  2. you create an Entity 'facebook', which is 1:1 related to User

Option i) is fine, but you don't get any support from sequelize to check integrity and validity if you make it a json field.

Column dataType - TEXT save as JSON format after find table - return json patse text

PodPodMaterial.afterFind(async (material) => {
  if(material.length) {
    for(let mat in material) {
      try {
          material[mat].ez_kolvo = JSON.parse(material[mat].ez_kolvo)
      } catch(e) {console.error(e)}
    }
  } else {
    try {
        material.ez_kolvo = JSON.stringify(material.ez_kolvo)
    }catch(e) {console.error(e)}
  }

  return material
})

发布评论

评论列表(0)

  1. 暂无评论