Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 605
Brieuce Wayne Avatar asked Jul 16 '13 17:07

Brieuce Wayne


People also ask

What is waterline in sails js?

Sails/Waterline lets you hook up different models to different datastores, locally or anywhere on the internet. You can build a User model that maps to a custom MySQL table in a legacy database (with weird crazy column names).

How does sails js work?

Sails is built on Node. js, a popular, lightweight server-side technology that allows developers to write blazing fast, scalable network appliations in JavaScript. Sails uses Express for handling HTTP requests, and wraps socket.io for managing WebSockets.

What is sails in node js?

js (or Sails) is a model–view–controller (MVC) web application framework developed atop the Node. js environment, released as free and open-source software under the MIT License. It is designed to make it easy to build custom, enterprise-grade Node.

Is sails js framework?

Sails. js is a web framework that you can use to easily build customized enterprise-grade Node. js applications. It resembles the MVC architecture from such frameworks as Ruby on Rails, but with improved support for the more data-oriented modern style of developing web apps.


2 Answers

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

  • One to Many
  • Many to Many
    • Cross-adapter Dominance
  • One to One
  • One Way

Planned Association Types

  • Through Associations

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); 
like image 184
particlebanana Avatar answered Sep 23 '22 18:09

particlebanana


The example from particle banana works but should actually use "new" like "var instance = new User._model(values)". I'm using the following code and it works.

Accounts.query(query, function(err, accounts) {   if (err)     return fn(err);    accounts = _.map(accounts, function(account) {     return new Accounts._model(account);   });    fn(null, accounts); }); 
like image 20
Bruce Kim Avatar answered Sep 21 '22 18:09

Bruce Kim