sails.js -postgresql returning string value instead of integer for bigint fields -
we migrating project php node.js using sails.js backend framework. cannot modify our database , have use existing database project.
if keep migrate: "alter"
newly created model, sails default keep id
field integer.
however, our existing database, id
fields bigint
. defined migrate: "safe"
, proceeded model creation.
now problem facing when blueprint routes return result, id column value, should returned number, being returned string instead. here example:
[ { "starttime": "07:00:00", "endtime": "14:00:00", "id": "1" }, { "starttime": "14:00:00", "endtime": "22:00:00", "id": "2" }, { "starttime": "22:00:00", "endtime": "07:00:00", "id": "3" } ]
how can fix issue?
here model:
module.exports = { tablename: "timeslots", autocreatedat: false, autoupdatedat: false, attributes: { starttime: { type: "string", required: true }, endtime: { type: "string", required: true } } };
and here postgresql table definition
table "public.timeslots" column | type | modifiers | storage | stats target | description -----------+--------+--------------------------------------------------------+----------+--------------+------------- id | bigint | not null default nextval('timeslots_id_seq'::regclass) | plain | | starttime | text | not null | extended | | endtime | text | not null | extended | | indexes: "idx_43504_primary" primary key, btree (id) referenced by: table "doctortimeslot" constraint "doctortimeslot_ibfk_2" foreign key (timeslot_id) references timeslots(id) on update cascade on delete cascade
waterline gets weird datatypes doesn't have built in. think defaults strings when it's not sure do. shouldn't matter js automatically coerce these values numbers on frontend.
however, if need number simplest solution override tojson method in model , have force value integer.
module.exports = { tablename: "timeslots", autocreatedat: false, autoupdatedat: false, attributes: { starttime: { type: "string", required: true }, endtime: { type: "string", required: true }, tojson: function(){ var obj = this.toobject(); obj.id = parseint(obj.id); return obj; } } };
Comments
Post a Comment