Skip to content

Commit

Permalink
Merge branch 'release-3.0.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 2, 2016
2 parents 0301662 + 738dd1c commit 1758bfa
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 34 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,14 @@
<a name="3.0.10"></a>
## [3.0.10](https://github.com/adonisjs/adonis-lucid/compare/v3.0.9...v3.0.10) (2016-11-02)


### Bug Fixes

* **database:** paginate count query to ignore order by ([ac16baa](https://github.com/adonisjs/adonis-lucid/commit/ac16baa)), closes [#64](https://github.com/adonisjs/adonis-lucid/issues/64)
* **lucid:relations:** implement delete method to delete relations ([0067bca](https://github.com/adonisjs/adonis-lucid/commit/0067bca)), closes [#63](https://github.com/adonisjs/adonis-lucid/issues/63)



<a name="3.0.9"></a>
## [3.0.9](https://github.com/adonisjs/adonis-lucid/compare/v3.0.8...v3.0.9) (2016-10-19)

Expand Down
12 changes: 12 additions & 0 deletions bin/coverage.js
@@ -0,0 +1,12 @@
'use strict'

const semver = require('semver')
const shell = require('shelljs')
const nodeJsVersion = process.version
let proxiesFlag = ''

if (semver.lt(nodeJsVersion, '6.0.0')) {
proxiesFlag = '--harmony_proxies'
}

shell.exec(`DB=${process.env.DB} node ${proxiesFlag} ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha test/unit test/acceptance`)
12 changes: 12 additions & 0 deletions bin/test.js
@@ -0,0 +1,12 @@
'use strict'

const semver = require('semver')
const shell = require('shelljs')
const nodeJsVersion = process.version
let proxiesFlag = ''

if (semver.lt(nodeJsVersion, '6.0.0')) {
proxiesFlag = '--harmony_proxies'
}

shell.exec(`DB=${process.env.DB} node ${proxiesFlag} ./node_modules/.bin/istanbul cover _mocha --colors --report lcovonly -- -R spec test/unit test/acceptance && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage`)
21 changes: 17 additions & 4 deletions package.json
Expand Up @@ -5,12 +5,24 @@
"directories": {
"test": "test"
},
"version": "3.0.9",
"version": "3.0.10",
"scripts": {
"lint": "standard src/**/*.js src/**/**/*.js src/**/**/**/*.js lib/*.js test/**/*.js providers/*.js",
"lint": "standard",
"test:all": "DB=sqlite3 npm run test && DB=mysql npm run test && DB=pg npm run test",
"coverage": "node --harmony_proxies ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha test/unit test/acceptance",
"test": "node --harmony_proxies ./node_modules/.bin/istanbul cover _mocha --report lcovonly -- -R spec test/unit test/acceptance && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage"
"coverage": "npm run lint && node ./bin/coverage",
"test": "npm run lint && node ./bin/test"
},
"standard": {
"global": [
"use",
"describe",
"it",
"after",
"before",
"context",
"make",
"beforeEach"
]
},
"author": "adonisjs",
"license": "MIT",
Expand All @@ -29,6 +41,7 @@
"mysql": "^2.10.2",
"pg": "^4.5.1",
"semantic-release": "^4.3.5",
"semver": "^5.3.0",
"shelljs": "^0.7.4",
"sqlite3": "^3.1.1",
"standard": "^8.0.0"
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Reset.js
Expand Up @@ -32,7 +32,7 @@ class Reset extends Command {
* @public
*/
get description () {
return 'Reset migrations to lastest batch'
return 'Reset migrations to latest batch'
}

/**
Expand Down
18 changes: 16 additions & 2 deletions src/Database/index.js
Expand Up @@ -66,6 +66,15 @@ const _emitSql = function (builder) {
})
}

/**
* Following attributes should be removed from the
* paginate count query since things like orderBy
* is not required when fetching the count.
*
* @type {Array}
*/
const excludeAttrFromCount = ['order']

/**
* Database provider to build sql queries
* @module Database
Expand Down Expand Up @@ -315,6 +324,12 @@ Database.paginate = function * (page, perPage, countByQuery) {
* query for getting results
*/
countByQuery = countByQuery || this.clone().count('* as total')

/**
* Filter unnecessary statements from the cloned query
*/
countByQuery._statements = _.filter(countByQuery._statements, (statement) => excludeAttrFromCount.indexOf(statement.grouping) < 0)

const count = yield countByQuery
if (!count[0] || parseInt(count[0].total, 10) === 0) {
return util.makePaginateMeta(0, parsedPage, parsedPerPage)
Expand Down Expand Up @@ -368,7 +383,7 @@ Database.chunk = function * (limit, cb, page) {
*/
Database.table = function (tableName) {
const prefix = this._instancePrefix || this.client.config.prefix
const prefixedTableName = (prefix && !this._skipPrefix) ? `${prefix}${tableName}`: tableName
const prefixedTableName = (prefix && !this._skipPrefix) ? `${prefix}${tableName}` : tableName
this._originalTable(prefixedTableName)
return this
}
Expand All @@ -395,7 +410,6 @@ Database.withPrefix = function (prefix) {
return this
}


/**
* these methods are not proxied and instead actual implementations
* are returned
Expand Down
9 changes: 9 additions & 0 deletions src/Lucid/Relations/BelongsToMany.js
Expand Up @@ -405,6 +405,15 @@ class BelongsToMany extends Relation {
return relatedInstance
}

/**
* Throws an exception since deleting the related model
* should be done via relation and detach should be
* used instead.
*/
* delete () {
throw new CE.ModelRelationException('delete is not supported by BelongsToMany, use detach instead')
}

}

module.exports = BelongsToMany
9 changes: 9 additions & 0 deletions src/Lucid/Relations/HasManyThrough.js
Expand Up @@ -227,6 +227,15 @@ class HasManyThrough extends Relation {
throw CE.ModelRelationException.unSupportedMethod('saveMany', this.constructor.name)
}

/**
* Throws an exception since deleting the related model
* should be done via relation and detach should be
* used instead.
*/
* delete () {
throw CE.ModelRelationException.unSupportedMethod('delete', this.constructor.name)
}

}

module.exports = HasManyThrough
11 changes: 11 additions & 0 deletions src/Lucid/Relations/Relation.js
Expand Up @@ -87,6 +87,17 @@ class Relation {
return this.relatedQuery.first()
}

/**
* Removes the related record for the given relationship
*
* @return {Object}
*/
delete () {
this._validateRead()
this._decorateRead()
return this.relatedQuery.delete()
}

/**
* calls the fetch method on the related query builder
*
Expand Down
9 changes: 9 additions & 0 deletions test/unit/database.spec.js
Expand Up @@ -252,6 +252,15 @@ describe('Database provider', function () {
expect(paginatedUsers.lastPage).to.equal(1)
})

it('should be able paginate results using order by on the original query', function * () {
const paginatedUsers = yield Database.table('users').orderBy('id', 'desc').paginate(1)
expect(paginatedUsers).to.have.property('total')
expect(paginatedUsers).to.have.property('lastPage')
expect(paginatedUsers).to.have.property('perPage')
expect(paginatedUsers).to.have.property('data')
expect(paginatedUsers.total).to.equal(paginatedUsers.data.length)
})

it('should be able to get results in chunks', function * () {
let callbackCalledForTimes = 0
const allUsers = yield Database.table('users')
Expand Down
4 changes: 2 additions & 2 deletions test/unit/factory.spec.js
Expand Up @@ -289,7 +289,7 @@ describe('Factory', function () {
yield modelFixtures.truncate(Database)
})

it('should be able to truncat the database table using the reset method', function * () {
it('should be able to truncate the database table using the reset method', function * () {
Factory.blueprint('users', function (fake) {
return {
username: fake.username(),
Expand All @@ -298,7 +298,7 @@ describe('Factory', function () {
})
yield Factory.get('users').create(10)
yield Factory.get('users').reset()
const ids = yield Database.table('users').pluck('ids')
const ids = yield Database.table('users').pluck('id')
expect(ids).to.be.an('array')
expect(ids.length).to.equal(0)
})
Expand Down
4 changes: 2 additions & 2 deletions test/unit/fixtures/files.js
Expand Up @@ -11,10 +11,10 @@ const path = require('path')

module.exports = {
cleanStorage: function * () {
return yield fs.emptyDir(path.join(__dirname,'../storage'))
return yield fs.emptyDir(path.join(__dirname, '../storage'))
},
createDir: function * () {
return yield fs.ensureDir(path.join(__dirname,'../storage'))
return yield fs.ensureDir(path.join(__dirname, '../storage'))
}
}

1 change: 0 additions & 1 deletion test/unit/fixtures/relations.js
Expand Up @@ -6,7 +6,6 @@
* MIT Licensed
*/

const path = require('path')
const bluebird = require('bluebird')
const files = require('./files')

Expand Down
22 changes: 11 additions & 11 deletions test/unit/helpers/mysqlConnections.js
Expand Up @@ -7,30 +7,30 @@
*/

module.exports = {
default : {
default: {
client: 'mysql',
connection: {
user : 'root',
password : '',
database : 'default'
user: 'root',
password: '',
database: 'default'
}
},

alternateConnection: {
client: 'mysql',
connection: {
user : 'root',
password : '',
database : 'alternate'
user: 'root',
password: '',
database: 'alternate'
}
},

defaultPrefix : {
defaultPrefix: {
client: 'mysql',
connection: {
user : 'root',
password : '',
database : 'default'
user: 'root',
password: '',
database: 'default'
},
prefix: 'ad_'
}
Expand Down
18 changes: 9 additions & 9 deletions test/unit/helpers/postgresConnection.js
Expand Up @@ -7,30 +7,30 @@
*/

module.exports = {
default : {
default: {
client: 'pg',
connection: {
user: '',
password : '',
database : 'default'
password: '',
database: 'default'
}
},

alternateConnection: {
client: 'pg',
connection: {
user : '',
password : '',
database : 'alternate'
user: '',
password: '',
database: 'alternate'
}
},

defaultPrefix : {
defaultPrefix: {
client: 'pg',
connection: {
user: '',
password : '',
database : 'default'
password: '',
database: 'default'
},
prefix: 'ad_'
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/helpers/sqliteConnections.js
Expand Up @@ -8,7 +8,7 @@
const path = require('path')

module.exports = {
default : {
default: {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, '../storage/test.sqlite3')
Expand Down

0 comments on commit 1758bfa

Please sign in to comment.