Skip to content

Commit feaba58

Browse files
committed
Added belongsTo relation for Lucid models
1 parent 0bc5db3 commit feaba58

File tree

3 files changed

+138
-15
lines changed

3 files changed

+138
-15
lines changed

src/Orm/Proxy/Model/index.js

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,36 @@ class Model {
219219
this._database = database
220220
}
221221

222+
/**
223+
* @function query
224+
* @description query chain that can be executed on relationship
225+
* definations
226+
* @param {Function} callback
227+
* @return {Object}
228+
*/
229+
query (callback) {
230+
this.constructor._activeRelation.query = callback
231+
return this
232+
}
233+
234+
235+
/**
236+
* @function withPivot
237+
* @description method to define columns to be selected on pivot
238+
* table with many to many relations
239+
* @return {Object}
240+
*/
241+
withPivot () {
242+
this.constructor._activeRelation.withPivot = arguments
243+
return this
244+
}
245+
222246
/**
223247
* returns defination for hasOne relation
224-
* @param {String} binding [description]
225-
* @param {String} primaryId [description]
226-
* @param {String} relationPrimaryId [description]
227-
* @return {Object} [description]
248+
* @param {String} binding
249+
* @param {String} primaryId
250+
* @param {String} relationPrimaryId
251+
* @return {Object}
228252
*/
229253
hasOne(binding, targetPrimaryKey, relationPrimaryKey){
230254

@@ -254,27 +278,49 @@ class Model {
254278
}
255279

256280

257-
query (callback) {
258-
259-
this.constructor._activeRelation.query = callback
260-
return this
281+
/**
282+
* @function belongsTo
283+
* @description belongsTo defines one to one relation from relation
284+
* model to host model.
285+
* @method belongsTo
286+
* @param {String} binding
287+
* @param {String} targetPrimaryKey
288+
* @param {String} relationPrimaryKey
289+
* @return {Object}
290+
*/
291+
belongsTo(binding, targetPrimaryKey, relationPrimaryKey) {
261292

262-
}
293+
/**
294+
* grabs model from Ioc container
295+
* @type {Object}
296+
*/
297+
const model = Ioc.use(binding)
263298

299+
/**
300+
* relationship primary key to be used on relation model
301+
* @type {String}
302+
*/
303+
relationPrimaryKey = relationPrimaryKey || model.primaryKey
264304

265-
withPivot () {
305+
/**
306+
* primary id for the target model, the one
307+
* who has defined relationship
308+
* @type {String}
309+
*/
310+
targetPrimaryKey = targetPrimaryKey || staticHelpers.getRelationKey(model,true)
266311

267-
this.constructor._activeRelation.withPivot = arguments
312+
this.constructor._activeRelation = {model, targetPrimaryKey, relationPrimaryKey, relation:'belongsTo'}
268313
return this
269314

270315
}
271316

317+
272318
/**
273319
* returns defination for hasMany relation
274-
* @param {String} binding [description]
275-
* @param {String} primaryId [description]
276-
* @param {String} relationPrimaryId [description]
277-
* @return {Object} [description]
320+
* @param {String} binding
321+
* @param {String} primaryId
322+
* @param {String} relationPrimaryId
323+
* @return {Object}
278324
*/
279325
hasMany(binding, targetPrimaryKey, relationPrimaryKey){
280326

src/Orm/Proxy/Static/helpers.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,18 @@ helpers.hasMany = function (values, model) {
378378
return helpers.hasOne(values, model, 'noLimit')
379379
}
380380

381+
/**
382+
* belongsTo method for model relation , it is similar to hasOne
383+
* but with opposite keys
384+
* @method belongsTo
385+
* @param {Object} values
386+
* @param {Object} model
387+
* @return {Object}
388+
*/
389+
helpers.belongsTo = function (values, model) {
390+
return helpers.hasOne(values, model)
391+
}
392+
381393
/**
382394
* @function belongsToMany
383395
* @description returns transformed values for belongs to

test/unit/model-relations.spec.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,71 @@ describe('Model Relations', function () {
147147
.catch(done)
148148
})
149149

150+
151+
it('should be able to define belongsTo relationship using belongsTo method', function(done) {
152+
153+
/**
154+
* Phone model binded to Ioc container
155+
* under App/Model/Phone namespace
156+
*/
157+
class Phone extends Model{
158+
user(){
159+
return this.belongsTo('App/Model/User')
160+
}
161+
}
162+
Phone.database = db; Phone = Phone.extend()
163+
164+
165+
class User extends Model{
166+
}
167+
User.database = db; User = User.extend()
168+
Ioc.bind('App/Model/User', function() {
169+
return User
170+
})
171+
172+
Phone
173+
.with(['user'])
174+
.then(function(phone){
175+
expect(phone.first().user).to.be.an('object')
176+
expect(phone.first().user).to.have.property('id')
177+
done()
178+
})
179+
.catch(done)
180+
})
181+
182+
183+
it('should be able to fetch belongsTo relationship values for a single result', function(done) {
184+
185+
/**
186+
* Phone model binded to Ioc container
187+
* under App/Model/Phone namespace
188+
*/
189+
class Phone extends Model{
190+
user(){
191+
return this.belongsTo('App/Model/User')
192+
}
193+
}
194+
Phone.database = db; Phone = Phone.extend()
195+
196+
197+
class User extends Model{
198+
}
199+
User.database = db; User = User.extend()
200+
Ioc.bind('App/Model/User', function() {
201+
return User
202+
})
203+
204+
Phone
205+
.first()
206+
.with(['user'])
207+
.then(function(phone){
208+
expect(phone.toJSON().user).to.be.an('object')
209+
expect(phone.toJSON().user).to.have.property('id')
210+
done()
211+
})
212+
.catch(done)
213+
})
214+
150215
it('should be able to define hasMany relationship', function(done) {
151216

152217
/**

0 commit comments

Comments
 (0)