Skip to content

Commit

Permalink
fix(client): nested query
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Jul 16, 2020
1 parent 21c69bb commit 464b494
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/packages/client/fixtures/blog/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ const prisma = new PrismaClient({
})

async function main() {
const res = await prisma.user.findMany({
distinct: ['age'],
})
const res = await prisma.user
.findOne({
where: {
email: 'a0@asd.de',
},
})
.posts()
console.log(res)
prisma.disconnect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Invalid \`prisma.user.findMany()\` invocation in

exports[`runtime middlewares: middlewares example should succeed 1`] = `"success"`;

exports[`runtime middlewares-manipulation: middlewares-manipulation example should succeed 1`] = `"success"`;

exports[`runtime missing-binary: missing-binary example should not succeed 1`] = `
"
Invalid \`prisma.user.findMany()\` invocation in
Expand Down
9 changes: 9 additions & 0 deletions src/packages/client/src/__tests__/runtime-tests/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ module.exports = async () => {

// Test connecting and disconnecting all the time
await db.user.findMany()
const posts = await db.user
.findOne({
where: {
email: 'a@a.de',
},
})
.posts()

assert.equal(posts.length, 0)
db.disconnect()
assert(requests.length === 1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ model Post {
published Boolean
title String
content String?
authorId String?
authorId String? @map("author")
author User? @relation(fields: [authorId], references: [id])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!dev.db
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { PrismaClient } = require('@prisma/client')
const assert = require('assert')

module.exports = async () => {
const prisma = new PrismaClient()

let theParams
let firstCall = true
prisma.use(async (params, fetch) => {
theParams = JSON.parse(JSON.stringify(params)) // clone
if (firstCall) {
params.args = {
where: {
email: 'asdasd', // this user doesn't exist
},
}
firstCall = false
}
const result = await fetch(params)
return result
})

prisma.use(async (params, fetch) => {
const result = await fetch(params)
if (result.length > 0) {
result[0].name += '2' // make sure that we can change the result
}
return result
})

const users = await prisma.user.findMany()

assert.deepEqual(theParams, {
dataPath: [],
runInTransaction: false,
action: 'findMany',
model: 'User',
})

assert.deepEqual(users, [])

const users2 = await prisma.user.findMany()
assert.deepEqual(users2, [
{
email: 'a@a.de',
id: '576eddf9-2434-421f-9a86-58bede16fd95',
name: 'Alice2',
},
])

prisma.disconnect()
}

if (require.main === module) {
module.exports()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"shouldSucceed": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "my-prisma-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
datasource db {
provider = "sqlite"
url = "file:dev.db"
default = true
}

generator client {
provider = "prisma-client-js"
experimentalFeatures = ["aggregations"]
}

// / User model comment
model User {
id String @default(uuid()) @id
email String @unique
// / 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? @map("author")
author User? @relation(fields: [authorId], references: [id])
}
22 changes: 17 additions & 5 deletions src/packages/client/src/runtime/getPrismaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,22 +776,30 @@ export function getPrismaClient(config: GetPrismaClientOptions): any {
throw new Error(`Invalid mapping ${mapping.model}, can't find model`)
}

const prismaClient = ({ operation, actionName, args, dataPath }) => {
const prismaClient = ({
operation,
actionName,
args,
dataPath,
modelName,
}) => {
dataPath = dataPath ?? []

const clientMethod = `${lowerCaseModel}.${actionName}`

let requestPromise: Promise<any>
const callsite = this._getCallsite()

const requestModelName = modelName ?? model.name

const clientImplementation = {
then: (onfulfilled, onrejected) => {
if (!requestPromise) {
requestPromise = this._request({
args,
dataPath,
action: actionName,
model: model.name,
model: requestModelName,
clientMethod,
callsite,
runInTransaction: false,
Expand All @@ -806,7 +814,7 @@ export function getPrismaClient(config: GetPrismaClientOptions): any {
args,
dataPath,
action: actionName,
model: model.name,
model: requestModelName,
clientMethod,
callsite,
runInTransaction: true,
Expand All @@ -821,7 +829,7 @@ export function getPrismaClient(config: GetPrismaClientOptions): any {
args,
dataPath,
action: actionName,
model: model.name,
model: requestModelName,
clientMethod,
callsite,
runInTransaction: false,
Expand All @@ -836,7 +844,7 @@ export function getPrismaClient(config: GetPrismaClientOptions): any {
args,
dataPath,
action: actionName,
model: model.name,
model: requestModelName,
clientMethod,
callsite,
runInTransaction: false,
Expand Down Expand Up @@ -864,6 +872,10 @@ export function getPrismaClient(config: GetPrismaClientOptions): any {
args: newArgs,
dataPath: newDataPath,
isList: field.isList,
/*
* necessary for user.posts() calls -> the original model name needs to be preserved
*/
modelName: model.name,
})
}
}
Expand Down

0 comments on commit 464b494

Please sign in to comment.