|
| 1 | +import type { |
| 2 | + ApiRequestOptions, |
| 3 | + QueryExecuteResponseBody, |
| 4 | + QueryExecuteCSVResponseBody, |
| 5 | +} from "@trigger.dev/core/v3"; |
| 6 | +import { apiClientManager, mergeRequestOptions } from "@trigger.dev/core/v3"; |
| 7 | +import { tracer } from "./tracer.js"; |
| 8 | + |
| 9 | +export type QueryScope = "environment" | "project" | "organization"; |
| 10 | +export type QueryFormat = "json" | "csv"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Options for executing a TSQL query |
| 14 | + */ |
| 15 | +export type QueryOptions<TFormat extends QueryFormat | undefined = QueryFormat | undefined> = { |
| 16 | + /** |
| 17 | + * The scope of the query - determines what data is accessible |
| 18 | + * - "environment": Current environment only (default) |
| 19 | + * - "project": All environments in the project |
| 20 | + * - "organization": All projects in the organization |
| 21 | + * |
| 22 | + * @default "environment" |
| 23 | + */ |
| 24 | + scope?: "environment" | "project" | "organization"; |
| 25 | + |
| 26 | + /** |
| 27 | + * Time period to query (e.g., "7d", "30d", "1h") |
| 28 | + * Cannot be used with `from` or `to` |
| 29 | + */ |
| 30 | + period?: string; |
| 31 | + |
| 32 | + /** |
| 33 | + * Start of time range (ISO 8601 timestamp) |
| 34 | + * Must be used with `to` |
| 35 | + */ |
| 36 | + from?: string; |
| 37 | + |
| 38 | + /** |
| 39 | + * End of time range (ISO 8601 timestamp) |
| 40 | + * Must be used with `from` |
| 41 | + */ |
| 42 | + to?: string; |
| 43 | + |
| 44 | + /** |
| 45 | + * Response format |
| 46 | + * - "json": Returns structured data (default) |
| 47 | + * - "csv": Returns CSV string |
| 48 | + * |
| 49 | + * @default "json" |
| 50 | + */ |
| 51 | + format?: TFormat; |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + * Result type that automatically narrows based on the format option |
| 56 | + * @template TFormat - The format type (json or csv) |
| 57 | + * @template TRow - The shape of each row in the result set |
| 58 | + */ |
| 59 | +export type QueryResult< |
| 60 | + TFormat extends QueryFormat | undefined = undefined, |
| 61 | + TRow extends Record<string, any> = Record<string, any> |
| 62 | +> = TFormat extends "csv" |
| 63 | + ? QueryExecuteCSVResponseBody |
| 64 | + : TFormat extends "json" |
| 65 | + ? { rows: Array<TRow> } |
| 66 | + : TFormat extends undefined |
| 67 | + ? { rows: Array<TRow> } |
| 68 | + : { rows: Array<TRow> } | QueryExecuteCSVResponseBody; |
| 69 | + |
| 70 | +/** |
| 71 | + * Execute a TSQL query against your Trigger.dev data |
| 72 | + * |
| 73 | + * @template TFormat - The format of the response (inferred from options) |
| 74 | + * @param {string} tsql - The TSQL query string to execute |
| 75 | + * @param {QueryOptions<TFormat>} [options] - Optional query configuration |
| 76 | + * @param {ApiRequestOptions} [requestOptions] - Optional API request configuration |
| 77 | + * @returns A promise that resolves with the query results |
| 78 | + * |
| 79 | + * @example |
| 80 | + * ```typescript |
| 81 | + * // Basic query with defaults (environment scope, json format) |
| 82 | + * const result = await query.execute("SELECT * FROM runs LIMIT 10"); |
| 83 | + * console.log(result.rows); |
| 84 | + * |
| 85 | + * // Query with custom period |
| 86 | + * const lastMonth = await query.execute( |
| 87 | + * "SELECT COUNT(*) as count FROM runs", |
| 88 | + * { period: "30d" } |
| 89 | + * ); |
| 90 | + * |
| 91 | + * // Query with custom date range |
| 92 | + * const januaryRuns = await query.execute( |
| 93 | + * "SELECT * FROM runs", |
| 94 | + * { |
| 95 | + * from: "2025-01-01T00:00:00Z", |
| 96 | + * to: "2025-02-01T00:00:00Z" |
| 97 | + * } |
| 98 | + * ); |
| 99 | + * |
| 100 | + * // Organization-wide query |
| 101 | + * const orgStats = await query.execute( |
| 102 | + * "SELECT project, COUNT(*) as count FROM runs GROUP BY project", |
| 103 | + * { scope: "organization", period: "7d" } |
| 104 | + * ); |
| 105 | + * |
| 106 | + * // Export as CSV |
| 107 | + * const csvData = await query.execute( |
| 108 | + * "SELECT * FROM runs", |
| 109 | + * { format: "csv", period: "7d" } |
| 110 | + * ); |
| 111 | + * // csvData is a string containing CSV |
| 112 | + * ``` |
| 113 | + */ |
| 114 | +function execute<TFormat extends QueryFormat | undefined = undefined>( |
| 115 | + tsql: string, |
| 116 | + options?: QueryOptions<TFormat>, |
| 117 | + requestOptions?: ApiRequestOptions |
| 118 | +): Promise<QueryResult<TFormat>> { |
| 119 | + const apiClient = apiClientManager.clientOrThrow(); |
| 120 | + |
| 121 | + const $requestOptions = mergeRequestOptions( |
| 122 | + { |
| 123 | + tracer, |
| 124 | + name: "query.execute()", |
| 125 | + icon: "sparkles", |
| 126 | + attributes: { |
| 127 | + scope: options?.scope ?? "environment", |
| 128 | + format: options?.format ?? "json", |
| 129 | + }, |
| 130 | + }, |
| 131 | + requestOptions |
| 132 | + ); |
| 133 | + |
| 134 | + return apiClient.executeQuery(tsql, options, $requestOptions) as Promise<QueryResult<TFormat>>; |
| 135 | +} |
| 136 | + |
| 137 | +export const query = { |
| 138 | + execute, |
| 139 | +}; |
0 commit comments