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

Commit 212734e9 by Mick Hansen

fix: provide create/update support for DataTypes.ARRAY(DataTypes.JSON), closes #2765, closes #2764

1 parent 18e808ec
...@@ -891,13 +891,17 @@ module.exports = (function() { ...@@ -891,13 +891,17 @@ module.exports = (function() {
} }
if (Utils._.isObject(value) && field && (field.type === DataTypes.HSTORE || field.type === DataTypes.ARRAY(DataTypes.HSTORE))) { if (Utils._.isObject(value) && field && (field.type === DataTypes.HSTORE || field.type === DataTypes.ARRAY(DataTypes.HSTORE))) {
if(field.type === DataTypes.HSTORE){ if (field.type === DataTypes.HSTORE){
return "'" + hstore.stringify(value) + "'"; return "'" + hstore.stringify(value) + "'";
}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 || field.type === DataTypes.JSONB)) { } else if (Utils._.isObject(value) && field && (field.type === DataTypes.JSON || field.type === DataTypes.JSONB)) {
value = JSON.stringify(value); value = JSON.stringify(value);
} else if (Array.isArray(value) && field.type === DataTypes.ARRAY(DataTypes.JSON)) {
return "ARRAY[" + value.map(function (v) {
return SqlString.escape(JSON.stringify(v), false, this.options.timezone, this.dialect, field);
}, this).join(",") + "]::JSON[]";
} }
return SqlString.escape(value, false, this.options.timezone, this.dialect, field); return SqlString.escape(value, false, this.options.timezone, this.dialect, field);
......
...@@ -43,26 +43,27 @@ if (dialect.match(/^postgres/)) { ...@@ -43,26 +43,27 @@ if (dialect.match(/^postgres/)) {
}); });
}); });
it('should be able update a field with type ARRAY(JSON)', function(){ it('should be able to update a field with type ARRAY(JSON)', function(){
return this.User.create({ return this.User.create({
username: 'bob', email: ['myemail@email.com'] username: 'bob',
}) email: ['myemail@email.com'],
.then(function(userInstance){ friends: [{
expect(userInstance.friends).to.have.length(0); name: 'John Smith'
}]
return userInstance.updateAttributes({ }).then(function(userInstance){
expect(userInstance.friends).to.have.length(1);
expect(userInstance.friends[0].name).to.equal('John Smith');
return userInstance.update({
friends: [{ friends: [{
name: 'John Smythe' name: 'John Smythe'
}], }]
//friends: [JSON.stringify({
//name: 'John Smythe'
//})]
}); });
}) })
.get('friends') .get('friends')
.tap(function(friends){ .tap(function(friends){
expect(friends).to.have.length(1); expect(friends).to.have.length(1);
expect(friends[0].name).to.equal('John Smythe');
}); });
}); });
...@@ -93,9 +94,11 @@ if (dialect.match(/^postgres/)) { ...@@ -93,9 +94,11 @@ if (dialect.match(/^postgres/)) {
return this.User.create({ return this.User.create({
username: 'bob', username: 'bob',
emergency_contact: { name: 'joe', phones: [1337, 42] } emergency_contact: { name: 'joe', phones: [1337, 42] }
}, {
fields: ['id', 'username', 'document', 'emergency_contact']
}).on('sql', function(sql) { }).on('sql', function(sql) {
var expected = 'INSERT INTO "Users" ("id","username","document","emergency_contact","createdAt","updatedAt") VALUES (DEFAULT,\'bob\',\'"default"=>"\'\'value\'\'"\',\'{"name":"joe","phones":[1337,42]}\''; var expected = '\'{"name":"joe","phones":[1337,42]}\'';
expect(sql.indexOf(expected)).to.equal(0); expect(sql.indexOf(expected)).not.to.equal(-1);
}); });
}); });
...@@ -253,8 +256,8 @@ if (dialect.match(/^postgres/)) { ...@@ -253,8 +256,8 @@ if (dialect.match(/^postgres/)) {
email: ['myemail@email.com'], email: ['myemail@email.com'],
settings: {mailing: false, push: 'facebook', frequency: 3} settings: {mailing: false, push: 'facebook', frequency: 3}
}).on('sql', function(sql) { }).on('sql', function(sql) {
var expected = 'INSERT INTO "Users" ("id","username","email","settings","document","createdAt","updatedAt") VALUES (DEFAULT,\'bob\',ARRAY[\'myemail@email.com\']::TEXT[],\'"mailing"=>"false","push"=>"facebook","frequency"=>"3"\',\'"default"=>"\'\'value\'\'"\''; var expected = '\'"mailing"=>"false","push"=>"facebook","frequency"=>"3"\',\'"default"=>"\'\'value\'\'"\'';
expect(sql.indexOf(expected)).to.equal(0); expect(sql.indexOf(expected)).not.to.equal(-1);
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!