Skip to content

Commit 007cdfa

Browse files
onalufSAMIBETTAYEB
authored andcommitted
Fix serialization of arrays of string in update (loopbackio#428)
Signed-off-by: SAMI BETTAYEB <[email protected]>
1 parent d779f13 commit 007cdfa

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

lib/postgresql.js

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

769+
if (Array.isArray(prop.type)) {
770+
// There is two possible cases for the type of "val" as well as two cases for dataType
771+
const isArrayDataType = prop.postgresql && prop.postgresql.dataType === 'varchar[]';
772+
if (Array.isArray(val)) {
773+
if (isArrayDataType) {
774+
return val;
775+
} else {
776+
return JSON.stringify(val);
777+
}
778+
} else {
779+
if (isArrayDataType) {
780+
return JSON.parse(val);
781+
} else {
782+
return val;
783+
}
784+
}
785+
}
786+
769787
return val;
770788
};
771789

test/postgresql.test.js

+62
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,59 @@ 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.should.be.Array();
220+
post.tags.length.should.eql(2);
221+
post.tags.should.eql(['AA', 'AB']);
222+
return Post.updateAll({where: {id: postId}}, {tags: ['AA', 'AC']});
223+
})
224+
.then(()=> {
225+
return Post.findOne({where: {id: postId}});
226+
})
227+
.then((post)=> {
228+
post.should.have.property('tags');
229+
post.tags.should.be.Array();
230+
post.tags.length.should.eql(2);
231+
post.tags.should.eql(['AA', 'AC']);
232+
done();
233+
})
234+
.catch((error) => {
235+
done(error);
236+
});
237+
});
238+
239+
it('should support creating and updating arrays with "varchar[]" dataType', function(done) {
240+
let postId;
241+
Post.create({title: 'Updating Arrays', content: 'Content', categories: ['AA', 'AB']})
242+
.then((post)=> {
243+
postId = post.id;
244+
post.should.have.property('categories');
245+
post.should.have.property('categories');
246+
post.categories.should.be.Array();
247+
post.categories.length.should.eql(2);
248+
post.categories.should.eql(['AA', 'AB']);
249+
return Post.updateAll({where: {id: postId}}, {categories: ['AA', 'AC']});
250+
})
251+
.then(()=> {
252+
return Post.findOne({where: {id: postId}});
253+
})
254+
.then((post)=> {
255+
post.should.have.property('categories');
256+
post.categories.should.be.Array();
257+
post.categories.length.should.eql(2);
258+
post.categories.should.eql(['AA', 'AC']);
259+
done();
260+
})
261+
.catch((error) => {
262+
done(error);
263+
});
264+
});
265+
204266
it('should support boolean types with false value', function(done) {
205267
Post.create(
206268
{title: 'T2', content: 'C2', approved: false, created: created},

0 commit comments

Comments
 (0)