Created
February 24, 2011 16:30
-
-
Save madmanlear/842389 to your computer and use it in GitHub Desktop.
Add HABTM support to joli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Joli is a lightweight js ORM built for Titanium Apps: | |
//https://github.com/xavierlacot/joli.js | |
//Add this to joli.query.prototype | |
/* | |
Create HABTM query | |
Assume standard relationship table naming standards | |
Attempt to create local and foreign key from relationship table if none is declared | |
*/ | |
fromThrough: function(table, relation_table, local_key, foreign_key, local_table, foreign_table) { | |
this.data.from = table; | |
var tables = relation_table.split("_"); | |
if(!local_key) { | |
local_key = tables[0].substr(0, tables[0].length - 1)+'_id'; | |
} | |
if(!foreign_key) { | |
foreign_key = tables[1].substr(0, tables[1].length - 1)+'_id'; | |
} | |
if(!local_table) { | |
local_table = tables[0]; | |
} | |
if(!foreign_table) { | |
foreign_table = tables[1]; | |
} | |
this.data.join.push([relation_table, relation_table+'.'+local_key, local_table+'.id']); | |
this.data.join.push([foreign_table, foreign_table+'.id', relation_table+'.'+foreign_key]); | |
return this; | |
} | |
/* | |
q = new joli.query() | |
.select('articles.*') | |
.fromThrough('tags', 'tags_articles') | |
.where('tags.id = ?', tag_id); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment