Skip to content

Commit

Permalink
fix(client): remove omitApi preview feature checks (prisma#25997)
Browse files Browse the repository at this point in the history
  • Loading branch information
aqrln authored Jan 8, 2025
1 parent 3b23a20 commit e6ef63b
Show file tree
Hide file tree
Showing 14 changed files with 3,551 additions and 914 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions packages/client/src/generation/TSClient/Args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ export class ArgsTypeBuilder {
}

addOmitArg(): this {
if (!this.context.isPreviewFeatureOn('omitApi')) {
return this
}
this.addProperty(
ts
.property(
Expand Down
66 changes: 23 additions & 43 deletions packages/client/src/generation/TSClient/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,12 @@ export type ${getAggregateGetName(model.name)}<T extends ${getAggregateArgsName(
const { model } = this
const isComposite = this.dmmf.isComposite(model.name)

const omitType = this.context.isPreviewFeatureOn('omitApi')
? ts.stringify(buildOmitType({ modelName: this.model.name, context: this.context, fields: this.type.fields }), {
newLine: 'leading',
})
: ''
const omitType = ts.stringify(
buildOmitType({ modelName: this.model.name, context: this.context, fields: this.type.fields }),
{
newLine: 'leading',
},
)

const hasRelationField = model.fields.some((f) => f.kind === 'object')
const includeType = hasRelationField
Expand Down Expand Up @@ -478,16 +479,13 @@ export class ModelDelegate implements Generable {
const groupByArgsName = getGroupByArgsName(name)
const countArgsName = getModelArgName(name, DMMF.ModelAction.count)

const genericDelegateParams = [extArgsParam]
const genericDelegateParams = [extArgsParam, ts.genericParameter('ClientOptions').default(ts.objectType())]

const excludedArgsForCount = ['select', 'include', 'distinct']
if (this.context.isPreviewFeatureOn('omitApi')) {
excludedArgsForCount.push('omit')
genericDelegateParams.push(ts.genericParameter('ClientOptions').default(ts.objectType()))
}
const excludedArgsForCount = ['select', 'include', 'distinct', 'omit']
if (this.context.isPreviewFeatureOn('relationJoins')) {
excludedArgsForCount.push('relationLoadStrategy')
}

const excludedArgsForCountType = excludedArgsForCount.map((name) => `'${name}'`).join(' | ')

return `\
Expand Down Expand Up @@ -617,7 +615,7 @@ function buildModelDelegateMethod(modelName: string, actionName: DMMF.ModelActio
.method(actionName)
.setDocComment(ts.docComment(getMethodJSDocBody(actionName, mapping, modelOrType)))
.addParameter(getNonAggregateMethodArgs(modelName, actionName))
.setReturnType(getReturnType({ modelName, actionName, context }))
.setReturnType(getReturnType({ modelName, actionName }))

const generic = getNonAggregateMethodGenericParam(modelName, actionName)
if (generic) {
Expand All @@ -627,7 +625,6 @@ function buildModelDelegateMethod(modelName: string, actionName: DMMF.ModelActio
}

function getNonAggregateMethodArgs(modelName: string, actionName: DMMF.ModelAction) {
getReturnType
const makeParameter = (type: ts.TypeBuilder) => ts.parameter('args', type)
if (actionName === DMMF.ModelAction.count) {
const type = ts.omit(
Expand Down Expand Up @@ -683,7 +680,6 @@ function getNonAggregateMethodGenericParam(modelName: string, actionName: DMMF.M
type GetReturnTypeOptions = {
modelName: string
actionName: DMMF.ModelAction
context: GenerateContext
isChaining?: boolean
isNullable?: boolean
}
Expand All @@ -696,7 +692,6 @@ type GetReturnTypeOptions = {
export function getReturnType({
modelName,
actionName,
context,
isChaining = false,
isNullable = false,
}: GetReturnTypeOptions): ts.TypeBuilder {
Expand Down Expand Up @@ -728,7 +723,7 @@ export function getReturnType({
* Important: We handle findMany or isList special, as we don't want chaining from there
*/
if (isList) {
let result: ts.TypeBuilder = getResultType(modelName, actionName, context)
let result: ts.TypeBuilder = getResultType(modelName, actionName)
if (isChaining) {
result = ts.unionType(result).addVariant(ts.namedType('Null'))
}
Expand All @@ -738,45 +733,34 @@ export function getReturnType({

if (isChaining && actionName === DMMF.ModelAction.findUniqueOrThrow) {
const nullType = isNullable ? ts.nullType : ts.namedType('Null')
const result = ts.unionType<ts.TypeBuilder>(getResultType(modelName, actionName, context)).addVariant(nullType)
return getFluentWrapper(modelName, context, result, nullType)
const result = ts.unionType<ts.TypeBuilder>(getResultType(modelName, actionName)).addVariant(nullType)
return getFluentWrapper(modelName, result, nullType)
}

if (actionName === DMMF.ModelAction.findFirst || actionName === DMMF.ModelAction.findUnique) {
const result = ts.unionType<ts.TypeBuilder>(getResultType(modelName, actionName, context)).addVariant(ts.nullType)
return getFluentWrapper(modelName, context, result, ts.nullType)
const result = ts.unionType<ts.TypeBuilder>(getResultType(modelName, actionName)).addVariant(ts.nullType)
return getFluentWrapper(modelName, result, ts.nullType)
}

return getFluentWrapper(modelName, context, getResultType(modelName, actionName, context))
return getFluentWrapper(modelName, getResultType(modelName, actionName))
}

function getFluentWrapper(
modelName: string,
context: GenerateContext,
resultType: ts.TypeBuilder,
nullType: ts.TypeBuilder = ts.neverType,
) {
const result = ts
function getFluentWrapper(modelName: string, resultType: ts.TypeBuilder, nullType: ts.TypeBuilder = ts.neverType) {
return ts
.namedType(fluentWrapperName(modelName))
.addGenericArgument(resultType)
.addGenericArgument(nullType)
.addGenericArgument(extArgsParam.toArgument())
if (context.isPreviewFeatureOn('omitApi')) {
result.addGenericArgument(ts.namedType('ClientOptions'))
}
return result
.addGenericArgument(ts.namedType('ClientOptions'))
}

function getResultType(modelName: string, actionName: DMMF.ModelAction, context: GenerateContext) {
const result = ts
function getResultType(modelName: string, actionName: DMMF.ModelAction) {
return ts
.namedType('$Result.GetResult')
.addGenericArgument(ts.namedType(getPayloadName(modelName)).addGenericArgument(extArgsParam.toArgument()))
.addGenericArgument(ts.namedType('T'))
.addGenericArgument(ts.stringLiteral(actionName))
if (context.isPreviewFeatureOn('omitApi')) {
result.addGenericArgument(ts.namedType('ClientOptions'))
}
return result
.addGenericArgument(ts.namedType('ClientOptions'))
}

function buildFluentWrapperDefinition(modelName: string, outputType: DMMF.OutputType, context: GenerateContext) {
Expand All @@ -785,12 +769,9 @@ function buildFluentWrapperDefinition(modelName: string, outputType: DMMF.Output
.addGenericParameter(ts.genericParameter('T'))
.addGenericParameter(ts.genericParameter('Null').default(ts.neverType))
.addGenericParameter(extArgsParam)
.addGenericParameter(ts.genericParameter('ClientOptions').default(ts.objectType()))
.extends(ts.prismaPromise(ts.namedType('T')))

if (context.isPreviewFeatureOn('omitApi')) {
definition.addGenericParameter(ts.genericParameter('ClientOptions').default(ts.objectType()))
}

definition.add(ts.property(ts.toStringTag, ts.stringLiteral('PrismaPromise')).readonly())
definition.addMultiple(
outputType.fields
Expand All @@ -815,7 +796,6 @@ function buildFluentWrapperDefinition(modelName: string, outputType: DMMF.Output
modelName: field.outputType.type,
actionName: field.outputType.isList ? DMMF.ModelAction.findMany : DMMF.ModelAction.findUniqueOrThrow,
isChaining: true,
context: context,
isNullable: field.isNullable,
}),
)
Expand Down
64 changes: 28 additions & 36 deletions packages/client/src/generation/TSClient/PrismaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,19 @@ function clientExtensionsDefinitions(context: GenerateContext) {
return [typeMap, ts.stringify(define)].join('\n')
}

function extendsPropertyDefinition(context: GenerateContext) {
function extendsPropertyDefinition() {
const extendsDefinition = ts
.namedType('$Extensions.ExtendsHook')
.addGenericArgument(ts.stringLiteral('extends'))
.addGenericArgument(ts.namedType('Prisma.TypeMapCb'))
.addGenericArgument(ts.namedType('ExtArgs'))
if (context.isPreviewFeatureOn('omitApi')) {
extendsDefinition
.addGenericArgument(
ts
.namedType('$Utils.Call')
.addGenericArgument(ts.namedType('Prisma.TypeMapCb'))
.addGenericArgument(ts.objectType().add(ts.property('extArgs', ts.namedType('ExtArgs')))),
)
.addGenericArgument(ts.namedType('ClientOptions'))
}
.addGenericArgument(
ts
.namedType('$Utils.Call')
.addGenericArgument(ts.namedType('Prisma.TypeMapCb'))
.addGenericArgument(ts.objectType().add(ts.property('extArgs', ts.namedType('ExtArgs')))),
)
.addGenericArgument(ts.namedType('ClientOptions'))
return ts.stringify(ts.property('$extends', extendsDefinition), { indentLevel: 1 })
}

Expand Down Expand Up @@ -501,7 +498,7 @@ ${[
runCommandRawDefinition(this.context),
metricDefinition(this.context),
applyPendingMigrationsDefinition.bind(this)(),
extendsPropertyDefinition(this.context),
extendsPropertyDefinition(),
]
.filter((d) => d !== null)
.join('\n')
Expand All @@ -515,10 +512,7 @@ ${[
if (methodName === 'constructor') {
methodName = '["constructor"]'
}
const generics = ['ExtArgs']
if (this.context.isPreviewFeatureOn('omitApi')) {
generics.push('ClientOptions')
}
const generics = ['ExtArgs', 'ClientOptions']
return `\
/**
* \`prisma.${methodName}\`: Exposes CRUD operations for the **${m.model}** model.
Expand All @@ -537,14 +531,13 @@ get ${methodName}(): Prisma.${m.model}Delegate<${generics.join(', ')}>;`
}
public toTS(): string {
const clientOptions = this.buildClientOptions()
const isOmitEnabled = this.context.isPreviewFeatureOn('omitApi')

return `${new Datasources(this.internalDatasources).toTS()}
${clientExtensionsDefinitions(this.context)}
export type DefaultPrismaClient = PrismaClient
export type ErrorFormat = 'pretty' | 'colorless' | 'minimal'
${ts.stringify(ts.moduleExport(clientOptions))}
${isOmitEnabled ? ts.stringify(globalOmitConfig(this.context.dmmf)) : ''}
${ts.stringify(globalOmitConfig(this.context.dmmf))}
/* Types for Logging */
export type LogLevel = 'info' | 'query' | 'warn' | 'error'
Expand Down Expand Up @@ -695,24 +688,23 @@ export type TransactionClient = Omit<Prisma.DefaultPrismaClient, runtime.ITXClie
)
}

if (this.context.isPreviewFeatureOn('omitApi')) {
clientOptions.add(
ts.property('omit', ts.namedType('Prisma.GlobalOmitConfig')).optional().setDocComment(ts.docComment`
Global configuration for omitting model fields by default.
@example
\`\`\`
const prisma = new PrismaClient({
omit: {
user: {
password: true
}
}
})
\`\`\`
`),
)
}
clientOptions.add(
ts.property('omit', ts.namedType('Prisma.GlobalOmitConfig')).optional().setDocComment(ts.docComment`
Global configuration for omitting model fields by default.
@example
\`\`\`
const prisma = new PrismaClient({
omit: {
user: {
password: true
}
}
})
\`\`\`
`),
)

return clientOptions
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,6 @@ test('omit', () => {
serialize({
modelName: 'User',
action: 'findMany',
previewFeatures: ['omitApi'],
args: { omit: { name: true } },
}),
).toMatchInlineSnapshot(`
Expand All @@ -1124,7 +1123,6 @@ test('omit(false)', () => {
serialize({
modelName: 'User',
action: 'findMany',
previewFeatures: ['omitApi'],
args: { omit: { name: false } },
}),
).toMatchInlineSnapshot(`
Expand All @@ -1148,7 +1146,6 @@ test('omit + include', () => {
serialize({
modelName: 'User',
action: 'findMany',
previewFeatures: ['omitApi'],
args: { include: { posts: true }, omit: { name: true } },
}),
).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -1179,7 +1176,6 @@ test('nested omit', () => {
serialize({
modelName: 'User',
action: 'findMany',
previewFeatures: ['omitApi'],
args: { include: { posts: { omit: { title: true } } } },
}),
).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -1210,7 +1206,6 @@ test('exclusion with extension', () => {
serialize({
modelName: 'User',
action: 'findMany',
previewFeatures: ['omitApi'],
args: { omit: { name: true } },
extensions: MergedExtensionsList.single({
result: {
Expand Down Expand Up @@ -1243,7 +1238,6 @@ test('exclusion with extension while excluding computed field too', () => {
serialize({
modelName: 'User',
action: 'findMany',
previewFeatures: ['omitApi'],
args: { omit: { name: true, fullName: true } },
extensions: MergedExtensionsList.single({
result: {
Expand Down Expand Up @@ -1277,7 +1271,6 @@ test('globalOmit', () => {
serialize({
modelName: 'User',
action: 'findMany',
previewFeatures: ['omitApi'],
globalOmit: {
user: {
name: true,
Expand Down Expand Up @@ -1305,7 +1298,6 @@ test('globalOmit + local omit', () => {
serialize({
modelName: 'User',
action: 'findMany',
previewFeatures: ['omitApi'],
args: {
omit: {
name: false,
Expand Down Expand Up @@ -1338,7 +1330,6 @@ test('globalOmit + local select', () => {
serialize({
modelName: 'User',
action: 'findMany',
previewFeatures: ['omitApi'],
args: {
select: {
name: true,
Expand Down Expand Up @@ -1369,7 +1360,6 @@ test('nested globalOmit (include)', () => {
serialize({
modelName: 'User',
action: 'findMany',
previewFeatures: ['omitApi'],
args: { include: { posts: true } },
globalOmit: {
post: {
Expand Down Expand Up @@ -1405,7 +1395,6 @@ test('nested globalOmit (select)', () => {
serialize({
modelName: 'User',
action: 'findMany',
previewFeatures: ['omitApi'],
args: { select: { posts: true } },
globalOmit: {
post: {
Expand Down Expand Up @@ -1552,7 +1541,6 @@ test(`Prisma.skip in omit`, () => {
modelName: 'User',
action: 'findMany',
args: { where: { name: 'Steve' }, omit: { password: true, name: skip } },
previewFeatures: ['omitApi'],
}),
).toMatchInlineSnapshot(`
"{
Expand Down
Loading

0 comments on commit e6ef63b

Please sign in to comment.