Skip to content

Commit

Permalink
feat: new count api (#4913)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Suchanek <Tim.Suchanek@gmail.com>
Co-authored-by: William Luke <william@ordino.ai>
Co-authored-by: William Luke <william@atto-byte.com>
  • Loading branch information
3 people committed Jan 11, 2021
1 parent 50e88b0 commit 59cf156
Show file tree
Hide file tree
Showing 24 changed files with 1,039 additions and 287 deletions.
59 changes: 28 additions & 31 deletions src/packages/client/fixtures/blog/main.ts
Expand Up @@ -10,43 +10,40 @@ const prisma = new PrismaClient({
})

async function main() {
prisma.$on('query', (q) => {
console.log({ q })
})

const res = await prisma.user.groupBy({
by: ['age', 'email'],
const user = await prisma.user.groupBy({
by: ['name'],
where: {
age: {
gt: -1,
},
},
// skip: 0,
// take: 10000,
avg: {
age: true,
},
having: {
AND: [
{
NOT: [
{
email: '',
name: {
min: {},
},
age: {
sum: {
gt: 1,
},
},
},
],
},
],
age: {
gt: 1,
avg: {
gt: 5,
},
},
count: true,
max: {
age: true,
},
min: {
age: true,
},
sum: {
age: true,
},
})
// const res = await prisma.user.aggregate({
// // select: true,
// // skip: 3
// count: true,
// min: {
// email: true,
// // json: true,
// },
// })

console.log(res)
console.log(user[0].count)

prisma.$disconnect()
}
Expand Down
4 changes: 2 additions & 2 deletions src/packages/client/src/__tests__/__helpers__/dmmf-types.ts
Expand Up @@ -4048,7 +4048,7 @@ const dmmf: DMMF.Document = {
"args": [],
"isNullable": false,
"isRequired": true,
"name": "_all",
"name": "$all",
"outputType": {
"isList": false,
"location": "scalar",
Expand Down Expand Up @@ -4238,7 +4238,7 @@ const dmmf: DMMF.Document = {
"args": [],
"isNullable": false,
"isRequired": true,
"name": "_all",
"name": "$all",
"outputType": {
"isList": false,
"location": "scalar",
Expand Down
2 changes: 1 addition & 1 deletion src/packages/client/src/__tests__/aggregate.test.ts
Expand Up @@ -59,7 +59,7 @@ describe('aggregate', () => {
select: {
count: {
select: {
_all: true,
$all: true,
},
},
},
Expand Down
Expand Up @@ -3,6 +3,7 @@ import { getTestClient } from '../../../../utils/getTestClient'
test('aggregations', async () => {
const PrismaClient = await getTestClient()
const prisma = new PrismaClient()
expect.assertions(3)
const result = await prisma.user.aggregate({
where: {
age: {
Expand All @@ -17,22 +18,126 @@ test('aggregations', async () => {
count: true,
max: {
age: true,
email: true,
},
min: {
age: true,
email: true,
},
sum: {
age: true,
},
})

expect(result).toMatchObject({
count: 10,
avg: { age: 80 },
max: { age: 163 },
min: { age: 5 },
sum: { age: 800 },
expect(result).toMatchInlineSnapshot(`
Object {
avg: Object {
age: 80,
},
count: 10,
max: Object {
age: 163,
email: bob+9@hey.com,
},
min: Object {
age: 5,
email: bob+0@hey.com,
},
sum: Object {
age: 800,
},
}
`)

const result2 = await prisma.user.aggregate({
where: {
age: {
gt: -1,
},
},
skip: 0,
take: 10000,
avg: {
age: true,
},
count: {
$all: true,
name: true,
},
max: {
age: true,
email: true,
},
min: {
age: true,
email: true,
},
sum: {
age: true,
},
})
expect(result2).toMatchInlineSnapshot(`
Object {
avg: Object {
age: 80,
},
count: Object {
$all: 10,
name: 10,
},
max: Object {
age: 163,
email: bob+9@hey.com,
},
min: Object {
age: 5,
email: bob+0@hey.com,
},
sum: Object {
age: 800,
},
}
`)

try {
await prisma.user.aggregate({
where: {
age: {
gt: -1,
},
},
skip: 0,
take: 10000,
avg: {
age: true,
email: true,
},
})
} catch (err) {
expect(err.message).toMatchInlineSnapshot(`
Invalid \`prisma.user.aggregate()\` invocation:
{
where: {
age: {
gt: -1
}
},
skip: 0,
take: 10000,
avg: {
? age?: true,
email: true
~~~~~
}
}
Unknown field \`email\` for select statement on model UserAvgAggregateOutputType. Available options are listed in green. Did you mean \`age\`?
`)
}

prisma.$disconnect()
})
@@ -0,0 +1 @@
!dev.db
Binary file not shown.
@@ -0,0 +1,30 @@
datasource db {
provider = "sqlite"
url = "file:dev.db"
default = true
}

generator client {
provider = "prisma-client-js"
}

// / User model comment
model User {
id String @default(uuid()) @id
email String @unique
age Int
// / name comment
name String?
posts Post[]
}

model Post {
id String @default(cuid()) @id
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean
title String
content String?
authorId String?
author User? @relation(fields: [authorId], references: [id])
}
64 changes: 64 additions & 0 deletions src/packages/client/src/__tests__/integration/happy/count/test.ts
@@ -0,0 +1,64 @@
import { getTestClient } from '../../../../utils/getTestClient'

test('count', async () => {
const PrismaClient = await getTestClient()
const prisma = new PrismaClient()
const result = await prisma.user.count()
expect.assertions(4)
expect(result).toMatchInlineSnapshot(`10`)

const result2 = await prisma.user.count({
select: true,
})
expect(result2).toMatchInlineSnapshot(`10`)

const result3 = await prisma.user.count({
select: {
$all: true,
email: true,
age: true,
name: true,
},
})
expect(result3).toMatchInlineSnapshot(`
Object {
$all: 10,
age: 10,
email: 10,
name: 10,
}
`)
try {
const result4 = await prisma.user.count({
select: {
$all: true,
email: true,
age: true,
name: true,
posts: true,
},
})
} catch (err) {
expect(err.message).toMatchInlineSnapshot(`
Invalid \`prisma.user.aggregate()\` invocation:
{
count: {
? $all?: true,
? email?: true,
? age?: true,
? name?: true,
posts: true,
~~~~~
? id?: true
}
}
Unknown field \`posts\` for select statement on model UserCountAggregateOutputType. Available options are listed in green. Did you mean \`id\`?
`)
}
prisma.$disconnect()
})

0 comments on commit 59cf156

Please sign in to comment.