不要怂,就是干,撸起袖子干!

Commit bc108f9b by Mick Hansen

Merge branch 'kleinsch-postgres-json-custom-field'

2 parents 129fc898 fa19ed9d
...@@ -315,7 +315,7 @@ module.exports = (function() { ...@@ -315,7 +315,7 @@ module.exports = (function() {
} }
var value = attrValueHash[key]; var value = attrValueHash[key];
values.push(this.quoteIdentifier(key) + '=' + this.escape(value, (!!attributes && !!attributes[key] ? attributes[key] : undefined))); values.push(this.quoteIdentifier(key) + '=' + this.escape(value, (modelAttributeMap && modelAttributeMap[key] || undefined)));
} }
var replacements = { var replacements = {
......
...@@ -896,7 +896,7 @@ module.exports = (function() { ...@@ -896,7 +896,7 @@ module.exports = (function() {
}else if (field.type === DataTypes.ARRAY(DataTypes.HSTORE)){ }else if (field.type === DataTypes.ARRAY(DataTypes.HSTORE)){
return "ARRAY[" + Utils._.map(value, function(v){return "'" + hstore.stringify(v) + "'::hstore";}).join(",") + "]::HSTORE[]"; return "ARRAY[" + Utils._.map(value, function(v){return "'" + hstore.stringify(v) + "'::hstore";}).join(",") + "]::HSTORE[]";
} }
} else if (Utils._.isObject(value) && field && (field.type === DataTypes.JSON)) { } else if (Utils._.isObject(value) && field && (field.type === DataTypes.JSON || field.type === DataTypes.JSONB)) {
value = JSON.stringify(value); value = JSON.stringify(value);
} }
......
...@@ -72,6 +72,39 @@ if (dialect.match(/^postgres/)) { ...@@ -72,6 +72,39 @@ if (dialect.match(/^postgres/)) {
}); });
}); });
it('should insert json using a custom field name', function() {
var self = this;
this.UserFields = this.sequelize.define('UserFields', {
emergencyContact: { type: DataTypes.JSON, field: 'emergy_contact' }
});
return this.UserFields.sync({ force: true }).then(function() {
return self.UserFields.create({
emergencyContact: { name: 'joe', phones: [1337, 42] }
}).then(function(user) {
expect(user.emergencyContact.name).to.equal('joe');
});
});
});
it('should update json using a custom field name', function() {
var self = this;
this.UserFields = this.sequelize.define('UserFields', {
emergencyContact: { type: DataTypes.JSON, field: 'emergy_contact' }
});
return this.UserFields.sync({ force: true }).then(function() {
return self.UserFields.create({
emergencyContact: { name: 'joe', phones: [1337, 42] }
}).then(function(user) {
user.emergencyContact = { name: 'larry' };
return user.save();
}).then(function(user) {
expect(user.emergencyContact.name).to.equal('larry');
});
});
});
it('should be able retrieve json value as object', function() { it('should be able retrieve json value as object', function() {
var self = this; var self = this;
var emergencyContact = { name: 'kate', phone: 1337 }; var emergencyContact = { name: 'kate', phone: 1337 };
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!