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 Cruz13mo 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
Igal13mo 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 Cruz13mo 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
Igal13mo 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
More Posts
insert into with mix of static and table valuesHow might I execute an insert into that combines JS-side values with a select, like: ``` INSERT INTOMigration error "TypeError: Cannot read properties of undefined (reading 'getExecutor')"I am trying to run a migration using Kysely, and its returning this error: ``` file:///Users/brunocrwhere clause with lengthHow do I have a where clause with a length, e.g. `select * from data where length(prodcode) = 3`show generated sqlhow can i see what the generated sql is for a kysely query? e.g. ``` result = await db.selectFrom("Property does not exist on typeHello. I'm trying to run the following query: ```javascript result = await db.selectFrom("data") Correct type definitions for function receiving builderI have something like this and I was wondering if it's the correct way to type my helper function. Transform Postgres array into JS arrayHey, I was wondering if there's a helper or something to transform Postgres arrays `{one, two}` intoNoob Question: SQL INSERT that combines static values with a SELECT statementHey Kysely community! I'm just getting into Kysely and I have a question about how I could run the fIs there a way to execute an ExpressionBuilder?Using the expression hasDogNamed example from the docs ``` const eb = expressionBuilder<DB, 'personArgument of type 'string' is not assignable to parameter of type 'DynamicReferenceBuilder<never>'.The following statement, is giving me the error on the title ```ts fn("round", [fn.min("ld.price_btc