Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save petrenkoVitaliy/d310719f87903373f1382baf17b1e13d to your computer and use it in GitHub Desktop.
Save petrenkoVitaliy/d310719f87903373f1382baf17b1e13d to your computer and use it in GitHub Desktop.
export type Conditional<T extends boolean, A1, A2> = T extends true ? A1 : A2;
export type Argument<Fn extends (args: any) => any> = Parameters<Fn>[0];
//-----
private UpdateMethodType: typeof this.prisma.articleVersion.update;
update<T extends boolean>(options: {
code: string;
actual?: true | null;
archived?: boolean;
enabled?: boolean;
isExtended?: T;
}) {
const updateOptions = {
where: {
code: options.code,
},
data: {
actual: options.actual,
archived: options.archived,
enabled: options.enabled,
},
};
const include = {
schema: {
include: {
body: true,
header: true,
},
},
};
return this.updateExtended({
updateOptions,
include,
isExtended: options.isExtended,
});
}
private updateExtended<
T extends boolean,
P extends Pick<Argument<typeof this.UpdateMethodType>, 'data' | 'where'>,
R extends Argument<typeof this.UpdateMethodType>['include'],
>(options: { updateOptions: P; isExtended?: T; include: R }) {
const { updateOptions, include } = options;
return this.prisma.articleVersion.update({
...updateOptions,
include: options.isExtended ? include : null,
}) as Conditional<
T,
ReturnType<typeof this.UpdateMethodType<typeof updateOptions & { include: typeof include }>>,
ReturnType<typeof this.UpdateMethodType<typeof updateOptions>>
>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment