Skip to content

Commit

Permalink
Added belongsTo relation for Lucid models
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 27, 2015
1 parent 0bc5db3 commit feaba58
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 15 deletions.
76 changes: 61 additions & 15 deletions src/Orm/Proxy/Model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,36 @@ class Model {
this._database = database
}

/**
* @function query
* @description query chain that can be executed on relationship
* definations
* @param {Function} callback
* @return {Object}
*/
query (callback) {
this.constructor._activeRelation.query = callback
return this
}


/**
* @function withPivot
* @description method to define columns to be selected on pivot
* table with many to many relations
* @return {Object}
*/
withPivot () {
this.constructor._activeRelation.withPivot = arguments
return this
}

/**
* returns defination for hasOne relation
* @param {String} binding [description]
* @param {String} primaryId [description]
* @param {String} relationPrimaryId [description]
* @return {Object} [description]
* @param {String} binding
* @param {String} primaryId
* @param {String} relationPrimaryId
* @return {Object}
*/
hasOne(binding, targetPrimaryKey, relationPrimaryKey){

Expand Down Expand Up @@ -254,27 +278,49 @@ class Model {
}


query (callback) {

this.constructor._activeRelation.query = callback
return this
/**
* @function belongsTo
* @description belongsTo defines one to one relation from relation
* model to host model.
* @method belongsTo
* @param {String} binding
* @param {String} targetPrimaryKey
* @param {String} relationPrimaryKey
* @return {Object}
*/
belongsTo(binding, targetPrimaryKey, relationPrimaryKey) {

}
/**
* grabs model from Ioc container
* @type {Object}
*/
const model = Ioc.use(binding)

/**
* relationship primary key to be used on relation model
* @type {String}
*/
relationPrimaryKey = relationPrimaryKey || model.primaryKey

withPivot () {
/**
* primary id for the target model, the one
* who has defined relationship
* @type {String}
*/
targetPrimaryKey = targetPrimaryKey || staticHelpers.getRelationKey(model,true)

this.constructor._activeRelation.withPivot = arguments
this.constructor._activeRelation = {model, targetPrimaryKey, relationPrimaryKey, relation:'belongsTo'}
return this

}


/**
* returns defination for hasMany relation
* @param {String} binding [description]
* @param {String} primaryId [description]
* @param {String} relationPrimaryId [description]
* @return {Object} [description]
* @param {String} binding
* @param {String} primaryId
* @param {String} relationPrimaryId
* @return {Object}
*/
hasMany(binding, targetPrimaryKey, relationPrimaryKey){

Expand Down
12 changes: 12 additions & 0 deletions src/Orm/Proxy/Static/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,18 @@ helpers.hasMany = function (values, model) {
return helpers.hasOne(values, model, 'noLimit')
}

/**
* belongsTo method for model relation , it is similar to hasOne
* but with opposite keys
* @method belongsTo
* @param {Object} values
* @param {Object} model
* @return {Object}
*/
helpers.belongsTo = function (values, model) {
return helpers.hasOne(values, model)
}

/**
* @function belongsToMany
* @description returns transformed values for belongs to
Expand Down
65 changes: 65 additions & 0 deletions test/unit/model-relations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,71 @@ describe('Model Relations', function () {
.catch(done)
})


it('should be able to define belongsTo relationship using belongsTo method', function(done) {

/**
* Phone model binded to Ioc container
* under App/Model/Phone namespace
*/
class Phone extends Model{
user(){
return this.belongsTo('App/Model/User')
}
}
Phone.database = db; Phone = Phone.extend()


class User extends Model{
}
User.database = db; User = User.extend()
Ioc.bind('App/Model/User', function() {
return User
})

Phone
.with(['user'])
.then(function(phone){
expect(phone.first().user).to.be.an('object')
expect(phone.first().user).to.have.property('id')
done()
})
.catch(done)
})


it('should be able to fetch belongsTo relationship values for a single result', function(done) {

/**
* Phone model binded to Ioc container
* under App/Model/Phone namespace
*/
class Phone extends Model{
user(){
return this.belongsTo('App/Model/User')
}
}
Phone.database = db; Phone = Phone.extend()


class User extends Model{
}
User.database = db; User = User.extend()
Ioc.bind('App/Model/User', function() {
return User
})

Phone
.first()
.with(['user'])
.then(function(phone){
expect(phone.toJSON().user).to.be.an('object')
expect(phone.toJSON().user).to.have.property('id')
done()
})
.catch(done)
})

it('should be able to define hasMany relationship', function(done) {

/**
Expand Down

0 comments on commit feaba58

Please sign in to comment.