Skip to content

Commit

Permalink
feat(client): add experimental hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Jul 1, 2020
1 parent fcb07db commit 745856c
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 21 deletions.
10 changes: 10 additions & 0 deletions src/packages/client/fixtures/blog/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ const prisma = new PrismaClient({
})

async function main() {
;(prisma as any).use('all', async ({ params, fetch }) => {
console.log(params.clientMethod, `before`, params)
const data = await fetch(params)
console.log(params.clientMethod, data)
data.push({
name: 'fake user',
})
console.log(params.clientMethod, `after`)
return data
})
const users = await prisma.transaction([
prisma.user.findMany({
include: {
Expand Down
18 changes: 18 additions & 0 deletions src/packages/client/src/__tests__/batching.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ describe('batching', () => {
isList: false,
rootField: 'query',
typeName: 'User',
args: {
where: {
id: '1',
},
},
}),
fetcher.request({
clientMethod: 'findOne',
Expand All @@ -57,6 +62,11 @@ describe('batching', () => {
isList: false,
rootField: 'query',
typeName: 'User',
args: {
where: {
id: '2',
},
},
}),
])

Expand Down Expand Up @@ -125,6 +135,9 @@ describe('batching', () => {
isList: false,
rootField: 'query',
typeName: 'User',
args: {
where: { id: '1' },
},
}),
fetcher.request({
clientMethod: 'findOne',
Expand All @@ -142,6 +155,9 @@ describe('batching', () => {
isList: false,
rootField: 'query',
typeName: 'User',
args: {
where: { id: '2' },
},
}),
])

Expand Down Expand Up @@ -211,6 +227,7 @@ describe('batching', () => {
isList: false,
rootField: 'query',
typeName: 'User',
args: { where: { email: 'a@a.de' } },
}),
fetcher.request({
clientMethod: 'findOne',
Expand All @@ -228,6 +245,7 @@ describe('batching', () => {
isList: false,
rootField: 'query',
typeName: 'User',
args: { where: { id: '2' } },
}),
])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ exports[`runtime works blog-env-postgresql example should succeed: blog-env-post

exports[`runtime works enums example should succeed: enums example should succeed 1`] = `undefined`;

exports[`runtime works hooks example should succeed: hooks example should succeed 1`] = `undefined`;

exports[`runtime works missing-binary example should not succeed: missing-binary example should not succeed 1`] = `[TypeError: e.message.split(...).slice(...).trim is not a function]`;

exports[`runtime works missing-binary-native example should not succeed: missing-binary-native example should not succeed 1`] = `[TypeError: e.message.split(...).slice(...).trim is not a function]`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!dev.db
Binary file not shown.
49 changes: 49 additions & 0 deletions src/packages/client/src/__tests__/runtime-tests/hooks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { PrismaClient } = require('@prisma/client')
const assert = require('assert')

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

const allResults = []
const engineResults = []

db.use('all', async ({ params, fetch }) => {
const result = await fetch(params)
allResults.push(result)
return result
})

db.use('engine', async ({ params, fetch }) => {
const result = await fetch(params)
engineResults.push(result)
return result
})

await db.user.findMany()
await db.post.findMany()

assert.deepEqual(allResults, [[], []])
assert.deepEqual(
engineResults.map((r) => r.data),
[
{
data: {
findManyUser: [],
},
},
{
data: {
findManyPost: [],
},
},
],
)
assert(typeof engineResults[0].elapsed === 'number')
assert(typeof engineResults[1].elapsed === 'number')

db.disconnect()
}

if (require.main === module) {
module.exports()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"shouldSucceed": true
}
13 changes: 13 additions & 0 deletions src/packages/client/src/__tests__/runtime-tests/hooks/package.json
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 = ["transactionApi"]
}

// / 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?
author User? @relation(fields: [authorId], references: [id])
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ jest.setTimeout(35000)

// TODO: Figure out the problem with debug

process.setMaxListeners(100)

describe('runtime works', () => {
const subDirs = getSubDirs(__dirname)
for (const dir of subDirs) {
Expand Down

0 comments on commit 745856c

Please sign in to comment.