How to write a generic method in typescript? #6863
-
import { PrismaClient } from '@prisma/client';
export const client = new PrismaClient();
/**
* Delete
* @param {string} id
* @param {string} db
* @returns {boolean}
*/
export function deleteById(id: string, db: string): Promise<boolean> {
// how to declare the type of db?
return client[db]
.deleteMany({
where: {
id
}
})
.then((result) => result.count === 1);
} Error message:
trial2: function isValidKey(key: string): key is keyof typeof client {
return key in client;
}
/**
* 删除
* @param {string} id
* @returns {boolean}
*/
export function deleteById(id: string, db: string): Promise<boolean> {
if (isValidKey(db)) {
return client[db]
.deleteMany({
where: {
id
}
})
.then((result) => result.count === 1);
}
return Promise.resolve(false);
} errors:
trial3: enum ValidDatabases {
'user',
'orgnization'
}
function isValidKey(key: ValidDatabases): key is keyof typeof client {
return key in client;
}
/**
* 删除
* @param {string} id
* @returns {boolean}
*/
export function deleteById(id: string, db: ValidDatabases): Promise<boolean> {
if (isValidKey(db)) {
return client[db]
.deleteMany({
where: {
id
}
})
.then((result) => result.count === 1);
}
return Promise.resolve(false);
} errors:
p.s. i don't want use it this way: import { client } from './client';
export function deleteById(id: string, db: string): Promise<boolean> {
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-call */
return (client as any)[db]
.deleteMany({
where: {
id
}
})
.then((result: any) => result.count === 1);
} it makes codes uncontrollable |
Beta Was this translation helpful? Give feedback.
Answered by
electrovir
Sep 22, 2022
Replies: 2 comments 3 replies
-
export const client = new PrismaClient();
export const getClient = <T, K extends keyof T>(obj: T, key: K): T[K] => obj[key];
export enum DB_TABLE {
User = 'user',
Orgnization = 'orgnization',
UserOrgnization = 'userOrgnization',
Server = 'server',
ServerInstance = 'serverInstance',
Deployment = 'deployment',
Network = 'network'
}
export function deleteById(id: string, db: 'user'): Promise<boolean> {
return getClient(client, db)
.deleteMany({
where: {
id
}
})
.then((result) => result.count === 1);
}
export function deleteById2(id: string, db: 'server'): Promise<boolean> {
return getClient(client, db)
.deleteMany({
where: {
id
}
})
.then((result) => result.count === 1);
}
export function deleteById3(id: string, db: 'user' | 'server'): Promise<boolean> {
return getClient(client, db)
.deleteMany({
where: {
id
}
})
.then((result) => result.count === 1);
}
export function deleteById4(id: string, db: 'user' | 'server'): Promise<boolean> {
return getClient(client, db as 'user')
.deleteMany({
where: {
id
}
})
.then((result) => result.count === 1);
} 1,2,4 is ok. 3 errored with:
|
Beta Was this translation helpful? Give feedback.
0 replies
-
You've gotta have at least one edit: simplified import {Prisma, PrismaClient} from '@prisma/client';
export const client = new PrismaClient();
type GenericModel = Prisma.PostDelegate<any>;
function getModel<SpecificModelName extends ModelName>(
inputClient: PrismaClient,
modelName: SpecificModelName,
): GenericModel {
return inputClient[modelName] as GenericModel;
}
// https://github.com/prisma/prisma/issues/11940#issuecomment-1049572440
type IgnorePrismaBuiltins<PrismaClientKeyUnionGeneric extends string> =
string extends PrismaClientKeyUnionGeneric
? string
: PrismaClientKeyUnionGeneric extends ''
? PrismaClientKeyUnionGeneric
: PrismaClientKeyUnionGeneric extends `$${string}`
? never
: PrismaClientKeyUnionGeneric;
type ModelName = IgnorePrismaBuiltins<keyof typeof client>;
async function deleteById3(id: string, modelName: ModelName): Promise<boolean> {
const result = await getModel(client, modelName).deleteMany({
where: {
id,
},
});
return result.count === 1;
}
deleteById3('test-id', 'user'); |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
willin
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You've gotta have at least one
as
cast somewhere but here you go:edit: simplified