Sequelize, convert entity to plain object

后端 未结 9 1223
说谎
说谎 2020-12-12 16:53

I\'m not very familiar with javascript, and stunning, because i can\'t add new property, to object, that fetched from database using ORM names Sequelize.js.

To avoid

9条回答
  •  孤街浪徒
    2020-12-12 17:22

    Here's what I'm using to get plain response object with non-stringified values and all nested associations from sequelize v4 query.

    With plain JavaScript (ES2015+):

    const toPlain = response => {
      const flattenDataValues = ({ dataValues }) => {
        const flattenedObject = {};
    
        Object.keys(dataValues).forEach(key => {
          const dataValue = dataValues[key];
    
          if (
            Array.isArray(dataValue) &&
            dataValue[0] &&
            dataValue[0].dataValues &&
            typeof dataValue[0].dataValues === 'object'
          ) {
            flattenedObject[key] = dataValues[key].map(flattenDataValues);
          } else if (dataValue && dataValue.dataValues && typeof dataValue.dataValues === 'object') {
            flattenedObject[key] = flattenDataValues(dataValues[key]);
          } else {
            flattenedObject[key] = dataValues[key];
          }
        });
    
        return flattenedObject;
      };
    
      return Array.isArray(response) ? response.map(flattenDataValues) : flattenDataValues(response);
    };
    

    With lodash (a bit more concise):

    const toPlain = response => {
      const flattenDataValues = ({ dataValues }) =>
        _.mapValues(dataValues, value => (
          _.isArray(value) && _.isObject(value[0]) && _.isObject(value[0].dataValues)
            ? _.map(value, flattenDataValues)
            : _.isObject(value) && _.isObject(value.dataValues)
              ? flattenDataValues(value)
              : value
        ));
    
      return _.isArray(response) ? _.map(response, flattenDataValues) : flattenDataValues(response);
    };
    

    Usage:

    const res = await User.findAll({
      include: [{
        model: Company,
        as: 'companies',
        include: [{
          model: Member,
          as: 'member',
        }],
      }],
    });
    
    const plain = toPlain(res);
    
    // 'plain' now contains simple db object without any getters/setters with following structure:
    // [{
    //   id: 123,
    //   name: 'John',
    //   companies: [{
    //     id: 234,
    //     name: 'Google',
    //     members: [{
    //       id: 345,
    //       name: 'Paul',
    //     }]
    //   }]
    // }]
    

提交回复
热议问题