Андрей Гузюк
Андрей Гузюк

Reputation: 2164

Sequelize create model with object type

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, ... } }

Upvotes: 4

Views: 5433

Answers (2)

Ser Tim Berness-Lii
Ser Tim Berness-Lii

Reputation: 1

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
})

Upvotes: 0

simon.ro
simon.ro

Reputation: 3312

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.

Upvotes: 8

Related Questions