sails: Datetime type no longer usable with blueprints on Sails 1.0
Sails version: 1.1.0 Node version 10.15.0 NPM version: 6.4.1 DB adapter name: sails-mongo DB adapter version: 1.0.1 Operating system: Ubuntu
Repository with simulations for Sails 0.12.x and 1.X: https://github.com/Goostavo/sails_datetime_test
While updating my sails application from 0.12 to 1.0, i’ve found out that datetime format is no longer supported. While 0.12 could parse Date() into db specific, there is no such solution available on 1.0 to convert 0.12.x applications.
On model i could achieve the desired behavior by using the code below. But blueprints are not longing working as it cannot parse to Date().
myField: {
type: 'ref',
columnType: 'datetime',
required: true,
custom: function (dateAttribute){
return (_.isDate(dateAttribute));
}
}
I’ve entered the 0.12.x sails-mongo code and found out that it was handling the datetime attribute on lib/query/index.js on Query.prototype.parseValue. But these functionality are not present anymore, as the only type used on adapter is the ref.
Steps to reproduce in sails 0.12.x and 1.0: **0.12 behaviour.**
- Create an model and add an attribute with
myAttrb: {type: "datetime"} - Lift Sails with mongodb
- Using blueprints make a post to create a new model-object. The model will be created with ISODate(‘YYYY-MM-DDTHH-MM-SS.ssssZ’) format.
- Do a find on using myAttrb. And you will query your data as expected.
1.0 behaviour
- Create an model and add an attribute with
type: "ref", columnType: "datetime" - Lift Sails with mongodb
- Using blueprints make a post to create a new model-object. The model will be created with ISODate(‘YYYY-MM-DDTHH-MM-SS.ssssZ’) format.
- Do a find on using myAttrb. You won’t find any data.
I believe that Sails.js need some way to transparently handle datetime format. As many applications relies heavily on timestamp data.
Proposal 1: -> Extending custom functions and overrides to achieve the desired response. And adding the datetime tutorial on sails-docs.
Proposal 2:
-> Adding an ‘datetime’ field that is adapter depent. If the adapter dont supports it will only throw an error on sails lift. On adapter implement parsing from js Date() and from adapter standard. Also this can be used on updatedAt/createdAt fields to have the same behaviour as 0.12
With some guidelines i can code the patch!
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 2
- Comments: 16 (4 by maintainers)
Done! Sails test on github
The behaviour on sails-disk is exactly the same in sails-mongo adapter. To make it easier to test i leave it to sails-disk for now.
On Sails 0.12 (expected behaviour):
datetimetype.On Sails 1.0 (case 1 with validation).
reftype. With lodash_isDate()validation.On Sails 1.0 (case 2 no validation).
reftype.Ok! I’ll write a sample code and simulate.
Hi @Goostavo, Thanks for exploring this and providing potential solutions. The sample code is also helpful.
@Goostavo the default
createdAtandupdatedAtattributes use numbers, and you can query them using'>','<', etc. in much the same way:I’m working on updating the upgrade guide with more info about why the available types changed in v1, but in the mean time here’s Mike’s explanation when some folks were asking about it awhile back: https://gitter.im/balderdashy/sails?at=58be0853f1a33b62758c3cc1