How to perform SQL Joins and Relations in Sails.js and Waterline?

Brieuce Wayne picture Brieuce Wayne · Jul 16, 2013 · Viewed 21k times · Source

Can anyone guide me on how to setup relational schema & performs joins in sails.js?

Answer

particlebanana picture particlebanana · Jul 31, 2013

Associations are officially Supported in Waterline

Overview

From the docs:

With Sails and Waterline, you can associate models across multiple data stores. This means that even if your users live in PostgreSQL and their photos live in MongoDB, you can interact with the data as if they lived together in the same database. You can also have associations that span different connections (i.e. datastores/databases) using the same adapter. This comes in handy if, for example, your app needs to access/update legacy recipe data stored in a MySQL database in your company's data center, but also store/retrieve ingredient data from a brand new MySQL database in the cloud.

Supported Association Types

Planned Association Types


Original Post

I'm the author of Waterline, the ORM used in Sails. Waterline is brand new and we are adding features all the time. Currently we don't have support for associations but it's next on the roadmap. We worked out an API for associations that I think most people will really like. You can view the work in progress and the proposed API at: [Proposed Sails Associations API][1].

We are going to tackle Associations and Transactions next and hope to have them ready in the next month or so.

In the mean time if you are using the MySQL or PostgreSQL adapters they both expose a raw .query() method that allows you to pass in a hand built sql query and have it executed. I totally realize this isn't ideal but should allow you to continue building your app while we get support for associations and joins.

The function signature for the query method is:

Model.query(<sql query>, <optional data>, callback);