Skip to content

Commit e118c49

Browse files
committed
Fix serialization of arrays of string in update
1 parent 3ca9536 commit e118c49

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

lib/postgresql.js

+20
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,26 @@ PostgreSQL.prototype.toColumnValue = function(prop, val, isWhereClause) {
766766
});
767767
}
768768

769+
if (prop.type instanceof Array) {
770+
// There is two possible cases for the type of "val" as well as two cases for dataType
771+
const isArrayDataType = prop.postgresql &&
772+
prop.postgresql.dataType &&
773+
(prop.postgresql.dataType.indexOf('[]') > -1);
774+
if (val instanceof Array) {
775+
if (isArrayDataType) {
776+
return val;
777+
} else {
778+
return JSON.stringify(val);
779+
}
780+
} else {
781+
if (isArrayDataType) {
782+
return JSON.parse(val);
783+
} else {
784+
return val;
785+
}
786+
}
787+
}
788+
769789
return val;
770790
};
771791

test/postgresql.test.js

+53
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ describe('postgresql connector', function() {
6464
loc: 'GeoPoint',
6565
created: Date,
6666
approved: Boolean,
67+
tags: {
68+
type: ['string'],
69+
},
70+
categories: {
71+
type: ['string'],
72+
postgresql: {
73+
dataType: 'varchar[]',
74+
},
75+
},
6776
});
6877
created = new Date();
6978
});
@@ -201,6 +210,50 @@ describe('postgresql connector', function() {
201210
});
202211
});
203212

213+
it('should support creating and updating arrays with default dataType', function(done) {
214+
let postId;
215+
Post.create({title: 'Updating Arrays', content: 'Content', tags: ['AA', 'AB']})
216+
.then((post)=> {
217+
postId = post.id;
218+
post.should.have.property('tags');
219+
post.tags[1].should.eql('AB');
220+
return Post.updateAll({where: {id: postId}}, {tags: ['AA', 'AC']});
221+
})
222+
.then((wooot)=> {
223+
return Post.findOne({where: {id: postId}});
224+
})
225+
.then((post)=> {
226+
post.should.have.property('tags');
227+
post.tags[1].should.eql('AC');
228+
done();
229+
})
230+
.catch((error) => {
231+
done(error);
232+
});
233+
});
234+
235+
it('should support creating and updating arrays with "varchar[]" dataType', function(done) {
236+
let postId;
237+
Post.create({title: 'Updating Arrays', content: 'Content', categories: ['AA', 'AB']})
238+
.then((post)=> {
239+
postId = post.id;
240+
post.should.have.property('categories');
241+
post.categories[1].should.eql('AB');
242+
return Post.updateAll({where: {id: postId}}, {categories: ['AA', 'AC']});
243+
})
244+
.then((wooot)=> {
245+
return Post.findOne({where: {id: postId}});
246+
})
247+
.then((post)=> {
248+
post.should.have.property('categories');
249+
post.categories[1].should.eql('AC');
250+
done();
251+
})
252+
.catch((error) => {
253+
done(error);
254+
});
255+
});
256+
204257
it('should support boolean types with false value', function(done) {
205258
Post.create(
206259
{title: 'T2', content: 'C2', approved: false, created: created},

0 commit comments

Comments
 (0)