Connecting Sails js with postgres database views (getting Server Error (500))

713 views
Skip to first unread message

Ashish Gupta

unread,
Aug 13, 2014, 8:28:42 PM8/13/14
to sai...@googlegroups.com
I have a working sails js application with postgresql as database, to improve the performance I am trying to use database views, I was able to sucessfully create the model/controller using sails js generate command, and filled in column names and types just like any other model. I was getting the error: **A hook (`orm`) failed to load!** which I was able to resolve after updating migrate: 'safe' in config->models.js.

But now I am getting server error 500 when trying to access via sails blueprints. Database permissions are OK. Is there anything else required, I searched the documentation but could not find any help on connecting the database views. 

**Model**

    module.exports = {

    migrate: 'safe',

attributes: {

        name: {type: 'string'},

        enquiryDate: {type: 'datetime'},

        totalFee: {type: 'float'},

     }

Controller:

module.exports = {
};



**My Setup:**

*"sails": "^0.10.2",
"sails-postgresql": "^0.10.7"*

Ashish Gupta

unread,
Aug 13, 2014, 9:13:23 PM8/13/14
to sai...@googlegroups.com
Found the problem! there was a typo in the column name in model.js file which was resulting in server error!.. After fixing the name all the routes are working fine.

Thanks,
Ashish

Rob Wormald

unread,
Aug 13, 2014, 9:35:53 PM8/13/14
to Ashish Gupta, sai...@googlegroups.com
Ashish -

Just to clarify - would you explain the basic steps for how you’re using views?

Is it pretty much “create a named view” and then just use that as the table name in Sails? That’s crazy powerful if so!

Rob

Ashish Gupta

unread,
Aug 13, 2014, 10:04:19 PM8/13/14
to sai...@googlegroups.com, ashish...@gmail.com
Yes, that's correct. I am just getting started, but here are the steps I followed.

1. Create database view (in my case postgres database), using following command.

CREATE VIEW [viewname] AS [Select * from xyz]

2. Generate sails models

sails generate api [viewName]

3. Add column definitions in sails model js file [viewName.js]

4.  Start sails server.... it works out of the box..

This is not documented anywhere, I think this is one of the most powerful feature which let the user make the call if one wants to keep the business logic in the database or at the server side!

Sails team is doing a geat work! Thanks to all!
Reply all
Reply to author
Forward
0 new messages