Skip to content

Commit

Permalink
fix: wrap queries with aggregates inside model instances
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Earlier the result of query with any aggregate was an
object. Now it is an instance of a model
  • Loading branch information
thetutlage committed Feb 19, 2021
1 parent 9896444 commit 21181a3
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 33 deletions.
24 changes: 12 additions & 12 deletions adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ declare module '@ioc:Adonis/Lucid/Model' {
/**
* Perform delete operation
*/
del(): ModelQueryBuilderContract<Model, number>
delete(): ModelQueryBuilderContract<Model, number>
del(): ModelQueryBuilderContract<Model, any>
delete(): ModelQueryBuilderContract<Model, any>

/**
* Execute query with pagination
Expand All @@ -378,9 +378,9 @@ declare module '@ioc:Adonis/Lucid/Model' {
/**
* Mutations (update and increment can be one query aswell)
*/
update: Update<ModelQueryBuilderContract<Model, number>>
increment: Counter<ModelQueryBuilderContract<Model, number>>
decrement: Counter<ModelQueryBuilderContract<Model, number>>
update: Update<ModelQueryBuilderContract<Model, any>>
increment: Counter<ModelQueryBuilderContract<Model, any>>
decrement: Counter<ModelQueryBuilderContract<Model, any>>

/**
* Fetch relationship count
Expand Down Expand Up @@ -415,13 +415,13 @@ declare module '@ioc:Adonis/Lucid/Model' {
/**
* Aggregates
*/
count: Aggregate<ModelQueryBuilderContract<Model, any>>
countDistinct: Aggregate<ModelQueryBuilderContract<Model, any>>
min: Aggregate<ModelQueryBuilderContract<Model, any>>
max: Aggregate<ModelQueryBuilderContract<Model, any>>
sum: Aggregate<ModelQueryBuilderContract<Model, any>>
avg: Aggregate<ModelQueryBuilderContract<Model, any>>
avgDistinct: Aggregate<ModelQueryBuilderContract<Model, any>>
count: Aggregate<this>
countDistinct: Aggregate<this>
min: Aggregate<this>
max: Aggregate<this>
sum: Aggregate<this>
avg: Aggregate<this>
avgDistinct: Aggregate<this>

/**
* Executes the callback when dialect matches one of the mentioned
Expand Down
2 changes: 1 addition & 1 deletion src/Orm/QueryBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon
/**
* Return the rows as it is when query is a write query
*/
if (isWriteQuery || this.hasAggregates || !this.wrapResultsToModelInstances) {
if (isWriteQuery || !this.wrapResultsToModelInstances) {
return Array.isArray(rows) ? rows : [rows]
}

Expand Down
10 changes: 6 additions & 4 deletions test/orm/base-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4373,8 +4373,9 @@ test.group('Base Model | aggregates', (group) => {
const usersCount = await User.query().count('* as total')
assert.deepEqual(
usersCount.map((row) => {
row.total = Number(row.total)
return row
return {
total: Number(row.$extras.total),
}
}),
[{ total: 2 }]
)
Expand All @@ -4399,8 +4400,9 @@ test.group('Base Model | aggregates', (group) => {
const usersCount = await User.query().countDistinct('username as total')
assert.deepEqual(
usersCount.map((row) => {
row.total = Number(row.total)
return row
return {
total: Number(row.$extras.total),
}
}),
[{ total: 2 }]
)
Expand Down
10 changes: 5 additions & 5 deletions test/orm/model-has-many-through.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ test.group('Model | Has Many Through | aggregates', (group) => {

const country = await Country.find(1)
const total = await country!.related('posts').query().count('* as total')
assert.deepEqual(Number(total[0].total), 2)
assert.deepEqual(Number(total[0].$extras.total), 2)
})

test('select extra columns with count', async (assert) => {
Expand Down Expand Up @@ -1012,10 +1012,10 @@ test.group('Model | Has Many Through | aggregates', (group) => {
.count('* as total')

assert.lengthOf(total, 2)
assert.deepEqual(Number(total[0].total), 1)
assert.equal(total[0].title, 'Adonis 101')
assert.deepEqual(Number(total[0].total), 1)
assert.equal(total[1].title, 'Lucid 101')
assert.deepEqual(Number(total[0].$extras.total), 1)
assert.equal(total[0].$extras.title, 'Adonis 101')
assert.deepEqual(Number(total[0].$extras.total), 1)
assert.equal(total[1].$extras.title, 'Lucid 101')
})
})

Expand Down
2 changes: 1 addition & 1 deletion test/orm/model-has-many.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ test.group('Model | HasMany | aggregates', (group) => {

const user = await User.find(1)
const total = await user!.related('posts').query().count('* as total')
assert.equal(Number(total[0].total), 2)
assert.equal(Number(total[0].$extras.total), 2)
})
})

Expand Down
18 changes: 9 additions & 9 deletions test/orm/model-many-to-many.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ test.group('Model | Many To Many | aggregates', (group) => {
const user = await User.find(1)
const total = await user!.related('skills').query().count('* as total')

assert.deepEqual(Number(total[0].total), 2)
assert.deepEqual(Number(total[0].$extras.total), 2)
})

test('select extra columns with count', async (assert) => {
Expand Down Expand Up @@ -941,11 +941,11 @@ test.group('Model | Many To Many | aggregates', (group) => {
.orderBy('skills.id', 'desc')

assert.lengthOf(total, 2)
assert.equal(total[0].name, 'Cooking')
assert.equal(Number(total[0].total), 1)
assert.equal(total[0].$extras.name, 'Cooking')
assert.equal(Number(total[0].$extras.total), 1)

assert.equal(total[1].name, 'Programming')
assert.equal(Number(total[1].total), 1)
assert.equal(total[1].$extras.name, 'Programming')
assert.equal(Number(total[1].$extras.total), 1)
})

test('select extra pivot columns with count', async (assert) => {
Expand Down Expand Up @@ -983,11 +983,11 @@ test.group('Model | Many To Many | aggregates', (group) => {
.orderBy('skills.id', 'desc')

assert.lengthOf(total, 2)
assert.equal(total[0].pivot_proficiency, 'Advanced')
assert.equal(Number(total[0].total), 1)
assert.equal(total[0].$extras.pivot_proficiency, 'Advanced')
assert.equal(Number(total[0].$extras.total), 1)

assert.equal(total[1].pivot_proficiency, 'Beginner')
assert.equal(Number(total[1].total), 1)
assert.equal(total[1].$extras.pivot_proficiency, 'Beginner')
assert.equal(Number(total[1].$extras.total), 1)
})
})

Expand Down
2 changes: 1 addition & 1 deletion test/orm/model-query-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,6 @@ test.group('Model query builder', (group) => {
.insert([{ username: 'virk' }, { username: 'nikk' }])

const users = await User.query().count('* as total')
assert.equal(Number(users[0].total), 2)
assert.equal(Number(users[0].$extras.total), 2)
})
})

0 comments on commit 21181a3

Please sign in to comment.