Skip to content

Instantly share code, notes, and snippets.

@sixlettervariables
Created August 6, 2014 19:45
Show Gist options
  • Save sixlettervariables/a814c1778b9165e43a30 to your computer and use it in GitHub Desktop.
Save sixlettervariables/a814c1778b9165e43a30 to your computer and use it in GitHub Desktop.
Sequelize: toPOJO for DustJS
///var _ = require('lodash');
/** Convert a Sequelize model instance into a true POJO.
*
* This differs from `Instance.toJSON` in that nested instances are
* converted as well.
*
* @param {Object|Array} [instance] Sequelize model instance or instances.
* @return {Object|Array} A plain object version of the instance.
*/
function toPOJO(instance) {
if (_.isObject(instance)) {
instance = _getDataValues(instance) || instance;
return _.cloneDeep(instance, _getDataValues);
}
return instance;
}
function _getDataValues(value) {
return _.has(value, 'dataValues') ? value.dataValues : undefined;
}
@sixlettervariables
Copy link
Author

For why this is necessary see issue sequelize/sequelize#2119.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment