0

i don't know if this is possible, there are 2 models which are associated through relation, and models are defined this way:

sequelize.define('account_has_plan', {
        account_id: {
            type: DataTypes.INTEGER(10),
            allowNull: false,
            primaryKey: true,
            references: {
                model: 'account',
                key: 'id'
            }
        },
        plan_id: {
            type: DataTypes.INTEGER(10),
            allowNull: false,
            primaryKey: true,
            references: {
                model: 'plan',
                key: 'id'
            }
        },
        type_id: {
            type: DataTypes.INTEGER(10),
            allowNull: false,
            primaryKey: true,
            references: {
                model: 'type',
                key: 'id'
            }
        },
        create_at: {
            type: DataTypes.DATE,
            allowNull: true,
            defaultValue: 'CURRENT_TIMESTAMP'
        },
        update_at: {
            type: DataTypes.DATE,
            allowNull: true,
            defaultValue: 'CURRENT_TIMESTAMP'
        }
    }, {
        tableName: 'account_has_plan',
        underscored: true,
        freezeTableName: true,
        timestamps: false
    });

sequelize.define('type', {
        id: {
            type: DataTypes.INTEGER(10),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING,
            allowNull: false
        },
        element: {
            type: DataTypes.ENUM('Meal','Workout','Status'),
            allowNull: false
        }
    }, {
        tableName: 'type',
        freezeTableName: true,
        timestamps: false
    });

the code on which calls the models and execute the query to DB is this:

var model = $.loadModel("account_has_plan");
var type  = $.loadModel("type");


model.belongsTo(type);

var query = {
    where: {
        account_id: $.params.accountId
    },
    limit: $.query.limit || 12,
    offset: $.query.offset || 0,
    attributes:["account_id"],
    include: [{
        model: type
    }]
};

model.findAll(query)
    .then(function(data){
        $.data = data;
        $.json();
    })
    .catch(function(err){
        console.log(err);
        errorHandler.throwStatus(500, $);
    });

this way the data from the server responds like this:

{
    "account_id": 1,
    "type": {
      "id": 2,
      "name": "Arms",
      "element": "Workout"
    }
  }

there is actually no problem with this data, but i am forced to follow a pattern from docs i was provided with, which the difference there is that type is actually a string value rather than object value as presented in the example, so in the end the data structure i try to get has to be like this:

{
    "account_id": 1,
    "type": "Arms"
}

i have actually how idea on how to achieve this, i know i have little practice with sequelize, maybe this has to be defined through a model method or using through in the reference (which i have tried but returns error) but the point is.. i need to know if it can be possible before i have to report a change on the REST documentation which is big

1 Answer 1

1

First of all, since what you'll be getting through a sequelize query are instances, you need to convert it to "plain" object. You could do that by using a module called sequelize-values.

Then you should be able to manually map the value to the one you're looking for.

model.findAll(query)
.then(function(data){
    return data.map(function(d){
        var rawValue = d.getValues();
        rawValue.type = rawValue.type.name;
        return rawValue;
    });
})
.then(function(data){
    $.data = data;
    $.json();
})
.catch(function(err){
    console.log(err);
    errorHandler.throwStatus(500, $);
});
Sign up to request clarification or add additional context in comments.

1 Comment

This is useful, but i want to know if there is a way to achieve this without the need of downloading a module or making this logic outside the model

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.