diff --git a/lib/modelGenerators/default.js b/lib/modelGenerators/default.js index 2395571..3df65cd 100644 --- a/lib/modelGenerators/default.js +++ b/lib/modelGenerators/default.js @@ -113,6 +113,19 @@ module.exports = class { } } } + if (attribute._type === 'object') { + this[attributeName] = { + type: DataTypes.STRING, + allowNull: true, + get: function () { + const data = this.getDataValue(attributeName) + return data ? JSON.parse(data) : {} + }, + set: function (val) { + this.setDataValue(attributeName, JSON.stringify(val)) + } + } + } if (attribute._flags) { if (attribute._flags.presence === 'required') { this[attributeName].allowNull = false diff --git a/lib/modelGenerators/postgres.js b/lib/modelGenerators/postgres.js index c5bae24..9732f74 100644 --- a/lib/modelGenerators/postgres.js +++ b/lib/modelGenerators/postgres.js @@ -52,6 +52,11 @@ module.exports = class extends Default { allowNull: true } break + case 'object': + this[attributeName] = { + type: DataTypes.ARRAY(DataTypes.JSONB), + allowNull: true + } } this[attributeName].get = function () { return this.getDataValue(attributeName) || [] @@ -60,6 +65,19 @@ module.exports = class extends Default { this.setDataValue(attributeName, val || []) } } + if (attribute._type === 'object') { + // PostgreSQL has JSONB support, so lets use that + this[attributeName] = { + type: DataTypes.JSONB, + allowNull: true + } + this[attributeName].get = function () { + return this.getDataValue(attributeName) || {} + } + this[attributeName].set = function (val) { + this.setDataValue(attributeName, val || {}) + } + } } } }