Skip to content

Instantly share code, notes, and snippets.

@tanqhnguyen
Last active March 20, 2016 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanqhnguyen/8a358d7bf0ec73bb91f2 to your computer and use it in GitHub Desktop.
Save tanqhnguyen/8a358d7bf0ec73bb91f2 to your computer and use it in GitHub Desktop.
Product.relations = {
'comments': {
'type': 'hasMany',
'model': 'Comment',
'foreignKey': 'product_id'
},
'tags': {
'type': 'hasManyAndBelongsTo',
'model': 'Tag',
'through': 'ProductTag',
'foreignKey': 'product_id'
},
'store': {
'type': 'belongsTo',
'model': 'Store',
'foreignKey': 'product_id'
}
}
_.chain(Registry.getModel('ProductTag').relations).toPairs().find(function(pair) {
return pair[1].model == meta.modelName;
}).value();
['product', {
'type': 'belongsTo',
'model': 'Product',
'foreignKey': 'product_id'
}]
ProductTag.relations = {
'product': {
'type': 'belongsTo',
'model': 'Product',
'foreignKey': 'product_id'
},
'tag': {
'type': 'belongsTo',
'model': 'Tag',
'foreignKey': 'tag_id'
}
}
Tag.relations = {
'products': {
'type': 'hasManyAndBelongsTo',
'model': 'Product',
'through': 'ProductTag',
'foreignKey': 'tag_id'
}
}
{
include: {
relation: 'comments',
filter: {
include: {
relation: 'votes'
}
}
}
}
for (var i in data) {
var d = data[i];
var join = 'LEFT JOIN';
var select = '', on = '';
switch (d.type) {
case 'hasMany':
case 'hasOne':
select = `(SELECT ${d.target.modelName}.*, ROW_NUMBER() OVER (PARTITION BY ${d.target.modelName}.${d.target.foreignKey}) as rn FROM ${d.target.modelName}) AS ${d.relation}`;
on = `ON ${d.parent ? d.parent : d.modelName}.${d.target.referenceKey} = ${d.relation}.${d.target.foreignKey}`;
break;
case 'belongsTo':
select = `${d.target.modelName} AS ${d.relation}`;
on = `ON ${d.relation}.${d.target.referenceKey} = ${d.parent ? d.parent : d.modelName}.${d.target.foreignKey}`;
break;
case 'hasManyAndBelongsTo':
select = `(SELECT ${d.through.modelName}.*, ROW_NUMBER() OVER (PARTITION BY ${d.through.modelName}.${d.through.foreignKey}) as rn FROM ${d.through.modelName}) AS ${d.through.modelName}`;
on = `ON ${d.through.modelName}.${d.through.foreignKey} = ${d.parent ? d.parent : d.modelName}.${d.through.referenceKey} LEFT JOIN ${d.target.modelName} ON ${d.through.modelName}.${d.target.foreignKey} = ${d.target.modelName}.${d.target.referenceKey}`;
}
console.log([join, select, on].join(' '));
}
[
{
'relation': 'comments',
'modelName': 'Product',
'type': 'hasMany',
'properties': [],
'target': {
'modelName': 'Comment',
'foreignKey': 'product_id',
'referenceKey': 'id',
'properties': [],
'filter': {}
}
},
{
'relation': 'author',
'modelName': 'Comment',
'type': 'belongsTo',
'properties': [],
'parent': 'comments',
'target': {
'modelName': 'User',
'foreignKey': 'user_id',
'referenceKey': 'id',
'properties': [],
'filter': {}
}
},
{
'relation': 'votes',
'modelName': 'Comment',
'type': 'hasMany',
'properties': [],
'parent': 'comments',
'target': {
'modelName': 'Vote',
'foreignKey': 'comment_id',
'referenceKey': 'id',
'properties': [],
'filter': {},
}
},
{
'relation': 'tags',
'modelName': 'Product',
'type': 'hasManyAndBelongsTo',
'properties': [],
'through': {
'modelName': 'ProductTag',
'foreignKey': 'product_id',
'referenceKey': 'id'
},
'target': {
'modelName': 'Tag',
'foreignKey': 'tag_id',
'referenceKey': 'id',
'properties': [],
'filter': {}
}
},
{
'relation': 'user',
'modelName': 'Product',
'type': 'belongsTo',
'properties': [],
'target': {
'modelName': 'User',
'foreignKey': 'user_id',
'referenceKey': 'id',
'properties': [],
'filter': {}
}
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment