balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

"sails.getDatastore() is not a function" in Service methods #5304

Open dmitrye opened 5 years ago

dmitrye commented 5 years ago

Node version: 10.13.0 Sails version (sails): 1.1.0 ORM hook version (sails-hook-orm): 2.1.0 Sockets hook version (sails-hook-sockets): 1.5.3 Organics hook version (sails-hook-organics): empty Grunt hook version (sails-hook-grunt): not used Uploads hook version (sails-hook-uploads): not used DB adapter & version (e.g. sails-mysql@5.55.5): not used, MongoDB Skipper adapter & version (e.g. skipper-s3@5.55.5): not used


Using sails.getDatastore() should be available in Service methods and not just Controllers. Service methods are often used for common DB Interactions as opposed to scattering methods across controller files. Therefore, if someone needs to use custom sails.getDatastore() method it should not just be in the controller.

sailsbot commented 5 years ago

@dmitrye Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

raqem commented 5 years ago

Hi @dmitrye even though Sails supports using services it is highly recommended to use helpers instead. Bonus helpers can use all database model methods.

image

dmitrye commented 5 years ago

@raqem Interesting choice. Solves the same problem I guess. I use services as a class type that handles specific processing and can contain public/private methods. I'll look at helpers which other than name sounds like it does the same thing.

I also worked around this issue by referencing getDatastore() from the model inside the service.

Works: sails.models["model_name"].getDatastore().manager;

Doesn't work: sails.getDatastore().manager;

For me this works since I'm working against existing models. But other people who may want to execute native queries against the DB without directly referencing the models (multi table native queries, or complex Mongo aggregates) will run into this problem.

raqem commented 5 years ago

@dmitrye Thanks for providing your work around! I hope helpers can live up to your expectations.