K
Kysely•2y ago
Daniel Cruz

Extract OrderBy TS Keys

Hey I have the following query
const getBaseQuery = ({ offset, pageSize, slug }: GetBaseQuery) =>
db
.selectFrom((eb) =>
eb
.selectFrom(...)
.innerJoin(...)
.where(...)
.select([
...
])
.as(...)
)
.select([
...
])
.offset(offset)
.limit(pageSize);
const getBaseQuery = ({ offset, pageSize, slug }: GetBaseQuery) =>
db
.selectFrom((eb) =>
eb
.selectFrom(...)
.innerJoin(...)
.where(...)
.select([
...
])
.as(...)
)
.select([
...
])
.offset(offset)
.limit(pageSize);
And I'm trying to create a function that accepts that query and adds and orderBy expression based in some conditions
type BaseQuery = ReturnType<typeof getBaseQuery>;

type AddOrderBy = {
query: BaseQuery;
key?: 'what do I put here';
dir?: "asc" | "desc";
};

const addOrderBy = ({ query, key, dir }: AddOrderBy) => {
...
};
type BaseQuery = ReturnType<typeof getBaseQuery>;

type AddOrderBy = {
query: BaseQuery;
key?: 'what do I put here';
dir?: "asc" | "desc";
};

const addOrderBy = ({ query, key, dir }: AddOrderBy) => {
...
};
How can I get the accepted key value types so I can put them in my type declaration?
4 Replies
Daniel Cruz
Daniel CruzOP•2y ago
@Igal sorry for the ping 😅 I think this might had lost between other posts I was able to make it work in the following way. I'd like your input if this is a good approach or is there any helper I'm not aware of @Igal
type BaseQuery = ReturnType<typeof getBaseQuery>;

//My approach👇
type Key = Parameters<BaseQuery["orderBy"]>["0"];

type AddOrderBy = {
query: BaseQuery;
key?: Key;
dir?: "asc" | "desc";
};
type BaseQuery = ReturnType<typeof getBaseQuery>;

//My approach👇
type Key = Parameters<BaseQuery["orderBy"]>["0"];

type AddOrderBy = {
query: BaseQuery;
key?: Key;
dir?: "asc" | "desc";
};
Igal
Igal•2y ago
Hey 👋 Your approach could break if orderBy has overloads. You should infer DB, TB & O of query. And then StringReference<DB, TB> | keyof O roughly:
type AddOrderBy<DB, TB extends keyof DB, O> = {
query: SelectQueryBuilder<DB, TB, O>,
key?: StringReference<DB, tB> | keyof O,
dir?: 'asc' | 'desc'
}
type AddOrderBy<DB, TB extends keyof DB, O> = {
query: SelectQueryBuilder<DB, TB, O>,
key?: StringReference<DB, tB> | keyof O,
dir?: 'asc' | 'desc'
}
not sure what you're gaining out of this wrapper tho
Daniel Cruz
Daniel CruzOP•2y ago
I dropped the function wrapper 😅 but I still need to get the possible keys, since I want to build a type safe white list to pass to my orderBy
type BaseQuery = ReturnType<typeof getBaseQuery>;

type Key = Parameters<BaseQuery["orderBy"]>["0"];

type WhiteList = Key[];

const WHITELIST_ORDER_BY_KEYS: WhiteList = [
"name",
"price_btc",
"sold",
"marketplace",
];
type BaseQuery = ReturnType<typeof getBaseQuery>;

type Key = Parameters<BaseQuery["orderBy"]>["0"];

type WhiteList = Key[];

const WHITELIST_ORDER_BY_KEYS: WhiteList = [
"name",
"price_btc",
"sold",
"marketplace",
];
What do you mean by orderBy having overloads?
Igal
Igal•2y ago
kysely uses function overloads, might make Parameters<Fn> behave unexpectedly or break between versions if we change overloads order, or add new overloads.
Want results from more Discord servers?
Add your server