mphasize/sails-generate-ember-blueprints

date queries appear broken

mgenev opened this issue · 6 comments

I tried out doing date queries the way the Sails docs has them

Model.find({ date: { '>': new Date('2/4/2014'), '<': new Date('2/7/2014') } })

this works when you have a custom controller.

The problem is in the case of the blueprint, http turns dates into strings and when the blueprint is oblivious that a date value comes it doesn't know to cast it and you get an error:

error: Sending 500 ("Server Error") response: 
 Error (E_UNKNOWN) :: Encountered an unexpected error
MongoError: invalid regular expression operator
    at Object.toError (/Users/mgenev/Projects/how-to-sane/server/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/utils.js:114:11)
    at /Users/mgenev/Projects/how-to-sane/server/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/cursor.js:700:54
    at Cursor.close (/Users/mgenev/Projects/how-to-sane/server/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/cursor.js:989:5)
    at commandHandler (/Users/mgenev/Projects/how-to-sane/server/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/cursor.js:700:21)
    at /Users/mgenev/Projects/how-to-sane/server/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/db.js:1874:9
    at Server.Base._callHandler (/Users/mgenev/Projects/how-to-sane/server/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/connection/base.js:453:41)

Please let me know if you have any solution for this.

A suggestion came up in the sane chat that the blueprint does a look up on a model's data types after it parses the model's name and figures out which ones should be dates and casts them...

I'm seeing that each model has the _attributes field which we can walk and check for datetime fields, then check if the client sent those fields in the request and if so cast them into dates. The createdAt and updatedAt fields are always such fields by default.

This may after all be an adapter issue in sails-mongo and may have been fixed by this PR which may not have been released yet:
balderdashy/sails-mongo@1e7f2e1

@mgenev Interesting, do you know if that would have worked with the Sails default blueprints?

I don't think so

They released the new sails-mongo per my request and that fixed it for every blueprint

Thanks for the update. This means that after an update of sails-mongo it should also work with the ember blueprints.

Yep, I already tried it