balderdashy / balderdashy/sails
Datetime type no longer usable with blueprints on Sails 1.0
- Dominant language
- JavaScript
- Stars
- 22.8k
- Forks
- 1.9k
- PR merge metrics
- No merged PRs in 30d
Description
**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().
```javascript
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.**
1. Create an model and add an attribute with `myAttrb: {type: "datetime"}`
2. Lift Sails with mongodb
3. 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.
4. Do a find on using myAttrb. And you will query your data as expected.
**1.0 behaviour**
1. Create an model and add an attribute with `type: "ref", columnType: "datetime"`
2. Lift Sails with mongodb
3. 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.
4. 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!
Contributor guide
Research direction
Start with the linked sails_datetime_test reproduction repository and compare its Sails 0.12.x and 1.x cases. Read the older sails-mongo implementation at lib/query/index.js, especially Query.prototype.parseValue, then trace the current blueprint create and find paths. Done means the documented datetime scenario behaves consistently for both blueprint writes and queries.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb, node.js
- Domain
- api, backend, databases
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100