Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transient property

Tags:

sequelize.js

I want to add additional properties to my object, before I return it to the client. At they moment they are omitted from the object when it is serialized. Is there are way to add a transient property?

like image 260
p15 Avatar asked Dec 21 '25 10:12

p15


1 Answers

I think setting type to DataTypes.VIRTUAL may help.

For example

var Line = sequelize.define("Line", {
    name: {
        type: DataTypes.STRING(50),
        allowNull: false
    },
    projects: {
        type: DataTypes.VIRTUAL
    }
});

Line.findAll().then(function (lines) {
    for (var i = 0; i < lines.length; i++)
        lines[i].projects = [];
    console.log(JSON.stringify(lines));
});

Output

[{"name":"test1","projects":[]},{"name":"test2","projects":[]}]

Documentation: http://docs.sequelizejs.com/en/latest/api/datatypes/

Hope it helps.

like image 148
张幸铖 Avatar answered Dec 24 '25 09:12

张幸铖



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!