Skip to content

Commit

Permalink
Merge b043d23 into cb40b80
Browse files Browse the repository at this point in the history
  • Loading branch information
webdevian committed Apr 6, 2017
2 parents cb40b80 + b043d23 commit 1f7b293
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Lucid/QueryBuilder/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,13 @@ methods.ids = function (target) {
* @return {Function}
*/
methods.pair = function (target) {
// console.log(target)
return function (lhs, rhs) {
return target.modelQueryBuilder.select(lhs, rhs).reduce(function (result, row) {
return target.modelQueryBuilder.select(lhs, rhs).where(function (builder) {
if (target.HostModel.deleteTimestamp) {
return builder.whereNull(target.HostModel.deleteTimestamp)
}
}).reduce(function (result, row) {
result[row[lhs]] = row[rhs]
return result
}, {})
Expand Down
20 changes: 20 additions & 0 deletions test/unit/lucid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,26 @@ describe('Lucid', function () {
expect(usersPair).deep.equal(manualPair)
})

it('should exclude soft deleted rows from pair', function * () {
class User extends Model {
static get deleteTimestamp () {
return 'deleted_at'
}
}
User.bootIfNotBooted()
yield User.createMany([{username: 'foo'}, {username: 'bar'}, {username: 'bash'}])
const deletedUser = yield User.first()
yield deletedUser.delete()
const usersPair = yield User.pair('id', 'username')
const users = yield User.all()
let manualPair = users.map(function (user) {
return [user.id, user.username]
}).fromPairs().value()
expect(usersPair).to.be.an('object')
expect(usersPair[1]).to.be.undefined
expect(usersPair).deep.equal(manualPair)
})

it('should be able to use pairs of the query builder chain', function * () {
class User extends Model {
}
Expand Down

0 comments on commit 1f7b293

Please sign in to comment.