jrf0110 / dirac

A Node Postgres DB layer built with MoSQL
http://dirac.j0.hn
6 stars 2 forks source link

Mixin relationship helper #53

Closed jrf0110 closed 9 years ago

jrf0110 commented 9 years ago

With dirac, you can pretty elegantly handle one-to-one, one-to-many, and many-to-many relationships

user_invoices (id, user_id, ...)
user_invoice_orders (id, user_invoice_id, order_id, ...)
orders (id, ...)

consider that sort of junction table for many-to-many

db.user_invoices.find( {}, {
 many: [ { table: 'user_invoice_orders', alias: 'orders'
         , one: [ { table: 'orders', alias: 'order'} ]
         }
       ]
})

You could do that and each junction record will now contain a full order object So, many on the junction table, and one on orders But that's kinda weird because it's a little redundant - we really just want to do a standard left join on those

I could just add that join manually actually, but that wouldn't take advantage of relationships middleware cached dependency graph

So, I guess I'll add a new relationships type - mixin

db.user_invoices.find( {}, {
 many: [ { table: 'user_invoice_orders'
         , alias: 'orders'
         , mixin: [ { table: 'orders' } ]
         }
       ]
})
jrf0110 commented 9 years ago

This serves my use-case for now, going to merge.