目前sequelize会序列化像这样的实例
id: 2,
title: 'bla bla',
url: 'https://mail.google.com/mail/u/0/#inbox/14c6002a21ae0679',
createdAt: Sat Mar 28 2015 18:58:42 GMT+0800 (SGT),
updatedAt: Sat Mar 28 2015 18:58:42 GMT+0800 (SGT),
userBookmarks:
{ createdAt: Sat Mar 28 2015 18:59:16 GMT+0800 (SGT),
updatedAt: Sat Mar 28 2015 18:59:16 GMT+0800 (SGT),
userId: 1,
bookmarkId: 2 } }
我只想强调关系序列化的方式。这是一个书签模型,想要构建或使用一个lib,它通常将用户标记xxx的关系修改为user:[];
任何想法?
答案 0 :(得分:0)
您可以提供自定义toJSON功能:
var model = sequelize.define(name, attributes, {
instanceMethods: {
toJSON: function () {
var values = this.constructor.prototype.toJSON.call(this);
// Do your magic here
}
}
});