Created
August 6, 2014 19:45
-
-
Save sixlettervariables/a814c1778b9165e43a30 to your computer and use it in GitHub Desktop.
Sequelize: toPOJO for DustJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For why this is necessary see issue sequelize/sequelize#2119.