@trigger.dev/sdk 4.5.0-rc.5 → 4.5.0-rc.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (213) hide show
  1. package/dist/commonjs/v3/ai.d.ts +178 -5
  2. package/dist/commonjs/v3/ai.js +603 -119
  3. package/dist/commonjs/v3/ai.js.map +1 -1
  4. package/dist/commonjs/v3/chat-client.js +3 -0
  5. package/dist/commonjs/v3/chat-client.js.map +1 -1
  6. package/dist/commonjs/v3/chat-react.js +10 -7
  7. package/dist/commonjs/v3/chat-react.js.map +1 -1
  8. package/dist/commonjs/v3/chat-server.d.ts +8 -0
  9. package/dist/commonjs/v3/chat-server.js +32 -10
  10. package/dist/commonjs/v3/chat-server.js.map +1 -1
  11. package/dist/commonjs/v3/chat-server.test.js +51 -0
  12. package/dist/commonjs/v3/chat-server.test.js.map +1 -1
  13. package/dist/commonjs/v3/chat.js +34 -6
  14. package/dist/commonjs/v3/chat.js.map +1 -1
  15. package/dist/commonjs/v3/chat.test.js +53 -0
  16. package/dist/commonjs/v3/chat.test.js.map +1 -1
  17. package/dist/commonjs/v3/createStartSessionAction.test.js +30 -0
  18. package/dist/commonjs/v3/createStartSessionAction.test.js.map +1 -1
  19. package/dist/commonjs/v3/sessions.d.ts +11 -6
  20. package/dist/commonjs/v3/sessions.js +10 -5
  21. package/dist/commonjs/v3/sessions.js.map +1 -1
  22. package/dist/commonjs/v3/test/mock-chat-agent.d.ts +6 -0
  23. package/dist/commonjs/v3/test/mock-chat-agent.js +1 -0
  24. package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -1
  25. package/dist/commonjs/version.js +1 -1
  26. package/dist/esm/v3/ai.d.ts +178 -5
  27. package/dist/esm/v3/ai.js +603 -120
  28. package/dist/esm/v3/ai.js.map +1 -1
  29. package/dist/esm/v3/chat-client.js +3 -0
  30. package/dist/esm/v3/chat-client.js.map +1 -1
  31. package/dist/esm/v3/chat-react.js +10 -7
  32. package/dist/esm/v3/chat-react.js.map +1 -1
  33. package/dist/esm/v3/chat-server.d.ts +8 -0
  34. package/dist/esm/v3/chat-server.js +32 -10
  35. package/dist/esm/v3/chat-server.js.map +1 -1
  36. package/dist/esm/v3/chat-server.test.js +51 -0
  37. package/dist/esm/v3/chat-server.test.js.map +1 -1
  38. package/dist/esm/v3/chat.js +34 -6
  39. package/dist/esm/v3/chat.js.map +1 -1
  40. package/dist/esm/v3/chat.test.js +53 -0
  41. package/dist/esm/v3/chat.test.js.map +1 -1
  42. package/dist/esm/v3/createStartSessionAction.test.js +30 -0
  43. package/dist/esm/v3/createStartSessionAction.test.js.map +1 -1
  44. package/dist/esm/v3/sessions.d.ts +11 -6
  45. package/dist/esm/v3/sessions.js +10 -5
  46. package/dist/esm/v3/sessions.js.map +1 -1
  47. package/dist/esm/v3/test/mock-chat-agent.d.ts +6 -0
  48. package/dist/esm/v3/test/mock-chat-agent.js +1 -0
  49. package/dist/esm/v3/test/mock-chat-agent.js.map +1 -1
  50. package/dist/esm/version.js +1 -1
  51. package/docs/ai/prompts.mdx +430 -0
  52. package/docs/ai-chat/actions.mdx +115 -0
  53. package/docs/ai-chat/anatomy.mdx +71 -0
  54. package/docs/ai-chat/backend.mdx +817 -0
  55. package/docs/ai-chat/background-injection.mdx +221 -0
  56. package/docs/ai-chat/changelog.mdx +850 -0
  57. package/docs/ai-chat/chat-local.mdx +174 -0
  58. package/docs/ai-chat/client-protocol.mdx +1081 -0
  59. package/docs/ai-chat/compaction.mdx +411 -0
  60. package/docs/ai-chat/custom-agents.mdx +364 -0
  61. package/docs/ai-chat/error-handling.mdx +415 -0
  62. package/docs/ai-chat/fast-starts.mdx +672 -0
  63. package/docs/ai-chat/frontend.mdx +580 -0
  64. package/docs/ai-chat/how-it-works.mdx +230 -0
  65. package/docs/ai-chat/lifecycle-hooks.mdx +530 -0
  66. package/docs/ai-chat/mcp.mdx +101 -0
  67. package/docs/ai-chat/overview.mdx +90 -0
  68. package/docs/ai-chat/patterns/branching-conversations.mdx +284 -0
  69. package/docs/ai-chat/patterns/code-sandbox.mdx +126 -0
  70. package/docs/ai-chat/patterns/database-persistence.mdx +414 -0
  71. package/docs/ai-chat/patterns/human-in-the-loop.mdx +275 -0
  72. package/docs/ai-chat/patterns/large-payloads.mdx +169 -0
  73. package/docs/ai-chat/patterns/oom-resilience.mdx +120 -0
  74. package/docs/ai-chat/patterns/persistence-and-replay.mdx +211 -0
  75. package/docs/ai-chat/patterns/recovery-boot.mdx +230 -0
  76. package/docs/ai-chat/patterns/skills.mdx +221 -0
  77. package/docs/ai-chat/patterns/sub-agents.mdx +383 -0
  78. package/docs/ai-chat/patterns/tool-result-auditing.mdx +148 -0
  79. package/docs/ai-chat/patterns/trusted-edge-signals.mdx +337 -0
  80. package/docs/ai-chat/patterns/version-upgrades.mdx +172 -0
  81. package/docs/ai-chat/pending-messages.mdx +343 -0
  82. package/docs/ai-chat/prompt-caching.mdx +206 -0
  83. package/docs/ai-chat/quick-start.mdx +161 -0
  84. package/docs/ai-chat/reference.mdx +909 -0
  85. package/docs/ai-chat/server-chat.mdx +263 -0
  86. package/docs/ai-chat/sessions.mdx +333 -0
  87. package/docs/ai-chat/testing.mdx +682 -0
  88. package/docs/ai-chat/tools.mdx +191 -0
  89. package/docs/ai-chat/types.mdx +242 -0
  90. package/docs/ai-chat/upgrade-guide.mdx +515 -0
  91. package/docs/apikeys.mdx +54 -0
  92. package/docs/building-with-ai.mdx +261 -0
  93. package/docs/bulk-actions.mdx +49 -0
  94. package/docs/changelog.mdx +6 -0
  95. package/docs/cli-deploy-commands.mdx +9 -0
  96. package/docs/cli-dev-commands.mdx +9 -0
  97. package/docs/cli-dev.mdx +8 -0
  98. package/docs/cli-init-commands.mdx +58 -0
  99. package/docs/cli-introduction.mdx +25 -0
  100. package/docs/cli-list-profiles-commands.mdx +42 -0
  101. package/docs/cli-login-commands.mdx +33 -0
  102. package/docs/cli-logout-commands.mdx +33 -0
  103. package/docs/cli-preview-archive.mdx +59 -0
  104. package/docs/cli-promote-commands.mdx +9 -0
  105. package/docs/cli-switch.mdx +43 -0
  106. package/docs/cli-update-commands.mdx +42 -0
  107. package/docs/cli-whoami-commands.mdx +33 -0
  108. package/docs/community.mdx +6 -0
  109. package/docs/config/config-file.mdx +602 -0
  110. package/docs/config/extensions/additionalFiles.mdx +38 -0
  111. package/docs/config/extensions/additionalPackages.mdx +40 -0
  112. package/docs/config/extensions/aptGet.mdx +34 -0
  113. package/docs/config/extensions/audioWaveform.mdx +20 -0
  114. package/docs/config/extensions/custom.mdx +380 -0
  115. package/docs/config/extensions/emitDecoratorMetadata.mdx +29 -0
  116. package/docs/config/extensions/esbuildPlugin.mdx +31 -0
  117. package/docs/config/extensions/ffmpeg.mdx +45 -0
  118. package/docs/config/extensions/lightpanda.mdx +56 -0
  119. package/docs/config/extensions/overview.mdx +67 -0
  120. package/docs/config/extensions/playwright.mdx +195 -0
  121. package/docs/config/extensions/prismaExtension.mdx +1014 -0
  122. package/docs/config/extensions/puppeteer.mdx +30 -0
  123. package/docs/config/extensions/pythonExtension.mdx +182 -0
  124. package/docs/config/extensions/syncEnvVars.mdx +291 -0
  125. package/docs/context.mdx +235 -0
  126. package/docs/database-connections.mdx +213 -0
  127. package/docs/deploy-environment-variables.mdx +435 -0
  128. package/docs/deployment/atomic-deployment.mdx +172 -0
  129. package/docs/deployment/overview.mdx +257 -0
  130. package/docs/deployment/preview-branches.mdx +224 -0
  131. package/docs/errors-retrying.mdx +379 -0
  132. package/docs/github-actions.mdx +222 -0
  133. package/docs/github-integration.mdx +136 -0
  134. package/docs/github-repo.mdx +8 -0
  135. package/docs/help-email.mdx +6 -0
  136. package/docs/help-slack.mdx +11 -0
  137. package/docs/hidden-tasks.mdx +56 -0
  138. package/docs/how-it-works.mdx +454 -0
  139. package/docs/how-to-reduce-your-spend.mdx +217 -0
  140. package/docs/idempotency.mdx +504 -0
  141. package/docs/introduction.mdx +223 -0
  142. package/docs/limits.mdx +241 -0
  143. package/docs/logging.mdx +195 -0
  144. package/docs/machines.mdx +952 -0
  145. package/docs/manual-setup.mdx +632 -0
  146. package/docs/mcp-agent-rules.mdx +41 -0
  147. package/docs/mcp-introduction.mdx +385 -0
  148. package/docs/mcp-tools.mdx +273 -0
  149. package/docs/migrating-from-v3.mdx +334 -0
  150. package/docs/observability/dashboards.mdx +102 -0
  151. package/docs/observability/query.mdx +585 -0
  152. package/docs/open-source-contributing.mdx +16 -0
  153. package/docs/open-source-self-hosting.mdx +541 -0
  154. package/docs/private-networking/aws-console-setup.mdx +304 -0
  155. package/docs/private-networking/overview.mdx +144 -0
  156. package/docs/private-networking/troubleshooting.mdx +78 -0
  157. package/docs/queue-concurrency.mdx +354 -0
  158. package/docs/quick-start.mdx +97 -0
  159. package/docs/realtime/auth.mdx +208 -0
  160. package/docs/realtime/backend/overview.mdx +45 -0
  161. package/docs/realtime/backend/streams.mdx +418 -0
  162. package/docs/realtime/backend/subscribe.mdx +225 -0
  163. package/docs/realtime/how-it-works.mdx +94 -0
  164. package/docs/realtime/overview.mdx +63 -0
  165. package/docs/realtime/react-hooks/overview.mdx +73 -0
  166. package/docs/realtime/react-hooks/streams.mdx +449 -0
  167. package/docs/realtime/react-hooks/subscribe.mdx +674 -0
  168. package/docs/realtime/react-hooks/swr.mdx +87 -0
  169. package/docs/realtime/react-hooks/triggering.mdx +194 -0
  170. package/docs/realtime/react-hooks/use-wait-token.mdx +34 -0
  171. package/docs/realtime/run-object.mdx +174 -0
  172. package/docs/replaying.mdx +72 -0
  173. package/docs/request-feature.mdx +6 -0
  174. package/docs/roadmap.mdx +6 -0
  175. package/docs/run-tests.mdx +20 -0
  176. package/docs/run-usage.mdx +113 -0
  177. package/docs/runs/heartbeats.mdx +38 -0
  178. package/docs/runs/max-duration.mdx +139 -0
  179. package/docs/runs/metadata.mdx +734 -0
  180. package/docs/runs/priority.mdx +31 -0
  181. package/docs/runs.mdx +396 -0
  182. package/docs/self-hosting/docker.mdx +458 -0
  183. package/docs/self-hosting/env/supervisor.mdx +74 -0
  184. package/docs/self-hosting/env/webapp.mdx +276 -0
  185. package/docs/self-hosting/kubernetes.mdx +601 -0
  186. package/docs/self-hosting/overview.mdx +108 -0
  187. package/docs/skills.mdx +85 -0
  188. package/docs/tags.mdx +120 -0
  189. package/docs/tasks/overview.mdx +697 -0
  190. package/docs/tasks/scheduled.mdx +382 -0
  191. package/docs/tasks/schemaTask.mdx +413 -0
  192. package/docs/tasks/streams.mdx +884 -0
  193. package/docs/triggering.mdx +1320 -0
  194. package/docs/troubleshooting-alerts.mdx +385 -0
  195. package/docs/troubleshooting-debugging-in-vscode.mdx +8 -0
  196. package/docs/troubleshooting-github-issues.mdx +6 -0
  197. package/docs/troubleshooting-uptime-status.mdx +6 -0
  198. package/docs/troubleshooting.mdx +398 -0
  199. package/docs/upgrading-packages.mdx +80 -0
  200. package/docs/vercel-integration.mdx +207 -0
  201. package/docs/versioning.mdx +56 -0
  202. package/docs/video-walkthrough.mdx +23 -0
  203. package/docs/wait-for-token.mdx +540 -0
  204. package/docs/wait-for.mdx +42 -0
  205. package/docs/wait-until.mdx +53 -0
  206. package/docs/wait.mdx +18 -0
  207. package/docs/writing-tasks-introduction.mdx +33 -0
  208. package/package.json +10 -6
  209. package/skills/trigger-authoring-chat-agent/SKILL.md +296 -0
  210. package/skills/trigger-authoring-tasks/SKILL.md +254 -0
  211. package/skills/trigger-chat-agent-advanced/SKILL.md +368 -0
  212. package/skills/trigger-cost-savings/SKILL.md +116 -0
  213. package/skills/trigger-realtime-and-frontend/SKILL.md +276 -0
