Skip to content

Commit

Permalink
feat(SoftDeletes): add check a query on group limit
Browse files Browse the repository at this point in the history
  • Loading branch information
LookinGit committed May 27, 2022
1 parent f6054fd commit c2715e9
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/SoftDeletes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ export function SoftDeletes<T extends NormalizeConstructor<LucidModel>> (supercl
if (query['ignoreDeleted'] === false) {
return
}
const isGroupLimitQuery = query.toQuery().includes('adonis_group_limit_counter')
const deletedAtColumn = query.model.$getColumn('deletedAt')?.columnName
query.whereNull(`${query.model.table}.${deletedAtColumn}`)

const queryIgnoreDeleted = isGroupLimitQuery ? query.knexQuery['_single'].table : query

queryIgnoreDeleted.whereNull(`${query.model.table}.${deletedAtColumn}`)
}

@beforePaginate()
Expand Down
75 changes: 74 additions & 1 deletion test/relations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,82 @@ test.group('Relations', (group) => {
await author2.delete()

const books = await Book.query()
.select('id', 'name').preload('authors')
.select('id', 'name')
.preload('authors')
.exec()

assert.lengthOf(books, 1)
assert.lengthOf(books[0].authors, 1)

await Promise.all([Book.truncate(), Author.truncate()])
})

test('querying many to many with preload and group limit', async (assert) => {
class Book extends BaseModel {
@column({ isPrimary: true })
public id: number

@column()
public name: string

@manyToMany(() => Author)
public authors: ManyToMany<typeof Author>
}

class Author extends compose(BaseModel, SoftDeletes) {
@column({ isPrimary: true })
public id: number

@column()
public name: string

@column.dateTime()
public deletedAt: DateTime

@manyToMany(() => Book)
public books: ManyToMany<typeof Book>
}

const book1 = new Book()
book1.fill({ name: 'Introduction AdonisJs' })
await book1.save()

const book2 = new Book()
book2.fill({ name: 'Introduction Javascript' })
await book2.save()

const author1 = new Author()
author1.fill({ name: 'John' })
await author1.save()

const author2 = new Author()
author2.fill({ name: 'Mary' })
await author2.save()

const author3 = new Author()
author3.fill({ name: 'Paul' })
await author3.save()

await book1.related('authors').attach([author1.id, author2.id, author3.id])
await book2.related('authors').attach([author2.id, author3.id])
await author1.delete()

const authorsLimit1 = await Book.query()
.preload('authors', (authors) => authors.groupLimit(1))
.paginate(1, 10)

assert.lengthOf(authorsLimit1, 2)
assert.lengthOf(authorsLimit1[0].authors, 1)
assert.lengthOf(authorsLimit1[1].authors, 1)

const authorsLimit2 = await Book.query()
.preload('authors', (authors) => authors.groupLimit(2))
.paginate(1, 10)

assert.lengthOf(authorsLimit2, 2)
assert.lengthOf(authorsLimit2[0].authors, 2)
assert.lengthOf(authorsLimit2[1].authors, 2)

await Promise.all([Book.truncate(), Author.truncate()])
})
})

0 comments on commit c2715e9

Please sign in to comment.