Sails.js - Get an object (model) using multiple join
node.js, sails.js
Solution
Do you know what populate is for ? its for including the whole associated object when loading it from the database. Its practically the join you are trying to do, if all you need is row retrieval than quering the table without populate will make both functions you built work.
To me it looks like you are re-writing how Sails did the association. Id suggest giving the Associations docs another read in Sails documentation: Associations. As depending on your case you are just trying a one-to-many association with each table, you could avoid a middle table in my guess, but to decide better id need to understand your use-case.
When I saw the mySQL code it seemed to me you are still thinking in MySQL and PHP which takes time to convert from :) forcing the joins and middle tables yourself, redoing a lot of the stuff sails automated for you. I redone your example on 'disk' adapter and it worked perfectly. The whole point of WaterlineORM is to abstract the layer of going down to SQL unless absolutely necessary. Here is what I would do for your example, first without SQL just on a disk adapter id create the models :
// Lang.js
attributes: {
id :{ type: "Integer" , autoIncrement : true, primaryKey: true },
code :"string"
}
you see what i did redundantly here ? I did not really need the Id part as Sails does it for me. Just an example.
// Meta.js
attributes: {
code :"string"
}
better :) ?
// MetaLang.js
attributes:
{
title : "string",
desc : "string",
meta_id :
{
model : "meta",
},
lang_id :
{
model : "lang",
}
}
Now after simply creating the same values as your example i run sails console type :
MetaLang.find({meta_id : 1 ,lang_id:2}).exec(function(er,res){
console.log(res);
});
Output >>>
sails> [ { meta_id: 1,
lang_id: 2,
title: 'My blog',
id: 2 } ]
Now if you want to display what is meta with id 1 and what is lang with id 2, we use populate, but the referencing for join/search is just as simple as this.
sails> Meta_lang.find({meta_id : 1 ,lang_id:2}).populate('lang_id').populate('meta_id').exec(function(er,res){ console.log(res); });
undefined
sails> [ {
meta_id:
{ code: 'default',
id: 1 },
lang_id:
{ code: 'En',
id: 2 },
title: 'My blog',
id: 2 } ]
At this point, id switch adapters to MySQL and then create the MySQL tables with the same column names as above. Create the FK_constraints and voila. Another strict policy you can add is to set up the 'via' and dominance on each model. you can read more about that in the Association documentation and it depends on the nature of association (many-to-many etc.)
To get the same result without knowing the Ids before-hand :
sails> Meta.findOne({code : "default"}).exec(function(err,needed_meta){
..... Lang.findOne({code : "En"}).exec(function(err_lang,needed_lang){
....... Meta_lang.find({meta_id : needed_meta.id , lang_id : needed_lang.id}).exec(function(err_all,result){
......... console.log(result);});
....... });
..... });
undefined
sails> [ { meta_id: 1,
lang_id: 2,
title: 'My blog',
id: 2 } ]
Problem
I am new to node.js and newer to Sails.js framework. I am currently trying to work with my database, I don't understand all the things with Sails.js but I manage to do what I want step by step. (I am used to some PHP MVC frameworks so it is not too difficult to understand the structure.) Here I am trying to get a row from my database, using 2 `JOIN` clause. I managed to do this using `SQL` and the `Model.query()` function, but I would like to do this in a "cleaner" way. So I have 3 tables in my database: meta, lang and meta_lang. It's quite simple and a picture being better than words, here are some screenshots. meta lang meta_lang What I want to do is to get the row in `meta_table` that match with 'default' meta and 'en' lang (for example). Here are Meta and Lang models (I created them with `sails generate model` command and edited them with what I needed): Meta ``` module.exports = { attributes: { code : { type: 'string' }, metaLangs:{ collection: 'MetaLang', via: 'meta' } } }; ``` Lang ``` module.exports = { attributes: { code : { type: 'string' }, metaLangs:{ collection: 'MetaLang', via: 'lang' } } }; ``` And here is my MetaLang model, with 3 functions I created to test several methods. The first function, `findCurrent`, works perfectly, but as you can see I had to write SQL. That is what I want to avoid if it is possible, I find it more clean (and I would like to use Sails.js tools as often as I can). ``` module.exports = { tableName: 'meta_lang', attributes: { title : { type: 'string' }, description : { type: 'text' }, keywords : { type: 'string' }, meta:{ model:'Meta', columnName: 'meta_id' }, lang:{ model:'Lang', columnName: 'lang_id' } }, findCurrent: function (metaCode, langCode) { var query = 'SELECT ml.* FROM meta_lang ml INNER JOIN meta m ON m.id = ml.meta_id INNER JOIN lang l ON l.id = ml.lang_id WHERE m.code = ? AND l.code = ?'; MetaLang.query(query, [metaCode, langCode], function(err, metaLang) { console.log('findCurrent'); if (err) return console.log(err); console.log(metaLang); // OK this works exactly as I want (I would have prefered a 'findOne' result, only 1 object instead of an array with 1 object in it, but I can do with it.) }); }, findCurrentTest: function (metaCode, langCode) { Meta.findByCode(metaCode).populate('metaLangs').exec(function(err, metaLang) { console.log('findCurrentTest'); if (err) return console.log(err); console.log(metaLang); // I get what I expected (though not what I want): my meta + all metaLangs related to meta with code "default". // What I want is to get ONE metaLang related to meta with code "default" AND lang with code "en". }); }, findCurrentOthertest: function (metaCode, langCode) { MetaLang.find().populate('meta', {where: {code:metaCode}}).populate('lang', {where: {code:langCode}}).exec(function(err, metaLang) { console.log('findCurrentOthertest'); if (err) return console.log(err); console.log(metaLang); // Doesn't work as I wanted: it gets ALL the metaLang rows. }); } }; ``` I also tried to first get my Meta by code, then my Lang by code, and MetaLang using Meta.id and Lang.id . But I would like to avoid 3 queries when I can have only one. What I'm looking for would be something like `MetaLang.find({meta.code:"default", lang.code:"en"})`. Hope you've got all needed details, just comment and ask for more if you don't.