Skip to content

Commit

Permalink
feat(belongsToMany): add countDistinct method (#224)
Browse files Browse the repository at this point in the history
* feat(belongsToMany): add countDistinct method

#221

* test(belongsToMany): test for countDistinct

* refactor(belongsToMany): remove duplicate code in aggregate methods
  • Loading branch information
webdevian authored and thetutlage committed Nov 7, 2017
1 parent 2c6c56c commit 26cca0e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/Lucid/Relations/BelongsToMany.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,17 @@ class BelongsToMany extends BaseRelation {
context.on(`${this.$primaryTable}.${this.primaryKey}`, '=', `${this.$pivotTable}.${this.foreignKey}`)
}

/**
* Prepare query for an aggregate function
*
* @method _prepareAggregate
*/
_prepareAggregate () {
this._validateRead()
this._makeJoinQuery()
this.wherePivot(this.foreignKey, this.$primaryKeyValue)
}

/**
* Returns count of rows.
*
Expand All @@ -668,12 +679,24 @@ class BelongsToMany extends BaseRelation {
* @return {Array}
*/
count (expression) {
this._validateRead()
this._makeJoinQuery()
this.wherePivot(this.foreignKey, this.$primaryKeyValue)
this._prepareAggregate()
return this.relatedQuery.count(expression)
}

/**
* Returns count of rows with distinct expression.
*
* @method count
*
* @param {String} expression
*
* @return {Array}
*/
countDistinct (expression) {
this._prepareAggregate()
return this.relatedQuery.countDistinct(expression)
}

/**
* Returns avg for a given column
*
Expand All @@ -684,9 +707,7 @@ class BelongsToMany extends BaseRelation {
* @return {Array}
*/
avg (column) {
this._validateRead()
this._makeJoinQuery()
this.wherePivot(this.foreignKey, this.$primaryKeyValue)
this._prepareAggregate()
return this.relatedQuery.avg(column)
}

Expand All @@ -700,9 +721,7 @@ class BelongsToMany extends BaseRelation {
* @return {Array}
*/
min (column) {
this._validateRead()
this._makeJoinQuery()
this.wherePivot(this.foreignKey, this.$primaryKeyValue)
this._prepareAggregate()
return this.relatedQuery.min(column)
}

Expand All @@ -716,9 +735,7 @@ class BelongsToMany extends BaseRelation {
* @return {Array}
*/
max (column) {
this._validateRead()
this._makeJoinQuery()
this.wherePivot(this.foreignKey, this.$primaryKeyValue)
this._prepareAggregate()
return this.relatedQuery.max(column)
}

Expand Down
25 changes: 25 additions & 0 deletions test/unit/lucid-belongs-to-many.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1738,4 +1738,29 @@ test.group('Relations | Belongs To Many', (group) => {
const postsCount = await user.posts().count('* as total')
assert.deepEqual(postsCount, [{ 'total': 2 }])
})

test('count distinct on given field', async (assert) => {
class Post extends Model {
}

class User extends Model {
posts () {
return this.belongsToMany(Post)
}
}

User._bootIfNotBooted()
Post._bootIfNotBooted()

await ioc.use('Database').table('users').insert({ id: 20, username: 'virk' })
await ioc.use('Database').table('posts').insert([{ id: 18, title: 'Adonis 101' }, { id: 19, title: 'Lucid 101' }])
await ioc.use('Database').table('post_user').insert([
{ post_id: 18, user_id: 20 },
{ post_id: 19, user_id: 20 }
])

const user = await User.find(20)
const postsCount = await user.posts().countDistinct('post_user.user_id as total')
assert.deepEqual(postsCount, [{ 'total': 1 }])
})
})

0 comments on commit 26cca0e

Please sign in to comment.