bun-types 1.1.37-canary.20241124T140524 → 1.1.37

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 (272) hide show
  1. package/docs/api/binary-data.md +1028 -0
  2. package/docs/api/cc.md +197 -0
  3. package/docs/api/color.md +262 -0
  4. package/docs/api/console.md +38 -0
  5. package/docs/api/dns.md +113 -0
  6. package/docs/api/fetch.md +308 -0
  7. package/docs/api/ffi.md +536 -0
  8. package/docs/api/file-io.md +358 -0
  9. package/docs/api/file-system-router.md +112 -0
  10. package/docs/api/file.md +19 -0
  11. package/docs/api/glob.md +157 -0
  12. package/docs/api/globals.md +387 -0
  13. package/docs/api/hashing.md +314 -0
  14. package/docs/api/html-rewriter.md +31 -0
  15. package/docs/api/http.md +661 -0
  16. package/docs/api/import-meta.md +69 -0
  17. package/docs/api/node-api.md +16 -0
  18. package/docs/api/semver.md +52 -0
  19. package/docs/api/spawn.md +441 -0
  20. package/docs/api/sqlite.md +692 -0
  21. package/docs/api/streams.md +214 -0
  22. package/docs/api/tcp.md +221 -0
  23. package/docs/api/test.md +1 -0
  24. package/docs/api/transpiler.md +274 -0
  25. package/docs/api/udp.md +125 -0
  26. package/docs/api/utils.md +773 -0
  27. package/docs/api/websockets.md +547 -0
  28. package/docs/api/workers.md +230 -0
  29. package/docs/benchmarks.md +120 -0
  30. package/docs/bun-flavored-toml.md +42 -0
  31. package/docs/bundler/executables.md +291 -0
  32. package/docs/bundler/index.md +1543 -0
  33. package/docs/bundler/intro.md +75 -0
  34. package/docs/bundler/loaders.md +288 -0
  35. package/docs/bundler/macros.md +327 -0
  36. package/docs/bundler/plugins.md +30 -0
  37. package/docs/bundler/vs-esbuild.md +1127 -0
  38. package/docs/cli/add.md +163 -0
  39. package/docs/cli/bun-completions.md +3 -0
  40. package/docs/cli/bun-create.md +254 -0
  41. package/docs/cli/bun-dev.md +21 -0
  42. package/docs/cli/bun-install.md +255 -0
  43. package/docs/cli/bun-upgrade.md +39 -0
  44. package/docs/cli/bunx.md +80 -0
  45. package/docs/cli/filter.md +57 -0
  46. package/docs/cli/init.md +40 -0
  47. package/docs/cli/install.md +205 -0
  48. package/docs/cli/link.md +38 -0
  49. package/docs/cli/outdated.md +61 -0
  50. package/docs/cli/patch-commit.md +9 -0
  51. package/docs/cli/pm.md +150 -0
  52. package/docs/cli/publish.md +107 -0
  53. package/docs/cli/remove.md +5 -0
  54. package/docs/cli/run.md +196 -0
  55. package/docs/cli/test.md +247 -0
  56. package/docs/cli/unlink.md +7 -0
  57. package/docs/cli/update.md +34 -0
  58. package/docs/contributing/upgrading-webkit.md +57 -0
  59. package/docs/ecosystem/elysia.md +24 -0
  60. package/docs/ecosystem/express.md +37 -0
  61. package/docs/ecosystem/hono.md +18 -0
  62. package/docs/ecosystem/react.md +65 -0
  63. package/docs/ecosystem/stric.md +37 -0
  64. package/docs/guides/binary/arraybuffer-to-array.md +27 -0
  65. package/docs/guides/binary/arraybuffer-to-blob.md +24 -0
  66. package/docs/guides/binary/arraybuffer-to-buffer.md +25 -0
  67. package/docs/guides/binary/arraybuffer-to-string.md +15 -0
  68. package/docs/guides/binary/arraybuffer-to-typedarray.md +39 -0
  69. package/docs/guides/binary/blob-to-arraybuffer.md +14 -0
  70. package/docs/guides/binary/blob-to-dataview.md +14 -0
  71. package/docs/guides/binary/blob-to-stream.md +14 -0
  72. package/docs/guides/binary/blob-to-string.md +15 -0
  73. package/docs/guides/binary/blob-to-typedarray.md +14 -0
  74. package/docs/guides/binary/buffer-to-arraybuffer.md +14 -0
  75. package/docs/guides/binary/buffer-to-blob.md +14 -0
  76. package/docs/guides/binary/buffer-to-readablestream.md +41 -0
  77. package/docs/guides/binary/buffer-to-string.md +25 -0
  78. package/docs/guides/binary/buffer-to-typedarray.md +14 -0
  79. package/docs/guides/binary/dataview-to-string.md +15 -0
  80. package/docs/guides/binary/typedarray-to-arraybuffer.md +25 -0
  81. package/docs/guides/binary/typedarray-to-blob.md +16 -0
  82. package/docs/guides/binary/typedarray-to-buffer.md +14 -0
  83. package/docs/guides/binary/typedarray-to-dataview.md +14 -0
  84. package/docs/guides/binary/typedarray-to-readablestream.md +41 -0
  85. package/docs/guides/binary/typedarray-to-string.md +16 -0
  86. package/docs/guides/ecosystem/astro.md +72 -0
  87. package/docs/guides/ecosystem/discordjs.md +77 -0
  88. package/docs/guides/ecosystem/docker.md +140 -0
  89. package/docs/guides/ecosystem/drizzle.md +185 -0
  90. package/docs/guides/ecosystem/edgedb.md +228 -0
  91. package/docs/guides/ecosystem/elysia.md +31 -0
  92. package/docs/guides/ecosystem/express.md +40 -0
  93. package/docs/guides/ecosystem/hono.md +39 -0
  94. package/docs/guides/ecosystem/mongoose.md +87 -0
  95. package/docs/guides/ecosystem/neon-drizzle.md +220 -0
  96. package/docs/guides/ecosystem/neon-serverless-postgres.md +55 -0
  97. package/docs/guides/ecosystem/nextjs.md +38 -0
  98. package/docs/guides/ecosystem/nuxt.md +56 -0
  99. package/docs/guides/ecosystem/pm2.md +57 -0
  100. package/docs/guides/ecosystem/prisma.md +140 -0
  101. package/docs/guides/ecosystem/qwik.md +107 -0
  102. package/docs/guides/ecosystem/react.md +49 -0
  103. package/docs/guides/ecosystem/remix.md +78 -0
  104. package/docs/guides/ecosystem/render.md +79 -0
  105. package/docs/guides/ecosystem/sentry.md +52 -0
  106. package/docs/guides/ecosystem/solidstart.md +58 -0
  107. package/docs/guides/ecosystem/ssr-react.md +51 -0
  108. package/docs/guides/ecosystem/stric.md +55 -0
  109. package/docs/guides/ecosystem/sveltekit.md +125 -0
  110. package/docs/guides/ecosystem/systemd.md +113 -0
  111. package/docs/guides/ecosystem/vite.md +70 -0
  112. package/docs/guides/http/cluster.md +66 -0
  113. package/docs/guides/http/fetch-unix.md +33 -0
  114. package/docs/guides/http/fetch.md +24 -0
  115. package/docs/guides/http/file-uploads.md +94 -0
  116. package/docs/guides/http/hot.md +22 -0
  117. package/docs/guides/http/proxy.md +24 -0
  118. package/docs/guides/http/server.md +46 -0
  119. package/docs/guides/http/simple.md +18 -0
  120. package/docs/guides/http/stream-file.md +48 -0
  121. package/docs/guides/http/stream-iterator.md +47 -0
  122. package/docs/guides/http/stream-node-streams-in-bun.md +20 -0
  123. package/docs/guides/http/tls.md +30 -0
  124. package/docs/guides/install/add-dev.md +26 -0
  125. package/docs/guides/install/add-git.md +36 -0
  126. package/docs/guides/install/add-optional.md +25 -0
  127. package/docs/guides/install/add-peer.md +17 -0
  128. package/docs/guides/install/add-tarball.md +33 -0
  129. package/docs/guides/install/add.md +42 -0
  130. package/docs/guides/install/azure-artifacts.md +73 -0
  131. package/docs/guides/install/cicd.md +41 -0
  132. package/docs/guides/install/custom-registry.md +30 -0
  133. package/docs/guides/install/from-npm-install-to-bun-install.md +214 -0
  134. package/docs/guides/install/git-diff-bun-lockfile.md +38 -0
  135. package/docs/guides/install/jfrog-artifactory.md +28 -0
  136. package/docs/guides/install/npm-alias.md +23 -0
  137. package/docs/guides/install/registry-scope.md +36 -0
  138. package/docs/guides/install/trusted.md +48 -0
  139. package/docs/guides/install/workspaces.md +70 -0
  140. package/docs/guides/install/yarnlock.md +42 -0
  141. package/docs/guides/process/argv.md +57 -0
  142. package/docs/guides/process/ctrl-c.md +16 -0
  143. package/docs/guides/process/ipc.md +66 -0
  144. package/docs/guides/process/nanoseconds.md +13 -0
  145. package/docs/guides/process/os-signals.md +39 -0
  146. package/docs/guides/process/spawn-stderr.md +31 -0
  147. package/docs/guides/process/spawn-stdout.md +26 -0
  148. package/docs/guides/process/spawn.md +41 -0
  149. package/docs/guides/process/stdin.md +54 -0
  150. package/docs/guides/read-file/arraybuffer.md +28 -0
  151. package/docs/guides/read-file/buffer.md +19 -0
  152. package/docs/guides/read-file/exists.md +16 -0
  153. package/docs/guides/read-file/json.md +17 -0
  154. package/docs/guides/read-file/mime.md +20 -0
  155. package/docs/guides/read-file/stream.md +26 -0
  156. package/docs/guides/read-file/string.md +22 -0
  157. package/docs/guides/read-file/uint8array.md +21 -0
  158. package/docs/guides/read-file/watch.md +68 -0
  159. package/docs/guides/runtime/cicd.md +43 -0
  160. package/docs/guides/runtime/define-constant.md +145 -0
  161. package/docs/guides/runtime/import-html.md +15 -0
  162. package/docs/guides/runtime/import-json.md +44 -0
  163. package/docs/guides/runtime/import-toml.md +30 -0
  164. package/docs/guides/runtime/read-env.md +32 -0
  165. package/docs/guides/runtime/set-env.md +37 -0
  166. package/docs/guides/runtime/shell.md +40 -0
  167. package/docs/guides/runtime/timezone.md +35 -0
  168. package/docs/guides/runtime/tsconfig-paths.md +29 -0
  169. package/docs/guides/runtime/typescript.md +47 -0
  170. package/docs/guides/runtime/vscode-debugger.md +47 -0
  171. package/docs/guides/runtime/web-debugger.md +82 -0
  172. package/docs/guides/streams/node-readable-to-arraybuffer.md +11 -0
  173. package/docs/guides/streams/node-readable-to-blob.md +11 -0
  174. package/docs/guides/streams/node-readable-to-json.md +12 -0
  175. package/docs/guides/streams/node-readable-to-string.md +12 -0
  176. package/docs/guides/streams/node-readable-to-uint8array.md +11 -0
  177. package/docs/guides/streams/to-array.md +14 -0
  178. package/docs/guides/streams/to-arraybuffer.md +14 -0
  179. package/docs/guides/streams/to-blob.md +14 -0
  180. package/docs/guides/streams/to-buffer.md +15 -0
  181. package/docs/guides/streams/to-json.md +14 -0
  182. package/docs/guides/streams/to-string.md +14 -0
  183. package/docs/guides/streams/to-typedarray.md +22 -0
  184. package/docs/guides/test/bail.md +22 -0
  185. package/docs/guides/test/coverage-threshold.md +60 -0
  186. package/docs/guides/test/coverage.md +44 -0
  187. package/docs/guides/test/happy-dom.md +68 -0
  188. package/docs/guides/test/migrate-from-jest.md +110 -0
  189. package/docs/guides/test/mock-clock.md +48 -0
  190. package/docs/guides/test/mock-functions.md +68 -0
  191. package/docs/guides/test/rerun-each.md +14 -0
  192. package/docs/guides/test/run-tests.md +111 -0
  193. package/docs/guides/test/skip-tests.md +39 -0
  194. package/docs/guides/test/snapshot.md +99 -0
  195. package/docs/guides/test/spy-on.md +46 -0
  196. package/docs/guides/test/testing-library.md +87 -0
  197. package/docs/guides/test/timeout.md +15 -0
  198. package/docs/guides/test/todo-tests.md +67 -0
  199. package/docs/guides/test/update-snapshots.md +50 -0
  200. package/docs/guides/test/watch-mode.md +19 -0
  201. package/docs/guides/util/base64.md +15 -0
  202. package/docs/guides/util/deep-equals.md +39 -0
  203. package/docs/guides/util/deflate.md +18 -0
  204. package/docs/guides/util/detect-bun.md +23 -0
  205. package/docs/guides/util/entrypoint.md +17 -0
  206. package/docs/guides/util/escape-html.md +22 -0
  207. package/docs/guides/util/file-url-to-path.md +14 -0
  208. package/docs/guides/util/gzip.md +18 -0
  209. package/docs/guides/util/hash-a-password.md +54 -0
  210. package/docs/guides/util/import-meta-dir.md +13 -0
  211. package/docs/guides/util/import-meta-file.md +13 -0
  212. package/docs/guides/util/import-meta-path.md +13 -0
  213. package/docs/guides/util/main.md +32 -0
  214. package/docs/guides/util/path-to-file-url.md +14 -0
  215. package/docs/guides/util/sleep.md +22 -0
  216. package/docs/guides/util/version.md +21 -0
  217. package/docs/guides/util/which-path-to-executable-bin.md +15 -0
  218. package/docs/guides/websocket/compression.md +31 -0
  219. package/docs/guides/websocket/context.md +72 -0
  220. package/docs/guides/websocket/pubsub.md +38 -0
  221. package/docs/guides/websocket/simple.md +33 -0
  222. package/docs/guides/write-file/append.md +52 -0
  223. package/docs/guides/write-file/basic.md +44 -0
  224. package/docs/guides/write-file/blob.md +28 -0
  225. package/docs/guides/write-file/cat.md +17 -0
  226. package/docs/guides/write-file/file-cp.md +16 -0
  227. package/docs/guides/write-file/filesink.md +52 -0
  228. package/docs/guides/write-file/response.md +17 -0
  229. package/docs/guides/write-file/stdout.md +21 -0
  230. package/docs/guides/write-file/stream.md +17 -0
  231. package/docs/guides/write-file/unlink.md +23 -0
  232. package/docs/index.md +77 -0
  233. package/docs/install/cache.md +57 -0
  234. package/docs/install/index.md +202 -0
  235. package/docs/install/lifecycle.md +46 -0
  236. package/docs/install/lockfile.md +90 -0
  237. package/docs/install/npmrc.md +75 -0
  238. package/docs/install/overrides.md +73 -0
  239. package/docs/install/patch.md +57 -0
  240. package/docs/install/registries.md +30 -0
  241. package/docs/install/workspaces.md +70 -0
  242. package/docs/installation.md +289 -0
  243. package/docs/project/benchmarking.md +203 -0
  244. package/docs/project/building-windows.md +162 -0
  245. package/docs/project/internals/build-process-for-ci.md +75 -0
  246. package/docs/project/roadmap.md +87 -0
  247. package/docs/quickstart.md +144 -0
  248. package/docs/runtime/autoimport.md +94 -0
  249. package/docs/runtime/bun-apis.md +129 -0
  250. package/docs/runtime/bunfig.md +532 -0
  251. package/docs/runtime/debugger.md +325 -0
  252. package/docs/runtime/env.md +214 -0
  253. package/docs/runtime/hot.md +139 -0
  254. package/docs/runtime/index.md +309 -0
  255. package/docs/runtime/jsx.md +326 -0
  256. package/docs/runtime/loaders.md +127 -0
  257. package/docs/runtime/modules.md +298 -0
  258. package/docs/runtime/nodejs-apis.md +456 -0
  259. package/docs/runtime/plugins.md +605 -0
  260. package/docs/runtime/shell.md +537 -0
  261. package/docs/runtime/typescript.md +60 -0
  262. package/docs/runtime/web-apis.md +128 -0
  263. package/docs/test/coverage.md +91 -0
  264. package/docs/test/dom.md +75 -0
  265. package/docs/test/hot.md +15 -0
  266. package/docs/test/lifecycle.md +81 -0
  267. package/docs/test/mocks.md +236 -0
  268. package/docs/test/snapshots.md +15 -0
  269. package/docs/test/time.md +106 -0
  270. package/docs/test/writing.md +547 -0
  271. package/docs/typescript.md +51 -0
  272. package/package.json +5 -4
