bun-types 1.3.2-canary.20251106T140813 → 1.3.2-canary.20251108T140624

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 (316) hide show
  1. package/bun.d.ts +102 -6
  2. package/docs/bundler/bytecode.mdx +465 -0
  3. package/docs/bundler/css.mdx +1024 -0
  4. package/docs/bundler/esbuild.mdx +253 -0
  5. package/docs/bundler/executables.mdx +535 -0
  6. package/docs/bundler/fullstack.mdx +1064 -0
  7. package/docs/bundler/hot-reloading.mdx +229 -0
  8. package/docs/bundler/html-static.mdx +386 -0
  9. package/docs/bundler/index.mdx +1499 -0
  10. package/docs/bundler/loaders.mdx +356 -0
  11. package/docs/bundler/macros.mdx +328 -0
  12. package/docs/bundler/minifier.mdx +1306 -0
  13. package/docs/bundler/plugins.mdx +411 -0
  14. package/docs/feedback.mdx +85 -0
  15. package/docs/guides/binary/arraybuffer-to-array.mdx +29 -0
  16. package/docs/guides/binary/arraybuffer-to-blob.mdx +26 -0
  17. package/docs/guides/binary/arraybuffer-to-buffer.mdx +27 -0
  18. package/docs/guides/binary/arraybuffer-to-string.mdx +17 -0
  19. package/docs/guides/binary/arraybuffer-to-typedarray.mdx +41 -0
  20. package/docs/guides/binary/blob-to-arraybuffer.mdx +16 -0
  21. package/docs/guides/binary/blob-to-dataview.mdx +16 -0
  22. package/docs/guides/binary/blob-to-stream.mdx +16 -0
  23. package/docs/guides/binary/blob-to-string.mdx +17 -0
  24. package/docs/guides/binary/blob-to-typedarray.mdx +16 -0
  25. package/docs/guides/binary/buffer-to-arraybuffer.mdx +16 -0
  26. package/docs/guides/binary/buffer-to-blob.mdx +16 -0
  27. package/docs/guides/binary/buffer-to-readablestream.mdx +43 -0
  28. package/docs/guides/binary/buffer-to-string.mdx +27 -0
  29. package/docs/guides/binary/buffer-to-typedarray.mdx +16 -0
  30. package/docs/guides/binary/dataview-to-string.mdx +17 -0
  31. package/docs/guides/binary/typedarray-to-arraybuffer.mdx +27 -0
  32. package/docs/guides/binary/typedarray-to-blob.mdx +18 -0
  33. package/docs/guides/binary/typedarray-to-buffer.mdx +16 -0
  34. package/docs/guides/binary/typedarray-to-dataview.mdx +16 -0
  35. package/docs/guides/binary/typedarray-to-readablestream.mdx +43 -0
  36. package/docs/guides/binary/typedarray-to-string.mdx +18 -0
  37. package/docs/guides/deployment/aws-lambda.mdx +204 -0
  38. package/docs/guides/deployment/digital-ocean.mdx +161 -0
  39. package/docs/guides/deployment/google-cloud-run.mdx +197 -0
  40. package/docs/guides/deployment/railway.mdx +145 -0
  41. package/docs/guides/deployment/render.mdx +82 -0
  42. package/docs/guides/deployment/vercel.mdx +99 -0
  43. package/docs/guides/ecosystem/astro.mdx +82 -0
  44. package/docs/guides/ecosystem/discordjs.mdx +80 -0
  45. package/docs/guides/ecosystem/docker.mdx +151 -0
  46. package/docs/guides/ecosystem/drizzle.mdx +195 -0
  47. package/docs/guides/ecosystem/edgedb.mdx +257 -0
  48. package/docs/guides/ecosystem/elysia.mdx +31 -0
  49. package/docs/guides/ecosystem/express.mdx +43 -0
  50. package/docs/guides/ecosystem/hono.mdx +47 -0
  51. package/docs/guides/ecosystem/mongoose.mdx +92 -0
  52. package/docs/guides/ecosystem/neon-drizzle.mdx +234 -0
  53. package/docs/guides/ecosystem/neon-serverless-postgres.mdx +60 -0
  54. package/docs/guides/ecosystem/nextjs.mdx +57 -0
  55. package/docs/guides/ecosystem/nuxt.mdx +90 -0
  56. package/docs/guides/ecosystem/pm2.mdx +55 -0
  57. package/docs/guides/ecosystem/prisma-postgres.mdx +169 -0
  58. package/docs/guides/ecosystem/prisma.mdx +164 -0
  59. package/docs/guides/ecosystem/qwik.mdx +114 -0
  60. package/docs/guides/ecosystem/react.mdx +52 -0
  61. package/docs/guides/ecosystem/remix.mdx +97 -0
  62. package/docs/guides/ecosystem/sentry.mdx +54 -0
  63. package/docs/guides/ecosystem/solidstart.mdx +66 -0
  64. package/docs/guides/ecosystem/ssr-react.mdx +49 -0
  65. package/docs/guides/ecosystem/stric.mdx +54 -0
  66. package/docs/guides/ecosystem/sveltekit.mdx +138 -0
  67. package/docs/guides/ecosystem/systemd.mdx +114 -0
  68. package/docs/guides/ecosystem/upstash.mdx +87 -0
  69. package/docs/guides/ecosystem/vite.mdx +77 -0
  70. package/docs/guides/html-rewriter/extract-links.mdx +72 -0
  71. package/docs/guides/html-rewriter/extract-social-meta.mdx +97 -0
  72. package/docs/guides/http/cluster.mdx +69 -0
  73. package/docs/guides/http/fetch-unix.mdx +35 -0
  74. package/docs/guides/http/fetch.mdx +26 -0
  75. package/docs/guides/http/file-uploads.mdx +97 -0
  76. package/docs/guides/http/hot.mdx +28 -0
  77. package/docs/guides/http/proxy.mdx +26 -0
  78. package/docs/guides/http/server.mdx +48 -0
  79. package/docs/guides/http/simple.mdx +20 -0
  80. package/docs/guides/http/stream-file.mdx +50 -0
  81. package/docs/guides/http/stream-iterator.mdx +49 -0
  82. package/docs/guides/http/stream-node-streams-in-bun.mdx +22 -0
  83. package/docs/guides/http/tls.mdx +32 -0
  84. package/docs/guides/index.mdx +10 -0
  85. package/docs/guides/install/add-dev.mdx +28 -0
  86. package/docs/guides/install/add-git.mdx +38 -0
  87. package/docs/guides/install/add-optional.mdx +27 -0
  88. package/docs/guides/install/add-peer.mdx +45 -0
  89. package/docs/guides/install/add-tarball.mdx +35 -0
  90. package/docs/guides/install/add.mdx +44 -0
  91. package/docs/guides/install/azure-artifacts.mdx +76 -0
  92. package/docs/guides/install/cicd.mdx +43 -0
  93. package/docs/guides/install/custom-registry.mdx +32 -0
  94. package/docs/guides/install/from-npm-install-to-bun-install.mdx +230 -0
  95. package/docs/guides/install/git-diff-bun-lockfile.mdx +47 -0
  96. package/docs/guides/install/jfrog-artifactory.mdx +28 -0
  97. package/docs/guides/install/npm-alias.mdx +25 -0
  98. package/docs/guides/install/registry-scope.mdx +40 -0
  99. package/docs/guides/install/trusted.mdx +50 -0
  100. package/docs/guides/install/workspaces.mdx +70 -0
  101. package/docs/guides/install/yarnlock.mdx +50 -0
  102. package/docs/guides/process/argv.mdx +66 -0
  103. package/docs/guides/process/ctrl-c.mdx +18 -0
  104. package/docs/guides/process/ipc.mdx +69 -0
  105. package/docs/guides/process/nanoseconds.mdx +15 -0
  106. package/docs/guides/process/os-signals.mdx +41 -0
  107. package/docs/guides/process/spawn-stderr.mdx +34 -0
  108. package/docs/guides/process/spawn-stdout.mdx +28 -0
  109. package/docs/guides/process/spawn.mdx +43 -0
  110. package/docs/guides/process/stdin.mdx +62 -0
  111. package/docs/guides/read-file/arraybuffer.mdx +30 -0
  112. package/docs/guides/read-file/buffer.mdx +21 -0
  113. package/docs/guides/read-file/exists.mdx +18 -0
  114. package/docs/guides/read-file/json.mdx +19 -0
  115. package/docs/guides/read-file/mime.mdx +22 -0
  116. package/docs/guides/read-file/stream.mdx +28 -0
  117. package/docs/guides/read-file/string.mdx +24 -0
  118. package/docs/guides/read-file/uint8array.mdx +23 -0
  119. package/docs/guides/read-file/watch.mdx +66 -0
  120. package/docs/guides/runtime/build-time-constants.mdx +295 -0
  121. package/docs/guides/runtime/cicd.mdx +45 -0
  122. package/docs/guides/runtime/codesign-macos-executable.mdx +61 -0
  123. package/docs/guides/runtime/define-constant.mdx +149 -0
  124. package/docs/guides/runtime/delete-directory.mdx +39 -0
  125. package/docs/guides/runtime/delete-file.mdx +21 -0
  126. package/docs/guides/runtime/heap-snapshot.mdx +28 -0
  127. package/docs/guides/runtime/import-html.mdx +17 -0
  128. package/docs/guides/runtime/import-json.mdx +46 -0
  129. package/docs/guides/runtime/import-toml.mdx +32 -0
  130. package/docs/guides/runtime/import-yaml.mdx +104 -0
  131. package/docs/guides/runtime/read-env.mdx +37 -0
  132. package/docs/guides/runtime/set-env.mdx +51 -0
  133. package/docs/guides/runtime/shell.mdx +42 -0
  134. package/docs/guides/runtime/timezone.mdx +38 -0
  135. package/docs/guides/runtime/tsconfig-paths.mdx +31 -0
  136. package/docs/guides/runtime/typescript.mdx +51 -0
  137. package/docs/guides/runtime/vscode-debugger.mdx +48 -0
  138. package/docs/guides/runtime/web-debugger.mdx +103 -0
  139. package/docs/guides/streams/node-readable-to-arraybuffer.mdx +13 -0
  140. package/docs/guides/streams/node-readable-to-blob.mdx +13 -0
  141. package/docs/guides/streams/node-readable-to-json.mdx +14 -0
  142. package/docs/guides/streams/node-readable-to-string.mdx +14 -0
  143. package/docs/guides/streams/node-readable-to-uint8array.mdx +13 -0
  144. package/docs/guides/streams/to-array.mdx +16 -0
  145. package/docs/guides/streams/to-arraybuffer.mdx +16 -0
  146. package/docs/guides/streams/to-blob.mdx +16 -0
  147. package/docs/guides/streams/to-buffer.mdx +17 -0
  148. package/docs/guides/streams/to-json.mdx +16 -0
  149. package/docs/guides/streams/to-string.mdx +16 -0
  150. package/docs/guides/streams/to-typedarray.mdx +24 -0
  151. package/docs/guides/test/bail.mdx +24 -0
  152. package/docs/guides/test/coverage-threshold.mdx +67 -0
  153. package/docs/guides/test/coverage.mdx +49 -0
  154. package/docs/guides/test/happy-dom.mdx +73 -0
  155. package/docs/guides/test/migrate-from-jest.mdx +125 -0
  156. package/docs/guides/test/mock-clock.mdx +50 -0
  157. package/docs/guides/test/mock-functions.mdx +70 -0
  158. package/docs/guides/test/rerun-each.mdx +16 -0
  159. package/docs/guides/test/run-tests.mdx +116 -0
  160. package/docs/guides/test/skip-tests.mdx +43 -0
  161. package/docs/guides/test/snapshot.mdx +102 -0
  162. package/docs/guides/test/spy-on.mdx +49 -0
  163. package/docs/guides/test/svelte-test.mdx +113 -0
  164. package/docs/guides/test/testing-library.mdx +93 -0
  165. package/docs/guides/test/timeout.mdx +17 -0
  166. package/docs/guides/test/todo-tests.mdx +74 -0
  167. package/docs/guides/test/update-snapshots.mdx +49 -0
  168. package/docs/guides/test/watch-mode.mdx +24 -0
  169. package/docs/guides/util/base64.mdx +17 -0
  170. package/docs/guides/util/deep-equals.mdx +41 -0
  171. package/docs/guides/util/deflate.mdx +20 -0
  172. package/docs/guides/util/detect-bun.mdx +25 -0
  173. package/docs/guides/util/entrypoint.mdx +19 -0
  174. package/docs/guides/util/escape-html.mdx +24 -0
  175. package/docs/guides/util/file-url-to-path.mdx +16 -0
  176. package/docs/guides/util/gzip.mdx +20 -0
  177. package/docs/guides/util/hash-a-password.mdx +56 -0
  178. package/docs/guides/util/import-meta-dir.mdx +15 -0
  179. package/docs/guides/util/import-meta-file.mdx +15 -0
  180. package/docs/guides/util/import-meta-path.mdx +15 -0
  181. package/docs/guides/util/javascript-uuid.mdx +25 -0
  182. package/docs/guides/util/main.mdx +43 -0
  183. package/docs/guides/util/path-to-file-url.mdx +16 -0
  184. package/docs/guides/util/sleep.mdx +24 -0
  185. package/docs/guides/util/version.mdx +23 -0
  186. package/docs/guides/util/which-path-to-executable-bin.mdx +17 -0
  187. package/docs/guides/websocket/compression.mdx +33 -0
  188. package/docs/guides/websocket/context.mdx +74 -0
  189. package/docs/guides/websocket/pubsub.mdx +40 -0
  190. package/docs/guides/websocket/simple.mdx +35 -0
  191. package/docs/guides/write-file/append.mdx +54 -0
  192. package/docs/guides/write-file/basic.mdx +46 -0
  193. package/docs/guides/write-file/blob.mdx +30 -0
  194. package/docs/guides/write-file/cat.mdx +19 -0
  195. package/docs/guides/write-file/file-cp.mdx +18 -0
  196. package/docs/guides/write-file/filesink.mdx +54 -0
  197. package/docs/guides/write-file/response.mdx +19 -0
  198. package/docs/guides/write-file/stdout.mdx +23 -0
  199. package/docs/guides/write-file/stream.mdx +19 -0
  200. package/docs/guides/write-file/unlink.mdx +18 -0
  201. package/docs/index.mdx +133 -0
  202. package/docs/installation.mdx +365 -0
  203. package/docs/pm/bunx.mdx +83 -0
  204. package/docs/pm/catalogs.mdx +292 -0
  205. package/docs/pm/cli/add.mdx +179 -0
  206. package/docs/pm/cli/audit.mdx +60 -0
  207. package/docs/pm/cli/install.mdx +471 -0
  208. package/docs/pm/cli/link.mdx +48 -0
  209. package/docs/pm/cli/outdated.mdx +197 -0
  210. package/docs/pm/cli/patch.mdx +69 -0
  211. package/docs/pm/cli/pm.mdx +319 -0
  212. package/docs/pm/cli/publish.mdx +123 -0
  213. package/docs/pm/cli/remove.mdx +16 -0
  214. package/docs/pm/cli/update.mdx +140 -0
  215. package/docs/pm/cli/why.mdx +84 -0
  216. package/docs/pm/filter.mdx +102 -0
  217. package/docs/pm/global-cache.mdx +72 -0
  218. package/docs/pm/isolated-installs.mdx +205 -0
  219. package/docs/pm/lifecycle.mdx +57 -0
  220. package/docs/pm/lockfile.mdx +64 -0
  221. package/docs/pm/npmrc.mdx +111 -0
  222. package/docs/pm/overrides.mdx +83 -0
  223. package/docs/pm/scopes-registries.mdx +35 -0
  224. package/docs/pm/security-scanner-api.mdx +95 -0
  225. package/docs/pm/workspaces.mdx +109 -0
  226. package/docs/project/benchmarking.mdx +218 -0
  227. package/docs/project/bindgen.mdx +223 -0
  228. package/docs/project/building-windows.mdx +133 -0
  229. package/docs/project/contributing.mdx +343 -0
  230. package/docs/project/feedback.mdx +20 -0
  231. package/docs/project/license.mdx +78 -0
  232. package/docs/project/roadmap.mdx +8 -0
  233. package/docs/quickstart.mdx +240 -0
  234. package/docs/runtime/auto-install.mdx +97 -0
  235. package/docs/runtime/binary-data.mdx +846 -0
  236. package/docs/runtime/bun-apis.mdx +59 -0
  237. package/docs/runtime/bunfig.mdx +642 -0
  238. package/docs/runtime/c-compiler.mdx +204 -0
  239. package/docs/runtime/child-process.mdx +532 -0
  240. package/docs/runtime/color.mdx +267 -0
  241. package/docs/runtime/console.mdx +67 -0
  242. package/docs/runtime/cookies.mdx +454 -0
  243. package/docs/runtime/debugger.mdx +335 -0
  244. package/docs/runtime/environment-variables.mdx +214 -0
  245. package/docs/runtime/ffi.mdx +565 -0
  246. package/docs/runtime/file-io.mdx +306 -0
  247. package/docs/runtime/file-system-router.mdx +118 -0
  248. package/docs/runtime/file-types.mdx +354 -0
  249. package/docs/runtime/glob.mdx +181 -0
  250. package/docs/runtime/globals.mdx +72 -0
  251. package/docs/runtime/hashing.mdx +315 -0
  252. package/docs/runtime/html-rewriter.mdx +340 -0
  253. package/docs/runtime/http/cookies.mdx +79 -0
  254. package/docs/runtime/http/error-handling.mdx +40 -0
  255. package/docs/runtime/http/metrics.mdx +36 -0
  256. package/docs/runtime/http/routing.mdx +289 -0
  257. package/docs/runtime/http/server.mdx +647 -0
  258. package/docs/runtime/http/tls.mdx +101 -0
  259. package/docs/runtime/http/websockets.mdx +404 -0
  260. package/docs/runtime/index.mdx +223 -0
  261. package/docs/runtime/jsx.mdx +115 -0
  262. package/docs/runtime/module-resolution.mdx +342 -0
  263. package/docs/runtime/networking/dns.mdx +111 -0
  264. package/docs/runtime/networking/fetch.mdx +468 -0
  265. package/docs/runtime/networking/tcp.mdx +239 -0
  266. package/docs/runtime/networking/udp.mdx +129 -0
  267. package/docs/runtime/node-api.mdx +19 -0
  268. package/docs/runtime/nodejs-compat.mdx +468 -0
  269. package/docs/runtime/plugins.mdx +405 -0
  270. package/docs/runtime/redis.mdx +582 -0
  271. package/docs/runtime/s3.mdx +863 -0
  272. package/docs/runtime/secrets.mdx +336 -0
  273. package/docs/runtime/semver.mdx +57 -0
  274. package/docs/runtime/shell.mdx +637 -0
  275. package/docs/runtime/sql.mdx +1404 -0
  276. package/docs/runtime/sqlite.mdx +699 -0
  277. package/docs/runtime/streams.mdx +232 -0
  278. package/docs/runtime/templating/create.mdx +269 -0
  279. package/docs/runtime/templating/init.mdx +58 -0
  280. package/docs/runtime/transpiler.mdx +288 -0
  281. package/docs/runtime/typescript.mdx +58 -0
  282. package/docs/runtime/utils.mdx +922 -0
  283. package/docs/runtime/watch-mode.mdx +161 -0
  284. package/docs/runtime/web-apis.mdx +29 -0
  285. package/docs/runtime/workers.mdx +328 -0
  286. package/docs/runtime/yaml.mdx +469 -0
  287. package/docs/snippets/cli/add.mdx +166 -0
  288. package/docs/snippets/cli/build.mdx +196 -0
  289. package/docs/snippets/cli/feedback.mdx +17 -0
  290. package/docs/snippets/cli/init.mdx +84 -0
  291. package/docs/snippets/cli/install.mdx +173 -0
  292. package/docs/snippets/cli/link.mdx +163 -0
  293. package/docs/snippets/cli/outdated.mdx +140 -0
  294. package/docs/snippets/cli/patch.mdx +171 -0
  295. package/docs/snippets/cli/publish.mdx +198 -0
  296. package/docs/snippets/cli/remove.mdx +146 -0
  297. package/docs/snippets/cli/run.mdx +293 -0
  298. package/docs/snippets/cli/test.mdx +100 -0
  299. package/docs/snippets/cli/update.mdx +144 -0
  300. package/docs/snippets/product-card.mdx +32 -0
  301. package/docs/snippets/product-tiles.mdx +94 -0
  302. package/docs/test/code-coverage.mdx +409 -0
  303. package/docs/test/configuration.mdx +467 -0
  304. package/docs/test/dates-times.mdx +129 -0
  305. package/docs/test/discovery.mdx +90 -0
  306. package/docs/test/dom.mdx +226 -0
  307. package/docs/test/index.mdx +380 -0
  308. package/docs/test/lifecycle.mdx +348 -0
  309. package/docs/test/mocks.mdx +637 -0
  310. package/docs/test/reporters.mdx +117 -0
  311. package/docs/test/runtime-behavior.mdx +342 -0
  312. package/docs/test/snapshots.mdx +434 -0
  313. package/docs/test/writing-tests.mdx +635 -0
  314. package/docs/typescript.mdx +54 -0
  315. package/package.json +8 -6
  316. package/test.d.ts +2 -2
@@ -0,0 +1,1404 @@
1
+ ---
2
+ title: SQL
3
+ description: Bun provides native bindings for working with SQL databases through a unified Promise-based API that supports PostgreSQL, MySQL, and SQLite.
4
+ ---
5
+
6
+ The interface is designed to be simple and performant, using tagged template literals for queries and offering features like connection pooling, transactions, and prepared statements.
7
+
8
+ ```ts title="db.ts" icon="/icons/typescript.svg"
9
+ import { sql, SQL } from "bun";
10
+
11
+ // PostgreSQL (default)
12
+ const users = await sql`
13
+ SELECT * FROM users
14
+ WHERE active = ${true}
15
+ LIMIT ${10}
16
+ `;
17
+
18
+ // With MySQL
19
+ const mysql = new SQL("mysql://user:pass@localhost:3306/mydb");
20
+ const mysqlResults = await mysql`
21
+ SELECT * FROM users
22
+ WHERE active = ${true}
23
+ `;
24
+
25
+ // With SQLite
26
+ const sqlite = new SQL("sqlite://myapp.db");
27
+ const sqliteResults = await sqlite`
28
+ SELECT * FROM users
29
+ WHERE active = ${1}
30
+ `;
31
+ ```
32
+
33
+ ### Features
34
+
35
+ - Tagged template literals to protect against SQL injection
36
+ - Transactions
37
+ - Named & positional parameters
38
+ - Connection pooling
39
+ - `BigInt` support
40
+ - SASL Auth support (SCRAM-SHA-256), MD5, and Clear Text
41
+ - Connection timeouts
42
+ - Returning rows as data objects, arrays of arrays, or Buffer
43
+ - Binary protocol support makes it faster
44
+ - TLS support (and auth mode)
45
+ - Automatic configuration with environment variable
46
+
47
+ ---
48
+
49
+ ## Database Support
50
+
51
+ `Bun.SQL` provides a unified API for multiple database systems:
52
+
53
+ ### PostgreSQL
54
+
55
+ PostgreSQL is used when:
56
+
57
+ - The connection string doesn't match SQLite or MySQL patterns (it's the fallback adapter)
58
+ - The connection string explicitly uses `postgres://` or `postgresql://` protocols
59
+ - No connection string is provided and environment variables point to PostgreSQL
60
+
61
+ ```ts title="db.ts" icon="/icons/typescript.svg"
62
+ import { sql } from "bun";
63
+ // Uses PostgreSQL if DATABASE_URL is not set or is a PostgreSQL URL
64
+ await sql`SELECT ...`;
65
+
66
+ import { SQL } from "bun";
67
+ const pg = new SQL("postgres://user:pass@localhost:5432/mydb");
68
+ await pg`SELECT ...`;
69
+ ```
70
+
71
+ ### MySQL
72
+
73
+ MySQL support is built into Bun.SQL, providing the same tagged template literal interface with full compatibility for MySQL 5.7+ and MySQL 8.0+:
74
+
75
+ ```ts title="db.ts" icon="/icons/typescript.svg"
76
+ import { SQL } from "bun";
77
+
78
+ // MySQL connection
79
+ const mysql = new SQL("mysql://user:password@localhost:3306/database");
80
+ const mysql2 = new SQL("mysql2://user:password@localhost:3306/database"); // mysql2 protocol also works
81
+
82
+ // Using options object
83
+ const mysql3 = new SQL({
84
+ adapter: "mysql",
85
+ hostname: "localhost",
86
+ port: 3306,
87
+ database: "myapp",
88
+ username: "dbuser",
89
+ password: "secretpass",
90
+ });
91
+
92
+ // Works with parameters - automatically uses prepared statements
93
+ const users = await mysql`SELECT * FROM users WHERE id = ${userId}`;
94
+
95
+ // Transactions work the same as PostgreSQL
96
+ await mysql.begin(async tx => {
97
+ await tx`INSERT INTO users (name) VALUES (${"Alice"})`;
98
+ await tx`UPDATE accounts SET balance = balance - 100 WHERE user_id = ${userId}`;
99
+ });
100
+
101
+ // Bulk inserts
102
+ const newUsers = [
103
+ { name: "Alice", email: "alice@example.com" },
104
+ { name: "Bob", email: "bob@example.com" },
105
+ ];
106
+ await mysql`INSERT INTO users ${mysql(newUsers)}`;
107
+ ```
108
+
109
+ <Accordion title="MySQL Connection String Formats">
110
+
111
+ MySQL accepts various URL formats for connection strings:
112
+
113
+ ```ts
114
+ // Standard mysql:// protocol
115
+ new SQL("mysql://user:pass@localhost:3306/database");
116
+ new SQL("mysql://user:pass@localhost/database"); // Default port 3306
117
+
118
+ // mysql2:// protocol (compatibility with mysql2 npm package)
119
+ new SQL("mysql2://user:pass@localhost:3306/database");
120
+
121
+ // With query parameters
122
+ new SQL("mysql://user:pass@localhost/db?ssl=true");
123
+
124
+ // Unix socket connection
125
+ new SQL("mysql://user:pass@/database?socket=/var/run/mysqld/mysqld.sock");
126
+ ```
127
+
128
+ </Accordion>
129
+
130
+ <Accordion title="MySQL-Specific Features">
131
+
132
+ MySQL databases support:
133
+
134
+ - **Prepared statements**: Automatically created for parameterized queries with statement caching
135
+ - **Binary protocol**: For better performance with prepared statements and accurate type handling
136
+ - **Multiple result sets**: Support for stored procedures returning multiple result sets
137
+ - **Authentication plugins**: Support for mysql_native_password, caching_sha2_password (MySQL 8.0 default), and sha256_password
138
+ - **SSL/TLS connections**: Configurable SSL modes similar to PostgreSQL
139
+ - **Connection attributes**: Client information sent to server for monitoring
140
+ - **Query pipelining**: Execute multiple prepared statements without waiting for responses
141
+
142
+ </Accordion>
143
+
144
+ ### SQLite
145
+
146
+ SQLite support is built into Bun.SQL, providing the same tagged template literal interface:
147
+
148
+ ```ts
149
+ import { SQL } from "bun";
150
+
151
+ // In-memory database
152
+ const memory = new SQL(":memory:");
153
+ const memory2 = new SQL("sqlite://:memory:");
154
+
155
+ // File-based database
156
+ const db = new SQL("sqlite://myapp.db");
157
+
158
+ // Using options object
159
+ const db2 = new SQL({
160
+ adapter: "sqlite",
161
+ filename: "./data/app.db",
162
+ });
163
+
164
+ // For simple filenames, specify adapter explicitly
165
+ const db3 = new SQL("myapp.db", { adapter: "sqlite" });
166
+ ```
167
+
168
+ <Accordion title="SQLite Connection String Formats">
169
+
170
+ SQLite accepts various URL formats for connection strings:
171
+
172
+ ```ts
173
+ // Standard sqlite:// protocol
174
+ new SQL("sqlite://path/to/database.db");
175
+ new SQL("sqlite:path/to/database.db"); // Without slashes
176
+
177
+ // file:// protocol (also recognized as SQLite)
178
+ new SQL("file://path/to/database.db");
179
+ new SQL("file:path/to/database.db");
180
+
181
+ // Special :memory: database
182
+ new SQL(":memory:");
183
+ new SQL("sqlite://:memory:");
184
+ new SQL("file://:memory:");
185
+
186
+ // Relative and absolute paths
187
+ new SQL("sqlite://./local.db"); // Relative to current directory
188
+ new SQL("sqlite://../parent/db.db"); // Parent directory
189
+ new SQL("sqlite:///absolute/path.db"); // Absolute path
190
+
191
+ // With query parameters
192
+ new SQL("sqlite://data.db?mode=ro"); // Read-only mode
193
+ new SQL("sqlite://data.db?mode=rw"); // Read-write mode (no create)
194
+ new SQL("sqlite://data.db?mode=rwc"); // Read-write-create mode (default)
195
+ ```
196
+
197
+ <Note>
198
+ Simple filenames without a protocol (like `"myapp.db"`) require explicitly specifying `{ adapter: "sqlite" }` to avoid ambiguity with PostgreSQL.
199
+ </Note>
200
+
201
+ </Accordion>
202
+
203
+ <Accordion title="SQLite-Specific Options">
204
+
205
+ SQLite databases support additional configuration options:
206
+
207
+ ```ts
208
+ const db = new SQL({
209
+ adapter: "sqlite",
210
+ filename: "app.db",
211
+
212
+ // SQLite-specific options
213
+ readonly: false, // Open in read-only mode
214
+ create: true, // Create database if it doesn't exist
215
+ readwrite: true, // Open for reading and writing
216
+
217
+ // Additional Bun:sqlite options
218
+ strict: true, // Enable strict mode
219
+ safeIntegers: false, // Use JavaScript numbers for integers
220
+ });
221
+ ```
222
+
223
+ Query parameters in the URL are parsed to set these options:
224
+
225
+ - `?mode=ro` → `readonly: true`
226
+ - `?mode=rw` → `readonly: false, create: false`
227
+ - `?mode=rwc` → `readonly: false, create: true` (default)
228
+
229
+ </Accordion>
230
+
231
+ ## Inserting data
232
+
233
+ You can pass JavaScript values directly to the SQL template literal and escaping will be handled for you.
234
+
235
+ ```ts
236
+ import { sql } from "bun";
237
+
238
+ // Basic insert with direct values
239
+ const [user] = await sql`
240
+ INSERT INTO users (name, email)
241
+ VALUES (${name}, ${email})
242
+ RETURNING *
243
+ `;
244
+
245
+ // Using object helper for cleaner syntax
246
+ const userData = {
247
+ name: "Alice",
248
+ email: "alice@example.com",
249
+ };
250
+
251
+ const [newUser] = await sql`
252
+ INSERT INTO users ${sql(userData)}
253
+ RETURNING *
254
+ `;
255
+ // Expands to: INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com')
256
+ ```
257
+
258
+ ### Bulk Insert
259
+
260
+ You can also pass arrays of objects to the SQL template literal and it will be expanded to a `INSERT INTO ... VALUES ...` statement.
261
+
262
+ ```ts
263
+ const users = [
264
+ { name: "Alice", email: "alice@example.com" },
265
+ { name: "Bob", email: "bob@example.com" },
266
+ { name: "Charlie", email: "charlie@example.com" },
267
+ ];
268
+
269
+ await sql`INSERT INTO users ${sql(users)}`;
270
+ ```
271
+
272
+ ### Picking columns to insert
273
+
274
+ You can use `sql(object, ...string)` to pick which columns to insert. Each of the columns must be defined on the object.
275
+
276
+ ```ts
277
+ const user = {
278
+ name: "Alice",
279
+ email: "alice@example.com",
280
+ age: 25,
281
+ };
282
+
283
+ await sql`INSERT INTO users ${sql(user, "name", "email")}`;
284
+ // Only inserts name and email columns, ignoring other fields
285
+ ```
286
+
287
+ ---
288
+
289
+ ## Query Results
290
+
291
+ By default, Bun's SQL client returns query results as arrays of objects, where each object represents a row with column names as keys. However, there are cases where you might want the data in a different format. The client provides two additional methods for this purpose.
292
+
293
+ ### `sql``.values()` format
294
+
295
+ The `sql``.values()` method returns rows as arrays of values rather than objects. Each row becomes an array where the values are in the same order as the columns in your query.
296
+
297
+ ```ts
298
+ const rows = await sql`SELECT * FROM users`.values();
299
+ console.log(rows);
300
+ ```
301
+
302
+ This returns something like:
303
+
304
+ ```ts
305
+ [
306
+ ["Alice", "alice@example.com"],
307
+ ["Bob", "bob@example.com"],
308
+ ];
309
+ ```
310
+
311
+ `sql``.values()` is especially useful if duplicate column names are returned in the query results. When using objects (the default), the last column name is used as the key in the object, which means duplicate column names overwrite each other &mdash; but when using `sql``.values()`, each column is present in the array so you can access the values of duplicate columns by index.
312
+
313
+ ### `sql``.raw()` format
314
+
315
+ The `.raw()` method returns rows as arrays of `Buffer` objects. This can be useful for working with binary data or for performance reasons.
316
+
317
+ ```ts
318
+ const rows = await sql`SELECT * FROM users`.raw();
319
+ console.log(rows); // [[Buffer, Buffer], [Buffer, Buffer], [Buffer, Buffer]]
320
+ ```
321
+
322
+ ---
323
+
324
+ ## SQL Fragments
325
+
326
+ A common need in database applications is the ability to construct queries dynamically based on runtime conditions. Bun provides safe ways to do this without risking SQL injection.
327
+
328
+ ### Dynamic Table Names
329
+
330
+ When you need to reference tables or schemas dynamically, use the `sql()` helper to ensure proper escaping:
331
+
332
+ ```ts
333
+ // Safely reference tables dynamically
334
+ await sql`SELECT * FROM ${sql("users")}`;
335
+
336
+ // With schema qualification
337
+ await sql`SELECT * FROM ${sql("public.users")}`;
338
+ ```
339
+
340
+ ### Conditional Queries
341
+
342
+ You can use the `sql()` helper to build queries with conditional clauses. This allows you to create flexible queries that adapt to your application's needs:
343
+
344
+ ```ts
345
+ // Optional WHERE clauses
346
+ const filterAge = true;
347
+ const minAge = 21;
348
+ const ageFilter = sql`AND age > ${minAge}`;
349
+ await sql`
350
+ SELECT * FROM users
351
+ WHERE active = ${true}
352
+ ${filterAge ? ageFilter : sql``}
353
+ `;
354
+ ```
355
+
356
+ ### Dynamic columns in updates
357
+
358
+ You can use `sql(object, ...string)` to pick which columns to update. Each of the columns must be defined on the object. If the columns are not informed all keys will be used to update the row.
359
+
360
+ ```ts
361
+ await sql`UPDATE users SET ${sql(user, "name", "email")} WHERE id = ${user.id}`;
362
+ // uses all keys from the object to update the row
363
+ await sql`UPDATE users SET ${sql(user)} WHERE id = ${user.id}`;
364
+ ```
365
+
366
+ ### Dynamic values and `where in`
367
+
368
+ Value lists can also be created dynamically, making where in queries simple too. Optionally you can pass a array of objects and inform what key to use to create the list.
369
+
370
+ ```ts
371
+ await sql`SELECT * FROM users WHERE id IN ${sql([1, 2, 3])}`;
372
+
373
+ const users = [
374
+ { id: 1, name: "Alice" },
375
+ { id: 2, name: "Bob" },
376
+ { id: 3, name: "Charlie" },
377
+ ];
378
+ await sql`SELECT * FROM users WHERE id IN ${sql(users, "id")}`;
379
+ ```
380
+
381
+ ### `sql.array` helper
382
+
383
+ The `sql.array` helper creates PostgreSQL array literals from JavaScript arrays:
384
+
385
+ ```ts
386
+ // Create array literals for PostgreSQL
387
+ await sql`INSERT INTO tags (items) VALUES (${sql.array(["red", "blue", "green"])})`;
388
+ // Generates: INSERT INTO tags (items) VALUES (ARRAY['red', 'blue', 'green'])
389
+
390
+ // Works with numeric arrays too
391
+ await sql`SELECT * FROM products WHERE ids = ANY(${sql.array([1, 2, 3])})`;
392
+ // Generates: SELECT * FROM products WHERE ids = ANY(ARRAY[1, 2, 3])
393
+ ```
394
+
395
+ <Note>`sql.array` is PostgreSQL-only. Multi-dimensional arrays and NULL elements may not be supported yet.</Note>
396
+
397
+ ---
398
+
399
+ ## `sql``.simple()`
400
+
401
+ The PostgreSQL wire protocol supports two types of queries: "simple" and "extended". Simple queries can contain multiple statements but don't support parameters, while extended queries (the default) support parameters but only allow one statement.
402
+
403
+ To run multiple statements in a single query, use `sql``.simple()`:
404
+
405
+ ```ts
406
+ // Multiple statements in one query
407
+ await sql`
408
+ SELECT 1;
409
+ SELECT 2;
410
+ `.simple();
411
+ ```
412
+
413
+ Simple queries are often useful for database migrations and setup scripts.
414
+
415
+ Note that simple queries cannot use parameters (`${value}`). If you need parameters, you must split your query into separate statements.
416
+
417
+ ### Queries in files
418
+
419
+ You can use the `sql.file` method to read a query from a file and execute it, if the file includes $1, $2, etc you can pass parameters to the query. If no parameters are used it can execute multiple commands per file.
420
+
421
+ ```ts
422
+ const result = await sql.file("query.sql", [1, 2, 3]);
423
+ ```
424
+
425
+ ### Unsafe Queries
426
+
427
+ You can use the `sql.unsafe` function to execute raw SQL strings. Use this with caution, as it will not escape user input. Executing more than one command per query is allowed if no parameters are used.
428
+
429
+ ```ts
430
+ // Multiple commands without parameters
431
+ const result = await sql.unsafe(`
432
+ SELECT ${userColumns} FROM users;
433
+ SELECT ${accountColumns} FROM accounts;
434
+ `);
435
+
436
+ // Using parameters (only one command is allowed)
437
+ const result = await sql.unsafe("SELECT " + dangerous + " FROM users WHERE id = $1", [id]);
438
+ ```
439
+
440
+ ### Execute and Cancelling Queries
441
+
442
+ Bun's SQL is lazy, which means it will only start executing when awaited or executed with `.execute()`.
443
+ You can cancel a query that is currently executing by calling the `cancel()` method on the query object.
444
+
445
+ ```ts
446
+ const query = await sql`SELECT * FROM users`.execute();
447
+ setTimeout(() => query.cancel(), 100);
448
+ await query;
449
+ ```
450
+
451
+ ---
452
+
453
+ ## Database Environment Variables
454
+
455
+ `sql` connection parameters can be configured using environment variables. The client checks these variables in a specific order of precedence and automatically detects the database type based on the connection string format.
456
+
457
+ ### Automatic Database Detection
458
+
459
+ When using `Bun.sql()` without arguments or `new SQL()` with a connection string, the adapter is automatically detected based on the URL format:
460
+
461
+ #### MySQL Auto-Detection
462
+
463
+ MySQL is automatically selected when the connection string matches these patterns:
464
+
465
+ - `mysql://...` - MySQL protocol URLs
466
+ - `mysql2://...` - MySQL2 protocol URLs (compatibility alias)
467
+
468
+ ```ts
469
+ // These all use MySQL automatically (no adapter needed)
470
+ const sql1 = new SQL("mysql://user:pass@localhost/mydb");
471
+ const sql2 = new SQL("mysql2://user:pass@localhost:3306/mydb");
472
+
473
+ // Works with DATABASE_URL environment variable
474
+ DATABASE_URL="mysql://user:pass@localhost/mydb" bun run app.js
475
+ DATABASE_URL="mysql2://user:pass@localhost:3306/mydb" bun run app.js
476
+ ```
477
+
478
+ #### SQLite Auto-Detection
479
+
480
+ SQLite is automatically selected when the connection string matches these patterns:
481
+
482
+ - `:memory:` - In-memory database
483
+ - `sqlite://...` - SQLite protocol URLs
484
+ - `sqlite:...` - SQLite protocol without slashes
485
+ - `file://...` - File protocol URLs
486
+ - `file:...` - File protocol without slashes
487
+
488
+ ```ts
489
+ // These all use SQLite automatically (no adapter needed)
490
+ const sql1 = new SQL(":memory:");
491
+ const sql2 = new SQL("sqlite://app.db");
492
+ const sql3 = new SQL("file://./database.db");
493
+
494
+ // Works with DATABASE_URL environment variable
495
+ DATABASE_URL=":memory:" bun run app.js
496
+ DATABASE_URL="sqlite://myapp.db" bun run app.js
497
+ DATABASE_URL="file://./data/app.db" bun run app.js
498
+ ```
499
+
500
+ #### PostgreSQL Auto-Detection
501
+
502
+ PostgreSQL is the default for connection strings that don't match MySQL or SQLite patterns:
503
+
504
+ ```bash
505
+ # PostgreSQL is detected for these patterns
506
+ DATABASE_URL="postgres://user:pass@localhost:5432/mydb" bun run app.js
507
+ DATABASE_URL="postgresql://user:pass@localhost:5432/mydb" bun run app.js
508
+
509
+ # Or any URL that doesn't match MySQL or SQLite patterns
510
+ DATABASE_URL="localhost:5432/mydb" bun run app.js
511
+ ```
512
+
513
+ ### MySQL Environment Variables
514
+
515
+ MySQL connections can be configured via environment variables:
516
+
517
+ ```bash
518
+ # Primary connection URL (checked first)
519
+ MYSQL_URL="mysql://user:pass@localhost:3306/mydb"
520
+
521
+ # Alternative: DATABASE_URL with MySQL protocol
522
+ DATABASE_URL="mysql://user:pass@localhost:3306/mydb"
523
+ DATABASE_URL="mysql2://user:pass@localhost:3306/mydb"
524
+ ```
525
+
526
+ If no connection URL is provided, MySQL checks these individual parameters:
527
+
528
+ | Environment Variable | Default Value | Description |
529
+ | ------------------------ | ------------- | -------------------------------- |
530
+ | `MYSQL_HOST` | `localhost` | Database host |
531
+ | `MYSQL_PORT` | `3306` | Database port |
532
+ | `MYSQL_USER` | `root` | Database user |
533
+ | `MYSQL_PASSWORD` | (empty) | Database password |
534
+ | `MYSQL_DATABASE` | `mysql` | Database name |
535
+ | `MYSQL_URL` | (empty) | Primary connection URL for MySQL |
536
+ | `TLS_MYSQL_DATABASE_URL` | (empty) | SSL/TLS-enabled connection URL |
537
+
538
+ ### PostgreSQL Environment Variables
539
+
540
+ The following environment variables can be used to define the PostgreSQL connection:
541
+
542
+ | Environment Variable | Description |
543
+ | --------------------------- | ------------------------------------------ |
544
+ | `POSTGRES_URL` | Primary connection URL for PostgreSQL |
545
+ | `DATABASE_URL` | Alternative connection URL (auto-detected) |
546
+ | `PGURL` | Alternative connection URL |
547
+ | `PG_URL` | Alternative connection URL |
548
+ | `TLS_POSTGRES_DATABASE_URL` | SSL/TLS-enabled connection URL |
549
+ | `TLS_DATABASE_URL` | Alternative SSL/TLS-enabled connection URL |
550
+
551
+ If no connection URL is provided, the system checks for the following individual parameters:
552
+
553
+ | Environment Variable | Fallback Variables | Default Value | Description |
554
+ | -------------------- | ---------------------------- | ------------- | ----------------- |
555
+ | `PGHOST` | - | `localhost` | Database host |
556
+ | `PGPORT` | - | `5432` | Database port |
557
+ | `PGUSERNAME` | `PGUSER`, `USER`, `USERNAME` | `postgres` | Database user |
558
+ | `PGPASSWORD` | - | (empty) | Database password |
559
+ | `PGDATABASE` | - | username | Database name |
560
+
561
+ ### SQLite Environment Variables
562
+
563
+ SQLite connections can be configured via `DATABASE_URL` when it contains a SQLite-compatible URL:
564
+
565
+ ```bash
566
+ # These are all recognized as SQLite
567
+ DATABASE_URL=":memory:"
568
+ DATABASE_URL="sqlite://./app.db"
569
+ DATABASE_URL="file:///absolute/path/to/db.sqlite"
570
+ ```
571
+
572
+ **Note:** PostgreSQL-specific environment variables (`POSTGRES_URL`, `PGHOST`, etc.) are ignored when using SQLite.
573
+
574
+ ---
575
+
576
+ ## Runtime Preconnection
577
+
578
+ Bun can preconnect to PostgreSQL at startup to improve performance by establishing database connections before your application code runs. This is useful for reducing connection latency on the first database query.
579
+
580
+ ```bash
581
+ # Enable PostgreSQL preconnection
582
+ bun --sql-preconnect index.js
583
+
584
+ # Works with DATABASE_URL environment variable
585
+ DATABASE_URL=postgres://user:pass@localhost:5432/db bun --sql-preconnect index.js
586
+
587
+ # Can be combined with other runtime flags
588
+ bun --sql-preconnect --hot index.js
589
+ ```
590
+
591
+ The `--sql-preconnect` flag will automatically establish a PostgreSQL connection using your configured environment variables at startup. If the connection fails, it won't crash your application - the error will be handled gracefully.
592
+
593
+ ---
594
+
595
+ ## Connection Options
596
+
597
+ You can configure your database connection manually by passing options to the SQL constructor. Options vary depending on the database adapter:
598
+
599
+ ### MySQL Options
600
+
601
+ ```ts
602
+ import { SQL } from "bun";
603
+
604
+ const db = new SQL({
605
+ // Required for MySQL when using options object
606
+ adapter: "mysql",
607
+
608
+ // Connection details
609
+ hostname: "localhost",
610
+ port: 3306,
611
+ database: "myapp",
612
+ username: "dbuser",
613
+ password: "secretpass",
614
+
615
+ // Unix socket connection (alternative to hostname/port)
616
+ // socket: "/var/run/mysqld/mysqld.sock",
617
+
618
+ // Connection pool settings
619
+ max: 20, // Maximum connections in pool (default: 10)
620
+ idleTimeout: 30, // Close idle connections after 30s
621
+ maxLifetime: 0, // Connection lifetime in seconds (0 = forever)
622
+ connectionTimeout: 30, // Timeout when establishing new connections
623
+
624
+ // SSL/TLS options
625
+ ssl: "prefer", // or "disable", "require", "verify-ca", "verify-full"
626
+ // tls: {
627
+ // rejectUnauthorized: true,
628
+ // ca: "path/to/ca.pem",
629
+ // key: "path/to/key.pem",
630
+ // cert: "path/to/cert.pem",
631
+ // },
632
+
633
+ // Callbacks
634
+ onconnect: client => {
635
+ console.log("Connected to MySQL");
636
+ },
637
+ onclose: (client, err) => {
638
+ if (err) {
639
+ console.error("MySQL connection error:", err);
640
+ } else {
641
+ console.log("MySQL connection closed");
642
+ }
643
+ },
644
+ });
645
+ ```
646
+
647
+ ### PostgreSQL Options
648
+
649
+ ```ts
650
+ import { SQL } from "bun";
651
+
652
+ const db = new SQL({
653
+ // Connection details (adapter is auto-detected as PostgreSQL)
654
+ url: "postgres://user:pass@localhost:5432/dbname",
655
+
656
+ // Alternative connection parameters
657
+ hostname: "localhost",
658
+ port: 5432,
659
+ database: "myapp",
660
+ username: "dbuser",
661
+ password: "secretpass",
662
+
663
+ // Connection pool settings
664
+ max: 20, // Maximum connections in pool
665
+ idleTimeout: 30, // Close idle connections after 30s
666
+ maxLifetime: 0, // Connection lifetime in seconds (0 = forever)
667
+ connectionTimeout: 30, // Timeout when establishing new connections
668
+
669
+ // SSL/TLS options
670
+ tls: true,
671
+ // tls: {
672
+ // rejectUnauthorized: true,
673
+ // requestCert: true,
674
+ // ca: "path/to/ca.pem",
675
+ // key: "path/to/key.pem",
676
+ // cert: "path/to/cert.pem",
677
+ // checkServerIdentity(hostname, cert) {
678
+ // ...
679
+ // },
680
+ // },
681
+
682
+ // Callbacks
683
+ onconnect: client => {
684
+ console.log("Connected to PostgreSQL");
685
+ },
686
+ onclose: client => {
687
+ console.log("PostgreSQL connection closed");
688
+ },
689
+ });
690
+ ```
691
+
692
+ ### SQLite Options
693
+
694
+ ```ts
695
+ import { SQL } from "bun";
696
+
697
+ const db = new SQL({
698
+ // Required for SQLite
699
+ adapter: "sqlite",
700
+ filename: "./data/app.db", // or ":memory:" for in-memory database
701
+
702
+ // SQLite-specific access modes
703
+ readonly: false, // Open in read-only mode
704
+ create: true, // Create database if it doesn't exist
705
+ readwrite: true, // Allow read and write operations
706
+
707
+ // SQLite data handling
708
+ strict: true, // Enable strict mode for better type safety
709
+ safeIntegers: false, // Use BigInt for integers exceeding JS number range
710
+
711
+ // Callbacks
712
+ onconnect: client => {
713
+ console.log("SQLite database opened");
714
+ },
715
+ onclose: client => {
716
+ console.log("SQLite database closed");
717
+ },
718
+ });
719
+ ```
720
+
721
+ <Accordion title="SQLite Connection Notes">
722
+
723
+ - **Connection Pooling**: SQLite doesn't use connection pooling as it's a file-based database. Each `SQL` instance represents a single connection.
724
+ - **Transactions**: SQLite supports nested transactions through savepoints, similar to PostgreSQL.
725
+ - **Concurrent Access**: SQLite handles concurrent access through file locking. Use WAL mode for better concurrency.
726
+ - **Memory Databases**: Using `:memory:` creates a temporary database that exists only for the connection lifetime.
727
+
728
+ </Accordion>
729
+
730
+ ---
731
+
732
+ ## Dynamic passwords
733
+
734
+ When clients need to use alternative authentication schemes such as access tokens or connections to databases with rotating passwords, provide either a synchronous or asynchronous function that will resolve the dynamic password value at connection time.
735
+
736
+ ```ts
737
+ import { SQL } from "bun";
738
+
739
+ const sql = new SQL(url, {
740
+ // Other connection config
741
+ ...
742
+ // Password function for the database user
743
+ password: async () => await signer.getAuthToken(),
744
+ });
745
+ ```
746
+
747
+ ---
748
+
749
+ ## SQLite-Specific Features
750
+
751
+ ### Query Execution
752
+
753
+ SQLite executes queries synchronously, unlike PostgreSQL which uses asynchronous I/O. However, the API remains consistent using Promises:
754
+
755
+ ```ts
756
+ const sqlite = new SQL("sqlite://app.db");
757
+
758
+ // Works the same as PostgreSQL, but executes synchronously under the hood
759
+ const users = await sqlite`SELECT * FROM users`;
760
+
761
+ // Parameters work identically
762
+ const user = await sqlite`SELECT * FROM users WHERE id = ${userId}`;
763
+ ```
764
+
765
+ ### SQLite Pragmas
766
+
767
+ You can use PRAGMA statements to configure SQLite behavior:
768
+
769
+ ```ts
770
+ const sqlite = new SQL("sqlite://app.db");
771
+
772
+ // Enable foreign keys
773
+ await sqlite`PRAGMA foreign_keys = ON`;
774
+
775
+ // Set journal mode to WAL for better concurrency
776
+ await sqlite`PRAGMA journal_mode = WAL`;
777
+
778
+ // Check integrity
779
+ const integrity = await sqlite`PRAGMA integrity_check`;
780
+ ```
781
+
782
+ ### Data Type Differences
783
+
784
+ SQLite has a more flexible type system than PostgreSQL:
785
+
786
+ ```ts
787
+ // SQLite stores data in 5 storage classes: NULL, INTEGER, REAL, TEXT, BLOB
788
+ const sqlite = new SQL("sqlite://app.db");
789
+
790
+ // SQLite is more lenient with types
791
+ await sqlite`
792
+ CREATE TABLE flexible (
793
+ id INTEGER PRIMARY KEY,
794
+ data TEXT, -- Can store numbers as strings
795
+ value NUMERIC, -- Can store integers, reals, or text
796
+ blob BLOB -- Binary data
797
+ )
798
+ `;
799
+
800
+ // JavaScript values are automatically converted
801
+ await sqlite`INSERT INTO flexible VALUES (${1}, ${"text"}, ${123.45}, ${Buffer.from("binary")})`;
802
+ ```
803
+
804
+ ---
805
+
806
+ ## Transactions
807
+
808
+ To start a new transaction, use `sql.begin`. This method works for both PostgreSQL and SQLite. For PostgreSQL, it reserves a dedicated connection from the pool. For SQLite, it begins a transaction on the single connection.
809
+
810
+ The `BEGIN` command is sent automatically, including any optional configurations you specify. If an error occurs during the transaction, a `ROLLBACK` is triggered to ensure the process continues smoothly.
811
+
812
+ ### Basic Transactions
813
+
814
+ ```ts
815
+ await sql.begin(async tx => {
816
+ // All queries in this function run in a transaction
817
+ await tx`INSERT INTO users (name) VALUES (${"Alice"})`;
818
+ await tx`UPDATE accounts SET balance = balance - 100 WHERE user_id = 1`;
819
+
820
+ // Transaction automatically commits if no errors are thrown
821
+ // Rolls back if any error occurs
822
+ });
823
+ ```
824
+
825
+ It's also possible to pipeline the requests in a transaction if needed by returning an array with queries from the callback function like this:
826
+
827
+ ```ts
828
+ await sql.begin(async tx => {
829
+ return [
830
+ tx`INSERT INTO users (name) VALUES (${"Alice"})`,
831
+ tx`UPDATE accounts SET balance = balance - 100 WHERE user_id = 1`,
832
+ ];
833
+ });
834
+ ```
835
+
836
+ ### Savepoints
837
+
838
+ Savepoints in SQL create intermediate checkpoints within a transaction, enabling partial rollbacks without affecting the entire operation. They are useful in complex transactions, allowing error recovery and maintaining consistent results.
839
+
840
+ ```ts
841
+ await sql.begin(async tx => {
842
+ await tx`INSERT INTO users (name) VALUES (${"Alice"})`;
843
+
844
+ await tx.savepoint(async sp => {
845
+ // This part can be rolled back separately
846
+ await sp`UPDATE users SET status = 'active'`;
847
+ if (someCondition) {
848
+ throw new Error("Rollback to savepoint");
849
+ }
850
+ });
851
+
852
+ // Continue with transaction even if savepoint rolled back
853
+ await tx`INSERT INTO audit_log (action) VALUES ('user_created')`;
854
+ });
855
+ ```
856
+
857
+ ### Distributed Transactions
858
+
859
+ Two-Phase Commit (2PC) is a distributed transaction protocol where Phase 1 has the coordinator preparing nodes by ensuring data is written and ready to commit, while Phase 2 finalizes with nodes either committing or rolling back based on the coordinator's decision. This process ensures data durability and proper lock management.
860
+
861
+ In PostgreSQL and MySQL, distributed transactions persist beyond their original session, allowing privileged users or coordinators to commit or rollback them later. This supports robust distributed transactions, recovery processes, and administrative operations.
862
+
863
+ Each database system implements distributed transactions differently:
864
+
865
+ PostgreSQL natively supports them through prepared transactions, while MySQL uses XA Transactions.
866
+
867
+ If any exceptions occur during the distributed transaction and aren't caught, the system will automatically rollback all changes. When everything proceeds normally, you maintain the flexibility to either commit or rollback the transaction later.
868
+
869
+ ```ts
870
+ // Begin a distributed transaction
871
+ await sql.beginDistributed("tx1", async tx => {
872
+ await tx`INSERT INTO users (name) VALUES (${"Alice"})`;
873
+ });
874
+
875
+ // Later, commit or rollback
876
+ await sql.commitDistributed("tx1");
877
+ // or
878
+ await sql.rollbackDistributed("tx1");
879
+ ```
880
+
881
+ ---
882
+
883
+ ## Authentication
884
+
885
+ Bun supports SCRAM-SHA-256 (SASL), MD5, and Clear Text authentication. SASL is recommended for better security. Check [Postgres SASL Authentication](https://www.postgresql.org/docs/current/sasl-authentication.html) for more information.
886
+
887
+ ### SSL Modes Overview
888
+
889
+ PostgreSQL supports different SSL/TLS modes to control how secure connections are established. These modes determine the behavior when connecting and the level of certificate verification performed.
890
+
891
+ ```ts
892
+ const sql = new SQL({
893
+ hostname: "localhost",
894
+ username: "user",
895
+ password: "password",
896
+ ssl: "disable", // | "prefer" | "require" | "verify-ca" | "verify-full"
897
+ });
898
+ ```
899
+
900
+ | SSL Mode | Description |
901
+ | ------------- | -------------------------------------------------------------------------------------------------------------------- |
902
+ | `disable` | No SSL/TLS used. Connections fail if server requires SSL. |
903
+ | `prefer` | Tries SSL first, falls back to non-SSL if SSL fails. Default mode if none specified. |
904
+ | `require` | Requires SSL without certificate verification. Fails if SSL cannot be established. |
905
+ | `verify-ca` | Verifies server certificate is signed by trusted CA. Fails if verification fails. |
906
+ | `verify-full` | Most secure mode. Verifies certificate and hostname match. Protects against untrusted certificates and MITM attacks. |
907
+
908
+ ### Using With Connection Strings
909
+
910
+ The SSL mode can also be specified in connection strings:
911
+
912
+ ```ts
913
+ // Using prefer mode
914
+ const sql = new SQL("postgres://user:password@localhost/mydb?sslmode=prefer");
915
+
916
+ // Using verify-full mode
917
+ const sql = new SQL("postgres://user:password@localhost/mydb?sslmode=verify-full");
918
+ ```
919
+
920
+ ---
921
+
922
+ ## Connection Pooling
923
+
924
+ Bun's SQL client automatically manages a connection pool, which is a pool of database connections that are reused for multiple queries. This helps to reduce the overhead of establishing and closing connections for each query, and it also helps to manage the number of concurrent connections to the database.
925
+
926
+ ```ts
927
+ const db = new SQL({
928
+ // Pool configuration
929
+ max: 20, // Maximum 20 concurrent connections
930
+ idleTimeout: 30, // Close idle connections after 30s
931
+ maxLifetime: 3600, // Max connection lifetime 1 hour
932
+ connectionTimeout: 10, // Connection timeout 10s
933
+ });
934
+ ```
935
+
936
+ No connection will be made until a query is made.
937
+
938
+ ```ts
939
+ const sql = Bun.sql(); // no connection are created
940
+
941
+ await sql`...`; // pool is started until max is reached (if possible), first available connection is used
942
+ await sql`...`; // previous connection is reused
943
+
944
+ // two connections are used now at the same time
945
+ await Promise.all([
946
+ sql`INSERT INTO users ${sql({ name: "Alice" })}`,
947
+ sql`UPDATE users SET name = ${user.name} WHERE id = ${user.id}`,
948
+ ]);
949
+
950
+ await sql.close(); // await all queries to finish and close all connections from the pool
951
+ await sql.close({ timeout: 5 }); // wait 5 seconds and close all connections from the pool
952
+ await sql.close({ timeout: 0 }); // close all connections from the pool immediately
953
+ ```
954
+
955
+ ---
956
+
957
+ ## Reserved Connections
958
+
959
+ Bun enables you to reserve a connection from the pool, and returns a client that wraps the single connection. This can be used for running queries on an isolated connection.
960
+
961
+ ```ts
962
+ // Get exclusive connection from pool
963
+ const reserved = await sql.reserve();
964
+
965
+ try {
966
+ await reserved`INSERT INTO users (name) VALUES (${"Alice"})`;
967
+ } finally {
968
+ // Important: Release connection back to pool
969
+ reserved.release();
970
+ }
971
+
972
+ // Or using Symbol.dispose
973
+ {
974
+ using reserved = await sql.reserve();
975
+ await reserved`SELECT 1`;
976
+ } // Automatically released
977
+ ```
978
+
979
+ ---
980
+
981
+ ## Prepared Statements
982
+
983
+ By default, Bun's SQL client automatically creates named prepared statements for queries where it can be inferred that the query is static. This provides better performance. However, you can change this behavior by setting `prepare: false` in the connection options:
984
+
985
+ ```ts
986
+ const sql = new SQL({
987
+ // ... other options ...
988
+ prepare: false, // Disable persisting named prepared statements on the server
989
+ });
990
+ ```
991
+
992
+ When `prepare: false` is set:
993
+
994
+ Queries are still executed using the "extended" protocol, but they are executed using [unnamed prepared statements](https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY), an unnamed prepared statement lasts only until the next Parse statement specifying the unnamed statement as destination is issued.
995
+
996
+ - Parameter binding is still safe against SQL injection
997
+ - Each query is parsed and planned from scratch by the server
998
+ - Queries will not be [pipelined](https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-PIPELINING)
999
+
1000
+ You might want to use `prepare: false` when:
1001
+
1002
+ - Using PGBouncer in transaction mode (though since PGBouncer 1.21.0, protocol-level named prepared statements are supported when configured properly)
1003
+ - Debugging query execution plans
1004
+ - Working with dynamic SQL where query plans need to be regenerated frequently
1005
+ - More than one command per query will not be supported (unless you use `sql``.simple()`)
1006
+
1007
+ Note that disabling prepared statements may impact performance for queries that are executed frequently with different parameters, as the server needs to parse and plan each query from scratch.
1008
+
1009
+ ---
1010
+
1011
+ ## Error Handling
1012
+
1013
+ The client provides typed errors for different failure scenarios. Errors are database-specific and extend from base error classes:
1014
+
1015
+ ### Error Classes
1016
+
1017
+ ```ts
1018
+ import { SQL } from "bun";
1019
+
1020
+ try {
1021
+ await sql`SELECT * FROM users`;
1022
+ } catch (error) {
1023
+ if (error instanceof SQL.PostgresError) {
1024
+ // PostgreSQL-specific error
1025
+ console.log(error.code); // PostgreSQL error code
1026
+ console.log(error.detail); // Detailed error message
1027
+ console.log(error.hint); // Helpful hint from PostgreSQL
1028
+ } else if (error instanceof SQL.SQLiteError) {
1029
+ // SQLite-specific error
1030
+ console.log(error.code); // SQLite error code (e.g., "SQLITE_CONSTRAINT")
1031
+ console.log(error.errno); // SQLite error number
1032
+ console.log(error.byteOffset); // Byte offset in SQL statement (if available)
1033
+ } else if (error instanceof SQL.SQLError) {
1034
+ // Generic SQL error (base class)
1035
+ console.log(error.message);
1036
+ }
1037
+ }
1038
+ ```
1039
+
1040
+ <Accordion title="PostgreSQL-Specific Error Codes">
1041
+
1042
+ ### PostgreSQL Connection Errors
1043
+
1044
+ | Connection Errors | Description |
1045
+ | --------------------------------- | ---------------------------------------------------- |
1046
+ | `ERR_POSTGRES_CONNECTION_CLOSED` | Connection was terminated or never established |
1047
+ | `ERR_POSTGRES_CONNECTION_TIMEOUT` | Failed to establish connection within timeout period |
1048
+ | `ERR_POSTGRES_IDLE_TIMEOUT` | Connection closed due to inactivity |
1049
+ | `ERR_POSTGRES_LIFETIME_TIMEOUT` | Connection exceeded maximum lifetime |
1050
+ | `ERR_POSTGRES_TLS_NOT_AVAILABLE` | SSL/TLS connection not available |
1051
+ | `ERR_POSTGRES_TLS_UPGRADE_FAILED` | Failed to upgrade connection to SSL/TLS |
1052
+
1053
+ ### Authentication Errors
1054
+
1055
+ | Authentication Errors | Description |
1056
+ | ------------------------------------------------ | ---------------------------------------- |
1057
+ | `ERR_POSTGRES_AUTHENTICATION_FAILED_PBKDF2` | Password authentication failed |
1058
+ | `ERR_POSTGRES_UNKNOWN_AUTHENTICATION_METHOD` | Server requested unknown auth method |
1059
+ | `ERR_POSTGRES_UNSUPPORTED_AUTHENTICATION_METHOD` | Server requested unsupported auth method |
1060
+ | `ERR_POSTGRES_INVALID_SERVER_KEY` | Invalid server key during authentication |
1061
+ | `ERR_POSTGRES_INVALID_SERVER_SIGNATURE` | Invalid server signature |
1062
+ | `ERR_POSTGRES_SASL_SIGNATURE_INVALID_BASE64` | Invalid SASL signature encoding |
1063
+ | `ERR_POSTGRES_SASL_SIGNATURE_MISMATCH` | SASL signature verification failed |
1064
+
1065
+ ### Query Errors
1066
+
1067
+ | Query Errors | Description |
1068
+ | ------------------------------------ | ------------------------------------------ |
1069
+ | `ERR_POSTGRES_SYNTAX_ERROR` | Invalid SQL syntax (extends `SyntaxError`) |
1070
+ | `ERR_POSTGRES_SERVER_ERROR` | General error from PostgreSQL server |
1071
+ | `ERR_POSTGRES_INVALID_QUERY_BINDING` | Invalid parameter binding |
1072
+ | `ERR_POSTGRES_QUERY_CANCELLED` | Query was cancelled |
1073
+ | `ERR_POSTGRES_NOT_TAGGED_CALL` | Query was called without a tagged call |
1074
+
1075
+ ### Data Type Errors
1076
+
1077
+ | Data Type Errors | Description |
1078
+ | ------------------------------------------------------- | ------------------------------------- |
1079
+ | `ERR_POSTGRES_INVALID_BINARY_DATA` | Invalid binary data format |
1080
+ | `ERR_POSTGRES_INVALID_BYTE_SEQUENCE` | Invalid byte sequence |
1081
+ | `ERR_POSTGRES_INVALID_BYTE_SEQUENCE_FOR_ENCODING` | Encoding error |
1082
+ | `ERR_POSTGRES_INVALID_CHARACTER` | Invalid character in data |
1083
+ | `ERR_POSTGRES_OVERFLOW` | Numeric overflow |
1084
+ | `ERR_POSTGRES_UNSUPPORTED_BYTEA_FORMAT` | Unsupported binary format |
1085
+ | `ERR_POSTGRES_UNSUPPORTED_INTEGER_SIZE` | Integer size not supported |
1086
+ | `ERR_POSTGRES_MULTIDIMENSIONAL_ARRAY_NOT_SUPPORTED_YET` | Multidimensional arrays not supported |
1087
+ | `ERR_POSTGRES_NULLS_IN_ARRAY_NOT_SUPPORTED_YET` | NULL values in arrays not supported |
1088
+
1089
+ ### Protocol Errors
1090
+
1091
+ | Protocol Errors | Description |
1092
+ | --------------------------------------- | --------------------------- |
1093
+ | `ERR_POSTGRES_EXPECTED_REQUEST` | Expected client request |
1094
+ | `ERR_POSTGRES_EXPECTED_STATEMENT` | Expected prepared statement |
1095
+ | `ERR_POSTGRES_INVALID_BACKEND_KEY_DATA` | Invalid backend key data |
1096
+ | `ERR_POSTGRES_INVALID_MESSAGE` | Invalid protocol message |
1097
+ | `ERR_POSTGRES_INVALID_MESSAGE_LENGTH` | Invalid message length |
1098
+ | `ERR_POSTGRES_UNEXPECTED_MESSAGE` | Unexpected message type |
1099
+
1100
+ ### Transaction Errors
1101
+
1102
+ | Transaction Errors | Description |
1103
+ | ---------------------------------------- | ------------------------------------- |
1104
+ | `ERR_POSTGRES_UNSAFE_TRANSACTION` | Unsafe transaction operation detected |
1105
+ | `ERR_POSTGRES_INVALID_TRANSACTION_STATE` | Invalid transaction state |
1106
+
1107
+ </Accordion>
1108
+
1109
+ ### SQLite-Specific Errors
1110
+
1111
+ SQLite errors provide error codes and numbers that correspond to SQLite's standard error codes:
1112
+
1113
+ <Accordion title="Common SQLite Error Codes">
1114
+
1115
+ | Error Code | errno | Description |
1116
+ | ------------------- | ----- | ---------------------------------------------------- |
1117
+ | `SQLITE_CONSTRAINT` | 19 | Constraint violation (UNIQUE, CHECK, NOT NULL, etc.) |
1118
+ | `SQLITE_BUSY` | 5 | Database is locked |
1119
+ | `SQLITE_LOCKED` | 6 | Table in the database is locked |
1120
+ | `SQLITE_READONLY` | 8 | Attempt to write to a readonly database |
1121
+ | `SQLITE_IOERR` | 10 | Disk I/O error |
1122
+ | `SQLITE_CORRUPT` | 11 | Database disk image is malformed |
1123
+ | `SQLITE_FULL` | 13 | Database or disk is full |
1124
+ | `SQLITE_CANTOPEN` | 14 | Unable to open database file |
1125
+ | `SQLITE_PROTOCOL` | 15 | Database lock protocol error |
1126
+ | `SQLITE_SCHEMA` | 17 | Database schema has changed |
1127
+ | `SQLITE_TOOBIG` | 18 | String or BLOB exceeds size limit |
1128
+ | `SQLITE_MISMATCH` | 20 | Data type mismatch |
1129
+ | `SQLITE_MISUSE` | 21 | Library used incorrectly |
1130
+ | `SQLITE_AUTH` | 23 | Authorization denied |
1131
+
1132
+ Example error handling:
1133
+
1134
+ ```ts
1135
+ const sqlite = new SQL("sqlite://app.db");
1136
+
1137
+ try {
1138
+ await sqlite`INSERT INTO users (id, name) VALUES (1, 'Alice')`;
1139
+ await sqlite`INSERT INTO users (id, name) VALUES (1, 'Bob')`; // Duplicate ID
1140
+ } catch (error) {
1141
+ if (error instanceof SQL.SQLiteError) {
1142
+ if (error.code === "SQLITE_CONSTRAINT") {
1143
+ console.log("Constraint violation:", error.message);
1144
+ // Handle unique constraint violation
1145
+ }
1146
+ }
1147
+ }
1148
+ ```
1149
+
1150
+ </Accordion>
1151
+
1152
+ ---
1153
+
1154
+ ## Numbers and BigInt
1155
+
1156
+ Bun's SQL client includes special handling for large numbers that exceed the range of a 53-bit integer. Here's how it works:
1157
+
1158
+ ```ts
1159
+ import { sql } from "bun";
1160
+
1161
+ const [{ x, y }] = await sql`SELECT 9223372036854777 as x, 12345 as y`;
1162
+
1163
+ console.log(typeof x, x); // "string" "9223372036854777"
1164
+ console.log(typeof y, y); // "number" 12345
1165
+ ```
1166
+
1167
+ ---
1168
+
1169
+ ## BigInt Instead of Strings
1170
+
1171
+ If you need large numbers as BigInt instead of strings, you can enable this by setting the `bigint` option to `true` when initializing the SQL client:
1172
+
1173
+ ```ts
1174
+ const sql = new SQL({
1175
+ bigint: true,
1176
+ });
1177
+
1178
+ const [{ x }] = await sql`SELECT 9223372036854777 as x`;
1179
+
1180
+ console.log(typeof x, x); // "bigint" 9223372036854777n
1181
+ ```
1182
+
1183
+ ---
1184
+
1185
+ ## Roadmap
1186
+
1187
+ There's still some things we haven't finished yet.
1188
+
1189
+ - Connection preloading via `--db-preconnect` Bun CLI flag
1190
+ - Column name transforms (e.g. `snake_case` to `camelCase`). This is mostly blocked on a unicode-aware implementation of changing the case in C++ using WebKit's `WTF::String`.
1191
+ - Column type transforms
1192
+
1193
+ ---
1194
+
1195
+ ## Database-Specific Features
1196
+
1197
+ #### Authentication Methods
1198
+
1199
+ MySQL supports multiple authentication plugins that are automatically negotiated:
1200
+
1201
+ - **`mysql_native_password`** - Traditional MySQL authentication, widely compatible
1202
+ - **`caching_sha2_password`** - Default in MySQL 8.0+, more secure with RSA key exchange
1203
+ - **`sha256_password`** - SHA-256 based authentication
1204
+
1205
+ The client automatically handles authentication plugin switching when requested by the server, including secure password exchange over non-SSL connections.
1206
+
1207
+ #### Prepared Statements & Performance
1208
+
1209
+ MySQL uses server-side prepared statements for all parameterized queries:
1210
+
1211
+ ```ts
1212
+ // This automatically creates a prepared statement on the server
1213
+ const user = await mysql`SELECT * FROM users WHERE id = ${userId}`;
1214
+
1215
+ // Prepared statements are cached and reused for identical queries
1216
+ for (const id of userIds) {
1217
+ // Same prepared statement is reused
1218
+ await mysql`SELECT * FROM users WHERE id = ${id}`;
1219
+ }
1220
+
1221
+ // Query pipelining - multiple statements sent without waiting
1222
+ const [users, orders, products] = await Promise.all([
1223
+ mysql`SELECT * FROM users WHERE active = ${true}`,
1224
+ mysql`SELECT * FROM orders WHERE status = ${"pending"}`,
1225
+ mysql`SELECT * FROM products WHERE in_stock = ${true}`,
1226
+ ]);
1227
+ ```
1228
+
1229
+ #### Multiple Result Sets
1230
+
1231
+ MySQL can return multiple result sets from multi-statement queries:
1232
+
1233
+ ```ts
1234
+ const mysql = new SQL("mysql://user:pass@localhost/mydb");
1235
+
1236
+ // Multi-statement queries with simple() method
1237
+ const multiResults = await mysql`
1238
+ SELECT * FROM users WHERE id = 1;
1239
+ SELECT * FROM orders WHERE user_id = 1;
1240
+ `.simple();
1241
+ ```
1242
+
1243
+ #### Character Sets & Collations
1244
+
1245
+ Bun.SQL automatically uses `utf8mb4` character set for MySQL connections, ensuring full Unicode support including emojis. This is the recommended character set for modern MySQL applications.
1246
+
1247
+ #### Connection Attributes
1248
+
1249
+ Bun automatically sends client information to MySQL for better monitoring:
1250
+
1251
+ ```ts
1252
+ // These attributes are sent automatically:
1253
+ // _client_name: "Bun"
1254
+ // _client_version: <bun version>
1255
+ // You can see these in MySQL's performance_schema.session_connect_attrs
1256
+ ```
1257
+
1258
+ #### Type Handling
1259
+
1260
+ MySQL types are automatically converted to JavaScript types:
1261
+
1262
+ | MySQL Type | JavaScript Type | Notes |
1263
+ | --------------------------------------- | ------------------------ | ---------------------------------------------------------------------------------------------------- |
1264
+ | INT, TINYINT, MEDIUMINT | number | Within safe integer range |
1265
+ | BIGINT | string, number or BigInt | If the value fits in i32/u32 size will be number otherwise string or BigInt Based on `bigint` option |
1266
+ | DECIMAL, NUMERIC | string | To preserve precision |
1267
+ | FLOAT, DOUBLE | number | |
1268
+ | DATE | Date | JavaScript Date object |
1269
+ | DATETIME, TIMESTAMP | Date | With timezone handling |
1270
+ | TIME | number | Total of microseconds |
1271
+ | YEAR | number | |
1272
+ | CHAR, VARCHAR, VARSTRING, STRING | string | |
1273
+ | TINY TEXT, MEDIUM TEXT, TEXT, LONG TEXT | string | |
1274
+ | TINY BLOB, MEDIUM BLOB, BLOG, LONG BLOB | string | BLOB Types are alias for TEXT types |
1275
+ | JSON | object/array | Automatically parsed |
1276
+ | BIT(1) | boolean | BIT(1) in MySQL |
1277
+ | GEOMETRY | string | Geometry data |
1278
+
1279
+ #### Differences from PostgreSQL
1280
+
1281
+ While the API is unified, there are some behavioral differences:
1282
+
1283
+ 1. **Parameter placeholders**: MySQL uses `?` internally but Bun converts `$1, $2` style automatically
1284
+ 2. **RETURNING clause**: MySQL doesn't support RETURNING; use `result.lastInsertRowid` or a separate SELECT
1285
+ 3. **Array types**: MySQL doesn't have native array types like PostgreSQL
1286
+
1287
+ ### MySQL-Specific Features
1288
+
1289
+ We haven't implemented `LOAD DATA INFILE` support yet
1290
+
1291
+ ### PostgreSQL-Specific Features
1292
+
1293
+ We haven't implemented these yet:
1294
+
1295
+ - `COPY` support
1296
+ - `LISTEN` support
1297
+ - `NOTIFY` support
1298
+
1299
+ We also haven't implemented some of the more uncommon features like:
1300
+
1301
+ - GSSAPI authentication
1302
+ - `SCRAM-SHA-256-PLUS` support
1303
+ - Point & PostGIS types
1304
+ - All the multi-dimensional integer array types (only a couple of the types are supported)
1305
+
1306
+ ---
1307
+
1308
+ ## Common Patterns & Best Practices
1309
+
1310
+ ### Working with MySQL Result Sets
1311
+
1312
+ ```ts
1313
+ // Getting insert ID after INSERT
1314
+ const result = await mysql`INSERT INTO users (name) VALUES (${"Alice"})`;
1315
+ console.log(result.lastInsertRowid); // MySQL's LAST_INSERT_ID()
1316
+
1317
+ // Handling affected rows
1318
+ const updated = await mysql`UPDATE users SET active = ${false} WHERE age < ${18}`;
1319
+ console.log(updated.affectedRows); // Number of rows updated
1320
+
1321
+ // Using MySQL-specific functions
1322
+ const now = await mysql`SELECT NOW() as current_time`;
1323
+ const uuid = await mysql`SELECT UUID() as id`;
1324
+ ```
1325
+
1326
+ ### MySQL Error Handling
1327
+
1328
+ ```ts
1329
+ try {
1330
+ await mysql`INSERT INTO users (email) VALUES (${"duplicate@email.com"})`;
1331
+ } catch (error) {
1332
+ if (error.code === "ER_DUP_ENTRY") {
1333
+ console.log("Duplicate entry detected");
1334
+ } else if (error.code === "ER_ACCESS_DENIED_ERROR") {
1335
+ console.log("Access denied");
1336
+ } else if (error.code === "ER_BAD_DB_ERROR") {
1337
+ console.log("Database does not exist");
1338
+ }
1339
+ // MySQL error codes are compatible with mysql/mysql2 packages
1340
+ }
1341
+ ```
1342
+
1343
+ ### Performance Tips for MySQL
1344
+
1345
+ 1. **Use connection pooling**: Set appropriate `max` pool size based on your workload
1346
+ 2. **Enable prepared statements**: They're enabled by default and improve performance
1347
+ 3. **Use transactions for bulk operations**: Group related queries in transactions
1348
+ 4. **Index properly**: MySQL relies heavily on indexes for query performance
1349
+ 5. **Use `utf8mb4` charset**: It's set by default and handles all Unicode characters
1350
+
1351
+ ---
1352
+
1353
+ ## Frequently Asked Questions
1354
+
1355
+ <AccordionGroup>
1356
+ <Accordion title="Why is this `Bun.sql` and not `Bun.postgres`?">
1357
+ The plan was to add more database drivers in the future. Now with MySQL support added, this unified API supports PostgreSQL, MySQL, and SQLite.
1358
+ </Accordion>
1359
+ <Accordion title="How do I know which database adapter is being used?">
1360
+ The adapter is automatically detected from the connection string:
1361
+
1362
+ - URLs starting with `mysql://` or `mysql2://` use MySQL
1363
+ - URLs matching SQLite patterns (`:memory:`, `sqlite://`, `file://`) use SQLite
1364
+ - Everything else defaults to PostgreSQL
1365
+ </Accordion>
1366
+ <Accordion title="Are MySQL stored procedures supported?">
1367
+ Yes, stored procedures are fully supported including OUT parameters and multiple result sets:
1368
+
1369
+ ```ts
1370
+ // Call stored procedure
1371
+ const results = await mysql`CALL GetUserStats(${userId}, @total_orders)`;
1372
+
1373
+ // Get OUT parameter
1374
+ const outParam = await mysql`SELECT @total_orders as total`;
1375
+ ```
1376
+ </Accordion>
1377
+ <Accordion title="Can I use MySQL-specific SQL syntax?">
1378
+ Yes, you can use any MySQL-specific syntax:
1379
+
1380
+ ```ts
1381
+ // MySQL-specific syntax works fine
1382
+ await mysql`SET @user_id = ${userId}`;
1383
+ await mysql`SHOW TABLES`;
1384
+ await mysql`DESCRIBE users`;
1385
+ await mysql`EXPLAIN SELECT * FROM users WHERE id = ${id}`;
1386
+ ```
1387
+ </Accordion>
1388
+
1389
+ </AccordionGroup>
1390
+
1391
+ ---
1392
+
1393
+ ## Why not just use an existing library?
1394
+
1395
+ npm packages like postgres.js, pg, and node-postgres can be used in Bun too. They're great options.
1396
+
1397
+ Two reasons why:
1398
+
1399
+ 1. We think it's simpler for developers to have a database driver built into Bun. The time you spend library shopping is time you could be building your app.
1400
+ 2. We leverage some JavaScriptCore engine internals to make it faster to create objects that would be difficult to implement in a library
1401
+
1402
+ ## Credits
1403
+
1404
+ Huge thanks to [@porsager](https://github.com/porsager)'s [postgres.js](https://github.com/porsager/postgres) for the inspiration for the API interface.