-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Query API and SDK #3060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
matt-aitken
wants to merge
14
commits into
main
Choose a base branch
from
query-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+687
−23
Draft
Query API and SDK #3060
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4982428
API endpoint for running TRQL queries
matt-aitken 967d653
SDK function for query
matt-aitken fccfb2a
Always return an object
matt-aitken 9e7b152
Rework the API/SDK so we can get nice spans
matt-aitken adccecd
Fix for csv discriminated union
matt-aitken 2528714
Fix for bad types in the example
matt-aitken 8c10ef8
Nice type support
matt-aitken c26723a
Do a 500 error if it's not a QueryError
matt-aitken c6b662b
Removed unused format var
matt-aitken f105b8f
Fix for wrong type
matt-aitken 885d17e
Improved the JSDocs
matt-aitken 00a7fa7
Changeset
matt-aitken c72e366
Unrelated: fix for unused RunIcon line
matt-aitken c02b564
Change version of @trigger.dev/sdk to minor
matt-aitken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --- | ||
| "@trigger.dev/sdk": minor | ||
| --- | ||
|
|
||
| Added `query.execute()` which lets you query your Trigger.dev data using TRQL (Trigger Query Language) and returns results as typed JSON rows or CSV. It supports configurable scope (environment, project, or organization), time filtering via `period` or `from`/`to` ranges, and a `format` option for JSON or CSV output. | ||
|
|
||
| ```typescript | ||
| import { query } from "@trigger.dev/sdk"; | ||
| import type { QueryTable } from "@trigger.dev/sdk"; | ||
|
|
||
| // Basic untyped query | ||
| const result = await query.execute("SELECT run_id, status FROM runs LIMIT 10"); | ||
|
|
||
| // Type-safe query using QueryTable to pick specific columns | ||
| const typedResult = await query.execute<QueryTable<"runs", "run_id" | "status" | "triggered_at">>( | ||
| "SELECT run_id, status, triggered_at FROM runs LIMIT 10" | ||
| ); | ||
| typedResult.results.forEach(row => { | ||
| console.log(row.run_id, row.status); // Fully typed | ||
| }); | ||
|
|
||
| // Aggregation query with inline types | ||
| const stats = await query.execute<{ status: string; count: number }>( | ||
| "SELECT status, COUNT(*) as count FROM runs GROUP BY status", | ||
| { scope: "project", period: "30d" } | ||
| ); | ||
|
|
||
| // CSV export | ||
| const csv = await query.execute( | ||
| "SELECT run_id, status FROM runs", | ||
| { format: "csv", period: "7d" } | ||
| ); | ||
| console.log(csv.results); // Raw CSV string | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { json } from "@remix-run/server-runtime"; | ||
| import { QueryError } from "@internal/clickhouse"; | ||
| import { z } from "zod"; | ||
| import { createActionApiRoute } from "~/services/routeBuilders/apiBuilder.server"; | ||
| import { executeQuery, type QueryScope } from "~/services/queryService.server"; | ||
| import { logger } from "~/services/logger.server"; | ||
| import { rowsToCSV } from "~/utils/dataExport"; | ||
|
|
||
| const BodySchema = z.object({ | ||
| query: z.string(), | ||
| scope: z.enum(["organization", "project", "environment"]).default("environment"), | ||
| period: z.string().nullish(), | ||
| from: z.string().nullish(), | ||
| to: z.string().nullish(), | ||
| format: z.enum(["json", "csv"]).default("json"), | ||
| }); | ||
|
|
||
| const { action, loader } = createActionApiRoute( | ||
| { | ||
| body: BodySchema, | ||
| corsStrategy: "all", | ||
| }, | ||
| async ({ body, authentication }) => { | ||
| const { query, scope, period, from, to, format } = body; | ||
| const env = authentication.environment; | ||
|
|
||
| const queryResult = await executeQuery({ | ||
| name: "api-query", | ||
| query, | ||
| scope: scope as QueryScope, | ||
| organizationId: env.organization.id, | ||
| projectId: env.project.id, | ||
| environmentId: env.id, | ||
| period, | ||
| from, | ||
| to, | ||
| history: { | ||
| source: "API", | ||
| }, | ||
| }); | ||
|
|
||
| if (!queryResult.success) { | ||
| const message = | ||
| queryResult.error instanceof QueryError | ||
| ? queryResult.error.message | ||
| : "An unexpected error occurred while executing the query."; | ||
|
|
||
| logger.error("Query API error", { | ||
| error: queryResult.error, | ||
| query, | ||
| }); | ||
|
|
||
| return json( | ||
| { error: message }, | ||
| { status: queryResult.error instanceof QueryError ? 400 : 500 } | ||
| ); | ||
| } | ||
|
|
||
| const { result, periodClipped, maxQueryPeriod } = queryResult; | ||
|
|
||
| if (format === "csv") { | ||
| const csv = rowsToCSV(result.rows, result.columns); | ||
|
|
||
| return json({ | ||
| format: "csv", | ||
| results: csv, | ||
| }); | ||
| } | ||
|
|
||
| return json({ | ||
| format: "json", | ||
| results: result.rows, | ||
| }); | ||
| } | ||
| ); | ||
|
|
||
| export { action, loader }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.