mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
Add Upstash guide with Bun Redis (#24493)
This commit is contained in:
@@ -375,7 +375,8 @@
|
||||
"/guides/ecosystem/stric",
|
||||
"/guides/ecosystem/sveltekit",
|
||||
"/guides/ecosystem/systemd",
|
||||
"/guides/ecosystem/vite"
|
||||
"/guides/ecosystem/vite",
|
||||
"/guides/ecosystem/upstash"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
87
docs/guides/ecosystem/upstash.mdx
Normal file
87
docs/guides/ecosystem/upstash.mdx
Normal file
@@ -0,0 +1,87 @@
|
||||
---
|
||||
title: Bun Redis with Upstash
|
||||
sidebarTitle: Upstash with Bun
|
||||
mode: center
|
||||
---
|
||||
|
||||
[Upstash](https://upstash.com/) is a fully managed Redis database as a service. Upstash works with the Redis® API, which means you can use Bun's native Redis client to connect to your Upstash database.
|
||||
|
||||
<Note>TLS is enabled by default for all Upstash Redis databases.</Note>
|
||||
|
||||
---
|
||||
|
||||
<Steps>
|
||||
<Step title="Create a new project">
|
||||
Create a new project by running `bun init`:
|
||||
|
||||
```sh terminal icon="terminal"
|
||||
bun init bun-upstash-redis
|
||||
cd bun-upstash-redis
|
||||
```
|
||||
</Step>
|
||||
<Step title="Create an Upstash Redis database">
|
||||
Go to the [Upstash dashboard](https://console.upstash.com/) and create a new Redis database. After completing the [getting started guide](https://upstash.com/docs/redis/overall/getstarted), you'll see your database page with connection information.
|
||||
|
||||
The database page displays two connection methods; HTTP and TLS. For Bun's Redis client, you need the **TLS** connection details. This URL starts with `rediss://`.
|
||||
|
||||
<Frame>
|
||||

|
||||
</Frame>
|
||||
|
||||
</Step>
|
||||
<Step title="Connect using Bun's Redis client">
|
||||
You can connect to Upstash by setting environment variables with Bun's default `redis` client.
|
||||
|
||||
Set the `REDIS_URL` environment variable in your `.env` file using the Redis endpoint (not the REST URL):
|
||||
|
||||
```env .env icon="settings"
|
||||
REDIS_URL=rediss://********@********.upstash.io:6379
|
||||
```
|
||||
|
||||
Bun's Redis client reads connection information from `REDIS_URL` by default:
|
||||
|
||||
```ts index.ts icon="/icons/typescript.svg"
|
||||
import { redis } from "bun";
|
||||
|
||||
// Reads from process.env.REDIS_URL automatically
|
||||
await redis.set("counter", "0"); // [!code ++]
|
||||
```
|
||||
|
||||
Alternatively, you can create a custom client using `RedisClient`:
|
||||
|
||||
```ts index.ts icon="/icons/typescript.svg"
|
||||
import { RedisClient } from "bun";
|
||||
|
||||
const redis = new RedisClient(process.env.REDIS_URL); // [!code ++]
|
||||
```
|
||||
|
||||
</Step>
|
||||
<Step title="Use the Redis client">
|
||||
You can now use the Redis client to interact with your Upstash Redis database:
|
||||
|
||||
```ts index.ts icon="/icons/typescript.svg"
|
||||
import { redis } from "bun";
|
||||
|
||||
// Get a value
|
||||
let counter = await redis.get("counter");
|
||||
|
||||
// Set a value if it doesn't exist
|
||||
if (!counter) {
|
||||
await redis.set("counter", "0");
|
||||
}
|
||||
|
||||
// Increment the counter
|
||||
await redis.incr("counter");
|
||||
|
||||
// Get the updated value
|
||||
counter = await redis.get("counter");
|
||||
console.log(counter);
|
||||
```
|
||||
```txt
|
||||
1
|
||||
```
|
||||
|
||||
The Redis client automatically handles connections in the background. No need to manually connect or disconnect for basic operations.
|
||||
</Step>
|
||||
|
||||
</Steps>
|
||||
BIN
docs/images/guides/upstash-1.png
Normal file
BIN
docs/images/guides/upstash-1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 195 KiB |
Reference in New Issue
Block a user