bun-types 1.3.2-canary.20251105T140650 → 1.3.2-canary.20251106T140813

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 (308) hide show
  1. package/docs/README.md +28 -0
  2. package/package.json +1 -1
  3. package/docs/api/binary-data.md +0 -1038
  4. package/docs/api/cc.md +0 -197
  5. package/docs/api/color.md +0 -262
  6. package/docs/api/console.md +0 -57
  7. package/docs/api/cookie.md +0 -449
  8. package/docs/api/dns.md +0 -110
  9. package/docs/api/fetch.md +0 -463
  10. package/docs/api/ffi.md +0 -557
  11. package/docs/api/file-io.md +0 -366
  12. package/docs/api/file-system-router.md +0 -112
  13. package/docs/api/file.md +0 -19
  14. package/docs/api/glob.md +0 -178
  15. package/docs/api/globals.md +0 -387
  16. package/docs/api/hashing.md +0 -319
  17. package/docs/api/html-rewriter.md +0 -334
  18. package/docs/api/http.md +0 -1408
  19. package/docs/api/import-meta.md +0 -69
  20. package/docs/api/node-api.md +0 -14
  21. package/docs/api/redis.md +0 -597
  22. package/docs/api/s3.md +0 -850
  23. package/docs/api/secrets.md +0 -319
  24. package/docs/api/semver.md +0 -52
  25. package/docs/api/spawn.md +0 -582
  26. package/docs/api/sql.md +0 -1374
  27. package/docs/api/sqlite.md +0 -694
  28. package/docs/api/streams.md +0 -230
  29. package/docs/api/tcp.md +0 -221
  30. package/docs/api/transpiler.md +0 -276
  31. package/docs/api/udp.md +0 -123
  32. package/docs/api/utils.md +0 -891
  33. package/docs/api/websockets.md +0 -573
  34. package/docs/api/workers.md +0 -322
  35. package/docs/api/yaml.md +0 -668
  36. package/docs/benchmarks.md +0 -120
  37. package/docs/bun-flavored-toml.md +0 -42
  38. package/docs/bundler/css.md +0 -1028
  39. package/docs/bundler/css_modules.md +0 -145
  40. package/docs/bundler/executables.md +0 -626
  41. package/docs/bundler/fullstack.md +0 -418
  42. package/docs/bundler/hmr.md +0 -234
  43. package/docs/bundler/html.md +0 -349
  44. package/docs/bundler/index.md +0 -1735
  45. package/docs/bundler/intro.md +0 -75
  46. package/docs/bundler/loaders.md +0 -410
  47. package/docs/bundler/macros.md +0 -329
  48. package/docs/bundler/plugins.md +0 -449
  49. package/docs/bundler/vs-esbuild.md +0 -1127
  50. package/docs/cli/add.md +0 -173
  51. package/docs/cli/bun-completions.md +0 -3
  52. package/docs/cli/bun-create.md +0 -338
  53. package/docs/cli/bun-install.md +0 -349
  54. package/docs/cli/bun-upgrade.md +0 -39
  55. package/docs/cli/bunx.md +0 -89
  56. package/docs/cli/filter.md +0 -90
  57. package/docs/cli/info.md +0 -65
  58. package/docs/cli/init.md +0 -85
  59. package/docs/cli/install.md +0 -344
  60. package/docs/cli/link.md +0 -40
  61. package/docs/cli/outdated.md +0 -90
  62. package/docs/cli/patch-commit.md +0 -11
  63. package/docs/cli/pm.md +0 -285
  64. package/docs/cli/publish.md +0 -120
  65. package/docs/cli/remove.md +0 -7
  66. package/docs/cli/run.md +0 -261
  67. package/docs/cli/test.md +0 -397
  68. package/docs/cli/unlink.md +0 -9
  69. package/docs/cli/update.md +0 -129
  70. package/docs/cli/why.md +0 -67
  71. package/docs/contributing/upgrading-webkit.md +0 -57
  72. package/docs/ecosystem/elysia.md +0 -24
  73. package/docs/ecosystem/express.md +0 -37
  74. package/docs/ecosystem/hono.md +0 -18
  75. package/docs/ecosystem/react.md +0 -65
  76. package/docs/ecosystem/stric.md +0 -38
  77. package/docs/guides/binary/arraybuffer-to-array.md +0 -27
  78. package/docs/guides/binary/arraybuffer-to-blob.md +0 -24
  79. package/docs/guides/binary/arraybuffer-to-buffer.md +0 -25
  80. package/docs/guides/binary/arraybuffer-to-string.md +0 -15
  81. package/docs/guides/binary/arraybuffer-to-typedarray.md +0 -39
  82. package/docs/guides/binary/blob-to-arraybuffer.md +0 -14
  83. package/docs/guides/binary/blob-to-dataview.md +0 -14
  84. package/docs/guides/binary/blob-to-stream.md +0 -14
  85. package/docs/guides/binary/blob-to-string.md +0 -15
  86. package/docs/guides/binary/blob-to-typedarray.md +0 -14
  87. package/docs/guides/binary/buffer-to-arraybuffer.md +0 -14
  88. package/docs/guides/binary/buffer-to-blob.md +0 -14
  89. package/docs/guides/binary/buffer-to-readablestream.md +0 -41
  90. package/docs/guides/binary/buffer-to-string.md +0 -25
  91. package/docs/guides/binary/buffer-to-typedarray.md +0 -14
  92. package/docs/guides/binary/dataview-to-string.md +0 -15
  93. package/docs/guides/binary/typedarray-to-arraybuffer.md +0 -25
  94. package/docs/guides/binary/typedarray-to-blob.md +0 -16
  95. package/docs/guides/binary/typedarray-to-buffer.md +0 -14
  96. package/docs/guides/binary/typedarray-to-dataview.md +0 -14
  97. package/docs/guides/binary/typedarray-to-readablestream.md +0 -41
  98. package/docs/guides/binary/typedarray-to-string.md +0 -16
  99. package/docs/guides/deployment/railway.md +0 -157
  100. package/docs/guides/ecosystem/astro.md +0 -72
  101. package/docs/guides/ecosystem/discordjs.md +0 -77
  102. package/docs/guides/ecosystem/docker.md +0 -140
  103. package/docs/guides/ecosystem/drizzle.md +0 -185
  104. package/docs/guides/ecosystem/edgedb.md +0 -228
  105. package/docs/guides/ecosystem/elysia.md +0 -31
  106. package/docs/guides/ecosystem/express.md +0 -40
  107. package/docs/guides/ecosystem/hono.md +0 -39
  108. package/docs/guides/ecosystem/mongoose.md +0 -87
  109. package/docs/guides/ecosystem/neon-drizzle.md +0 -220
  110. package/docs/guides/ecosystem/neon-serverless-postgres.md +0 -55
  111. package/docs/guides/ecosystem/nextjs.md +0 -49
  112. package/docs/guides/ecosystem/nuxt.md +0 -56
  113. package/docs/guides/ecosystem/pm2.md +0 -57
  114. package/docs/guides/ecosystem/prisma.md +0 -141
  115. package/docs/guides/ecosystem/qwik.md +0 -107
  116. package/docs/guides/ecosystem/react.md +0 -50
  117. package/docs/guides/ecosystem/remix.md +0 -78
  118. package/docs/guides/ecosystem/render.md +0 -79
  119. package/docs/guides/ecosystem/sentry.md +0 -52
  120. package/docs/guides/ecosystem/solidstart.md +0 -58
  121. package/docs/guides/ecosystem/ssr-react.md +0 -51
  122. package/docs/guides/ecosystem/stric.md +0 -55
  123. package/docs/guides/ecosystem/sveltekit.md +0 -125
  124. package/docs/guides/ecosystem/systemd.md +0 -113
  125. package/docs/guides/ecosystem/vite.md +0 -71
  126. package/docs/guides/html-rewriter/extract-links.md +0 -68
  127. package/docs/guides/html-rewriter/extract-social-meta.md +0 -93
  128. package/docs/guides/http/cluster.md +0 -66
  129. package/docs/guides/http/fetch-unix.md +0 -33
  130. package/docs/guides/http/fetch.md +0 -24
  131. package/docs/guides/http/file-uploads.md +0 -94
  132. package/docs/guides/http/hot.md +0 -26
  133. package/docs/guides/http/proxy.md +0 -24
  134. package/docs/guides/http/server.md +0 -46
  135. package/docs/guides/http/simple.md +0 -18
  136. package/docs/guides/http/stream-file.md +0 -48
  137. package/docs/guides/http/stream-iterator.md +0 -47
  138. package/docs/guides/http/stream-node-streams-in-bun.md +0 -20
  139. package/docs/guides/http/tls.md +0 -30
  140. package/docs/guides/install/add-dev.md +0 -26
  141. package/docs/guides/install/add-git.md +0 -36
  142. package/docs/guides/install/add-optional.md +0 -25
  143. package/docs/guides/install/add-peer.md +0 -43
  144. package/docs/guides/install/add-tarball.md +0 -33
  145. package/docs/guides/install/add.md +0 -42
  146. package/docs/guides/install/azure-artifacts.md +0 -73
  147. package/docs/guides/install/cicd.md +0 -41
  148. package/docs/guides/install/custom-registry.md +0 -30
  149. package/docs/guides/install/from-npm-install-to-bun-install.md +0 -214
  150. package/docs/guides/install/git-diff-bun-lockfile.md +0 -44
  151. package/docs/guides/install/jfrog-artifactory.md +0 -28
  152. package/docs/guides/install/npm-alias.md +0 -23
  153. package/docs/guides/install/registry-scope.md +0 -38
  154. package/docs/guides/install/trusted.md +0 -46
  155. package/docs/guides/install/workspaces.md +0 -70
  156. package/docs/guides/install/yarnlock.md +0 -44
  157. package/docs/guides/process/argv.md +0 -58
  158. package/docs/guides/process/ctrl-c.md +0 -16
  159. package/docs/guides/process/ipc.md +0 -66
  160. package/docs/guides/process/nanoseconds.md +0 -13
  161. package/docs/guides/process/os-signals.md +0 -39
  162. package/docs/guides/process/spawn-stderr.md +0 -31
  163. package/docs/guides/process/spawn-stdout.md +0 -26
  164. package/docs/guides/process/spawn.md +0 -41
  165. package/docs/guides/process/stdin.md +0 -54
  166. package/docs/guides/read-file/arraybuffer.md +0 -28
  167. package/docs/guides/read-file/buffer.md +0 -19
  168. package/docs/guides/read-file/exists.md +0 -16
  169. package/docs/guides/read-file/json.md +0 -17
  170. package/docs/guides/read-file/mime.md +0 -20
  171. package/docs/guides/read-file/stream.md +0 -26
  172. package/docs/guides/read-file/string.md +0 -22
  173. package/docs/guides/read-file/uint8array.md +0 -21
  174. package/docs/guides/read-file/watch.md +0 -68
  175. package/docs/guides/runtime/build-time-constants.md +0 -293
  176. package/docs/guides/runtime/cicd.md +0 -43
  177. package/docs/guides/runtime/codesign-macos-executable.md +0 -56
  178. package/docs/guides/runtime/define-constant.md +0 -145
  179. package/docs/guides/runtime/delete-directory.md +0 -37
  180. package/docs/guides/runtime/delete-file.md +0 -19
  181. package/docs/guides/runtime/heap-snapshot.md +0 -26
  182. package/docs/guides/runtime/import-html.md +0 -15
  183. package/docs/guides/runtime/import-json.md +0 -44
  184. package/docs/guides/runtime/import-toml.md +0 -30
  185. package/docs/guides/runtime/import-yaml.md +0 -102
  186. package/docs/guides/runtime/read-env.md +0 -32
  187. package/docs/guides/runtime/set-env.md +0 -47
  188. package/docs/guides/runtime/shell.md +0 -40
  189. package/docs/guides/runtime/timezone.md +0 -35
  190. package/docs/guides/runtime/tsconfig-paths.md +0 -29
  191. package/docs/guides/runtime/typescript.md +0 -49
  192. package/docs/guides/runtime/vscode-debugger.md +0 -47
  193. package/docs/guides/runtime/web-debugger.md +0 -82
  194. package/docs/guides/streams/node-readable-to-arraybuffer.md +0 -11
  195. package/docs/guides/streams/node-readable-to-blob.md +0 -11
  196. package/docs/guides/streams/node-readable-to-json.md +0 -12
  197. package/docs/guides/streams/node-readable-to-string.md +0 -12
  198. package/docs/guides/streams/node-readable-to-uint8array.md +0 -11
  199. package/docs/guides/streams/to-array.md +0 -14
  200. package/docs/guides/streams/to-arraybuffer.md +0 -14
  201. package/docs/guides/streams/to-blob.md +0 -14
  202. package/docs/guides/streams/to-buffer.md +0 -15
  203. package/docs/guides/streams/to-json.md +0 -14
  204. package/docs/guides/streams/to-string.md +0 -14
  205. package/docs/guides/streams/to-typedarray.md +0 -22
  206. package/docs/guides/test/bail.md +0 -22
  207. package/docs/guides/test/coverage-threshold.md +0 -60
  208. package/docs/guides/test/coverage.md +0 -44
  209. package/docs/guides/test/happy-dom.md +0 -68
  210. package/docs/guides/test/migrate-from-jest.md +0 -143
  211. package/docs/guides/test/mock-clock.md +0 -48
  212. package/docs/guides/test/mock-functions.md +0 -68
  213. package/docs/guides/test/rerun-each.md +0 -14
  214. package/docs/guides/test/run-tests.md +0 -111
  215. package/docs/guides/test/skip-tests.md +0 -39
  216. package/docs/guides/test/snapshot.md +0 -95
  217. package/docs/guides/test/spy-on.md +0 -46
  218. package/docs/guides/test/svelte-test.md +0 -120
  219. package/docs/guides/test/testing-library.md +0 -92
  220. package/docs/guides/test/timeout.md +0 -15
  221. package/docs/guides/test/todo-tests.md +0 -67
  222. package/docs/guides/test/update-snapshots.md +0 -46
  223. package/docs/guides/test/watch-mode.md +0 -19
  224. package/docs/guides/util/base64.md +0 -15
  225. package/docs/guides/util/deep-equals.md +0 -39
  226. package/docs/guides/util/deflate.md +0 -18
  227. package/docs/guides/util/detect-bun.md +0 -23
  228. package/docs/guides/util/entrypoint.md +0 -17
  229. package/docs/guides/util/escape-html.md +0 -22
  230. package/docs/guides/util/file-url-to-path.md +0 -14
  231. package/docs/guides/util/gzip.md +0 -18
  232. package/docs/guides/util/hash-a-password.md +0 -54
  233. package/docs/guides/util/import-meta-dir.md +0 -13
  234. package/docs/guides/util/import-meta-file.md +0 -13
  235. package/docs/guides/util/import-meta-path.md +0 -13
  236. package/docs/guides/util/javascript-uuid.md +0 -23
  237. package/docs/guides/util/main.md +0 -32
  238. package/docs/guides/util/path-to-file-url.md +0 -14
  239. package/docs/guides/util/sleep.md +0 -22
  240. package/docs/guides/util/version.md +0 -21
  241. package/docs/guides/util/which-path-to-executable-bin.md +0 -15
  242. package/docs/guides/websocket/compression.md +0 -31
  243. package/docs/guides/websocket/context.md +0 -77
  244. package/docs/guides/websocket/pubsub.md +0 -41
  245. package/docs/guides/websocket/simple.md +0 -33
  246. package/docs/guides/write-file/append.md +0 -52
  247. package/docs/guides/write-file/basic.md +0 -44
  248. package/docs/guides/write-file/blob.md +0 -28
  249. package/docs/guides/write-file/cat.md +0 -17
  250. package/docs/guides/write-file/file-cp.md +0 -16
  251. package/docs/guides/write-file/filesink.md +0 -52
  252. package/docs/guides/write-file/response.md +0 -17
  253. package/docs/guides/write-file/stdout.md +0 -21
  254. package/docs/guides/write-file/stream.md +0 -17
  255. package/docs/guides/write-file/unlink.md +0 -16
  256. package/docs/index.md +0 -77
  257. package/docs/install/audit.md +0 -57
  258. package/docs/install/cache.md +0 -59
  259. package/docs/install/catalogs.md +0 -298
  260. package/docs/install/index.md +0 -248
  261. package/docs/install/isolated.md +0 -197
  262. package/docs/install/lifecycle.md +0 -46
  263. package/docs/install/lockfile.md +0 -58
  264. package/docs/install/npmrc.md +0 -105
  265. package/docs/install/overrides.md +0 -73
  266. package/docs/install/patch.md +0 -59
  267. package/docs/install/registries.md +0 -30
  268. package/docs/install/security-scanner-api.md +0 -81
  269. package/docs/install/workspaces.md +0 -114
  270. package/docs/installation.md +0 -327
  271. package/docs/project/asan.md +0 -124
  272. package/docs/project/benchmarking.md +0 -203
  273. package/docs/project/bindgen.md +0 -225
  274. package/docs/project/building-windows.md +0 -162
  275. package/docs/project/contributing.md +0 -347
  276. package/docs/project/internals/build-process-for-ci.md +0 -75
  277. package/docs/project/licensing.md +0 -73
  278. package/docs/project/roadmap.md +0 -87
  279. package/docs/quickstart.md +0 -157
  280. package/docs/runtime/autoimport.md +0 -94
  281. package/docs/runtime/bun-apis.md +0 -207
  282. package/docs/runtime/bunfig.md +0 -772
  283. package/docs/runtime/debugger.md +0 -325
  284. package/docs/runtime/env.md +0 -253
  285. package/docs/runtime/hot.md +0 -145
  286. package/docs/runtime/index.md +0 -312
  287. package/docs/runtime/jsx.md +0 -385
  288. package/docs/runtime/loaders.md +0 -130
  289. package/docs/runtime/modules.md +0 -320
  290. package/docs/runtime/nodejs-apis.md +0 -463
  291. package/docs/runtime/plugins.md +0 -561
  292. package/docs/runtime/shell.md +0 -605
  293. package/docs/runtime/typescript.md +0 -139
  294. package/docs/runtime/web-apis.md +0 -128
  295. package/docs/test/configuration.md +0 -154
  296. package/docs/test/coverage.md +0 -142
  297. package/docs/test/discovery.md +0 -85
  298. package/docs/test/dom.md +0 -75
  299. package/docs/test/examples/concurrent-test-glob.md +0 -132
  300. package/docs/test/hot.md +0 -15
  301. package/docs/test/lifecycle.md +0 -81
  302. package/docs/test/mocks.md +0 -313
  303. package/docs/test/reporters.md +0 -117
  304. package/docs/test/runtime-behavior.md +0 -95
  305. package/docs/test/snapshots.md +0 -68
  306. package/docs/test/time.md +0 -126
  307. package/docs/test/writing.md +0 -825
  308. package/docs/typescript.md +0 -53
