NicoFish
NicoFish
Explore posts from servers
PPrisma
Created by NicoFish on 10/9/2024 in #help-and-questions
Extending client not typesafe
I'm working in a NestJS project and need to extend the prisma client to replace all Date with string. I have this code:
import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '~/types/prisma-schema';

export type WithSerializedDates<T> = T extends Date
? string
: T extends Array<infer R>
? Array<WithSerializedDates<R>>
: T extends object
? { [K in keyof T]: WithSerializedDates<T[K]> }
: T;

export const withoutDates = <T>(model: T): WithSerializedDates<T> =>
JSON.parse(JSON.stringify(model)) as WithSerializedDates<T>;

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
async onModuleInit(): Promise<void> {
await this.$extends({
query: {
$allModels: {
$allOperations: async ({ query, args }) => {
return withoutDates(await query(args));
},
},
},
}).$connect();
}
}
import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '~/types/prisma-schema';

export type WithSerializedDates<T> = T extends Date
? string
: T extends Array<infer R>
? Array<WithSerializedDates<R>>
: T extends object
? { [K in keyof T]: WithSerializedDates<T[K]> }
: T;

export const withoutDates = <T>(model: T): WithSerializedDates<T> =>
JSON.parse(JSON.stringify(model)) as WithSerializedDates<T>;

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
async onModuleInit(): Promise<void> {
await this.$extends({
query: {
$allModels: {
$allOperations: async ({ query, args }) => {
return withoutDates(await query(args));
},
},
},
}).$connect();
}
}
But when using the client, it does not apply the extension types.
7 replies