Created
November 13, 2022 08:01
-
-
Save marklawlor/7b9a998f51dd1e972859aa9bb93a316d to your computer and use it in GitHub Desktop.
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
/* eslint-disable @typescript-eslint/ban-types */ | |
/* eslint-disable @typescript-eslint/no-explicit-any */ | |
import type { Prisma, PrismaClient, PrismaPromise } from "@prisma/client"; | |
import type { CamelCase, Merge, SnakeCase, PascalCase } from "type-fest"; | |
import { ColumnType } from "kysely"; | |
export type KyselyOverrides = { | |
[Model in Prisma.ModelName]?: { | |
[P in keyof Table<Model>]?: ColumnType<any>; | |
}; | |
}; | |
export type NamingConvention = "snake_case" | "camelCase" | "PascalCase"; | |
type ChangeCase<Value, T extends NamingConvention> = T extends "PascalCase" | |
? PascalCase<Value> | |
: T extends "camelCase" | |
? CamelCase<Value> | |
: T extends "snake_case" | |
? SnakeCase<Value> | |
: Value; | |
// A deep version of ChangeCaseProperties that respects Prisma scalar types | |
type ChangeCasePropertiesDeep< | |
Value, | |
Convention extends NamingConvention | |
> = Value extends Prisma.Decimal | |
? Value | |
: Value extends Buffer | |
? Value | |
: Value extends Function | Date | RegExp | |
? Value | |
: Value extends Array<infer U> | |
? Array<ChangeCasePropertiesDeep<U, Convention>> | |
: Value extends Set<infer U> | |
? Set<ChangeCasePropertiesDeep<U, Convention>> | |
: { | |
[K in keyof Value as ChangeCase<K, Convention>]: ChangeCasePropertiesDeep< | |
Value[K], | |
Convention | |
>; | |
}; | |
// Hack to get the raw table types. We extract the ReturnType from a "findFirst" query | |
type Table<TableName extends Prisma.ModelName> = ReturnType< | |
PrismaClient[CamelCase<TableName>]["findFirst"] | |
> extends PrismaPromise<infer T> | |
? NonNullable<T> | |
: never; | |
export type Database< | |
Overrides extends KyselyOverrides = Record<string, never>, | |
TableConvention extends NamingConvention = "PascalCase", | |
ColumnConvention extends NamingConvention = "camelCase" | |
> = { | |
[P in Prisma.ModelName as ChangeCase< | |
P, | |
TableConvention | |
>]: Overrides extends Record<string, never> | |
? ChangeCasePropertiesDeep<Table<P>, ColumnConvention> | |
: Merge< | |
ChangeCasePropertiesDeep<Table<P>, ColumnConvention>, | |
ChangeCase<Overrides[P], ColumnConvention> | |
>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment