From 52ea4e77c041f7a43ea08db35548cf3840385467 Mon Sep 17 00:00:00 2001 From: Derek Gray Date: Thu, 18 Oct 2018 20:42:51 -0400 Subject: [PATCH 1/3] Support 'object' as STRING --- lib/modelGenerators/default.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 From 36fde74928f65d85dacb2b2ca1285e17574b3014 Mon Sep 17 00:00:00 2001 From: Derek Gray Date: Thu, 18 Oct 2018 20:43:27 -0400 Subject: [PATCH 2/3] In postgres, support 'object' as JSONB --- lib/modelGenerators/postgres.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/modelGenerators/postgres.js b/lib/modelGenerators/postgres.js index c5bae24..c87d9e2 100644 --- a/lib/modelGenerators/postgres.js +++ b/lib/modelGenerators/postgres.js @@ -60,6 +60,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 || {}) + } + } } } } From 49991619acd1d1617c860d5b15967cfae55351c5 Mon Sep 17 00:00:00 2001 From: Derek Gray Date: Thu, 18 Oct 2018 21:27:55 -0400 Subject: [PATCH 3/3] Support array of JSONB too. --- lib/modelGenerators/postgres.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/modelGenerators/postgres.js b/lib/modelGenerators/postgres.js index c87d9e2..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) || []