Skip to content

Commit

Permalink
Merge branch 'release/4.0.23'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 13, 2017
2 parents bb64d8c + 3fce5e9 commit 7fc636d
Show file tree
Hide file tree
Showing 14 changed files with 407 additions and 9 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,18 @@
<a name="4.0.23"></a>
## [4.0.23](https://github.com/adonisjs/adonis-lucid/compare/v4.0.22...v4.0.23) (2017-11-13)


### Bug Fixes

* **relations:** fix withCount query for self joins ([8b51561](https://github.com/adonisjs/adonis-lucid/commit/8b51561)), closes [#231](https://github.com/adonisjs/adonis-lucid/issues/231)


### Features

* **serializer:** add nth method to pull row for specific index ([e578eeb](https://github.com/adonisjs/adonis-lucid/commit/e578eeb))



<a name="4.0.22"></a>
## [4.0.22](https://github.com/adonisjs/adonis-lucid/compare/v4.0.21...v4.0.22) (2017-11-08)

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@adonisjs/lucid",
"version": "4.0.22",
"version": "4.0.23",
"description": "SQL ORM built on top of Active Record pattern",
"main": "index.js",
"scripts": {
Expand Down
15 changes: 14 additions & 1 deletion src/Lucid/QueryBuilder/index.js
Expand Up @@ -95,6 +95,17 @@ class QueryBuilder {
*/
this._hiddenFields = this.Model.hidden

/**
* Storing the counter for how many withCount queries
* have been made by this query builder chain.
*
* This is required so that self joins have generate
* unique table names
*
* @type {Number}
*/
this._withCountCounter = -1

return new Proxy(this, proxyHandler)
}

Expand Down Expand Up @@ -709,6 +720,8 @@ class QueryBuilder {
throw CE.RuntimeException.cannotNestRelation(_.first(_.keys(nested)), name, 'withCount')
}

this._withCountCounter++

/**
* Since user can set the `count as` statement, we need
* to parse them properly.
Expand Down Expand Up @@ -739,7 +752,7 @@ class QueryBuilder {
if (!_.find(this.query._statements, (statement) => statement.grouping === 'columns')) {
columns.push('*')
}
columns.push(relationInstance.relatedWhere(true).as(asStatement))
columns.push(relationInstance.relatedWhere(true, this._withCountCounter).as(asStatement))

/**
* Saving reference of count inside _sideloaded
Expand Down
8 changes: 8 additions & 0 deletions src/Lucid/Relations/BaseRelation.js
Expand Up @@ -69,6 +69,14 @@ class BaseRelation {
query.whereIn(fk, values)
}

/**
* This is set by `relatedWhere` method in case of a self
* join
*
* @type {Number|Null}
*/
this.relatedTableAlias = null

return new Proxy(this, {
get: proxyGet('relatedQuery')
})
Expand Down
20 changes: 18 additions & 2 deletions src/Lucid/Relations/BelongsTo.js
Expand Up @@ -102,14 +102,30 @@ class BelongsTo extends BaseRelation {
* @method relatedWhere
*
* @param {Boolean} count
* @param {Integer} counter
*
* @return {Object}
*/
relatedWhere (count) {
this.relatedQuery.whereRaw(`${this.$primaryTable}.${this.primaryKey} = ${this.$foreignTable}.${this.foreignKey}`)
relatedWhere (count, counter) {
/**
* When we are making self joins, we should alias the current
* table with the counter, which is sent by the consumer of
* this method.
*
* Also the counter should be incremented by the consumer itself.
*/
if (this.$primaryTable === this.$foreignTable) {
this.relatedTableAlias = `sj_${counter}`
this.relatedQuery.table(`${this.$foreignTable} AS ${this.relatedTableAlias}`)
}

const tableAlias = this.relatedTableAlias || this.$foreignTable
this.relatedQuery.whereRaw(`${this.$primaryTable}.${this.primaryKey} = ${tableAlias}.${this.foreignKey}`)

if (count) {
this.relatedQuery.count('*')
}

return this.relatedQuery.query
}

Expand Down
18 changes: 16 additions & 2 deletions src/Lucid/Relations/BelongsToMany.js
Expand Up @@ -199,12 +199,13 @@ class BelongsToMany extends BaseRelation {
*/
_makeJoinQuery () {
const self = this
const foreignTable = this.relatedTableAlias || this.$foreignTable

/**
* Inner join to limit the rows
*/
this.relatedQuery.innerJoin(this.$pivotTable, function () {
this.on(`${self.$foreignTable}.${self.relatedPrimaryKey}`, `${self.$pivotTable}.${self.relatedForeignKey}`)
this.on(`${foreignTable}.${self.relatedPrimaryKey}`, `${self.$pivotTable}.${self.relatedForeignKey}`)
})
}

Expand Down Expand Up @@ -628,10 +629,23 @@ class BelongsToMany extends BaseRelation {
* @method relatedWhere
*
* @param {Boolean} count
* @param {Integer} counter
*
* @return {Object}
*/
relatedWhere (count) {
relatedWhere (count, counter) {
/**
* When we are making self joins, we should alias the current
* table with the counter, which is sent by the consumer of
* this method.
*
* Also the counter should be incremented by the consumer itself.
*/
if (this.$primaryTable === this.$foreignTable) {
this.relatedTableAlias = `sj_${counter}`
this.relatedQuery.table(`${this.$foreignTable} as ${this.relatedTableAlias}`)
}

this._makeJoinQuery()
this.relatedQuery.whereRaw(`${this.$primaryTable}.${this.primaryKey} = ${this.$pivotTable}.${this.foreignKey}`)

Expand Down
18 changes: 16 additions & 2 deletions src/Lucid/Relations/HasMany.js
Expand Up @@ -105,11 +105,25 @@ class HasMany extends BaseRelation {
* @method relatedWhere
*
* @param {Boolean} count
* @param {Number} counter
*
* @return {Object}
*/
relatedWhere (count) {
this.relatedQuery.whereRaw(`${this.$primaryTable}.${this.primaryKey} = ${this.$foreignTable}.${this.foreignKey}`)
relatedWhere (count, counter) {
/**
* When we are making self joins, we should alias the current
* table with the counter, which is sent by the consumer of
* this method.
*
* Also the counter should be incremented by the consumer itself.
*/
if (this.$primaryTable === this.$foreignTable) {
this.relatedTableAlias = `sj_${counter}`
this.relatedQuery.table(`${this.$foreignTable} AS ${this.relatedTableAlias}`)
}

const tableAlias = this.relatedTableAlias || this.$foreignTable
this.relatedQuery.whereRaw(`${this.$primaryTable}.${this.primaryKey} = ${tableAlias}.${this.foreignKey}`)
if (count) {
this.relatedQuery.count('*')
}
Expand Down
13 changes: 13 additions & 0 deletions src/Lucid/Serializers/Vanilla.js
Expand Up @@ -131,6 +131,19 @@ class VanillaSerializer {
return _.first(this.rows)
}

/**
* Returns the row for the given index
*
* @method nth
*
* @param {Number} index
*
* @return {Model|Null}
*/
nth (index) {
return _.nth(this.rows, index) || null
}

/**
* Get last model instance
*
Expand Down
10 changes: 10 additions & 0 deletions test/unit/helpers/index.js
Expand Up @@ -71,6 +71,8 @@ module.exports = {
table.increments()
table.integer('vid')
table.integer('country_id')
table.integer('manager_id')
table.integer('lead_id')
table.string('username')
table.string('email')
table.timestamps()
Expand Down Expand Up @@ -163,6 +165,14 @@ module.exports = {
table.integer('section_id')
table.timestamps()
table.timestamp('deleted_at').nullable()
}),
db.schema.createTable('followers', function (table) {
table.increments('id')
table.integer('user_id')
table.integer('follower_id')
table.boolean('has_blocked')
table.timestamps()
table.timestamp('deleted_at').nullable()
})
])
},
Expand Down

0 comments on commit 7fc636d

Please sign in to comment.