bun-types 1.3.2-canary.20251104T140728 → 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 (312) hide show
  1. package/bun.d.ts +4 -4
  2. package/docs/README.md +28 -0
  3. package/globals.d.ts +0 -1
  4. package/overrides.d.ts +45 -15
  5. package/package.json +1 -1
  6. package/test.d.ts +1 -1
  7. package/docs/api/binary-data.md +0 -1038
  8. package/docs/api/cc.md +0 -197
  9. package/docs/api/color.md +0 -262
  10. package/docs/api/console.md +0 -57
  11. package/docs/api/cookie.md +0 -449
  12. package/docs/api/dns.md +0 -110
  13. package/docs/api/fetch.md +0 -463
  14. package/docs/api/ffi.md +0 -557
  15. package/docs/api/file-io.md +0 -366
  16. package/docs/api/file-system-router.md +0 -112
  17. package/docs/api/file.md +0 -19
  18. package/docs/api/glob.md +0 -178
  19. package/docs/api/globals.md +0 -387
  20. package/docs/api/hashing.md +0 -319
  21. package/docs/api/html-rewriter.md +0 -334
  22. package/docs/api/http.md +0 -1408
  23. package/docs/api/import-meta.md +0 -69
  24. package/docs/api/node-api.md +0 -14
  25. package/docs/api/redis.md +0 -597
  26. package/docs/api/s3.md +0 -850
  27. package/docs/api/secrets.md +0 -319
  28. package/docs/api/semver.md +0 -52
  29. package/docs/api/spawn.md +0 -582
  30. package/docs/api/sql.md +0 -1374
  31. package/docs/api/sqlite.md +0 -694
  32. package/docs/api/streams.md +0 -230
  33. package/docs/api/tcp.md +0 -221
  34. package/docs/api/transpiler.md +0 -276
  35. package/docs/api/udp.md +0 -123
  36. package/docs/api/utils.md +0 -891
  37. package/docs/api/websockets.md +0 -573
  38. package/docs/api/workers.md +0 -322
  39. package/docs/api/yaml.md +0 -668
  40. package/docs/benchmarks.md +0 -120
  41. package/docs/bun-flavored-toml.md +0 -42
  42. package/docs/bundler/css.md +0 -1028
  43. package/docs/bundler/css_modules.md +0 -145
  44. package/docs/bundler/executables.md +0 -626
  45. package/docs/bundler/fullstack.md +0 -418
  46. package/docs/bundler/hmr.md +0 -234
  47. package/docs/bundler/html.md +0 -349
  48. package/docs/bundler/index.md +0 -1735
  49. package/docs/bundler/intro.md +0 -75
  50. package/docs/bundler/loaders.md +0 -410
  51. package/docs/bundler/macros.md +0 -329
  52. package/docs/bundler/plugins.md +0 -449
  53. package/docs/bundler/vs-esbuild.md +0 -1127
  54. package/docs/cli/add.md +0 -173
  55. package/docs/cli/bun-completions.md +0 -3
  56. package/docs/cli/bun-create.md +0 -338
  57. package/docs/cli/bun-install.md +0 -349
  58. package/docs/cli/bun-upgrade.md +0 -39
  59. package/docs/cli/bunx.md +0 -89
  60. package/docs/cli/filter.md +0 -90
  61. package/docs/cli/info.md +0 -65
  62. package/docs/cli/init.md +0 -85
  63. package/docs/cli/install.md +0 -344
  64. package/docs/cli/link.md +0 -40
  65. package/docs/cli/outdated.md +0 -90
  66. package/docs/cli/patch-commit.md +0 -11
  67. package/docs/cli/pm.md +0 -285
  68. package/docs/cli/publish.md +0 -120
  69. package/docs/cli/remove.md +0 -7
  70. package/docs/cli/run.md +0 -261
  71. package/docs/cli/test.md +0 -397
  72. package/docs/cli/unlink.md +0 -9
  73. package/docs/cli/update.md +0 -129
  74. package/docs/cli/why.md +0 -67
  75. package/docs/contributing/upgrading-webkit.md +0 -57
  76. package/docs/ecosystem/elysia.md +0 -24
  77. package/docs/ecosystem/express.md +0 -37
  78. package/docs/ecosystem/hono.md +0 -18
  79. package/docs/ecosystem/react.md +0 -65
  80. package/docs/ecosystem/stric.md +0 -38
  81. package/docs/guides/binary/arraybuffer-to-array.md +0 -27
  82. package/docs/guides/binary/arraybuffer-to-blob.md +0 -24
  83. package/docs/guides/binary/arraybuffer-to-buffer.md +0 -25
  84. package/docs/guides/binary/arraybuffer-to-string.md +0 -15
  85. package/docs/guides/binary/arraybuffer-to-typedarray.md +0 -39
  86. package/docs/guides/binary/blob-to-arraybuffer.md +0 -14
  87. package/docs/guides/binary/blob-to-dataview.md +0 -14
  88. package/docs/guides/binary/blob-to-stream.md +0 -14
  89. package/docs/guides/binary/blob-to-string.md +0 -15
  90. package/docs/guides/binary/blob-to-typedarray.md +0 -14
  91. package/docs/guides/binary/buffer-to-arraybuffer.md +0 -14
  92. package/docs/guides/binary/buffer-to-blob.md +0 -14
  93. package/docs/guides/binary/buffer-to-readablestream.md +0 -41
  94. package/docs/guides/binary/buffer-to-string.md +0 -25
  95. package/docs/guides/binary/buffer-to-typedarray.md +0 -14
  96. package/docs/guides/binary/dataview-to-string.md +0 -15
  97. package/docs/guides/binary/typedarray-to-arraybuffer.md +0 -25
  98. package/docs/guides/binary/typedarray-to-blob.md +0 -16
  99. package/docs/guides/binary/typedarray-to-buffer.md +0 -14
  100. package/docs/guides/binary/typedarray-to-dataview.md +0 -14
  101. package/docs/guides/binary/typedarray-to-readablestream.md +0 -41
  102. package/docs/guides/binary/typedarray-to-string.md +0 -16
  103. package/docs/guides/deployment/railway.md +0 -157
  104. package/docs/guides/ecosystem/astro.md +0 -72
  105. package/docs/guides/ecosystem/discordjs.md +0 -77
  106. package/docs/guides/ecosystem/docker.md +0 -140
  107. package/docs/guides/ecosystem/drizzle.md +0 -185
  108. package/docs/guides/ecosystem/edgedb.md +0 -228
  109. package/docs/guides/ecosystem/elysia.md +0 -31
  110. package/docs/guides/ecosystem/express.md +0 -40
  111. package/docs/guides/ecosystem/hono.md +0 -39
  112. package/docs/guides/ecosystem/mongoose.md +0 -87
  113. package/docs/guides/ecosystem/neon-drizzle.md +0 -220
  114. package/docs/guides/ecosystem/neon-serverless-postgres.md +0 -55
  115. package/docs/guides/ecosystem/nextjs.md +0 -49
  116. package/docs/guides/ecosystem/nuxt.md +0 -56
  117. package/docs/guides/ecosystem/pm2.md +0 -57
  118. package/docs/guides/ecosystem/prisma.md +0 -141
  119. package/docs/guides/ecosystem/qwik.md +0 -107
  120. package/docs/guides/ecosystem/react.md +0 -50
  121. package/docs/guides/ecosystem/remix.md +0 -78
  122. package/docs/guides/ecosystem/render.md +0 -79
  123. package/docs/guides/ecosystem/sentry.md +0 -52
  124. package/docs/guides/ecosystem/solidstart.md +0 -58
  125. package/docs/guides/ecosystem/ssr-react.md +0 -51
  126. package/docs/guides/ecosystem/stric.md +0 -55
  127. package/docs/guides/ecosystem/sveltekit.md +0 -125
  128. package/docs/guides/ecosystem/systemd.md +0 -113
  129. package/docs/guides/ecosystem/vite.md +0 -71
  130. package/docs/guides/html-rewriter/extract-links.md +0 -68
  131. package/docs/guides/html-rewriter/extract-social-meta.md +0 -93
  132. package/docs/guides/http/cluster.md +0 -66
  133. package/docs/guides/http/fetch-unix.md +0 -33
  134. package/docs/guides/http/fetch.md +0 -24
  135. package/docs/guides/http/file-uploads.md +0 -94
  136. package/docs/guides/http/hot.md +0 -26
  137. package/docs/guides/http/proxy.md +0 -24
  138. package/docs/guides/http/server.md +0 -46
  139. package/docs/guides/http/simple.md +0 -18
  140. package/docs/guides/http/stream-file.md +0 -48
  141. package/docs/guides/http/stream-iterator.md +0 -47
  142. package/docs/guides/http/stream-node-streams-in-bun.md +0 -20
  143. package/docs/guides/http/tls.md +0 -30
  144. package/docs/guides/install/add-dev.md +0 -26
  145. package/docs/guides/install/add-git.md +0 -36
  146. package/docs/guides/install/add-optional.md +0 -25
  147. package/docs/guides/install/add-peer.md +0 -43
  148. package/docs/guides/install/add-tarball.md +0 -33
  149. package/docs/guides/install/add.md +0 -42
  150. package/docs/guides/install/azure-artifacts.md +0 -73
  151. package/docs/guides/install/cicd.md +0 -41
  152. package/docs/guides/install/custom-registry.md +0 -30
  153. package/docs/guides/install/from-npm-install-to-bun-install.md +0 -214
  154. package/docs/guides/install/git-diff-bun-lockfile.md +0 -44
  155. package/docs/guides/install/jfrog-artifactory.md +0 -28
  156. package/docs/guides/install/npm-alias.md +0 -23
  157. package/docs/guides/install/registry-scope.md +0 -38
  158. package/docs/guides/install/trusted.md +0 -46
  159. package/docs/guides/install/workspaces.md +0 -70
  160. package/docs/guides/install/yarnlock.md +0 -44
  161. package/docs/guides/process/argv.md +0 -58
  162. package/docs/guides/process/ctrl-c.md +0 -16
  163. package/docs/guides/process/ipc.md +0 -66
  164. package/docs/guides/process/nanoseconds.md +0 -13
  165. package/docs/guides/process/os-signals.md +0 -39
  166. package/docs/guides/process/spawn-stderr.md +0 -31
  167. package/docs/guides/process/spawn-stdout.md +0 -26
  168. package/docs/guides/process/spawn.md +0 -41
  169. package/docs/guides/process/stdin.md +0 -54
  170. package/docs/guides/read-file/arraybuffer.md +0 -28
  171. package/docs/guides/read-file/buffer.md +0 -19
  172. package/docs/guides/read-file/exists.md +0 -16
  173. package/docs/guides/read-file/json.md +0 -17
  174. package/docs/guides/read-file/mime.md +0 -20
  175. package/docs/guides/read-file/stream.md +0 -26
  176. package/docs/guides/read-file/string.md +0 -22
  177. package/docs/guides/read-file/uint8array.md +0 -21
  178. package/docs/guides/read-file/watch.md +0 -68
  179. package/docs/guides/runtime/build-time-constants.md +0 -293
  180. package/docs/guides/runtime/cicd.md +0 -43
  181. package/docs/guides/runtime/codesign-macos-executable.md +0 -56
  182. package/docs/guides/runtime/define-constant.md +0 -145
  183. package/docs/guides/runtime/delete-directory.md +0 -37
  184. package/docs/guides/runtime/delete-file.md +0 -19
  185. package/docs/guides/runtime/heap-snapshot.md +0 -26
  186. package/docs/guides/runtime/import-html.md +0 -15
  187. package/docs/guides/runtime/import-json.md +0 -44
  188. package/docs/guides/runtime/import-toml.md +0 -30
  189. package/docs/guides/runtime/import-yaml.md +0 -102
  190. package/docs/guides/runtime/read-env.md +0 -32
  191. package/docs/guides/runtime/set-env.md +0 -47
  192. package/docs/guides/runtime/shell.md +0 -40
  193. package/docs/guides/runtime/timezone.md +0 -35
  194. package/docs/guides/runtime/tsconfig-paths.md +0 -29
  195. package/docs/guides/runtime/typescript.md +0 -49
  196. package/docs/guides/runtime/vscode-debugger.md +0 -47
  197. package/docs/guides/runtime/web-debugger.md +0 -82
  198. package/docs/guides/streams/node-readable-to-arraybuffer.md +0 -11
  199. package/docs/guides/streams/node-readable-to-blob.md +0 -11
  200. package/docs/guides/streams/node-readable-to-json.md +0 -12
  201. package/docs/guides/streams/node-readable-to-string.md +0 -12
  202. package/docs/guides/streams/node-readable-to-uint8array.md +0 -11
  203. package/docs/guides/streams/to-array.md +0 -14
  204. package/docs/guides/streams/to-arraybuffer.md +0 -14
  205. package/docs/guides/streams/to-blob.md +0 -14
  206. package/docs/guides/streams/to-buffer.md +0 -15
  207. package/docs/guides/streams/to-json.md +0 -14
  208. package/docs/guides/streams/to-string.md +0 -14
  209. package/docs/guides/streams/to-typedarray.md +0 -22
  210. package/docs/guides/test/bail.md +0 -22
  211. package/docs/guides/test/coverage-threshold.md +0 -60
  212. package/docs/guides/test/coverage.md +0 -44
  213. package/docs/guides/test/happy-dom.md +0 -68
  214. package/docs/guides/test/migrate-from-jest.md +0 -143
  215. package/docs/guides/test/mock-clock.md +0 -48
  216. package/docs/guides/test/mock-functions.md +0 -68
  217. package/docs/guides/test/rerun-each.md +0 -14
  218. package/docs/guides/test/run-tests.md +0 -111
  219. package/docs/guides/test/skip-tests.md +0 -39
  220. package/docs/guides/test/snapshot.md +0 -95
  221. package/docs/guides/test/spy-on.md +0 -46
  222. package/docs/guides/test/svelte-test.md +0 -120
  223. package/docs/guides/test/testing-library.md +0 -92
  224. package/docs/guides/test/timeout.md +0 -15
  225. package/docs/guides/test/todo-tests.md +0 -67
  226. package/docs/guides/test/update-snapshots.md +0 -46
  227. package/docs/guides/test/watch-mode.md +0 -19
  228. package/docs/guides/util/base64.md +0 -15
  229. package/docs/guides/util/deep-equals.md +0 -39
  230. package/docs/guides/util/deflate.md +0 -18
  231. package/docs/guides/util/detect-bun.md +0 -23
  232. package/docs/guides/util/entrypoint.md +0 -17
  233. package/docs/guides/util/escape-html.md +0 -22
  234. package/docs/guides/util/file-url-to-path.md +0 -14
  235. package/docs/guides/util/gzip.md +0 -18
  236. package/docs/guides/util/hash-a-password.md +0 -54
  237. package/docs/guides/util/import-meta-dir.md +0 -13
  238. package/docs/guides/util/import-meta-file.md +0 -13
  239. package/docs/guides/util/import-meta-path.md +0 -13
  240. package/docs/guides/util/javascript-uuid.md +0 -23
  241. package/docs/guides/util/main.md +0 -32
  242. package/docs/guides/util/path-to-file-url.md +0 -14
  243. package/docs/guides/util/sleep.md +0 -22
  244. package/docs/guides/util/version.md +0 -21
  245. package/docs/guides/util/which-path-to-executable-bin.md +0 -15
  246. package/docs/guides/websocket/compression.md +0 -31
  247. package/docs/guides/websocket/context.md +0 -77
  248. package/docs/guides/websocket/pubsub.md +0 -41
  249. package/docs/guides/websocket/simple.md +0 -33
  250. package/docs/guides/write-file/append.md +0 -52
  251. package/docs/guides/write-file/basic.md +0 -44
  252. package/docs/guides/write-file/blob.md +0 -28
  253. package/docs/guides/write-file/cat.md +0 -17
  254. package/docs/guides/write-file/file-cp.md +0 -16
  255. package/docs/guides/write-file/filesink.md +0 -52
  256. package/docs/guides/write-file/response.md +0 -17
  257. package/docs/guides/write-file/stdout.md +0 -21
  258. package/docs/guides/write-file/stream.md +0 -17
  259. package/docs/guides/write-file/unlink.md +0 -16
  260. package/docs/index.md +0 -77
  261. package/docs/install/audit.md +0 -57
  262. package/docs/install/cache.md +0 -59
  263. package/docs/install/catalogs.md +0 -298
  264. package/docs/install/index.md +0 -248
  265. package/docs/install/isolated.md +0 -197
  266. package/docs/install/lifecycle.md +0 -46
  267. package/docs/install/lockfile.md +0 -58
  268. package/docs/install/npmrc.md +0 -105
  269. package/docs/install/overrides.md +0 -73
  270. package/docs/install/patch.md +0 -59
  271. package/docs/install/registries.md +0 -30
  272. package/docs/install/security-scanner-api.md +0 -81
  273. package/docs/install/workspaces.md +0 -114
  274. package/docs/installation.md +0 -327
  275. package/docs/project/asan.md +0 -124
  276. package/docs/project/benchmarking.md +0 -203
  277. package/docs/project/bindgen.md +0 -225
  278. package/docs/project/building-windows.md +0 -162
  279. package/docs/project/contributing.md +0 -347
  280. package/docs/project/internals/build-process-for-ci.md +0 -75
  281. package/docs/project/licensing.md +0 -73
  282. package/docs/project/roadmap.md +0 -87
  283. package/docs/quickstart.md +0 -157
  284. package/docs/runtime/autoimport.md +0 -94
  285. package/docs/runtime/bun-apis.md +0 -207
  286. package/docs/runtime/bunfig.md +0 -772
  287. package/docs/runtime/debugger.md +0 -325
  288. package/docs/runtime/env.md +0 -253
  289. package/docs/runtime/hot.md +0 -145
  290. package/docs/runtime/index.md +0 -312
  291. package/docs/runtime/jsx.md +0 -385
  292. package/docs/runtime/loaders.md +0 -130
  293. package/docs/runtime/modules.md +0 -320
  294. package/docs/runtime/nodejs-apis.md +0 -463
  295. package/docs/runtime/plugins.md +0 -561
  296. package/docs/runtime/shell.md +0 -605
  297. package/docs/runtime/typescript.md +0 -139
  298. package/docs/runtime/web-apis.md +0 -128
  299. package/docs/test/configuration.md +0 -154
  300. package/docs/test/coverage.md +0 -142
  301. package/docs/test/discovery.md +0 -85
  302. package/docs/test/dom.md +0 -75
  303. package/docs/test/examples/concurrent-test-glob.md +0 -132
  304. package/docs/test/hot.md +0 -15
  305. package/docs/test/lifecycle.md +0 -81
  306. package/docs/test/mocks.md +0 -313
  307. package/docs/test/reporters.md +0 -117
  308. package/docs/test/runtime-behavior.md +0 -95
  309. package/docs/test/snapshots.md +0 -68
  310. package/docs/test/time.md +0 -126
  311. package/docs/test/writing.md +0 -825
  312. 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` |