Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/database",
"version": "5.53.0",
"version": "5.54.0",
"description": "The Athenna database handler for SQL/NoSQL.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
36 changes: 4 additions & 32 deletions src/models/builders/ModelQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,15 +940,8 @@ export class ModelQueryBuilder<
*/
public where(statement: any, operation?: any | Operations, value?: any) {
if (Is.Function(statement)) {
const driver = this.driver.clone()

super.where(query => {
const modelQb = new ModelQueryBuilder(
this.Model,
driver.setQueryBuilder(query, { useSetQB: true })
)

statement(modelQb)
statement(new ModelQueryBuilder(this.Model, query as unknown as Driver))
})

return this
Expand Down Expand Up @@ -979,15 +972,8 @@ export class ModelQueryBuilder<
*/
public whereNot(statement: any, value?: any) {
if (Is.Function(statement)) {
const driver = this.driver.clone()

super.whereNot(query => {
const modelQb = new ModelQueryBuilder(
this.Model,
driver.setQueryBuilder(query, { useSetQB: true })
)

statement(modelQb)
statement(new ModelQueryBuilder(this.Model, query as unknown as Driver))
})

return this
Expand Down Expand Up @@ -1125,15 +1111,8 @@ export class ModelQueryBuilder<
*/
public orWhere(statement: any, operation?: any | Operations, value?: any) {
if (Is.Function(statement)) {
const driver = this.driver.clone()

super.orWhere(query => {
const modelQb = new ModelQueryBuilder(
this.Model,
driver.setQueryBuilder(query, { useSetQB: true })
)

statement(modelQb)
statement(new ModelQueryBuilder(this.Model, query as unknown as Driver))
})

return this
Expand Down Expand Up @@ -1164,15 +1143,8 @@ export class ModelQueryBuilder<
*/
public orWhereNot(statement: any, value?: any) {
if (Is.Function(statement)) {
const driver = this.driver.clone()

super.orWhereNot(query => {
const modelQb = new ModelQueryBuilder(
this.Model,
driver.setQueryBuilder(query, { useSetQB: true })
)

statement(modelQb)
statement(new ModelQueryBuilder(this.Model, query as unknown as Driver))
})

return this
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/models/relations/BelongsTo/BelongsToRelationTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,32 @@ export default class BelongsToRelationTest {
assert.isUndefined(product.user)
assert.instanceOf(profile, Profile)
}

@Test()
public async shouldFilterByRelationColumnInsideAGroupedWhereClosure({ assert }: Context) {
const matching = await Product.query()
.where(qb => qb.whereHas('user', q => q.where('id', 1)))
.findMany()

assert.instanceOf(matching[0], Product)
assert.equal(matching.length, 1)

const none = await Product.query()
.where(qb => qb.whereHas('user', q => q.where('id', 99)))
.findMany()

assert.lengthOf(none, 0)
}

@Test()
public async shouldFilterUsingSearchAcrossDirectAndRelationColumns({ assert }: Context) {
const matchingByRelation = await Product.query().search(['user.name'], 'lenon').findMany()

assert.equal(matchingByRelation.length, 1)
assert.instanceOf(matchingByRelation[0], Product)

const noneByRelation = await Product.query().search(['user.name'], 'nobody').findMany()

assert.lengthOf(noneByRelation, 0)
}
}
Loading