@@ -1,694 +0,0 @@
1
- Bun natively implements a high-performance [SQLite3](https://www.sqlite.org/) driver. To use it import from the built-in `bun:sqlite` module.
2
-
3
- ```ts
4
- import { Database } from "bun:sqlite";
5
-
6
- const db = new Database(":memory:");
7
- const query = db.query("select 'Hello world' as message;");
8
- query.get(); // => { message: "Hello world" }
9
- ```
10
-
11
- The API is simple, synchronous, and fast. Credit to [better-sqlite3](https://github.com/JoshuaWise/better-sqlite3) and its contributors for inspiring the API of `bun:sqlite`.
12
-
13
- Features include:
14
-
15
- - Transactions
16
- - Parameters (named & positional)
17
- - Prepared statements
18
- - Datatype conversions (`BLOB` becomes `Uint8Array`)
19
- - Map query results to classes without an ORM - `query.as(MyClass)`
20
- - The fastest performance of any SQLite driver for JavaScript
21
- - `bigint` support
22
- - Multi-query statements (e.g. `SELECT 1; SELECT 2;`) in a single call to database.run(query)
23
-
24
- The `bun:sqlite` module is roughly 3-6x faster than `better-sqlite3` and 8-9x faster than `deno.land/x/sqlite` for read queries. Each driver was benchmarked against the [Northwind Traders](https://github.com/jpwhite3/northwind-SQLite3/blob/46d5f8a64f396f87cd374d1600dbf521523980e8/Northwind_large.sqlite.zip) dataset. View and run the [benchmark source](https://github.com/oven-sh/bun/tree/main/bench/sqlite).
25
-
26
- {% image width="738" alt="SQLite benchmarks for Bun, better-sqlite3, and deno.land/x/sqlite" src="https://user-images.githubusercontent.com/709451/168459263-8cd51ca3-a924-41e9-908d-cf3478a3b7f3.png" caption="Benchmarked on an M1 MacBook Pro (64GB) running macOS 12.3.1" /%}
27
-
28
- ## Database
29
-
30
- To open or create a SQLite3 database:
31
-
32
- ```ts
33
- import { Database } from "bun:sqlite";
34
-
35
- const db = new Database("mydb.sqlite");
36
- ```
37
-
38
- To open an in-memory database:
39
-
40
- ```ts
41
- import { Database } from "bun:sqlite";
42
-
43
- // all of these do the same thing
44
- const db = new Database(":memory:");
45
- const db = new Database();
46
- const db = new Database("");
47
- ```
48
-
49
- To open in `readonly` mode:
50
-
51
- ```ts
52
- import { Database } from "bun:sqlite";
53
- const db = new Database("mydb.sqlite", { readonly: true });
54
- ```
55
-
56
- To create the database if the file doesn't exist:
57
-
58
- ```ts
59
- import { Database } from "bun:sqlite";
60
- const db = new Database("mydb.sqlite", { create: true });
61
- ```
62
-
63
- ### Strict mode
64
-
65
- {% callout %}
66
- Added in Bun v1.1.14
67
- {% /callout %}
68
-
69
- By default, `bun:sqlite` requires binding parameters to include the `$`, `:`, or `@` prefix, and does not throw an error if a parameter is missing.
70
-
71
- To instead throw an error when a parameter is missing and allow binding without a prefix, set `strict: true` on the `Database` constructor:
72
-
73
- <!-- prettier-ignore -->
74
- ```ts
75
- import { Database } from "bun:sqlite";
76
-
77
- const strict = new Database(
78
- ":memory:",
79
- { strict: true }
80
- );
81
-
82
- // throws error because of the typo:
83
- const query = strict
84
- .query("SELECT $message;")
85
- .all({ message: "Hello world" });
86
-
87
- const notStrict = new Database(
88
- ":memory:"
89
- );
90
- // does not throw error:
91
- notStrict
92
- .query("SELECT $message;")
93
- .all({ message: "Hello world" });
94
- ```
95
-
96
- ### Load via ES module import
97
-
98
- You can also use an import attribute to load a database.
99
-
100
- ```ts
101
- import db from "./mydb.sqlite" with { "type": "sqlite" };
102
-
103
- console.log(db.query("select * from users LIMIT 1").get());
104
- ```
105
-
106
- This is equivalent to the following:
107
-
108
- ```ts
109
- import { Database } from "bun:sqlite";
110
- const db = new Database("./mydb.sqlite");
111
- ```
112
-
113
- ### `.close(throwOnError: boolean = false)`
114
-
115
- To close a database connection, but allow existing queries to finish, call `.close(false)`:
116
-
117
- ```ts
118
- const db = new Database();
119
- // ... do stuff
120
- db.close(false);
121
- ```
122
-
123
- To close the database and throw an error if there are any pending queries, call `.close(true)`:
124
-
125
- ```ts
126
- const db = new Database();
127
- // ... do stuff
128
- db.close(true);
129
- ```
130
-
131
- Note: `close(false)` is called automatically when the database is garbage collected. It is safe to call multiple times but has no effect after the first.
132
-
133
- ### `using` statement
134
-
135
- You can use the `using` statement to ensure that a database connection is closed when the `using` block is exited.
136
-
137
- ```ts
138
- import { Database } from "bun:sqlite";
139
-
140
- {
141
- using db = new Database("mydb.sqlite");
142
- using query = db.query("select 'Hello world' as message;");
143
- console.log(query.get()); // => { message: "Hello world" }
144
- }
145
- ```
146
-
147
- ### `.serialize()`
148
-
149
- `bun:sqlite` supports SQLite's built-in mechanism for [serializing](https://www.sqlite.org/c3ref/serialize.html) and [deserializing](https://www.sqlite.org/c3ref/deserialize.html) databases to and from memory.
150
-
151
- ```ts
152
- const olddb = new Database("mydb.sqlite");
153
- const contents = olddb.serialize(); // => Uint8Array
154
- const newdb = Database.deserialize(contents);
155
- ```
156
-
157
- Internally, `.serialize()` calls [`sqlite3_serialize`](https://www.sqlite.org/c3ref/serialize.html).
158
-
159
- ### `.query()`
160
-
161
- Use the `db.query()` method on your `Database` instance to [prepare](https://www.sqlite.org/c3ref/prepare.html) a SQL query. The result is a `Statement` instance that will be cached on the `Database` instance. _The query will not be executed._
162
-
163
- ```ts
164
- const query = db.query(`select "Hello world" as message`);
165
- ```
166
-
167
- {% callout %}
168
-
169
- **Note** — Use the `.prepare()` method to prepare a query _without_ caching it on the `Database` instance.
170
-
171
- ```ts
172
- // compile the prepared statement
173
- const query = db.prepare("SELECT * FROM foo WHERE bar = ?");
174
- ```
175
-
176
- {% /callout %}
177
-
178
- ## WAL mode
179
-
180
- SQLite supports [write-ahead log mode](https://www.sqlite.org/wal.html) (WAL) which dramatically improves performance, especially in situations with many concurrent readers and a single writer. It's broadly recommended to enable WAL mode for most typical applications.
181
-
182
- To enable WAL mode, run this pragma query at the beginning of your application:
183
-
184
- ```ts
185
- db.exec("PRAGMA journal_mode = WAL;");
186
- ```
187
-
188
- {% details summary="What is WAL mode" %}
189
- In WAL mode, writes to the database are written directly to a separate file called the "WAL file" (write-ahead log). This file will be later integrated into the main database file. Think of it as a buffer for pending writes. Refer to the [SQLite docs](https://www.sqlite.org/wal.html) for a more detailed overview.
190
-
191
- On macOS, WAL files may be persistent by default. This is not a bug, it is how macOS configured the system version of SQLite.
192
- {% /details %}
193
-
194
- ## Statements
195
-
196
- A `Statement` is a _prepared query_, which means it's been parsed and compiled into an efficient binary form. It can be executed multiple times in a performant way.
197
-
198
- Create a statement with the `.query` method on your `Database` instance.
199
-
200
- ```ts
201
- const query = db.query(`select "Hello world" as message`);
202
- ```
203
-
204
- Queries can contain parameters. These can be numerical (`?1`) or named (`$param` or `:param` or `@param`).
205
-
206
- ```ts
207
- const query = db.query(`SELECT ?1, ?2;`);
208
- const query = db.query(`SELECT $param1, $param2;`);
209
- ```
210
-
211
- Values are bound to these parameters when the query is executed. A `Statement` can be executed with several different methods, each returning the results in a different form.
212
-
213
- ### Binding values
214
-
215
- To bind values to a statement, pass an object to the `.all()`, `.get()`, `.run()`, or `.values()` method.
216
-
217
- ```ts
218
- const query = db.query(`select $message;`);
219
- query.all({ $message: "Hello world" });
220
- ```
221
-
222
- You can bind using positional parameters too:
223
-
224
- ```ts
225
- const query = db.query(`select ?1;`);
226
- query.all("Hello world");
227
- ```
228
-
229
- #### `strict: true` lets you bind values without prefixes
230
-
231
- {% callout %}
232
- Added in Bun v1.1.14
233
- {% /callout %}
234
-
235
- By default, the `$`, `:`, and `@` prefixes are **included** when binding values to named parameters. To bind without these prefixes, use the `strict` option in the `Database` constructor.
236
-
237
- ```ts
238
- import { Database } from "bun:sqlite";
239
-
240
- const db = new Database(":memory:", {
241
- // bind values without prefixes
242
- strict: true,
243
- });
244
-
245
- const query = db.query(`select $message;`);
246
-
247
- // strict: true
248
- query.all({ message: "Hello world" });
249
-
250
- // strict: false
251
- // query.all({ $message: "Hello world" });
252
- ```
253
-
254
- ### `.all()`
255
-
256
- Use `.all()` to run a query and get back the results as an array of objects.
257
-
258
- ```ts
259
- const query = db.query(`select $message;`);
260
- query.all({ $message: "Hello world" });
261
- // => [{ message: "Hello world" }]
262
- ```
263
-
264
- Internally, this calls [`sqlite3_reset`](https://www.sqlite.org/capi3ref.html#sqlite3_reset) and repeatedly calls [`sqlite3_step`](https://www.sqlite.org/capi3ref.html#sqlite3_step) until it returns `SQLITE_DONE`.
265
-
266
- ### `.get()`
267
-
268
- Use `.get()` to run a query and get back the first result as an object.
269
-
270
- ```ts
271
- const query = db.query(`select $message;`);
272
- query.get({ $message: "Hello world" });
273
- // => { $message: "Hello world" }
274
- ```
275
-
276
- Internally, this calls [`sqlite3_reset`](https://www.sqlite.org/capi3ref.html#sqlite3_reset) followed by [`sqlite3_step`](https://www.sqlite.org/capi3ref.html#sqlite3_step) until it no longer returns `SQLITE_ROW`. If the query returns no rows, `undefined` is returned.
277
-
278
- ### `.run()`
279
-
280
- Use `.run()` to run a query and get back `undefined`. This is useful for schema-modifying queries (e.g. `CREATE TABLE`) or bulk write operations.
281
-
282
- ```ts
283
- const query = db.query(`create table foo;`);
284
- query.run();
285
- // {
286
- // lastInsertRowid: 0,
287
- // changes: 0,
288
- // }
289
- ```
290
-
291
- Internally, this calls [`sqlite3_reset`](https://www.sqlite.org/capi3ref.html#sqlite3_reset) and calls [`sqlite3_step`](https://www.sqlite.org/capi3ref.html#sqlite3_step) once. Stepping through all the rows is not necessary when you don't care about the results.
292
-
293
- {% callout %}
294
- Since Bun v1.1.14, `.run()` returns an object with two properties: `lastInsertRowid` and `changes`.
295
- {% /callout %}
296
-
297
- The `lastInsertRowid` property returns the ID of the last row inserted into the database. The `changes` property is the number of rows affected by the query.
298
-
299
- ### `.as(Class)` - Map query results to a class
300
-
301
- {% callout %}
302
- Added in Bun v1.1.14
303
- {% /callout %}
304
-
305
- Use `.as(Class)` to run a query and get back the results as instances of a class. This lets you attach methods & getters/setters to results.
306
-
307
- ```ts
308
- class Movie {
309
- title: string;
310
- year: number;
311
-
312
- get isMarvel() {
313
- return this.title.includes("Marvel");
314
- }
315
- }
316
-
317
- const query = db.query("SELECT title, year FROM movies").as(Movie);
318
- const movies = query.all();
319
- const first = query.get();
320
- console.log(movies[0].isMarvel); // => true
321
- console.log(first.isMarvel); // => true
322
- ```
323
-
324
- As a performance optimization, the class constructor is not called, default initializers are not run, and private fields are not accessible. This is more like using `Object.create` than `new`. The class's prototype is assigned to the object, methods are attached, and getters/setters are set up, but the constructor is not called.
325
-
326
- The database columns are set as properties on the class instance.
327
-
328
- ### `.iterate()` (`@@iterator`)
329
-
330
- Use `.iterate()` to run a query and incrementally return results. This is useful for large result sets that you want to process one row at a time without loading all the results into memory.
331
-
332
- ```ts
333
- const query = db.query("SELECT * FROM foo");
334
- for (const row of query.iterate()) {
335
- console.log(row);
336
- }
337
- ```
338
-
339
- You can also use the `@@iterator` protocol:
340
-
341
- ```ts
342
- const query = db.query("SELECT * FROM foo");
343
- for (const row of query) {
344
- console.log(row);
345
- }
346
- ```
347
-
348
- This feature was added in Bun v1.1.31.
349
-
350
- ### `.values()`
351
-
352
- Use `values()` to run a query and get back all results as an array of arrays.
353
-
354
- ```ts
355
- const query = db.query(`select $message;`);
356
- query.values({ $message: "Hello world" });
357
-
358
- query.values(2);
359
- // [
360
- // [ "Iron Man", 2008 ],
361
- // [ "The Avengers", 2012 ],
362
- // [ "Ant-Man: Quantumania", 2023 ],
363
- // ]
364
- ```
365
-
366
- Internally, this calls [`sqlite3_reset`](https://www.sqlite.org/capi3ref.html#sqlite3_reset) and repeatedly calls [`sqlite3_step`](https://www.sqlite.org/capi3ref.html#sqlite3_step) until it returns `SQLITE_DONE`.
367
-
368
- ### `.finalize()`
369
-
370
- Use `.finalize()` to destroy a `Statement` and free any resources associated with it. Once finalized, a `Statement` cannot be executed again. Typically, the garbage collector will do this for you, but explicit finalization may be useful in performance-sensitive applications.
371
-
372
- ```ts
373
- const query = db.query("SELECT title, year FROM movies");
374
- const movies = query.all();
375
- query.finalize();
376
- ```
377
-
378
- ### `.toString()`
379
-
380
- Calling `toString()` on a `Statement` instance prints the expanded SQL query. This is useful for debugging.
381
-
382
- ```ts
383
- import { Database } from "bun:sqlite";
384
-
385
- // setup
386
- const query = db.query("SELECT $param;");
387
-
388
- console.log(query.toString()); // => "SELECT NULL"
389
-
390
- query.run(42);
391
- console.log(query.toString()); // => "SELECT 42"
392
-
393
- query.run(365);
394
- console.log(query.toString()); // => "SELECT 365"
395
- ```
396
-
397
- Internally, this calls [`sqlite3_expanded_sql`](https://www.sqlite.org/capi3ref.html#sqlite3_expanded_sql). The parameters are expanded using the most recently bound values.
398
-
399
- ## Parameters
400
-
401
- Queries can contain parameters. These can be numerical (`?1`) or named (`$param` or `:param` or `@param`). Bind values to these parameters when executing the query:
402
-
403
- {% codetabs %}
404
-
405
- ```ts#Query
406
- const query = db.query("SELECT * FROM foo WHERE bar = $bar");
407
- const results = query.all({
408
- $bar: "bar",
409
- });
410
- ```
411
-
412
- ```json#Results
413
- [
414
- { "$bar": "bar" }
415
- ]
416
- ```
417
-
418
- {% /codetabs %}
419
-
420
- Numbered (positional) parameters work too:
421
-
422
- {% codetabs %}
423
-
424
- ```ts#Query
425
- const query = db.query("SELECT ?1, ?2");
426
- const results = query.all("hello", "goodbye");
427
- ```
428
-
429
- ```ts#Results
430
- [
431
- {
432
- "?1": "hello",
433
- "?2": "goodbye"
434
- }
435
- ]
436
- ```
437
-
438
- {% /codetabs %}
439
-
440
- ## Integers
441
-
442
- sqlite supports signed 64 bit integers, but JavaScript only supports signed 52 bit integers or arbitrary precision integers with `bigint`.
443
-
444
- `bigint` input is supported everywhere, but by default `bun:sqlite` returns integers as `number` types. If you need to handle integers larger than 2^53, set `safeIntegers` option to `true` when creating a `Database` instance. This also validates that `bigint` passed to `bun:sqlite` do not exceed 64 bits.
445
-
446
- By default, `bun:sqlite` returns integers as `number` types. If you need to handle integers larger than 2^53, you can use the `bigint` type.
447
-
448
- ### `safeIntegers: true`
449
-
450
- {% callout %}
451
- Added in Bun v1.1.14
452
- {% /callout %}
453
-
454
- When `safeIntegers` is `true`, `bun:sqlite` will return integers as `bigint` types:
455
-
456
- ```ts
457
- import { Database } from "bun:sqlite";
458
-
459
- const db = new Database(":memory:", { safeIntegers: true });
460
- const query = db.query(
461
- `SELECT ${BigInt(Number.MAX_SAFE_INTEGER) + 102n} as max_int`,
462
- );
463
- const result = query.get();
464
- console.log(result.max_int); // => 9007199254741093n
465
- ```
466
-
467
- When `safeIntegers` is `true`, `bun:sqlite` will throw an error if a `bigint` value in a bound parameter exceeds 64 bits:
468
-
469
- ```ts
470
- import { Database } from "bun:sqlite";
471
-
472
- const db = new Database(":memory:", { safeIntegers: true });
473
- db.run("CREATE TABLE test (id INTEGER PRIMARY KEY, value INTEGER)");
474
-
475
- const query = db.query("INSERT INTO test (value) VALUES ($value)");
476
-
477
- try {
478
- query.run({ $value: BigInt(Number.MAX_SAFE_INTEGER) ** 2n });
479
- } catch (e) {
480
- console.log(e.message); // => BigInt value '81129638414606663681390495662081' is out of range
481
- }
482
- ```
483
-
484
- ### `safeIntegers: false` (default)
485
-
486
- When `safeIntegers` is `false`, `bun:sqlite` will return integers as `number` types and truncate any bits beyond 53:
487
-
488
- ```ts
489
- import { Database } from "bun:sqlite";
490
-
491
- const db = new Database(":memory:", { safeIntegers: false });
492
- const query = db.query(
493
- `SELECT ${BigInt(Number.MAX_SAFE_INTEGER) + 102n} as max_int`,
494
- );
495
- const result = query.get();
496
- console.log(result.max_int); // => 9007199254741092
497
- ```
498
-
499
- ## Transactions
500
-
501
- Transactions are a mechanism for executing multiple queries in an _atomic_ way; that is, either all of the queries succeed or none of them do. Create a transaction with the `db.transaction()` method:
502
-
503
- ```ts
504
- const insertCat = db.prepare("INSERT INTO cats (name) VALUES ($name)");
505
- const insertCats = db.transaction(cats => {
506
- for (const cat of cats) insertCat.run(cat);
507
- });
508
- ```
509
-
510
- At this stage, we haven't inserted any cats! The call to `db.transaction()` returns a new function (`insertCats`) that _wraps_ the function that executes the queries.
511
-
512
- To execute the transaction, call this function. All arguments will be passed through to the wrapped function; the return value of the wrapped function will be returned by the transaction function. The wrapped function also has access to the `this` context as defined where the transaction is executed.
513
-
514
- ```ts
515
- const insert = db.prepare("INSERT INTO cats (name) VALUES ($name)");
516
- const insertCats = db.transaction(cats => {
517
- for (const cat of cats) insert.run(cat);
518
- return cats.length;
519
- });
520
-
521
- const count = insertCats([
522
- { $name: "Keanu" },
523
- { $name: "Salem" },
524
- { $name: "Crookshanks" },
525
- ]);
526
-
527
- console.log(`Inserted ${count} cats`);
528
- ```
529
-
530
- The driver will automatically [`begin`](https://www.sqlite.org/lang_transaction.html) a transaction when `insertCats` is called and `commit` it when the wrapped function returns. If an exception is thrown, the transaction will be rolled back. The exception will propagate as usual; it is not caught.
531
-
532
- {% callout %}
533
- **Nested transactions** — Transaction functions can be called from inside other transaction functions. When doing so, the inner transaction becomes a [savepoint](https://www.sqlite.org/lang_savepoint.html).
534
-
535
- {% details summary="View nested transaction example" %}
536
-
537
- ```ts
538
- // setup
539
- import { Database } from "bun:sqlite";
540
- const db = Database.open(":memory:");
541
- db.run(
542
- "CREATE TABLE expenses (id INTEGER PRIMARY KEY AUTOINCREMENT, note TEXT, dollars INTEGER);",
543
- );
544
- db.run(
545
- "CREATE TABLE cats (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE, age INTEGER)",
546
- );
547
- const insertExpense = db.prepare(
548
- "INSERT INTO expenses (note, dollars) VALUES (?, ?)",
549
- );
550
- const insert = db.prepare("INSERT INTO cats (name, age) VALUES ($name, $age)");
551
- const insertCats = db.transaction(cats => {
552
- for (const cat of cats) insert.run(cat);
553
- });
554
-
555
- const adopt = db.transaction(cats => {
556
- insertExpense.run("adoption fees", 20);
557
- insertCats(cats); // nested transaction
558
- });
559
-
560
- adopt([
561
- { $name: "Joey", $age: 2 },
562
- { $name: "Sally", $age: 4 },
563
- { $name: "Junior", $age: 1 },
564
- ]);
565
- ```
566
-
567
- {% /details %}
568
- {% /callout %}
569
-
570
- Transactions also come with `deferred`, `immediate`, and `exclusive` versions.
571
-
572
- ```ts
573
- insertCats(cats); // uses "BEGIN"
574
- insertCats.deferred(cats); // uses "BEGIN DEFERRED"
575
- insertCats.immediate(cats); // uses "BEGIN IMMEDIATE"
576
- insertCats.exclusive(cats); // uses "BEGIN EXCLUSIVE"
577
- ```
578
-
579
- ### `.loadExtension()`
580
-
581
- To load a [SQLite extension](https://www.sqlite.org/loadext.html), call `.loadExtension(name)` on your `Database` instance
582
-
583
- ```ts
584
- import { Database } from "bun:sqlite";
585
-
586
- const db = new Database();
587
- db.loadExtension("myext");
588
- ```
589
-
590
- {% details summary="For macOS users" %}
591
- **MacOS users** By default, macOS ships with Apple's proprietary build of SQLite, which doesn't support extensions. To use extensions, you'll need to install a vanilla build of SQLite.
592
-
593
- ```bash
594
- $ brew install sqlite
595
- $ which sqlite # get path to binary
596
- ```
597
-
598
- To point `bun:sqlite` to the new build, call `Database.setCustomSQLite(path)` before creating any `Database` instances. (On other operating systems, this is a no-op.) Pass a path to the SQLite `.dylib` file, _not_ the executable. With recent versions of Homebrew this is something like `/opt/homebrew/Cellar/sqlite/<version>/libsqlite3.dylib`.
599
-
600
- ```ts
601
- import { Database } from "bun:sqlite";
602
-
603
- Database.setCustomSQLite("/path/to/libsqlite.dylib");
604
-
605
- const db = new Database();
606
- db.loadExtension("myext");
607
- ```
608
-
609
- {% /details %}
610
-
611
- ### .fileControl(cmd: number, value: any)
612
-
613
- To use the advanced `sqlite3_file_control` API, call `.fileControl(cmd, value)` on your `Database` instance.
614
-
615
- ```ts
616
- import { Database, constants } from "bun:sqlite";
617
-
618
- const db = new Database();
619
- // Ensure WAL mode is NOT persistent
620
- // this prevents wal files from lingering after the database is closed
621
- db.fileControl(constants.SQLITE_FCNTL_PERSIST_WAL, 0);
622
- ```
623
-
624
- `value` can be:
625
-
626
- - `number`
627
- - `TypedArray`
628
- - `undefined` or `null`
629
-
630
- ## Reference
631
-
632
- ```ts
633
- class Database {
634
- constructor(
635
- filename: string,
636
- options?:
637
- | number
638
- | {
639
- readonly?: boolean;
640
- create?: boolean;
641
- readwrite?: boolean;
642
- },
643
- );
644
-
645
- query<Params, ReturnType>(sql: string): Statement<Params, ReturnType>;
646
- run(
647
- sql: string,
648
- params?: SQLQueryBindings,
649
- ): { lastInsertRowid: number; changes: number };
650
- exec = this.run;
651
- }
652
-
653
- class Statement<Params, ReturnType> {
654
- all(params: Params): ReturnType[];
655
- get(params: Params): ReturnType | undefined;
656
- run(params: Params): {
657
- lastInsertRowid: number;
658
- changes: number;
659
- };
660
- values(params: Params): unknown[][];
661
-
662
- finalize(): void; // destroy statement and clean up resources
663
- toString(): string; // serialize to SQL
664
-
665
- columnNames: string[]; // the column names of the result set
666
- columnTypes: string[]; // types based on actual values in first row (call .get()/.all() first)
667
- declaredTypes: (string | null)[]; // types from CREATE TABLE schema (call .get()/.all() first)
668
- paramsCount: number; // the number of parameters expected by the statement
669
- native: any; // the native object representing the statement
670
-
671
- as(Class: new () => ReturnType): this;
672
- }
673
-
674
- type SQLQueryBindings =
675
- | string
676
- | bigint
677
- | TypedArray
678
- | number
679
- | boolean
680
- | null
681
- | Record<string, string | bigint | TypedArray | number | boolean | null>;
682
- ```
683
-
684
- ### Datatypes
685
-
686
- | JavaScript type | SQLite type |
687
- | --------------- | ---------------------- |
688
- | `string` | `TEXT` |
689
- | `number` | `INTEGER` or `DECIMAL` |
690
- | `boolean` | `INTEGER` (1 or 0) |
691
- | `Uint8Array` | `BLOB` |
692
- | `Buffer` | `BLOB` |
693
- | `bigint` | `INTEGER` |
694
- | `null` | `NULL` |