Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: cleanup middleware implementation (#6969)
Co-authored-by: Pierre-Antoine Mills <pierreantoine.urvoy@gmail.com> Co-authored-by: Joël Galeran <Jolg42@users.noreply.github.com>
- Loading branch information
1 parent
ed43150
commit dec79f5
Showing
12 changed files
with
509 additions
and
422 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+0 Bytes
(100%)
src/packages/client/src/__tests__/integration/happy/middlewares/dev.db
Binary file not shown.
149 changes: 103 additions & 46 deletions
149
src/packages/client/src/__tests__/integration/happy/middlewares/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,121 @@ | ||
import { getTestClient } from '../../../../utils/getTestClient' | ||
|
||
test('middlewares', async () => { | ||
const PrismaClient = await getTestClient() | ||
const db = new PrismaClient() | ||
describe('middleware', () => { | ||
test('basic', async () => { | ||
const PrismaClient = await getTestClient() | ||
|
||
const allResults: any[] = [] | ||
const engineResults: any[] = [] | ||
const db = new PrismaClient() | ||
|
||
const order: number[] = [] | ||
const allResults: any[] = [] | ||
|
||
db.$use(async (params, fetch) => { | ||
order.push(1) | ||
const result = await fetch(params) | ||
order.push(4) | ||
return result | ||
}) | ||
db.$use(async (params, next) => { | ||
const result = await next(params) | ||
allResults.push(result) | ||
return result | ||
}) | ||
|
||
await db.user.findMany() | ||
await db.post.findMany() | ||
|
||
expect(allResults).toEqual([[], []]) | ||
|
||
db.$use(async (params, fetch) => { | ||
order.push(2) | ||
const result = await fetch(params) | ||
order.push(3) | ||
allResults.push(result) | ||
return result | ||
db.$disconnect() | ||
}) | ||
test('order', async () => { | ||
const PrismaClient = await getTestClient() | ||
const db = new PrismaClient() | ||
const order: number[] = [] | ||
|
||
db.$use(async (params, next) => { | ||
order.push(1) | ||
const result = await next(params) | ||
order.push(4) | ||
return result | ||
}) | ||
|
||
db.$use(async (params, next) => { | ||
order.push(2) | ||
const result = await next(params) | ||
order.push(3) | ||
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() | ||
|
||
expect(order).toEqual([1, 2, 3, 4, 1, 2, 3, 4]) | ||
|
||
db.$disconnect() | ||
}) | ||
test('engine middleware', async () => { | ||
const PrismaClient = await getTestClient() | ||
const db = new PrismaClient() | ||
|
||
await db.user.findMany() | ||
await db.post.findMany() | ||
const engineResults: any[] = [] | ||
|
||
expect(order).toEqual([1, 2, 3, 4, 1, 2, 3, 4]) | ||
expect(allResults).toEqual([[], []]) | ||
expect(engineResults.map((r) => r.data)).toEqual([ | ||
{ | ||
data: { | ||
findManyUser: [], | ||
db.$use('engine', async (params, next) => { | ||
const result = await next(params) | ||
engineResults.push(result) | ||
return result | ||
}) | ||
|
||
await db.user.findMany() | ||
await db.post.findMany() | ||
expect(engineResults.map((r) => r.data)).toEqual([ | ||
{ | ||
data: { | ||
findManyUser: [], | ||
}, | ||
}, | ||
}, | ||
{ | ||
{ | ||
data: { | ||
findManyPost: [], | ||
}, | ||
}, | ||
]) | ||
expect(typeof engineResults[0].elapsed).toEqual('number') | ||
expect(typeof engineResults[1].elapsed).toEqual('number') | ||
|
||
db.$disconnect() | ||
}) | ||
test('modify params', async () => { | ||
const PrismaClient = await getTestClient() | ||
const db = new PrismaClient() | ||
|
||
const user = await db.user.create({ | ||
data: { | ||
findManyPost: [], | ||
email: 'test@test.com', | ||
name: 'test', | ||
}, | ||
}, | ||
]) | ||
expect(typeof engineResults[0].elapsed).toEqual('number') | ||
expect(typeof engineResults[1].elapsed).toEqual('number') | ||
}) | ||
db.$use(async (params, next) => { | ||
if (params.action === 'findFirst' && params.model === 'User') { | ||
params.args = { ...params.args, where: { name: 'test' } } | ||
} | ||
const result = await next(params) | ||
return result | ||
}) | ||
|
||
db.$disconnect() | ||
}) | ||
const users = await db.user.findMany() | ||
console.warn(users) | ||
// The name should be overwritten by the middleware | ||
const u = await db.user.findFirst({ | ||
where: { | ||
name: 'fake', | ||
}, | ||
}) | ||
expect(u.id).toBe(user.id) | ||
await db.user.deleteMany() | ||
|
||
test('middlewares unpack', async () => { | ||
const PrismaClient = await getTestClient() | ||
const db = new PrismaClient() | ||
db.$use((params, next) => next(params)) | ||
const result = await db.user.count() | ||
expect(typeof result).toBe('number') | ||
db.$disconnect() | ||
}) | ||
test('count unpack', async () => { | ||
const PrismaClient = await getTestClient() | ||
const db = new PrismaClient() | ||
db.$use((params, next) => next(params)) | ||
const result = await db.user.count() | ||
expect(typeof result).toBe('number') | ||
|
||
db.$disconnect() | ||
db.$disconnect() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Action } from './getPrismaClient' | ||
import { Document } from './query' | ||
|
||
export type QueryMiddleware<T = unknown> = ( | ||
params: QueryMiddlewareParams, | ||
next: (params: QueryMiddlewareParams) => Promise<T>, | ||
) => Promise<T> | ||
|
||
export type QueryMiddlewareParams = { | ||
/** The model this is executed on */ | ||
model?: string | ||
/** The action that is being handled */ | ||
action: Action | ||
/** TODO what is this */ | ||
dataPath: string[] | ||
/** TODO what is this */ | ||
runInTransaction: boolean | ||
/** TODO what is this */ | ||
args: any // TODO remove any, does this make sense, what is args? | ||
} | ||
|
||
export type EngineMiddleware<T = unknown> = ( | ||
params: EngineMiddlewareParams, | ||
next: ( | ||
params: EngineMiddlewareParams, | ||
) => Promise<{ data: T; elapsed: number }>, | ||
) => Promise<{ data: T; elapsed: number }> | ||
|
||
export type EngineMiddlewareParams = { | ||
document: Document | ||
runInTransaction?: boolean | ||
} | ||
|
||
export type Namespace = 'all' | 'engine' | ||
|
||
class MiddlewareHandler<M extends Function> { | ||
private _middlewares: M[] = [] | ||
|
||
use(middleware: M) { | ||
this._middlewares.push(middleware) | ||
} | ||
|
||
get(id: number): M | undefined { | ||
return this._middlewares[id] | ||
} | ||
|
||
has(id: number) { | ||
return !!this._middlewares[id] | ||
} | ||
|
||
length() { | ||
return this._middlewares.length | ||
} | ||
} | ||
|
||
export class Middlewares { | ||
query = new MiddlewareHandler<QueryMiddleware>() | ||
engine = new MiddlewareHandler<EngineMiddleware>() | ||
} |
Oops, something went wrong.