This repository has been archived by the owner on Aug 5, 2021. It is now read-only.
Custom relations
It's now possible to create custom relations
e.g.
// models/User.js
this.has('has_posts_written', {
query: function(store, parentRecords){
const ids = parentRecords.map(r => r.id)
const Post = store.Model('Post')
// query all posts by user_id, group by user_id and count(distinct(id))
return Post.totalCount().group('user_id').where({user_id: ids})
},
convert: function(parent, records){
if(!records) return false
// records => [{user_id: 1, count: 4}, {user_id: 2, count: 1}]
// == the result of the above query!
const result = records.find(r => r.user_id === parent.id)
if(!result) return false
return result.count > 0
}
})
A custom relation could return anything. In the example above it'll return a boolean value.
Works with include()
like any other relation:
User.include('has_posts_written')