@@ -13,7 +13,7 @@ export type QueryScope = "environment" | "project" | "organization";
1313export type QueryFormat = "json" | "csv" ;
1414
1515/**
16- * Options for executing a TSQL query
16+ * Options for executing a TRQL query
1717 */
1818export type QueryOptions = {
1919 /**
@@ -55,82 +55,76 @@ export type QueryOptions = {
5555} ;
5656
5757/**
58- * Execute a TSQL query and export as CSV
58+ * Execute a TRQL query and get the results as a CSV string.
59+ *
60+ * @param {string } query - The TRQL query string to execute
61+ * @param {QueryOptions & { format: "csv" } } options - Query options with `format: "csv"`
62+ * @param {ApiRequestOptions } [requestOptions] - Optional API request configuration
63+ * @returns A promise resolving to `{ format: "csv", results: string }` where `results` is the raw CSV text
64+ *
65+ * @example
66+ * ```typescript
67+ * const csvResult = await query.execute(
68+ * "SELECT run_id, status, triggered_at FROM runs",
69+ * { format: "csv", period: "7d" }
70+ * );
71+ * const lines = csvResult.results.split('\n');
72+ * ```
5973 */
6074function execute (
61- tsql : string ,
75+ query : string ,
6276 options : QueryOptions & { format : "csv" } ,
6377 requestOptions ?: ApiRequestOptions
6478) : Promise < { format : "csv" ; results : string } > ;
6579
6680/**
67- * Execute a TSQL query and return typed JSON rows
68- */
69- function execute < TRow extends Record < string , any > = Record < string , any > > (
70- tsql : string ,
71- options ?: Omit < QueryOptions , "format" > | ( QueryOptions & { format ?: "json" } ) ,
72- requestOptions ?: ApiRequestOptions
73- ) : Promise < { format : "json" ; results : Array < Prettify < TRow > > } > ;
74-
75- /**
76- * Execute a TSQL query against your Trigger.dev data
81+ * Execute a TRQL query and return typed JSON rows.
7782 *
78- * @template TRow - The shape of each row in the result set (provide for type safety )
79- * @param {string } tsql - The TSQL query string to execute
83+ * @template TRow - The shape of each row in the result set. Use { @link QueryTable} for type-safe column access (e.g. `QueryTable<"runs", "status" | "run_id">` )
84+ * @param {string } query - The TRQL query string to execute
8085 * @param {QueryOptions } [options] - Optional query configuration
8186 * @param {ApiRequestOptions } [requestOptions] - Optional API request configuration
82- * @returns A promise that resolves with the query results
87+ * @returns A promise resolving to `{ format: "json", results: Array<TRow> }`
8388 *
8489 * @example
8590 * ```typescript
8691 * // Basic query with defaults (environment scope, json format)
8792 * const result = await query.execute("SELECT run_id, status FROM runs LIMIT 10");
88- * console.log(result.format); // "json"
8993 * console.log(result.results); // Array<Record<string, any>>
9094 *
91- * // Type-safe query with row type
92- * type RunRow = { id: string; status: string; duration: number };
93- * const typedResult = await query.execute<RunRow>(
95+ * // Type-safe query using QueryTable with specific columns
96+ * const typedResult = await query.execute<QueryTable<"runs", "run_id" | "status" | "triggered_at">>(
9497 * "SELECT run_id, status, triggered_at FROM runs LIMIT 10"
9598 * );
9699 * typedResult.results.forEach(row => {
97- * console.log(row.id , row.status); // Fully typed!
100+ * console.log(row.run_id , row.status); // Fully typed!
98101 * });
99102 *
100- * // Inline type for aggregation query
103+ * // Inline type for aggregation queries
101104 * const stats = await query.execute<{ status: string; count: number }>(
102105 * "SELECT status, COUNT(*) as count FROM runs GROUP BY status"
103106 * );
104107 * stats.results.forEach(row => {
105108 * console.log(row.status, row.count); // Fully type-safe
106109 * });
107110 *
108- * // Query with custom period
109- * const lastMonth = await query.execute(
111+ * // Query with a custom time period
112+ * const recent = await query.execute(
110113 * "SELECT COUNT(*) as count FROM runs",
111114 * { period: "3d" }
112115 * );
113- * console.log(lastMonth.results[0].count); // Type-safe access
114- *
115- * // Export as CSV - automatically narrowed!
116- * const csvResult = await query.execute(
117- * "SELECT * FROM runs",
118- * { format: "csv", period: "1d" }
119- * );
120- * console.log(csvResult.format); // "csv"
121- * const lines = csvResult.results.split('\n'); // ✓ results is string
122- *
123- * // Discriminated union - can check format at runtime
124- * const dynamicResult = await query.execute("SELECT * FROM runs");
125- * if (dynamicResult.format === "json") {
126- * dynamicResult.results.forEach(row => console.log(row)); // ✓ Typed as array
127- * } else {
128- * console.log(dynamicResult.results.length); // ✓ Typed as string
129- * }
116+ * console.log(recent.results[0].count);
130117 * ```
131118 */
132119function execute < TRow extends Record < string , any > = Record < string , any > > (
133- tsql : string ,
120+ query : string ,
121+ options ?: Omit < QueryOptions , "format" > | ( QueryOptions & { format ?: "json" } ) ,
122+ requestOptions ?: ApiRequestOptions
123+ ) : Promise < { format : "json" ; results : Array < Prettify < TRow > > } > ;
124+
125+ // Implementation
126+ function execute < TRow extends Record < string , any > = Record < string , any > > (
127+ query : string ,
134128 options ?: QueryOptions ,
135129 requestOptions ?: ApiRequestOptions
136130) : Promise < { format : "json" ; results : Array < TRow > } | { format : "csv" ; results : string } > {
@@ -144,7 +138,7 @@ function execute<TRow extends Record<string, any> = Record<string, any>>(
144138 attributes : {
145139 scope : options ?. scope ?? "environment" ,
146140 format : options ?. format ?? "json" ,
147- query : tsql ,
141+ query,
148142 period : options ?. period ,
149143 from : options ?. from ,
150144 to : options ?. to ,
@@ -153,7 +147,7 @@ function execute<TRow extends Record<string, any> = Record<string, any>>(
153147 requestOptions
154148 ) ;
155149
156- return apiClient . executeQuery ( tsql , options , $requestOptions ) . then ( ( response ) => {
150+ return apiClient . executeQuery ( query , options , $requestOptions ) . then ( ( response ) => {
157151 return response ;
158152 } ) as Promise < { format : "json" ; results : Array < TRow > } | { format : "csv" ; results : string } > ;
159153}
0 commit comments