Skip to content

Commit ce9e37c

Browse files
committed
from and to can be Date or timestamps
1 parent c02b564 commit ce9e37c

File tree

2 files changed

+83
-38
lines changed

2 files changed

+83
-38
lines changed

packages/trigger-sdk/src/v3/query.ts

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ export type QueryOptions = {
3333
period?: string;
3434

3535
/**
36-
* Start of time range (ISO 8601 timestamp)
37-
* Must be used with `to`
36+
* Start of time range as a Date object or Unix timestamp in milliseconds.
37+
* Must be used with `to`.
3838
*/
39-
from?: string;
39+
from?: Date | number;
4040

4141
/**
42-
* End of time range (ISO 8601 timestamp)
43-
* Must be used with `from`
42+
* End of time range as a Date object or Unix timestamp in milliseconds.
43+
* Must be used with `from`.
4444
*/
45-
to?: string;
45+
to?: Date | number;
4646

4747
/**
4848
* Response format
@@ -130,6 +130,9 @@ function execute<TRow extends Record<string, any> = Record<string, any>>(
130130
): Promise<{ format: "json"; results: Array<TRow> } | { format: "csv"; results: string }> {
131131
const apiClient = apiClientManager.clientOrThrow();
132132

133+
const from = dateToISOString(options?.from);
134+
const to = dateToISOString(options?.to);
135+
133136
const $requestOptions = mergeRequestOptions(
134137
{
135138
tracer,
@@ -140,16 +143,40 @@ function execute<TRow extends Record<string, any> = Record<string, any>>(
140143
format: options?.format ?? "json",
141144
query,
142145
period: options?.period,
143-
from: options?.from,
144-
to: options?.to,
146+
from,
147+
to,
145148
},
146149
},
147150
requestOptions
148151
);
149152

150-
return apiClient.executeQuery(query, options, $requestOptions).then((response) => {
151-
return response;
152-
}) as Promise<{ format: "json"; results: Array<TRow> } | { format: "csv"; results: string }>;
153+
return apiClient
154+
.executeQuery(
155+
query,
156+
{
157+
scope: options?.scope,
158+
period: options?.period,
159+
from,
160+
to,
161+
format: options?.format,
162+
},
163+
$requestOptions
164+
)
165+
.then((response) => {
166+
return response;
167+
}) as Promise<{ format: "json"; results: Array<TRow> } | { format: "csv"; results: string }>;
168+
}
169+
170+
function dateToISOString(date: Date | number | undefined): string | undefined {
171+
if (date === undefined) {
172+
return undefined;
173+
}
174+
175+
if (date instanceof Date) {
176+
return date.toISOString();
177+
}
178+
179+
return new Date(date).toISOString();
153180
}
154181

155182
export const query = {

references/hello-world/src/trigger/query.ts

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,62 @@
11
import { logger, query, task } from "@trigger.dev/sdk";
22
import type { QueryTable } from "@trigger.dev/sdk";
33

4-
// Simple query example - just the query string, all defaults
4+
// Simple query example - tests different from/to formats
55
export const simpleQueryTask = task({
66
id: "simple-query",
77
run: async () => {
88
logger.info("Running simple query example");
99

10-
// Simplest usage - uses environment scope, json format, default period
11-
const result = await query.execute("SELECT * FROM runs LIMIT 10");
12-
13-
logger.info("Query results (untyped)", {
14-
format: result.format,
15-
rowCount: result.results.length,
16-
firstRow: result.results[0],
10+
// 1. Default: no from/to, uses default period
11+
const defaultResult = await query.execute("SELECT * FROM runs LIMIT 5");
12+
logger.info("Default (no from/to)", {
13+
rowCount: defaultResult.results.length,
14+
firstRow: defaultResult.results[0],
1715
});
1816

19-
// Type-safe query using QueryTable with specific columns
20-
const typedResult = await query.execute<
21-
QueryTable<"runs", "run_id" | "status" | "triggered_at" | "total_duration">
22-
>("SELECT run_id, status, triggered_at, total_duration FROM runs LIMIT 10");
17+
// 2. Using Date objects for from/to
18+
const withDates = await query.execute<
19+
QueryTable<"runs", "run_id" | "status" | "triggered_at">
20+
>("SELECT run_id, status, triggered_at FROM runs LIMIT 5", {
21+
from: new Date("2025-01-01T00:00:00Z"),
22+
to: new Date(),
23+
});
24+
logger.info("With Date objects", {
25+
rowCount: withDates.results.length,
26+
firstRow: withDates.results[0],
27+
});
2328

24-
logger.info("Query results (typed)", {
25-
format: typedResult.format,
26-
rowCount: typedResult.results.length,
27-
firstRow: typedResult.results[0],
29+
// 3. Using Unix timestamps in milliseconds (Date.now() returns ms)
30+
const now = Date.now();
31+
const sevenDaysAgo = now - 7 * 24 * 60 * 60 * 1000;
32+
const withTimestamps = await query.execute<
33+
QueryTable<"runs", "run_id" | "status" | "triggered_at">
34+
>("SELECT run_id, status, triggered_at FROM runs LIMIT 5", {
35+
from: sevenDaysAgo,
36+
to: now,
37+
});
38+
logger.info("With Unix timestamps (ms)", {
39+
rowCount: withTimestamps.results.length,
40+
firstRow: withTimestamps.results[0],
2841
});
2942

30-
// Full type safety on the rows - status is narrowly typed!
31-
typedResult.results.forEach((row, index) => {
32-
logger.info(`Run ${index + 1}`, {
33-
run_id: row.run_id, // string
34-
status: row.status, // RunFriendlyStatus ("Completed" | "Failed" | ...)
35-
total_duration: row.total_duration, // number | null
36-
});
43+
// 4. Mixing Date and number
44+
const mixed = await query.execute<
45+
QueryTable<"runs", "run_id" | "status" | "triggered_at">
46+
>("SELECT run_id, status, triggered_at FROM runs LIMIT 5", {
47+
from: new Date("2025-01-01"),
48+
to: Date.now(),
49+
});
50+
logger.info("Mixed Date + timestamp", {
51+
rowCount: mixed.results.length,
52+
firstRow: mixed.results[0],
3753
});
3854

3955
return {
40-
totalRows: typedResult.results.length,
41-
rows: typedResult.results,
56+
defaultRows: defaultResult.results.length,
57+
dateRows: withDates.results.length,
58+
timestampRows: withTimestamps.results.length,
59+
mixedRows: mixed.results.length,
4260
};
4361
},
4462
});
@@ -136,8 +154,8 @@ export const orgQueryTask = task({
136154
LIMIT 50`,
137155
{
138156
scope: "organization", // Query across all projects
139-
from: "2025-02-01T00:00:00Z", // Custom date range
140-
to: "2025-02-11T23:59:59Z",
157+
from: new Date("2025-02-01T00:00:00Z"), // Custom date range
158+
to: new Date("2025-02-11T23:59:59Z"),
141159
}
142160
);
143161

0 commit comments

Comments
 (0)