Skip to content

Commit

Permalink
Merge pull request #1230 from 2coo/add-prisma-types-from-client-utili…
Browse files Browse the repository at this point in the history
…ty-type

Add PrismaTypesFromClient utility type
  • Loading branch information
hayes authored Jul 10, 2024
2 parents 6eab0a3 + a4ee664 commit da25da6
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions packages/plugin-prisma/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from '@pothos/core';
import { PrismaInterfaceRef, PrismaRef } from './interface-ref';
import type { PrismaObjectFieldBuilder } from './prisma-field-builder';
import type { Prisma } from '@prisma/client';

export interface PrismaDelegate {
findUniqueOrThrow?: (...args: any[]) => Promise<unknown>;
Expand Down Expand Up @@ -827,6 +828,59 @@ export type UniqueFieldsFromWhereUnique<T> = string &
: K
: never);

// Infer the type of a specific field from a given type
type InferField<T, N extends string> = T extends { [K in N]?: infer U } ? U : never;

// Check if a type is nullable
type IsNullable<T> = null extends T ? true : undefined extends T ? true : false;

// Capitalize the first letter of a string
type CapitalizeFirst<S extends string> = S extends `${infer F}${infer R}`
? `${Uppercase<F>}${R}`
: S;

// Find the field type in an object or array of objects
type FindField<T, F extends string> =
T extends Array<any> ? T[0][F] : T extends Record<string, unknown> ? T[F] : never;

// Infer relations for a given model
type InferRelations<Model> = {
[K in keyof Model]: {
Shape: Model[K] extends Array<any>
? FindField<Model[K], 'scalars'>[]
: FindField<Model[K], 'scalars'>;
Name: FindField<Model[K], 'name'>;
Nullable: IsNullable<Model[K]>;
};
};

// Create a list of keys that represent array relations
type InferRelationList<Model> = {
[K in keyof Model as Model[K] extends Array<any> ? K : never]: K;
};

// Extract model keys excluding those starting with '$'
type ExtractModelKeys<T> = {
[K in keyof T]: K extends `$${string}` ? never : K;
}[keyof T];

// Main type to generate Prisma types from the client
export type PrismaTypesFromClient<ClientType> = {
[K in ExtractModelKeys<ClientType> as CapitalizeFirst<K & string>]: {
Name: CapitalizeFirst<K & string>;
Shape: InferField<Prisma.Payload<ClientType[K], 'findUnique'>, 'scalars'>;
Include: InferField<Prisma.Args<ClientType[K], 'findUnique'>, 'include'>;
Select: InferField<Prisma.Args<ClientType[K], 'findUnique'>, 'select'>;
OrderBy: InferField<Prisma.Args<ClientType[K], 'findUnique'>, 'orderBy'>;
ListRelations: keyof InferRelationList<
InferField<Prisma.Payload<ClientType[K], 'findUnique'>, 'objects'>
>;
RelationName: keyof InferField<Prisma.Payload<ClientType[K], 'findUnique'>, 'objects'>;
Relations: InferRelations<InferField<Prisma.Payload<ClientType[K], 'findUnique'>, 'objects'>>;
};
};


interface DMMFField {
type: string;
kind: string;
Expand Down

0 comments on commit da25da6

Please sign in to comment.