Get return-type from QueryBuilder

I do not have a connection to a DB, I just use the query-builder for SQL generation.
I try to write a simple function, that takes a querybuilder query in and should return a object of the result type, as if I have awaited a DB with the query:
(I use another db to fetch data - but I need the type from drizzle)
const playerQuery = await queryBuilder
.select( /* doesnt need to be just a table */ )
.from( schema.players )
//.join(...)

const players = MockRequest( playerQuery )

export async function MockRequest<T extends MySqlSelectQueryBuilder>( query: T ): Promise<Awaited<T[]>>
{
const untypedData = {}
const data = untypedData as Awaited<T[]>

return data
}
const playerQuery = await queryBuilder
.select( /* doesnt need to be just a table */ )
.from( schema.players )
//.join(...)

const players = MockRequest( playerQuery )

export async function MockRequest<T extends MySqlSelectQueryBuilder>( query: T ): Promise<Awaited<T[]>>
{
const untypedData = {}
const data = untypedData as Awaited<T[]>

return data
}
This is the return-type:
MySqlSelectQueryBuilderBase<MySqlSelectQueryBuilderHKT, "players", {
id: MySqlColumn<{
name: "id";
tableName: "players";
dataType: "number";
columnType: "MySqlInt";
data: number;
driverParam: string | number;
notNull: true;
hasDefault: false;
enumValues: undefined;
baseColumn: never;
}, object>;
playerId: MySqlColumn<...>;
firstName: MySqlColumn<...>;
lastName: MySqlColumn<...>;
}, ... 6 more ..., {
...;
}>
MySqlSelectQueryBuilderBase<MySqlSelectQueryBuilderHKT, "players", {
id: MySqlColumn<{
name: "id";
tableName: "players";
dataType: "number";
columnType: "MySqlInt";
data: number;
driverParam: string | number;
notNull: true;
hasDefault: false;
enumValues: undefined;
baseColumn: never;
}, object>;
playerId: MySqlColumn<...>;
firstName: MySqlColumn<...>;
lastName: MySqlColumn<...>;
}, ... 6 more ..., {
...;
}>
When it should be:
{
id: number;
playerId: number | null;
firstName: string;
lastName: string;
}[]
{
id: number;
playerId: number | null;
firstName: string;
lastName: string;
}[]
How do I type this correctly?
3 Replies
Eli
Eli4mo ago
After digging through alot of issues, i fund this: https://github.com/drizzle-team/drizzle-orm/pull/1662 So it seems not possible (yet?)
GitHub
[All] Fix: export internal types by Angelelz · Pull Request #1662 ·...
Closes #561 and closes #1319. Added index.ts to src/query-builders to allow the build script to pick it up. Added query builders to the index.ts files to all the dialects.
Eli
Eli4mo ago
I managed to get a hacky solution, I would be able to infer the types, such that the caller-side has no types explicitly stated
export async function MockRequest
<T extends MySqlSelectQueryBuilder,R>
( qb: T )
: Promise<R[]>
{
const untypedData = {} // get data from somewhere else..
const data = untypedData as R[]

return data
}

// Ugly call:
const players = ( await MockRequest
<
ReturnType<typeof playerQuery.$dynamic>,
typeof playerQuery._.result
>
( playerQuery.$dynamic() )
)
export async function MockRequest
<T extends MySqlSelectQueryBuilder,R>
( qb: T )
: Promise<R[]>
{
const untypedData = {} // get data from somewhere else..
const data = untypedData as R[]

return data
}

// Ugly call:
const players = ( await MockRequest
<
ReturnType<typeof playerQuery.$dynamic>,
typeof playerQuery._.result
>
( playerQuery.$dynamic() )
)
Eli
Eli4mo ago
GitHub
[FEATURE]: Ability to specify selected fields dynamically · Issue #...
Describe what you want I have a model on my project that fetching users from the database. Each time I need different set of fields and I would like to define a function with generics so only the e...
Want results from more Discord servers?
Add your server