DT
Drizzle Teamā€¢2y ago
JT

What is the type for an .orderBy() parameter

Inside of a function, i have a query more complicated than this, but something like:
db.select().from(users).where(...).orderBy(asc(users.username))
db.select().from(users).where(...).orderBy(asc(users.username))
I'd like to be able to pass in the
asc(users.username)
asc(users.username)
part of that from the outside., but to do that I need to know what type i should be putting on the function parameter.
5 Replies
Dan
Danā€¢2y ago
You can take a look at the function definition in the .d.ts file for cases like this:
orderBy(builder: (aliases: TSelection) => ValueOrArray<AnyPgColumn | SQL | SQL.Aliased>): this;
orderBy(...columns: (AnyPgColumn | SQL)[]): this;
orderBy(builder: (aliases: TSelection) => ValueOrArray<AnyPgColumn | SQL | SQL.Aliased>): this;
orderBy(...columns: (AnyPgColumn | SQL)[]): this;
What this means is it accepts two sets of arguments: - a callback that returns a single value or an array of either AnyPgColumn, SQL or SQL.Aliased - spreaded arguments of either AnyPgColumn or SQL SQL instances are created with sql tag calls (which all the operator functions, like asc, do under the hood), SQL.Aliased is sql`query`.as('alias') You can just go with the second set of arguments if it satisfies your needs. Also, a good way to figure out an expression type is to assign it to a variable and looking at its type.
JT
JTOPā€¢2y ago
i did try that, before posting here, but it doesn't work TSelection is something you are creating outside orderBy same with ...columns so just trying to make something partial like this:
const passOrderBy = async (orderBy: (SQL | AnyMySqlColumn)[]) => await db.select().from(UserTable).orderBy(orderBy);
const passOrderBy = async (orderBy: (SQL | AnyMySqlColumn)[]) => await db.select().from(UserTable).orderBy(orderBy);
results with this:
No overload matches this call.
Overload 1 of 2, '(builder: (aliases: { id: MySqlColumn<{ data: string; driverParam: string | number; hasDefault: true; notNull: true; tableName: "users"; }>; createdAt: MySqlColumn<{ data: Date; driverParam: string | number; hasDefault: true; notNull: true; tableName: "users"; }>; ... 8 more ...; developer: MySqlColumn<...>; }) => ValueOrArray<...>): MySqlSelect<...>', gave the following error.
Argument of type '(AnyMySqlColumn<{}> | SQL<unknown>)[]' is not assignable to parameter of type '(aliases: { id: MySqlColumn<{ data: string; driverParam: string | number; hasDefault: true; notNull: true; tableName: "users"; }>; createdAt: MySqlColumn<{ data: Date; driverParam: string | number; hasDefault: true; notNull: true; tableName: "users"; }>; ... 8 more ...; developer: MySqlColumn<...>; }) => ValueOrArra...'.
Type '(AnyMySqlColumn<{}> | SQL<unknown>)[]' provides no match for the signature '(aliases: { id: MySqlColumn<{ data: string; driverParam: string | number; hasDefault: true; notNull: true; tableName: "users"; }>; createdAt: MySqlColumn<{ data: Date; driverParam: string | number; hasDefault: true; notNull: true; tableName: "users"; }>; ... 8 more ...; developer: MySqlColumn<...>; }): ValueOrArray<...>'.
Overload 2 of 2, '(...columns: (AnyMySqlColumn<{}> | SQL<unknown>)[]): MySqlSelect<"users", { id: MySqlColumn<{ data: string; driverParam: string | number; hasDefault: true; notNull: true; tableName: "users"; }>; ... 9 more ...; developer: MySqlColumn<...>; }, "single", Record<...>>', gave the following error.
Argument of type '(AnyMySqlColumn<{}> | SQL<unknown>)[]' is not assignable to parameter of type 'AnyMySqlColumn<{}> | SQL<unknown>'.ts(2769)
No overload matches this call.
Overload 1 of 2, '(builder: (aliases: { id: MySqlColumn<{ data: string; driverParam: string | number; hasDefault: true; notNull: true; tableName: "users"; }>; createdAt: MySqlColumn<{ data: Date; driverParam: string | number; hasDefault: true; notNull: true; tableName: "users"; }>; ... 8 more ...; developer: MySqlColumn<...>; }) => ValueOrArray<...>): MySqlSelect<...>', gave the following error.
Argument of type '(AnyMySqlColumn<{}> | SQL<unknown>)[]' is not assignable to parameter of type '(aliases: { id: MySqlColumn<{ data: string; driverParam: string | number; hasDefault: true; notNull: true; tableName: "users"; }>; createdAt: MySqlColumn<{ data: Date; driverParam: string | number; hasDefault: true; notNull: true; tableName: "users"; }>; ... 8 more ...; developer: MySqlColumn<...>; }) => ValueOrArra...'.
Type '(AnyMySqlColumn<{}> | SQL<unknown>)[]' provides no match for the signature '(aliases: { id: MySqlColumn<{ data: string; driverParam: string | number; hasDefault: true; notNull: true; tableName: "users"; }>; createdAt: MySqlColumn<{ data: Date; driverParam: string | number; hasDefault: true; notNull: true; tableName: "users"; }>; ... 8 more ...; developer: MySqlColumn<...>; }): ValueOrArray<...>'.
Overload 2 of 2, '(...columns: (AnyMySqlColumn<{}> | SQL<unknown>)[]): MySqlSelect<"users", { id: MySqlColumn<{ data: string; driverParam: string | number; hasDefault: true; notNull: true; tableName: "users"; }>; ... 9 more ...; developer: MySqlColumn<...>; }, "single", Record<...>>', gave the following error.
Argument of type '(AnyMySqlColumn<{}> | SQL<unknown>)[]' is not assignable to parameter of type 'AnyMySqlColumn<{}> | SQL<unknown>'.ts(2769)
on the orderBy parameter
Dan
Danā€¢2y ago
That's because that's a spread argument. You need to pass it like this: .orderBy(...orderBy)
JT
JTOPā€¢2y ago
i see, i'm dumb šŸ™‚ thank you
Sabbir Hossain Shuvo
Sabbir Hossain Shuvoā€¢11mo ago
how can I order row as random?? no need this I got solution,
orderBy(sql.raw("RANDOM()"))
orderBy(sql.raw("RANDOM()"))
Want results from more Discord servers?
Add your server