Skip to content

Commit

Permalink
feat(lucid): add aggregates on relationship
Browse files Browse the repository at this point in the history
aggregates support have been added to the relationships

Closes #48
  • Loading branch information
thetutlage committed Jul 16, 2017
1 parent e60d9c2 commit d614c96
Show file tree
Hide file tree
Showing 4 changed files with 527 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/Lucid/Relations/proxyHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@

const proxyHandler = exports = module.exports = {}
const _ = require('lodash')
const queryExecMethods = [
'increment',
'decrement',
'avg',
'min',
'max',
'count',
'truncate',
'ids',
'pair',
'pluckFirst',
'pluckId',
'pick',
'pickInverse'
]

proxyHandler.get = function (target, name) {
/**
Expand All @@ -27,5 +42,16 @@ proxyHandler.get = function (target, name) {
return this
}
}

/**
* Here we called methods on the relationships
* to decorate the query chain by chain required
* methods.
*/
if (queryExecMethods.indexOf(name) > -1) {
target._validateRead()
target._decorateRead()
}

return target.relatedQuery[name]
}
4 changes: 4 additions & 0 deletions test/unit/fixtures/relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
knex.schema.createTable('accounts', function (table) {
table.increments()
table.integer('supplier_id')
table.integer('points').defaultTo(0)
table.string('name')
table.timestamps()
table.timestamp('deleted_at').nullable()
Expand Down Expand Up @@ -73,6 +74,7 @@ module.exports = {
table.increments()
table.integer('post_id')
table.string('body')
table.integer('likes').defaultTo(0)
table.timestamps()
table.timestamp('deleted_at').nullable()
}),
Expand All @@ -85,6 +87,7 @@ module.exports = {
knex.schema.createTable('courses', function (table) {
table.increments()
table.string('title')
table.integer('weightage')
table.timestamps()
table.timestamp('deleted_at').nullable()
}),
Expand Down Expand Up @@ -113,6 +116,7 @@ module.exports = {
table.integer('author_id')
table.string('title')
table.string('body')
table.integer('amount')
table.timestamps()
table.timestamp('deleted_at').nullable()
}),
Expand Down
5 changes: 5 additions & 0 deletions test/unit/helpers/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ module.exports = {
},

formatBindings: function (bindings) {
if (process.env.DB === 'pg') {
return bindings.map((binding) => {
return String(binding)
})
}
return bindings
}
}

0 comments on commit d614c96

Please sign in to comment.