How does the Sequelize POJO JSON.stringify magic work?

我怕爱的太早我们不能终老 提交于 2019-12-12 01:46:31

问题


In sequelize if you do a

db.MyTable.findAll({}).function(response){
    console.log(response);
}

You will see output that looks like this (depending on what your table looks like, of course):

[ { dataValues:
     { id: 1,
       text: 'sdf',
       complete: false,
       createdAt: Thu Jan 19 2017 11:55:38 GMT-0500 (Eastern Standard Time),
       updatedAt: Thu Jan 19 2017 11:55:38 GMT-0500 (Eastern Standard Time) },
    _previousDataValues:
     { id: 1,
       text: 'sdf',
       complete: false,
       createdAt: Thu Jan 19 2017 11:55:38 GMT-0500 (Eastern Standard Time),
       updatedAt: Thu Jan 19 2017 11:55:38 GMT-0500 (Eastern Standard Time) },
    _changed: {},
    '$modelOptions':
     { timestamps: true,
       instanceMethods: {},
       classMethods: {},
       validate: {},
       freezeTableName: false,
       underscored: false,
       underscoredAll: false,
       paranoid: false,
       rejectOnEmpty: false,
       whereCollection: null,
       schema: null,
       schemaDelimiter: '',
       defaultScope: {},
       scopes: [],
       hooks: {},
       indexes: [],
       name: [Object],
       omitNul: false,
       sequelize: [Object],
       uniqueKeys: {},
       hasPrimaryKeys: true },
    '$options':
     { isNewRecord: false,
       '$schema': null,
       '$schemaDelimiter': '',
       raw: true,
       attributes: [Object] },
    hasPrimaryKeys: true,
    __eagerlyLoadedAssociations: [],
    isNewRecord: false } ]

Suffice to say, it is a big complex object with a bunch of metaData on it.

However, if you try and turn that big complicated object into a string using either:

console.log(JSON.stringify(dbTodo));

or

res.json(dbTodo);

you will get back just the information for the actual entity (the items in the dataValues property of the big complex object). For my test table here, it looks like this:

[{
    "id":1,
    "text":"sdf",
    "complete":false,
    "createdAt":"2017-01-19T16:55:38.000Z",
    "updatedAt":"2017-01-19T16:55:38.000Z"
 }]

Which is significantly simpler (and just what I want).

How does this magic happen? Is sequelize creating their own versions of JSON.stringify() and express' res.json?

What are the rules around when it happens and when it doesn't happen?

I've searched through the sequelize docs and haven't found a good explanation.


回答1:


There are two ways this behavior could manifest.

  1. The missing properties are non-enumerable. Properties defined by Object.defineProperty set to enumerable: false (or simply don't supply a value for the enumerable setting) are non-enumerable properties, and are ignored by JSON.stringify. You can check a property's enumerable setting in the output of Object.getOwnPropertyDescriptor(obj, propName).

  2. The object has (or has in its prototype chain) a toJSON method:

    If an object being stringified has a property named toJSON whose value is a function, then the toJSON() method customizes JSON stringification behavior: instead of the object being serialized, the value returned by the toJSON() method when called will be serialized.

    If the object has or inherits a toJSON method, then the return value of that method will be the target for stringification. You can check if one exists on some object obj simply by reading the value of obj.toJSON.



来源:https://stackoverflow.com/questions/41747844/how-does-the-sequelize-pojo-json-stringify-magic-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!