Skip to content

Commit

Permalink
fix(soft-deletes): pairs and ids ignore soft deleted
Browse files Browse the repository at this point in the history
Closes #109
  • Loading branch information
thetutlage committed Apr 8, 2017
1 parent 38acb47 commit c394950
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Lucid/QueryBuilder/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ methods.scope = function (target) {
*/
methods.ids = function (target) {
return function () {
const serializer = new target.HostModel.QuerySerializer(target, this)
serializer._decorateQuery()
return target.modelQueryBuilder.select(target.HostModel.primaryKey).pluck(target.HostModel.primaryKey)
}
}
Expand All @@ -465,6 +467,8 @@ methods.ids = function (target) {
*/
methods.pair = function (target) {
return function (lhs, rhs) {
const serializer = new target.HostModel.QuerySerializer(target, this)
serializer._decorateQuery()
return target.modelQueryBuilder.select(lhs, rhs).reduce(function (result, row) {
result[row[lhs]] = row[rhs]
return result
Expand Down
45 changes: 45 additions & 0 deletions test/unit/lucid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2221,5 +2221,50 @@ describe('Lucid', function () {
expect(queryHelpers.formatQuery(query.sql)).to.equal(queryHelpers.formatQuery('select * from "users" order by "id" desc limit ?'))
expect(queryHelpers.formatBindings(query.bindings)).deep.equal(queryHelpers.formatBindings([1]))
})

it('should not select soft deleted rows with pair method', function * () {
class User extends Model {
static get deleteTimestamp () {
return 'deleted_at'
}
}

User.bootIfNotBooted()

yield User.createMany([{username: 'foo'}, {username: 'bar'}, {username: 'baz'}])
const bazUser = yield User.findBy('username', 'baz')
yield bazUser.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).deep.equal(manualPair)
yield User.truncate()
})

it('should not select soft deleted with ids method', function * () {
class User extends Model {
static get deleteTimestamp () {
return 'deleted_at'
}
}

User.bootIfNotBooted()

yield User.createMany([{username: 'foo'}, {username: 'bar'}, {username: 'baz'}])
const bazUser = yield User.findBy('username', 'baz')
yield bazUser.delete()

const userIds = yield User.ids()
expect(userIds).to.be.an('array')
expect(userIds).has.length(2)
userIds.forEach(function (id) {
expect(id).to.be.a('number')
})
yield User.truncate()
})
})
})

0 comments on commit c394950

Please sign in to comment.