@@ -0,0 +1,235 @@
1
+ ---
2
+ title: "Context"
3
+ description: "Get the context of a task run."
4
+ ---
5
+
6
+ Context (`ctx`) is a way to get information about a run.
7
+
8
+ <Note>
9
+ The context object does not change whilst your code is executing. This means values like
10
+ `ctx.run.durationMs` will be fixed at the moment the `run()` function is called.
11
+ </Note>
12
+
13
+ <RequestExample>
14
+
15
+ ```typescript Context example
16
+ import { task } from "@trigger.dev/sdk";
17
+
18
+ export const parentTask = task({
19
+ id: "parent-task",
20
+ run: async (payload: { message: string }, { ctx }) => {
21
+ if (ctx.environment.type === "DEVELOPMENT") {
22
+ return;
23
+ }
24
+ },
25
+ });
26
+ ```
27
+
28
+ </RequestExample>
29
+
30
+ ## Context properties
31
+
32
+ <ResponseField name="task" type="object">
33
+ <Expandable title="properties" defaultOpen={true}>
34
+ <ResponseField name="exportName" type="string">
35
+ The exported function name of the task e.g. `myTask` if you defined it like this: `export
36
+ const myTask = task(...)`.
37
+ </ResponseField>
38
+ <ResponseField name="id" type="string">
39
+ The ID of the task.
40
+ </ResponseField>
41
+ <ResponseField name="filePath" type="string">
42
+ The file path of the task.
43
+ </ResponseField>
44
+ </Expandable>
45
+ </ResponseField>
46
+
47
+ <ResponseField name="attempt" type="object">
48
+ <Expandable title="properties">
49
+ <ResponseField name="id" type="string">
50
+ The ID of the execution attempt.
51
+ </ResponseField>
52
+ <ResponseField name="number" type="number">
53
+ The attempt number.
54
+ </ResponseField>
55
+ <ResponseField name="startedAt" type="date">
56
+ The start time of the attempt.
57
+ </ResponseField>
58
+ <ResponseField name="backgroundWorkerId" type="string">
59
+ The ID of the background worker.
60
+ </ResponseField>
61
+ <ResponseField name="backgroundWorkerTaskId" type="string">
62
+ The ID of the background worker task.
63
+ </ResponseField>
64
+ <ResponseField name="status" type="string">
65
+ The current status of the attempt.
66
+ </ResponseField>
67
+ </Expandable>
68
+ </ResponseField>
69
+
70
+ <ResponseField name="run" type="object">
71
+ <Expandable title="properties">
72
+ <ResponseField name="id" type="string">
73
+ The ID of the task run.
74
+ </ResponseField>
75
+ <ResponseField name="context" type="any" optional>
76
+ The context of the task run.
77
+ </ResponseField>
78
+ <ResponseField name="tags" type="array">
79
+ An array of [tags](/tags) associated with the task run.
80
+ </ResponseField>
81
+ <ResponseField name="isTest" type="boolean">
82
+ Whether this is a [test run](/run-tests).
83
+ </ResponseField>
84
+ <ResponseField name="isReplay" type="boolean">
85
+ Whether this run is a [replay](/replaying) of a previous run.
86
+ </ResponseField>
87
+ <ResponseField name="createdAt" type="date">
88
+ The creation time of the task run.
89
+ </ResponseField>
90
+ <ResponseField name="startedAt" type="date">
91
+ The start time of the task run.
92
+ </ResponseField>
93
+ <ResponseField name="idempotencyKey" type="string" optional>
94
+ An optional [idempotency key](/idempotency) for the task run.
95
+ </ResponseField>
96
+ <ResponseField name="maxAttempts" type="number" optional>
97
+ The [maximum number of attempts](/triggering#maxattempts) allowed for this task run.
98
+ </ResponseField>
99
+ <ResponseField name="durationMs" type="number">
100
+ The duration of the task run in milliseconds when the `run()` function is called. For live
101
+ values use the [usage SDK functions](/run-usage).
102
+ </ResponseField>
103
+ <ResponseField name="costInCents" type="number">
104
+ The cost of the task run in cents when the `run()` function is called. For live values use the
105
+ [usage SDK functions](/run-usage).
106
+ </ResponseField>
107
+ <ResponseField name="baseCostInCents" type="number">
108
+ The base cost of the task run in cents when the `run()` function is called. For live values
109
+ use the [usage SDK functions](/run-usage).
110
+ </ResponseField>
111
+ <ResponseField name="version" type="string" optional>
112
+ The [version](/versioning) of the task run.
113
+ </ResponseField>
114
+ <ResponseField name="maxDuration" type="number" optional>
115
+ The [maximum allowed duration](/runs/max-duration) for the task run.
116
+ </ResponseField>
117
+ </Expandable>
118
+ </ResponseField>
119
+
120
+ <ResponseField name="queue" type="object">
121
+ <Expandable title="properties">
122
+ <ResponseField name="id" type="string">
123
+ The ID of the queue.
124
+ </ResponseField>
125
+ <ResponseField name="name" type="string">
126
+ The name of the queue.
127
+ </ResponseField>
128
+ </Expandable>
129
+ </ResponseField>
130
+
131
+ <ResponseField name="environment" type="object">
132
+ <Expandable title="properties">
133
+ <ResponseField name="id" type="string">
134
+ The ID of the environment.
135
+ </ResponseField>
136
+ <ResponseField name="slug" type="string">
137
+ The slug of the environment.
138
+ </ResponseField>
139
+ <ResponseField name="type" type="string">
140
+ The type of the environment (PRODUCTION, STAGING, DEVELOPMENT, or PREVIEW).
141
+ </ResponseField>
142
+ <ResponseField name="branchName" type="string" optional>
143
+ If the environment is `PREVIEW` then this will be the branch name.
144
+ </ResponseField>
145
+ <ResponseField name="git" type="object">
146
+ <Expandable title="properties">
147
+ <ResponseField name="commitAuthorName" type="string" optional>
148
+ The name of the commit author.
149
+ </ResponseField>
150
+ <ResponseField name="commitMessage" type="string" optional>
151
+ The message of the commit.
152
+ </ResponseField>
153
+ <ResponseField name="commitRef" type="string" optional>
154
+ The ref of the commit.
155
+ </ResponseField>
156
+ <ResponseField name="commitSha" type="string" optional>
157
+ The SHA of the commit.
158
+ </ResponseField>
159
+ <ResponseField name="dirty" type="boolean" optional>
160
+ Whether the commit is dirty, i.e. there are uncommitted changes.
161
+ </ResponseField>
162
+ <ResponseField name="remoteUrl" type="string" optional>
163
+ The remote URL of the repository.
164
+ </ResponseField>
165
+ <ResponseField name="pullRequestNumber" type="number" optional>
166
+ The number of the pull request.
167
+ </ResponseField>
168
+ <ResponseField name="pullRequestTitle" type="string" optional>
169
+ The title of the pull request.
170
+ </ResponseField>
171
+ <ResponseField name="pullRequestState" type="string" optional>
172
+ The state of the pull request (open, closed, or merged).
173
+ </ResponseField>
174
+ </Expandable>
175
+ </ResponseField>
176
+ </Expandable>
177
+ </ResponseField>
178
+
179
+ <ResponseField name="organization" type="object">
180
+ <Expandable title="properties">
181
+ <ResponseField name="id" type="string">
182
+ The ID of the organization.
183
+ </ResponseField>
184
+ <ResponseField name="slug" type="string">
185
+ The slug of the organization.
186
+ </ResponseField>
187
+ <ResponseField name="name" type="string">
188
+ The name of the organization.
189
+ </ResponseField>
190
+ </Expandable>
191
+ </ResponseField>
192
+
193
+ <ResponseField name="project" type="object">
194
+ <Expandable title="properties">
195
+ <ResponseField name="id" type="string">
196
+ The ID of the project.
197
+ </ResponseField>
198
+ <ResponseField name="ref" type="string">
199
+ The reference of the project.
200
+ </ResponseField>
201
+ <ResponseField name="slug" type="string">
202
+ The slug of the project.
203
+ </ResponseField>
204
+ <ResponseField name="name" type="string">
205
+ The name of the project.
206
+ </ResponseField>
207
+ </Expandable>
208
+ </ResponseField>
209
+
210
+ <ResponseField name="batch" type="object" optional>
211
+ Optional information about the batch, if applicable.
212
+ <Expandable title="properties">
213
+ <ResponseField name="id" type="string">
214
+ The ID of the batch.
215
+ </ResponseField>
216
+ </Expandable>
217
+ </ResponseField>
218
+
219
+ <ResponseField name="machine" type="object" optional>
220
+ Optional information about the machine preset used for execution.
221
+ <Expandable title="properties">
222
+ <ResponseField name="name" type="string">
223
+ The name of the machine preset.
224
+ </ResponseField>
225
+ <ResponseField name="cpu" type="number">
226
+ The CPU allocation for the machine.
227
+ </ResponseField>
228
+ <ResponseField name="memory" type="number">
229
+ The memory allocation for the machine.
230
+ </ResponseField>
231
+ <ResponseField name="centsPerMs" type="number">
232
+ The cost in cents per millisecond for this machine preset.
233
+ </ResponseField>
234
+ </Expandable>
235
+ </ResponseField>
@@ -0,0 +1,213 @@
1
+ ---
2
+ title: "Database connections"
3
+ sidebarTitle: "Database connections"
4
+ description: "Connect a database to your tasks: where to create the client, how to size the pool for your provider's connection limit, and how to release connections so you don't run out."
5
+ ---
6
+
7
+ Tasks connect to your database from their own process. This guide covers the recommended setup for each client, how to size the pool against your provider's connection limit, and how to release connections at waits.
8
+
9
+ ## Create the client once
10
+
11
+ Create the client at module scope and import it wherever you query. The worker loads the module once per process, so every run on that worker reuses the same pool. Keep the pool small (see [Size the pool](#size-the-pool)) and attach an error handler, since an idle connection can error asynchronously and an unhandled `error` event crashes the worker.
12
+
13
+ <CodeGroup>
14
+ ```ts lib/db.ts (node-postgres)
15
+ import { Pool } from "pg";
16
+
17
+ export const pool = new Pool({
18
+ connectionString: process.env.DATABASE_URL,
19
+ max: 1, // one connection per run; raise only for in-run parallel queries
20
+ });
21
+
22
+ pool.on("error", (err) => console.error("pg pool error", err));
23
+ ```
24
+
25
+ ```ts lib/db.ts (Prisma)
26
+ import { PrismaPg } from "@prisma/adapter-pg";
27
+ import { PrismaClient } from "./generated/prisma/client";
28
+
29
+ const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL, max: 1 });
30
+
31
+ export const prisma = new PrismaClient({ adapter });
32
+ ```
33
+
34
+ ```ts lib/db.ts (Drizzle)
35
+ import { drizzle } from "drizzle-orm/node-postgres";
36
+ import { Pool } from "pg";
37
+
38
+ const pool = new Pool({ connectionString: process.env.DATABASE_URL, max: 1 });
39
+ pool.on("error", (err) => console.error("pg pool error", err));
40
+
41
+ export const db = drizzle({ client: pool });
42
+ ```
43
+
44
+ ```ts lib/db.ts (MongoDB)
45
+ import { MongoClient } from "mongodb";
46
+
47
+ export const client = new MongoClient(process.env.DATABASE_URL!, { maxPoolSize: 5 });
48
+ ```
49
+ </CodeGroup>
50
+
51
+ <Note>
52
+ Import this one client everywhere. Don't create a client inside `run()` or a lifecycle hook, which opens a new pool on every run, and don't store one in [`chat.local`](/ai-chat/chat-local), which is per-run state that gets serialized into subtasks.
53
+ </Note>
54
+
55
+ ## Size the pool
56
+
57
+ A run uses connections only while it is actively executing. Queued, waiting, and suspended runs use none. So the connections in use at any moment are:
58
+
59
+ > concurrent executing runs × pool size per run
60
+
61
+ Set the pool small. A task usually runs its queries in sequence, so one connection per run (`max: 1`) is enough for node-postgres, Prisma, and Drizzle; raise it only when a single run issues queries in parallel. The MongoDB driver shares one pool across all operations, so keep `maxPoolSize` in the low single digits. Each client's out-of-the-box default is far larger:
62
+
63
+ | Client | Default pool size |
64
+ | --- | --- |
65
+ | [node-postgres (`pg`)](https://node-postgres.com/guides/pool-sizing) | 10 |
66
+ | postgres-js | 10 |
67
+ | [Prisma (v7, `pg` adapter)](https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/databases-connections/connection-pool) | 10 (the adapter's `pg` default) |
68
+ | Drizzle (node-postgres) | 10 (the underlying `pg` pool) |
69
+ | [MongoDB driver](https://www.mongodb.com/docs/drivers/node/current/connect/connection-options/connection-pools/) | 100 (`maxPoolSize`) |
70
+
71
+ Keep `concurrent runs × pool size` under your provider's connection limit, and cap how many runs execute at once with [concurrency limits](/queue-concurrency) so runs queue instead of overrunning the database. Direct connection limits for common Postgres providers:
72
+
73
+ | Provider | Direct connection limit |
74
+ | --- | --- |
75
+ | [PostgreSQL (self-hosted)](https://www.postgresql.org/docs/current/runtime-config-connection.html) | `max_connections`, default `100` |
76
+ | [Supabase](https://supabase.com/docs/guides/platform/compute-and-disk) | `60` (Nano/Micro) up to `500` (16XL), by compute size |
77
+ | [Neon](https://neon.com/docs/connect/connection-pooling) | `104` (0.25 CU) up to `4000` (capped at 9 CU and above), by compute size |
78
+ | [AWS RDS / Aurora](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Limits.html) | `LEAST(DBInstanceClassMemory / 9531392, 5000)`, ~5 reserved for superusers |
79
+ | [PlanetScale Postgres](https://planetscale.com/docs/postgres/connecting) | set per cluster size (Cluster, then Parameters, then `max_connections`) |
80
+
81
+ [MongoDB Atlas](https://www.mongodb.com/docs/atlas/reference/atlas-limits/) limits connections per node: `500` on Free and Flex, `1500` on M10, `3000` on M20.
82
+
83
+ When `concurrent runs × pool size` approaches these numbers, connect through a pooler instead.
84
+
85
+ ## Use a connection pooler
86
+
87
+ A pooler (PgBouncer, RDS Proxy, Supavisor, Prisma Accelerate) sits between your tasks and the database and multiplexes many client connections onto a few backend connections. Point your connection string at the pooler's endpoint and the ceiling rises without changing your code. Use one when many runs execute concurrently, and for chat agents.
88
+
89
+ | Provider | Pooled endpoint | Pooled client limit |
90
+ | --- | --- | --- |
91
+ | [Supabase Supavisor](https://supabase.com/docs/guides/database/connection-management) | port `6543` (transaction mode) | `200` (Nano) up to `12,000` (16XL) |
92
+ | [Neon](https://neon.com/docs/connect/connection-pooling) | add `-pooler` to the endpoint host | up to `10,000` |
93
+ | [AWS RDS Proxy](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/rds-proxy.html) | the proxy endpoint | managed |
94
+ | [PlanetScale Postgres](https://planetscale.com/blog/scaling-postgres-connections-with-pgbouncer) | PgBouncer endpoint | managed |
95
+ | Self-hosted | PgBouncer or PgCat | configured |
96
+
97
+ Use the pooled endpoint for your tasks. Use the direct endpoint for schema migrations (Prisma Migrate, Drizzle Kit), which need a stable session that a transaction pooler does not provide.
98
+
99
+ Transaction-mode poolers (Supavisor on `6543`, PgBouncer in transaction mode) do not keep server-side prepared statements across queries. With Prisma, add `?pgbouncer=true` to the pooled URL. With node-postgres, don't rely on prepared statements.
100
+
101
+ ## Private databases
102
+
103
+ If your database lives in a private VPC and isn't reachable over the public internet, connect to it with [private networking](/private-networking/overview), which links your tasks to resources in your own AWS account over AWS PrivateLink. It supports Postgres (RDS, Aurora), MySQL, MongoDB, and any other TCP service behind an internal load balancer.
104
+
105
+ Once the connection is active, set your connection-string variable (for example `DATABASE_URL`) to the endpoint IP shown in the dashboard, and the client setup above is unchanged. Private networking is a Pro and Enterprise feature, and the endpoint is reachable only from deployed environments, so use a public connection in local development.
106
+
107
+ ## Provider notes
108
+
109
+ - [Supabase](https://supabase.com/docs/guides/database/connecting-to-postgres): the direct connection (`db.<ref>.supabase.co:5432`) resolves to IPv6 only and is unreachable from many environments, so connect through the Supavisor pooler or add the IPv4 add-on. The pooler presents Supabase's own CA, so prefer passing that CA and keeping verification on (`rejectUnauthorized: true`). Use `rejectUnauthorized: false` only as a temporary troubleshooting step in non-production environments.
110
+ - A `DATABASE_URL` with `sslmode=verify-full&sslrootcert=system` uses a libpq feature the `pg` driver (node-postgres, and the Prisma and Drizzle pools built on it) cannot read. Build the pool from discrete fields with `ssl: { rejectUnauthorized: true }` (Node's CA store), or point `sslrootcert` at a real CA file.
111
+
112
+ ## Release connections at a wait
113
+
114
+ A run holds its connections while it is paused at a wait until the process is torn down, which is not instant. Free them sooner so other runs can reuse them. How you release depends on the client:
115
+
116
+ - Prisma reconnects lazily, so disconnect it from a global [`tasks.onWait`](/tasks/overview#onwait-and-onresume-functions) handler colocated with the client. One handler covers every task.
117
+ - A `pg` Pool (node-postgres and Drizzle) and the MongoDB client can't be reused after a full close, so give them a short idle timeout instead. Idle connections close themselves during the wait while the pool stays usable.
118
+
119
+ <CodeGroup>
120
+ ```ts lib/db.ts (node-postgres)
121
+ import { Pool } from "pg";
122
+
123
+ export const pool = new Pool({
124
+ connectionString: process.env.DATABASE_URL,
125
+ max: 1,
126
+ idleTimeoutMillis: 10_000, // idle connections close during a wait; the pool stays usable
127
+ });
128
+
129
+ pool.on("error", (err) => console.error("pg pool error", err));
130
+ ```
131
+
132
+ ```ts lib/db.ts (Prisma)
133
+ import { tasks } from "@trigger.dev/sdk";
134
+ import { PrismaPg } from "@prisma/adapter-pg";
135
+ import { PrismaClient } from "./generated/prisma/client";
136
+
137
+ const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL, max: 1 });
138
+ export const prisma = new PrismaClient({ adapter });
139
+
140
+ // Disconnect when any run pauses; Prisma reconnects on the next query.
141
+ tasks.onWait("db", () => prisma.$disconnect());
142
+ ```
143
+
144
+ ```ts lib/db.ts (Drizzle)
145
+ import { drizzle } from "drizzle-orm/node-postgres";
146
+ import { Pool } from "pg";
147
+
148
+ const pool = new Pool({
149
+ connectionString: process.env.DATABASE_URL,
150
+ max: 1,
151
+ idleTimeoutMillis: 10_000,
152
+ });
153
+ pool.on("error", (err) => console.error("pg pool error", err));
154
+
155
+ export const db = drizzle({ client: pool });
156
+ ```
157
+
158
+ ```ts lib/db.ts (MongoDB)
159
+ import { MongoClient } from "mongodb";
160
+
161
+ export const client = new MongoClient(process.env.DATABASE_URL!, {
162
+ maxPoolSize: 5,
163
+ maxIdleTimeMS: 10_000, // idle sockets close during a wait; the client stays usable
164
+ });
165
+ ```
166
+ </CodeGroup>
167
+
168
+ Don't hold a client across a slow await, either. `pool.query()` checks a connection out and returns it in one call. If you `pool.connect()` and keep the client across an external HTTP call or a model stream, you pin that connection for the whole operation. Query, release, then do the slow work.
169
+
170
+ ## Chat agents
171
+
172
+ A chat agent runs one long-lived worker per conversation and suspends between messages, so its connection count tracks the conversations streaming a turn at the same moment. The global `tasks.onWait` handler above covers chat agents too. Two more specifics:
173
+
174
+ - Don't hold a connection across `streamText()`. A turn spends most of its time waiting on the model, so query and release before the stream starts.
175
+ - To release only when a conversation goes idle (rather than on every internal wait within a turn), use [`onChatSuspend`](/ai-chat/lifecycle-hooks#onchatsuspend--onchatresume) instead of the global handler.
176
+
177
+ ```ts /trigger/chat.ts
178
+ import { chat } from "@trigger.dev/sdk/ai";
179
+ import { streamText } from "ai";
180
+ import { openai } from "@ai-sdk/openai";
181
+ import { prisma } from "@/lib/db";
182
+
183
+ export const myChat = chat.agent({
184
+ id: "my-chat",
185
+ run: async ({ messages, clientData, signal }) => {
186
+ const user = await prisma.user.findUnique({ where: { id: clientData.userId } });
187
+ // The connection is back in the pool before the model stream starts.
188
+ return streamText({
189
+ model: openai("gpt-4o"),
190
+ system: `Helping ${user?.name ?? "the user"}.`,
191
+ messages,
192
+ abortSignal: signal,
193
+ });
194
+ },
195
+ });
196
+ ```
197
+
198
+ ## Troubleshooting
199
+
200
+ `too many connections` or connection refused: `concurrent runs × pool size` is over your provider's limit. Lower the pool size, cap [concurrency](/queue-concurrency), or connect through a pooler.
201
+
202
+ The worker crashes right after resuming from a wait: an idle connection that closed during the suspend emitted an unhandled `error` event. Attach `pool.on("error", ...)` on a `pg` pool (node-postgres or Drizzle); Prisma and the MongoDB driver handle this internally.
203
+
204
+ ## How suspend affects connections
205
+
206
+ When a task waits, the runtime can [checkpoint](/how-it-works#the-checkpoint-resume-system) the run: it snapshots the process and frees the compute, then restores the process when the wait resolves. Process memory comes back, so your pool object survives, but the database closed the idle connections in the meantime. The pool reconnects on the first query after resume. This is why a suspended run holds no connections, and why the pool needs an error handler to absorb the closed connection cleanly.
207
+
208
+ ## See also
209
+
210
+ - [Wait](/wait) for the primitives that trigger a checkpoint.
211
+ - [Concurrency and queues](/queue-concurrency) to cap how many runs execute at once.
212
+ - [Lifecycle functions](/tasks/overview#onwait-and-onresume-functions) for global `tasks.onWait` and `tasks.onResume`.
213
+ - [Chat agent lifecycle hooks](/ai-chat/lifecycle-hooks) for `onChatSuspend` and `onChatResume`.