Skip to content

Fix serialization of arrays of string in update #428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,24 @@ PostgreSQL.prototype.toColumnValue = function(prop, val, isWhereClause) {
});
}

if (Array.isArray(prop.type)) {
// There is two possible cases for the type of "val" as well as two cases for dataType
const isArrayDataType = prop.postgresql && prop.postgresql.dataType === 'varchar[]';
if (Array.isArray(val)) {
if (isArrayDataType) {
return val;
} else {
return JSON.stringify(val);
}
} else {
if (isArrayDataType) {
return JSON.parse(val);
} else {
return val;
}
}
}

return val;
};

Expand Down
62 changes: 62 additions & 0 deletions test/postgresql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ describe('postgresql connector', function() {
loc: 'GeoPoint',
created: Date,
approved: Boolean,
tags: {
type: ['string'],
},
categories: {
type: ['string'],
postgresql: {
dataType: 'varchar[]',
},
},
});
created = new Date();
});
Expand Down Expand Up @@ -201,6 +210,59 @@ describe('postgresql connector', function() {
});
});

it('should support creating and updating arrays with default dataType', function(done) {
let postId;
Post.create({title: 'Updating Arrays', content: 'Content', tags: ['AA', 'AB']})
.then((post)=> {
postId = post.id;
post.should.have.property('tags');
post.tags.should.be.Array();
post.tags.length.should.eql(2);
post.tags.should.eql(['AA', 'AB']);
return Post.updateAll({where: {id: postId}}, {tags: ['AA', 'AC']});
})
.then(()=> {
return Post.findOne({where: {id: postId}});
})
.then((post)=> {
post.should.have.property('tags');
post.tags.should.be.Array();
post.tags.length.should.eql(2);
post.tags.should.eql(['AA', 'AC']);
done();
})
.catch((error) => {
done(error);
});
});

it('should support creating and updating arrays with "varchar[]" dataType', function(done) {
let postId;
Post.create({title: 'Updating Arrays', content: 'Content', categories: ['AA', 'AB']})
.then((post)=> {
postId = post.id;
post.should.have.property('categories');
post.should.have.property('categories');
post.categories.should.be.Array();
post.categories.length.should.eql(2);
post.categories.should.eql(['AA', 'AB']);
return Post.updateAll({where: {id: postId}}, {categories: ['AA', 'AC']});
})
.then(()=> {
return Post.findOne({where: {id: postId}});
})
.then((post)=> {
post.should.have.property('categories');
post.categories.should.be.Array();
post.categories.length.should.eql(2);
post.categories.should.eql(['AA', 'AC']);
done();
})
.catch((error) => {
done(error);
});
});

it('should support boolean types with false value', function(done) {
Post.create(
{title: 'T2', content: 'C2', approved: false, created: created},
Expand Down