-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[minor] add generic prisma model type helpers to @augment-vir/prisma-…
…node-js
- Loading branch information
1 parent
eb1d2cf
commit 25dc027
Showing
15 changed files
with
115 additions
and
42 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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
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
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
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
10 changes: 10 additions & 0 deletions
10
packages/prisma-node-js/src/augments/generic-prisma-client.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** This is not completely filled out yet. */ | ||
export type GenericPrismaClient = Readonly< | ||
Record< | ||
string, | ||
{ | ||
findFirstOrThrow: () => Promise<any>; | ||
create: () => Promise<any>; | ||
} | ||
> | ||
>; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {EmptyObject} from 'type-fest'; | ||
import {GenericPrismaClient} from './generic-prisma-client'; | ||
|
||
/** Extracts all model names from the given Prisma client. */ | ||
export type ModelName<PrismaClient extends GenericPrismaClient> = keyof { | ||
[Model in keyof PrismaClient as PrismaClient[Model] extends {findFirstOrThrow: Function} | ||
? Model | ||
: never]: boolean; | ||
}; | ||
|
||
/** Extracts the creation data for a model from the given Prisma client. */ | ||
export type ModelCreationEntry< | ||
PrismaClient extends GenericPrismaClient, | ||
Model extends ModelName<PrismaClient>, | ||
> = NonNullable<NonNullable<Parameters<PrismaClient[Model]['create']>[0]>['data']>; | ||
|
||
/** For a given model, extract all the available "include" properties and set them all to true. */ | ||
export type IncludeAll< | ||
PrismaClient extends GenericPrismaClient, | ||
Model extends ModelName<PrismaClient>, | ||
> = | ||
NonNullable<NonNullable<Parameters<PrismaClient[Model]['findFirstOrThrow']>[0]>> extends { | ||
include?: infer IncludeArg; | ||
} | ||
? Record<Exclude<keyof NonNullable<IncludeArg>, '_count'>, true> | ||
: EmptyObject; | ||
|
||
export type BaseModel< | ||
PrismaClient extends GenericPrismaClient, | ||
Model extends ModelName<PrismaClient>, | ||
> = NonNullable<Awaited<ReturnType<PrismaClient[Model]['findFirstOrThrow']>>>; | ||
|
||
export type JoinedModel< | ||
PrismaClient extends GenericPrismaClient, | ||
Model extends ModelName<PrismaClient>, | ||
> = { | ||
[FieldName in Extract<keyof IncludeAll<PrismaClient, Model>, string>]: Omit< | ||
ReturnType<PrismaClient[Model]['findFirstOrThrow']>, | ||
'then' | 'catch' | 'finally' | ||
> extends Record<FieldName, () => Promise<infer Result>> | ||
? Result | ||
: `Error: failed to find relation for ${FieldName}`; | ||
}; | ||
|
||
export type FullModel< | ||
PrismaClient extends GenericPrismaClient, | ||
Model extends ModelName<PrismaClient>, | ||
> = JoinedModel<PrismaClient, Model> & BaseModel<PrismaClient, Model>; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './augments/generic-prisma-client'; | ||
export * from './augments/prisma-clients-generation'; | ||
export * from './augments/prisma-database'; | ||
export * from './augments/prisma-migrations'; | ||
export * from './augments/prisma-models'; |
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
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