Skip to content

Commit

Permalink
chore: put middlewares behind feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Jul 20, 2020
1 parent a5a4eef commit da2393a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ datasource db {

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

// / User model comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ datasource db {

generator client {
provider = "prisma-client-js"
experimentalFeatures = ["transactionApi"]
experimentalFeatures = ["transactionApi", "middlewaresApi"]
}

// / User model comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ datasource db {
generator client {
provider = "prisma-client-js"
output = "@prisma/client"
experimentalFeatures = ["aggregateApi"]
experimentalFeatures = ["aggregateApi", "middlewaresApi"]
}

// / User model comment
Expand Down
6 changes: 6 additions & 0 deletions src/packages/client/src/generation/TSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,16 @@ ${indent(this.jsDoc, tab)}
*/
disconnect(): Promise<any>;
${
this.generator?.previewFeatures?.includes('middlewaresApi')
? `
/**
* Add a middleware
*/
use(cb: Middleware): void
`
: ''
}
/**
* Executes a raw query and returns the number of affected rows
Expand Down
24 changes: 15 additions & 9 deletions src/packages/client/src/runtime/getPrismaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,18 +393,24 @@ export function getPrismaClient(config: GetPrismaClientOptions): any {
use(namespace: 'all', cb: Middleware)
use(namespace: 'engine', cb: EngineMiddleware)
use(namespace: HookPoint | Middleware, cb?: Middleware | EngineMiddleware) {
if (typeof namespace === 'function') {
this._middlewares.push(namespace)
} else if (typeof namespace === 'string') {
if (namespace === 'all') {
this._middlewares.push(cb! as Middleware)
} else if (namespace === 'engine') {
this._engineMiddlewares.push(cb! as EngineMiddleware)
if (config.generator?.previewFeatures?.includes('middlewaresApi')) {
if (typeof namespace === 'function') {
this._middlewares.push(namespace)
} else if (typeof namespace === 'string') {
if (namespace === 'all') {
this._middlewares.push(cb! as Middleware)
} else if (namespace === 'engine') {
this._engineMiddlewares.push(cb! as EngineMiddleware)
} else {
throw new Error(`Unknown middleware hook ${namespace}`)
}
} else {
throw new Error(`Unknown middleware hook ${namespace}`)
throw new Error(`Invalid middleware ${namespace}`)
}
} else {
throw new Error(`Invalid middleware ${namespace}`)
throw new Error(
`In order to use the middlewares api, please enable set previewFeatures = ["middlewaresApi"]`,
)
}
}
on(eventType: any, callback: (event: any) => void) {
Expand Down
3 changes: 3 additions & 0 deletions src/packages/engine-core/src/NodeEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ export class NodeEngine {
this.clientVersion = clientVersion
this.flags = flags ?? []
this.enableExperimental = enableExperimental ?? []
this.enableExperimental = this.enableExperimental.filter(
(e) => e !== 'middlewaresApi',
)
this.engineEndpoint = engineEndpoint

if (engineEndpoint) {
Expand Down

0 comments on commit da2393a

Please sign in to comment.