@@ -0,0 +1,31 @@
1
+ ---
2
+ name: Build an HTTP server using Elysia and Bun
3
+ ---
4
+
5
+ [Elysia](https://elysiajs.com) is a Bun-first performance focused web framework that takes full advantage of Bun's HTTP, file system, and hot reloading APIs. Get started with `bun create`.
6
+
7
+ ```bash
8
+ $ bun create elysia myapp
9
+ $ cd myapp
10
+ $ bun run dev
11
+ ```
12
+
13
+ ---
14
+
15
+ To define a simple HTTP route and start a server with Elysia:
16
+
17
+ ```ts#server.ts
18
+ import { Elysia } from 'elysia'
19
+
20
+ const app = new Elysia()
21
+ .get('/', () => 'Hello Elysia')
22
+ .listen(8080)
23
+
24
+ console.log(`🦊 Elysia is running at on port ${app.server?.port}...`)
25
+ ```
26
+
27
+ ---
28
+
29
+ Elysia is a full-featured server framework with Express-like syntax, type inference, middleware, file uploads, and plugins for JWT authentication, tRPC, and more. It's also is one of the [fastest Bun web frameworks](https://github.com/SaltyAom/bun-http-framework-benchmark).
30
+
31
+ Refer to the Elysia [documentation](https://elysiajs.com/quick-start.html) for more information.
@@ -0,0 +1,40 @@
1
+ ---
2
+ name: Build an HTTP server using Express and Bun
3
+ ---
4
+
5
+ Express and other major Node.js HTTP libraries should work out of the box. Bun implements the [`node:http`](https://nodejs.org/api/http.html) and [`node:https`](https://nodejs.org/api/https.html) modules that these libraries rely on.
6
+
7
+ {% callout %}
8
+ Refer to the [Runtime > Node.js APIs](https://bun.sh/docs/runtime/nodejs-apis#node-http) page for more detailed compatibility information.
9
+ {% /callout %}
10
+
11
+ ```sh
12
+ $ bun add express
13
+ ```
14
+
15
+ ---
16
+
17
+ To define a simple HTTP route and start a server with Express:
18
+
19
+ ```ts#server.ts
20
+ import express from "express";
21
+
22
+ const app = express();
23
+ const port = 8080;
24
+
25
+ app.get("/", (req, res) => {
26
+ res.send("Hello World!");
27
+ });
28
+
29
+ app.listen(port, () => {
30
+ console.log(`Listening on port ${port}...`);
31
+ });
32
+ ```
33
+
34
+ ---
35
+
36
+ To start the server on `localhost`:
37
+
38
+ ```sh
39
+ $ bun server.ts
40
+ ```
@@ -0,0 +1,39 @@
1
+ ---
2
+ name: Build an HTTP server using Hono and Bun
3
+ ---
4
+
5
+ [Hono](https://github.com/honojs/hono) is a lightweight ultrafast web framework designed for the edge.
6
+
7
+ ```ts
8
+ import { Hono } from "hono";
9
+ const app = new Hono();
10
+
11
+ app.get("/", c => c.text("Hono!"));
12
+
13
+ export default app;
14
+ ```
15
+
16
+ ---
17
+
18
+ Use `create-hono` to get started with one of Hono's project templates. Select `bun` when prompted for a template.
19
+
20
+ ```bash
21
+ $ bun create hono myapp
22
+ ✔ Which template do you want to use? › bun
23
+ cloned honojs/starter#main to /path/to/myapp
24
+ ✔ Copied project files
25
+ $ cd myapp
26
+ $ bun install
27
+ ```
28
+
29
+ ---
30
+
31
+ Then start the dev server and visit [localhost:3000](http://localhost:3000).
32
+
33
+ ```bash
34
+ $ bun run dev
35
+ ```
36
+
37
+ ---
38
+
39
+ Refer to Hono's guide on [getting started with Bun](https://hono.dev/getting-started/bun) for more information.
@@ -0,0 +1,87 @@
1
+ ---
2
+ name: Read and write data to MongoDB using Mongoose and Bun
3
+ ---
4
+
5
+ MongoDB and Mongoose work out of the box with Bun. This guide assumes you've already installed MongoDB and are running it as background process/service on your development machine. Follow [this guide](https://www.mongodb.com/docs/manual/installation/) for details.
6
+
7
+ ---
8
+
9
+ Once MongoDB is running, create a directory and initialize it with `bun init`.
10
+
11
+ ```bash
12
+ mkdir mongoose-app
13
+ cd mongoose-app
14
+ bun init
15
+ ```
16
+
17
+ ---
18
+
19
+ Then add Mongoose as a dependency.
20
+
21
+ ```bash
22
+ bun add mongoose
23
+ ```
24
+
25
+ ---
26
+
27
+ In `schema.ts` we'll declare and export a simple `Animal` model.
28
+
29
+ ```ts#schema.ts
30
+ import * as mongoose from 'mongoose';
31
+
32
+ const animalSchema = new mongoose.Schema(
33
+ {
34
+ name: {type: String, required: true},
35
+ sound: {type: String, required: true},
36
+ },
37
+ {
38
+ methods: {
39
+ speak() {
40
+ console.log(`${this.sound}!`);
41
+ },
42
+ },
43
+ }
44
+ );
45
+
46
+ export type Animal = mongoose.InferSchemaType<typeof animalSchema>;
47
+ export const Animal = mongoose.model('Animal', animalSchema);
48
+ ```
49
+
50
+ ---
51
+
52
+ Now from `index.ts` we can import `Animal`, connect to MongoDB, and add some data to our database.
53
+
54
+ ```ts#index.ts
55
+ import * as mongoose from 'mongoose';
56
+ import {Animal} from './schema';
57
+
58
+ // connect to database
59
+ await mongoose.connect('mongodb://127.0.0.1:27017/mongoose-app');
60
+
61
+ // create new Animal
62
+ const cow = new Animal({
63
+ name: 'Cow',
64
+ sound: 'Moo',
65
+ });
66
+ await cow.save(); // saves to the database
67
+
68
+ // read all Animals
69
+ const animals = await Animal.find();
70
+ animals[0].speak(); // logs "Moo!"
71
+
72
+ // disconnect
73
+ await mongoose.disconnect();
74
+ ```
75
+
76
+ ---
77
+
78
+ Let's run this with `bun run`.
79
+
80
+ ```bash
81
+ $ bun run index.ts
82
+ Moo!
83
+ ```
84
+
85
+ ---
86
+
87
+ This is a simple introduction to using Mongoose with TypeScript and Bun. As you build your application, refer to the official [MongoDB](https://docs.mongodb.com/) and [Mongoose](https://mongoosejs.com/docs/) sites for complete documentation.
@@ -0,0 +1,220 @@
1
+ ---
2
+ name: Use Neon Postgres through Drizzle ORM
3
+ ---
4
+
5
+ [Neon](https://neon.tech/) is a fully managed serverless Postgres, separating compute and storage to offer features like autoscaling, branching and bottomless storage. Neon can be used from Bun directly using the `@neondatabase/serverless` driver or through an ORM like `Drizzle`.
6
+
7
+ Drizzle ORM supports both a SQL-like "query builder" API and an ORM-like [Queries API](https://orm.drizzle.team/docs/rqb). Get started by creating a project directory, initializing the directory using `bun init`, and installing Drizzle and the [Neon serverless driver](https://github.com/neondatabase/serverless/).
8
+
9
+ ```sh
10
+ $ mkdir bun-drizzle-neon
11
+ $ cd bun-drizzle-neon
12
+ $ bun init -y
13
+ $ bun add drizzle-orm @neondatabase/serverless
14
+ $ bun add -D drizzle-kit
15
+ ```
16
+
17
+ ---
18
+
19
+ Create a `.env.local` file and add your [Neon Postgres connection string](https://neon.tech/docs/connect/connect-from-any-app) to it.
20
+
21
+ ```sh
22
+ DATABASE_URL=postgresql://username:password@ep-adj-noun-guid.us-east-1.aws.neon.tech/neondb?sslmode=require
23
+ ```
24
+
25
+ ---
26
+
27
+ We will connect to the Neon database using the Neon serverless driver, wrapped in a Drizzle database instance.
28
+
29
+ ```ts#db.ts
30
+ import { neon } from '@neondatabase/serverless';
31
+ import { drizzle } from 'drizzle-orm/neon-http';
32
+
33
+ // Bun automatically loads the DATABASE_URL from .env.local
34
+ // Refer to: https://bun.sh/docs/runtime/env for more information
35
+ const sql = neon(process.env.DATABASE_URL!);
36
+
37
+ export const db = drizzle(sql);
38
+ ```
39
+
40
+ ---
41
+
42
+ To see the database in action, add these lines to `index.ts`.
43
+
44
+ ```ts#index.ts
45
+ import { db } from "./db";
46
+ import { sql } from "drizzle-orm";
47
+
48
+ const query = sql`select 'hello world' as text`;
49
+ const result = await db.execute(query);
50
+ console.log(result.rows);
51
+ ```
52
+
53
+ ---
54
+
55
+ Then run `index.ts` with Bun.
56
+
57
+ ```sh
58
+ $ bun run index.ts
59
+ [
60
+ {
61
+ text: "hello world",
62
+ }
63
+ ]
64
+ ```
65
+
66
+ ---
67
+
68
+ We can define a schema for our database using Drizzle ORM primitives. Create a `schema.ts` file and add this code.
69
+
70
+ ```ts#schema.ts
71
+ import { pgTable, integer, serial, text, timestamp } from "drizzle-orm/pg-core";
72
+
73
+ export const authors = pgTable("authors", {
74
+ id: serial("id").primaryKey(),
75
+ name: text("name").notNull(),
76
+ bio: text("bio"),
77
+ createdAt: timestamp("created_at").notNull().defaultNow(),
78
+ });
79
+ ```
80
+
81
+ ---
82
+
83
+ We then use the `drizzle-kit` CLI to generate an initial SQL migration.
84
+
85
+ ```sh
86
+ $ bunx drizzle-kit generate --dialect postgresql --schema ./schema.ts --out ./drizzle
87
+ ```
88
+
89
+ ---
90
+
91
+ This creates a new `drizzle` directory containing a `.sql` migration file and `meta` directory.
92
+
93
+ ```txt
94
+ drizzle
95
+ ├── 0000_aspiring_post.sql
96
+ └── meta
97
+ ├── 0000_snapshot.json
98
+ └── _journal.json
99
+ ```
100
+
101
+ ---
102
+
103
+ We can execute these migrations with a simple `migrate.ts` script. This script creates a new connection to the Neon database and executes all unexecuted migrations in the `drizzle` directory.
104
+
105
+ ```ts#migrate.ts
106
+ import { db } from './db';
107
+ import { migrate } from "drizzle-orm/neon-http/migrator";
108
+
109
+ const main = async () => {
110
+ try {
111
+ await migrate(db, { migrationsFolder: "drizzle" });
112
+ console.log("Migration completed");
113
+ } catch (error) {
114
+ console.error("Error during migration:", error);
115
+ process.exit(1);
116
+ }
117
+ };
118
+
119
+ main();
120
+ ```
121
+
122
+ ---
123
+
124
+ We can run this script with `bun` to execute the migration.
125
+
126
+ ```sh
127
+ $ bun run migrate.ts
128
+ Migration completed
129
+ ```
130
+
131
+ ---
132
+
133
+ We can now add some data to our database. Create a `seed.ts` file with the following contents.
134
+
135
+ ```ts#seed.ts
136
+ import { db } from "./db";
137
+ import * as schema from "./schema";
138
+
139
+ async function seed() {
140
+ await db.insert(schema.authors).values([
141
+ {
142
+ name: "J.R.R. Tolkien",
143
+ bio: "The creator of Middle-earth and author of The Lord of the Rings.",
144
+ },
145
+ {
146
+ name: "George R.R. Martin",
147
+ bio: "The author of the epic fantasy series A Song of Ice and Fire.",
148
+ },
149
+ {
150
+ name: "J.K. Rowling",
151
+ bio: "The creator of the Harry Potter series.",
152
+ },
153
+ ]);
154
+ }
155
+
156
+ async function main() {
157
+ try {
158
+ await seed();
159
+ console.log("Seeding completed");
160
+ } catch (error) {
161
+ console.error("Error during seeding:", error);
162
+ process.exit(1);
163
+ }
164
+ }
165
+
166
+ main();
167
+ ```
168
+
169
+ ---
170
+
171
+ Then run this file.
172
+
173
+ ```sh
174
+ $ bun run seed.ts
175
+ Seeding completed
176
+ ```
177
+
178
+ ---
179
+
180
+ We now have a database with a schema and sample data. We can use Drizzle to query it. Replace the contents of `index.ts` with the following.
181
+
182
+ ```ts#index.ts
183
+ import * as schema from "./schema";
184
+ import { db } from "./db";
185
+
186
+ const result = await db.select().from(schema.authors);
187
+ console.log(result);
188
+ ```
189
+
190
+ ---
191
+
192
+ Then run the file. You should see the three authors we inserted.
193
+
194
+ ```sh
195
+ $ bun run index.ts
196
+ [
197
+ {
198
+ id: 1,
199
+ name: "J.R.R. Tolkien",
200
+ bio: "The creator of Middle-earth and author of The Lord of the Rings.",
201
+ createdAt: 2024-05-11T10:28:46.029Z,
202
+ }, {
203
+ id: 2,
204
+ name: "George R.R. Martin",
205
+ bio: "The author of the epic fantasy series A Song of Ice and Fire.",
206
+ createdAt: 2024-05-11T10:28:46.029Z,
207
+ }, {
208
+ id: 3,
209
+ name: "J.K. Rowling",
210
+ bio: "The creator of the Harry Potter series.",
211
+ createdAt: 2024-05-11T10:28:46.029Z,
212
+ }
213
+ ]
214
+ ```
215
+
216
+ ---
217
+
218
+ This example used the Neon serverless driver's SQL-over-HTTP functionality. Neon's serverless driver also exposes `Client` and `Pool` constructors to enable sessions, interactive transactions, and node-postgres compatibility. Refer to [Neon's documentation](https://neon.tech/docs/serverless/serverless-driver) for a complete overview.
219
+
220
+ Refer to the [Drizzle website](https://orm.drizzle.team/docs/overview) for more documentation on using the Drizzle ORM.
@@ -0,0 +1,55 @@
1
+ ---
2
+ name: Use Neon's Serverless Postgres with Bun
3
+ ---
4
+
5
+ [Neon](https://neon.tech/) is a fully managed serverless Postgres. Neon separates compute and storage to offer modern developer features such as autoscaling, branching, bottomless storage, and more.
6
+
7
+ ---
8
+
9
+ Get started by creating a project directory, initializing the directory using `bun init`, and adding the [Neon serverless driver](https://github.com/neondatabase/serverless/) as a project dependency.
10
+
11
+ ```sh
12
+ $ mkdir bun-neon-postgres
13
+ $ cd bun-neon-postgres
14
+ $ bun init -y
15
+ $ bun add @neondatabase/serverless
16
+ ```
17
+
18
+ ---
19
+
20
+ Create a `.env.local` file and add your [Neon Postgres connection string](https://neon.tech/docs/connect/connect-from-any-app) to it.
21
+
22
+ ```sh
23
+ DATABASE_URL=postgresql://username:password@ep-adj-noun-guid.us-east-1.aws.neon.tech/neondb?sslmode=require
24
+ ```
25
+
26
+ ---
27
+
28
+ Paste the following code into your project's `index.ts` file.
29
+
30
+ ```ts
31
+ import { neon } from "@neondatabase/serverless";
32
+
33
+ // Bun automatically loads the DATABASE_URL from .env.local
34
+ // Refer to: https://bun.sh/docs/runtime/env for more information
35
+ const sql = neon(process.env.DATABASE_URL);
36
+
37
+ const rows = await sql`SELECT version()`;
38
+
39
+ console.log(rows[0].version);
40
+ ```
41
+
42
+ ---
43
+
44
+ Start the program using `bun ./index.ts`. The Postgres version should be printed to the console.
45
+
46
+ ```sh
47
+ $ bun ./index.ts
48
+ PostgreSQL 16.2 on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
49
+ ```
50
+
51
+ ---
52
+
53
+ This example used the Neon serverless driver's SQL-over-HTTP functionality. Neon's serverless driver also exposes `Client` and `Pool` constructors to enable sessions, interactive transactions, and node-postgres compatibility.
54
+
55
+ Refer to [Neon's documentation](https://neon.tech/docs/serverless/serverless-driver) for a complete overview of the serverless driver.
@@ -0,0 +1,38 @@
1
+ ---
2
+ name: Build an app with Next.js and Bun
3
+ ---
4
+
5
+ Initialize a Next.js app with `create-next-app`. This will scaffold a new Next.js project and automatically install dependencies.
6
+
7
+ ```sh
8
+ $ bun create next-app
9
+ ✔ What is your project named? … my-app
10
+ ✔ Would you like to use TypeScript with this project? … No / Yes
11
+ ✔ Would you like to use ESLint with this project? … No / Yes
12
+ ✔ Would you like to use `src/` directory with this project? … No / Yes
13
+ ✔ Would you like to use experimental `app/` directory with this project? … No / Yes
14
+ ✔ What import alias would you like configured? … @/*
15
+ Creating a new Next.js app in /path/to/my-app.
16
+ ```
17
+
18
+ ---
19
+
20
+ To start the dev server with Bun, run `bun --bun run dev` from the project root.
21
+
22
+ ```sh
23
+ $ cd my-app
24
+ $ bun --bun run dev
25
+ ```
26
+
27
+ ---
28
+
29
+ To run the dev server with Node.js instead, omit `--bun`.
30
+
31
+ ```sh
32
+ $ cd my-app
33
+ $ bun run dev
34
+ ```
35
+
36
+ ---
37
+
38
+ Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. Any changes you make to `(pages/app)/index.tsx` will be hot-reloaded in the browser.
@@ -0,0 +1,56 @@
1
+ ---
2
+ name: Build an app with Nuxt and Bun
3
+ ---
4
+
5
+ Bun supports [Nuxt](https://nuxt.com) out of the box. Initialize a Nuxt app with official `nuxi` CLI.
6
+
7
+ ```sh
8
+ $ bunx nuxi init my-nuxt-app
9
+ ✔ Which package manager would you like to use?
10
+ bun
11
+ ◐ Installing dependencies...
12
+ bun install v1.x (16b4bf34)
13
+ + @nuxt/devtools@0.8.2
14
+ + nuxt@3.7.0
15
+ 785 packages installed [2.67s]
16
+ ✔ Installation completed.
17
+ ✔ Types generated in .nuxt
18
+ ✨ Nuxt project has been created with the v3 template. Next steps:
19
+ › cd my-nuxt-app
20
+ › Start development server with bun run dev
21
+ ```
22
+
23
+ ---
24
+
25
+ To start the dev server, run `bun --bun run dev` from the project root. This will execute the `nuxt dev` command (as defined in the `"dev"` script in `package.json`).
26
+
27
+ {% callout %}
28
+ The `nuxt` CLI uses Node.js by default; passing the `--bun` flag forces the dev server to use the Bun runtime instead.
29
+ {% /callout %}
30
+
31
+ ```
32
+ $ cd my-nuxt-app
33
+ $ bun --bun run dev
34
+ $ nuxt dev
35
+ Nuxi 3.6.5
36
+ Nuxt 3.6.5 with Nitro 2.5.2
37
+ > Local: http://localhost:3000/
38
+ > Network: http://192.168.0.21:3000/
39
+ > Network: http://[fd8a:d31d:481c:4883:1c64:3d90:9f83:d8a2]:3000/
40
+
41
+ ✔ Nuxt DevTools is enabled v0.8.0 (experimental)
42
+ ℹ Vite client warmed up in 547ms
43
+ ✔ Nitro built in 244 ms
44
+ ```
45
+
46
+ ---
47
+
48
+ Once the dev server spins up, open [http://localhost:3000](http://localhost:3000) to see the app. The app will render Nuxt's built-in `NuxtWelcome` template component.
49
+
50
+ To start developing your app, replace `<NuxtWelcome />` in `app.vue` with your own UI.
51
+
52
+ {% image src="https://github.com/oven-sh/bun/assets/3084745/2c683ecc-3298-4bb0-b8c0-cf4cfaea1daa" caption="Demo Nuxt app running on localhost" /%}
53
+
54
+ ---
55
+
56
+ Refer to the [Nuxt website](https://nuxt.com/docs) for complete documentation.
@@ -0,0 +1,57 @@
1
+ ---
2
+ name: Run Bun as a daemon with PM2
3
+ ---
4
+
5
+ [PM2](https://pm2.keymetrics.io/) is a popular process manager that manages and runs your applications as daemons (background processes).
6
+
7
+ It offers features like process monitoring, automatic restarts, and easy scaling. Using a process manager is common when deploying a Bun application on a cloud-hosted virtual private server (VPS), as it:
8
+
9
+ - Keeps your Node.js application running continuously.
10
+ - Ensure high availability and reliability of your application.
11
+ - Monitor and manage multiple processes with ease.
12
+ - Simplify the deployment process.
13
+
14
+ ---
15
+
16
+ You can use PM2 with Bun in two ways: as a CLI option or in a configuration file.
17
+
18
+ ### With `--interpreter`
19
+
20
+ ---
21
+
22
+ To start your application with PM2 and Bun as the interpreter, open your terminal and run the following command:
23
+
24
+ ```bash
25
+ pm2 start --interpreter ~/.bun/bin/bun index.ts
26
+ ```
27
+
28
+ ---
29
+
30
+ ### With a configuration file
31
+
32
+ ---
33
+
34
+ Alternatively, you can create a PM2 configuration file. Create a file named `pm2.config.js` in your project directory and add the following content.
35
+
36
+ ```javascript
37
+ module.exports = {
38
+ name: "app", // Name of your application
39
+ script: "index.ts", // Entry point of your application
40
+ interpreter: "bun", // Bun interpreter
41
+ env: {
42
+ PATH: `${process.env.HOME}/.bun/bin:${process.env.PATH}`, // Add "~/.bun/bin/bun" to PATH
43
+ }
44
+ };
45
+ ```
46
+
47
+ ---
48
+
49
+ After saving the file, you can start your application with PM2
50
+
51
+ ```bash
52
+ pm2 start pm2.config.js
53
+ ```
54
+
55
+ ---
56
+
57
+ That’s it! Your JavaScript/TypeScript web server is now running as a daemon with PM2 using Bun as the interpreter.