Skip to main content

SELECT statements

To read more about how SQLX-TS translates query parameters, visit this page

Binary operations

If you have the follow MySQL query in typescript

// example
const someQuery = sql`
SELECT *
FROM items
WHERE points > ?;

would generate

export type SomeQueryParams = []

export interface ISomeQueryResult {
id: string
points: number
}

export interface ISomeQueryQuery {
params: SomeQueryParams;
result: ISomeQueryResult;
}

IN list query

If you have the following MySQL query

// example
const someQuery = sql`
SELECT *
FROM items
WHERE id IN (?);

would generate following typescript types

export type SomeQueryParams = [Array<number>]

export interface ISomeQueryResult {
id: string
points: number
}

export interface ISomeQueryQuery {
params: SomeQueryParams;
result: ISomeQueryResult;
}

Subqueries

Query params within subqueries are interpreted as well. If you have the following MySQL query

const someQuery = sql`
SELECT id, points
FROM items
WHERE id IN (
SELECT id FROM items
WHERE
points > ?
AND id IN (SELECT id FROM items WHERE food_type = ?)
)
AND points < ?
`

would generate following type definitions

export type SomeQueryParams = [number, string, number]

export interface ISomeQueryResult {
id: string
points: number
}

export interface ISomeQueryQuery {
params: SomeQueryParams;
result: ISomeQueryResult;
}

Note that QueryParams array respects the order of params present in the query above