Skip to content

Commit

Permalink
perf(client): stop exporting dmmf.schema (#13811)
Browse files Browse the repository at this point in the history
  • Loading branch information
aqrln committed Jun 22, 2022
1 parent 36285c8 commit 67dabe1
Show file tree
Hide file tree
Showing 17 changed files with 266 additions and 113 deletions.
Expand Up @@ -20810,6 +20810,6 @@ export namespace Prisma {
/**
* DMMF
*/
export const dmmf: runtime.DMMF.Document;
export const dmmf: runtime.BaseDMMF
}
`;
Expand Up @@ -18495,6 +18495,6 @@ export namespace Prisma {
/**
* DMMF
*/
export const dmmf: runtime.DMMF.Document;
export const dmmf: runtime.BaseDMMF
}
`;
9 changes: 4 additions & 5 deletions packages/client/src/generation/TSClient/TSClient.ts
Expand Up @@ -22,7 +22,7 @@ import { commonCodeJS, commonCodeTS } from './common'
import { Count } from './Count'
import { Enum } from './Enum'
import type { Generatable } from './Generatable'
import { escapeJson, ExportCollector } from './helpers'
import { ExportCollector } from './helpers'
import { InputType } from './Input'
import { Model } from './Model'
import { PrismaClientClass } from './PrismaClient'
Expand All @@ -47,9 +47,8 @@ export interface TSClientOptions {

export class TSClient implements Generatable {
protected readonly dmmf: DMMFHelper
protected readonly dmmfString: string

constructor(protected readonly options: TSClientOptions) {
this.dmmfString = escapeJson(JSON.stringify(options.document))
this.dmmf = new DMMFHelper(klona(options.document))
}

Expand Down Expand Up @@ -114,7 +113,7 @@ ${new Enum(
},
true,
).toJS()}
${buildDMMF(dataProxy, this.dmmfString)}
${buildDMMF(dataProxy, this.options.document)}
/**
* Create the Client
Expand Down Expand Up @@ -265,7 +264,7 @@ export type BatchPayload = {
/**
* DMMF
*/
export const dmmf: runtime.DMMF.Document;
export const dmmf: runtime.BaseDMMF
`,
2,
)}}`
Expand Down
15 changes: 11 additions & 4 deletions packages/client/src/generation/utils/buildDMMF.ts
@@ -1,18 +1,25 @@
import { DMMF } from '@prisma/generator-helper'
import lzString from 'lz-string'

import { escapeJson } from '../TSClient/helpers'

/**
* Creates the necessary declarations to embed the generated DMMF into the
* generated client. It compresses the DMMF for the data proxy engine.
* generated client. It compresses the DMMF for the data proxy engine. For
* data proxy, the full DMMF is exported, otherwise `schema` is removed.
* @param dataProxy
* @param dmmf
* @returns
*/
export function buildDMMF(dataProxy: boolean | undefined, dmmf: string) {
export function buildDMMF(dataProxy: boolean | undefined, dmmf: DMMF.Document) {
if (dataProxy === true) {
return buildCompressedDMMF(dmmf)
const dmmfString = escapeJson(JSON.stringify(dmmf))
return buildCompressedDMMF(dmmfString)
}

return buildUncompressedDMMF(dmmf)
const { datamodel, mappings } = dmmf
const dmmfString = escapeJson(JSON.stringify({ datamodel, mappings }))
return buildUncompressedDMMF(dmmfString)
}

/**
Expand Down
67 changes: 38 additions & 29 deletions packages/client/src/runtime/RequestHandler.ts
Expand Up @@ -28,14 +28,19 @@ export type RequestParams = {
callsite?: string
rejectOnNotFound?: RejectOnNotFound
runInTransaction?: boolean
showColors?: boolean
engineHook?: EngineMiddleware
args: any
headers?: Record<string, string>
transactionId?: string | number
unpacker?: Unpacker
}

export type HandleErrorParams = {
error: any
clientMethod: string
callsite?: string
}

export type Request = {
document: Document
runInTransaction?: boolean
Expand Down Expand Up @@ -99,7 +104,6 @@ export class RequestHandler {
rejectOnNotFound,
clientMethod,
runInTransaction,
showColors,
engineHook,
args,
headers,
Expand Down Expand Up @@ -154,37 +158,42 @@ export class RequestHandler {
return { data: unpackResult, elapsed }
}
return unpackResult
} catch (e: any) {
debug(e)
let message = e.message
if (callsite) {
const { stack } = printStack({
callsite,
originalMethod: clientMethod,
onUs: e.isPanic,
showColors,
})
message = `${stack}\n ${e.message}`
}
} catch (error) {
this.handleRequestError({ error, clientMethod, callsite })
}
}

message = this.sanitizeMessage(message)
// TODO: Do request with callsite instead, so we don't need to rethrow
if (e.code) {
throw new PrismaClientKnownRequestError(message, e.code, this.client._clientVersion, e.meta)
} else if (e.isPanic) {
throw new PrismaClientRustPanicError(message, this.client._clientVersion)
} else if (e instanceof PrismaClientUnknownRequestError) {
throw new PrismaClientUnknownRequestError(message, this.client._clientVersion)
} else if (e instanceof PrismaClientInitializationError) {
throw new PrismaClientInitializationError(message, this.client._clientVersion)
} else if (e instanceof PrismaClientRustPanicError) {
throw new PrismaClientRustPanicError(message, this.client._clientVersion)
}
handleRequestError({ error, clientMethod, callsite }: HandleErrorParams): never {
debug(error)

e.clientVersion = this.client._clientVersion
let message = error.message
if (callsite) {
const { stack } = printStack({
callsite,
originalMethod: clientMethod,
onUs: error.isPanic,
showColors: this.client._errorFormat === 'pretty',
})
message = `${stack}\n ${error.message}`
}

throw e
message = this.sanitizeMessage(message)
// TODO: Do request with callsite instead, so we don't need to rethrow
if (error.code) {
throw new PrismaClientKnownRequestError(message, error.code, this.client._clientVersion, error.meta)
} else if (error.isPanic) {
throw new PrismaClientRustPanicError(message, this.client._clientVersion)
} else if (error instanceof PrismaClientUnknownRequestError) {
throw new PrismaClientUnknownRequestError(message, this.client._clientVersion)
} else if (error instanceof PrismaClientInitializationError) {
throw new PrismaClientInitializationError(message, this.client._clientVersion)
} else if (error instanceof PrismaClientRustPanicError) {
throw new PrismaClientRustPanicError(message, this.client._clientVersion)
}

error.clientVersion = this.client._clientVersion

throw error
}

sanitizeMessage(message) {
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/runtime/core/model/applyFluent.ts
Expand Up @@ -86,7 +86,7 @@ export function applyFluent(
prevUserArgs?: UserArgs,
) {
// we retrieve the model that is described from the DMMF
const dmmfModel = client._dmmf.modelMap[dmmfModelName]
const dmmfModel = client._baseDmmf.modelMap[dmmfModelName]

// map[field.name] === field, basically for quick access
const dmmfModelFieldMap = dmmfModel.fields.reduce(
Expand Down Expand Up @@ -126,7 +126,7 @@ export function applyFluent(

// the only accessible fields are relations to be chained on
function getOwnKeys(client: Client, dmmfModelName: string) {
return client._dmmf.modelMap[dmmfModelName].fields
return client._baseDmmf.modelMap[dmmfModelName].fields
.filter((field) => field.kind === 'object') // relations
.map((field) => field.name)
}
2 changes: 1 addition & 1 deletion packages/client/src/runtime/core/model/applyModel.ts
Expand Up @@ -71,7 +71,7 @@ export function applyModel(client: Client, dmmfModelName: string) {

// the only accessible fields are the ones that are actions
function getOwnKeys(client: Client, dmmfModelName: string) {
return [...Object.keys(client._dmmf.mappingsMap[dmmfModelName]), 'count'].filter(
return [...Object.keys(client._baseDmmf.mappingsMap[dmmfModelName]), 'count'].filter(
(key) => !['model', 'plural'].includes(key),
)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/runtime/core/model/applyModels.ts
Expand Up @@ -29,12 +29,12 @@ export function applyModels<C extends Client>(client: C) {
}

// creates a new model proxy on the fly and caches it
if (client._dmmf.modelMap[dmmfModelName] !== undefined) {
if (client._baseDmmf.modelMap[dmmfModelName] !== undefined) {
return (modelCache[dmmfModelName] = applyModel(client, dmmfModelName))
}

// above silently failed if model name is lower cased
if (client._dmmf.modelMap[prop] !== undefined) {
if (client._baseDmmf.modelMap[prop] !== undefined) {
return (modelCache[dmmfModelName] = applyModel(client, prop))
}
},
Expand All @@ -44,5 +44,5 @@ export function applyModels<C extends Client>(client: C) {

// the only accessible fields are the ones that are models
function getOwnKeys(client: Client) {
return [...Object.keys(client._dmmf.modelMap).map(dmmfToJSModelName), ...Object.keys(client)]
return [...Object.keys(client._baseDmmf.modelMap).map(dmmfToJSModelName), ...Object.keys(client)]
}
2 changes: 2 additions & 0 deletions packages/client/src/runtime/dmmf-types.ts
@@ -1,3 +1,5 @@
import { DMMF } from '@prisma/generator-helper'

export { DMMF }

export type BaseDMMF = Pick<DMMF.Document, 'datamodel' | 'mappings'>

0 comments on commit 67dabe1

Please sign in to comment.