kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.22k stars 259 forks source link

Check that string is within <DB, TB keyof DB> and its fields #917

Closed MrVhek closed 5 months ago

MrVhek commented 5 months ago

I want to be able to use dynamic orderBy expression on kysely on my API with an attribute given by user, for that purpose I need to verify that the string given of format modelName.modelField is a valid model and attribute, how can I achieve that ?

I've tried using generic typing function<DB, TB keyof DB>(modelName: string, modelField: string) but I can't test it easily ? Is there any way of doing this ?

Thanks !

koskimas commented 5 months ago

All kysely knows about your schema and available columns is in the type-level. Since the types get completely erased during compilation to JavaScript, there's no way kysely can do that. You need to check against a list of fields you maintain separately in the code.