@upstash/redis 0.0.0-ci.8ae20814 → 0.0.0-ci.ba39ba1e
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.
- package/README.md +34 -273
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,24 +23,6 @@ See
|
|
|
23
23
|
[the list of APIs](https://docs.upstash.com/features/restapi#rest---redis-api-compatibility)
|
|
24
24
|
supported.
|
|
25
25
|
|
|
26
|
-
## Upgrading to v1.4.0 **(ReferenceError: fetch is not defined)**
|
|
27
|
-
|
|
28
|
-
If you are running on nodejs v17 and earlier, `fetch` will not be natively
|
|
29
|
-
supported. Platforms like Vercel, Netlify, Deno, Fastly etc. provide a polyfill
|
|
30
|
-
for you. But if you are running on bare node, you need to either specify a
|
|
31
|
-
polyfill yourself or change the import path to:
|
|
32
|
-
|
|
33
|
-
```typescript
|
|
34
|
-
import { Redis } from "@upstash/redis/with-fetch";
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
## Upgrading from v0.2.0?
|
|
38
|
-
|
|
39
|
-
Please read the
|
|
40
|
-
[migration guide](https://github.com/upstash/upstash-redis#migrating-to-v1). For
|
|
41
|
-
further explanation we wrote a
|
|
42
|
-
[blog post](https://blog.upstash.com/upstash-redis-sdk-v1).
|
|
43
|
-
|
|
44
26
|
## Quick Start
|
|
45
27
|
|
|
46
28
|
### Install
|
|
@@ -61,134 +43,7 @@ import { Redis } from "https://deno.land/x/upstash_redis/mod.ts";
|
|
|
61
43
|
|
|
62
44
|
Create a new redis database on [upstash](https://console.upstash.com/)
|
|
63
45
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
We support various platforms, such as nodejs, cloudflare and fastly. Platforms
|
|
67
|
-
differ slightly when it comes to environment variables and their `fetch` api.
|
|
68
|
-
Please use the correct import when deploying to special platforms.
|
|
69
|
-
|
|
70
|
-
#### Node.js
|
|
71
|
-
|
|
72
|
-
Examples: Vercel, Netlify, AWS Lambda
|
|
73
|
-
|
|
74
|
-
If you are running on nodejs you can set `UPSTASH_REDIS_REST_URL` and
|
|
75
|
-
`UPSTASH_REDIS_REST_TOKEN` as environment variable and create a redis instance
|
|
76
|
-
like this:
|
|
77
|
-
|
|
78
|
-
```ts
|
|
79
|
-
import { Redis } from "@upstash/redis"
|
|
80
|
-
|
|
81
|
-
const redis = new Redis({
|
|
82
|
-
url: <UPSTASH_REDIS_REST_URL>,
|
|
83
|
-
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
// or load directly from env
|
|
87
|
-
const redis = Redis.fromEnv()
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
If you are running on nodejs v17 and earlier, `fetch` will not be natively
|
|
91
|
-
supported. Platforms like Vercel, Netlify, Deno, Fastly etc. provide a polyfill
|
|
92
|
-
for you. But if you are running on bare node, you need to either specify a
|
|
93
|
-
polyfill yourself or change the import path to:
|
|
94
|
-
|
|
95
|
-
```typescript
|
|
96
|
-
import { Redis } from "@upstash/redis/with-fetch";
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
- [Code example](https://github.com/upstash/upstash-redis/blob/main/examples/nodejs)
|
|
100
|
-
|
|
101
|
-
#### Cloudflare Workers
|
|
102
|
-
|
|
103
|
-
Cloudflare handles environment variables differently than nodejs. Please add
|
|
104
|
-
`UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` using
|
|
105
|
-
`wrangler secret put ...` or in the cloudflare dashboard.
|
|
106
|
-
|
|
107
|
-
Afterwards you can create a redis instance:
|
|
108
|
-
|
|
109
|
-
```ts
|
|
110
|
-
import { Redis } from "@upstash/redis/cloudflare"
|
|
111
|
-
|
|
112
|
-
const redis = new Redis({
|
|
113
|
-
url: <UPSTASH_REDIS_REST_URL>,
|
|
114
|
-
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
// or load directly from global env
|
|
119
|
-
|
|
120
|
-
// service worker
|
|
121
|
-
const redis = Redis.fromEnv()
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
// module worker
|
|
125
|
-
export default {
|
|
126
|
-
async fetch(request: Request, env: Bindings) {
|
|
127
|
-
const redis = Redis.fromEnv(env)
|
|
128
|
-
// ...
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
- [Code example](https://github.com/upstash/upstash-redis/tree/main/examples/cloudflare-workers)
|
|
134
|
-
- [Code example typescript](https://github.com/upstash/upstash-redis/tree/main/examples/cloudflare-workers-with-typescript)
|
|
135
|
-
- [Code example Wrangler 1](https://github.com/upstash/upstash-redis/tree/main/examples/cloudflare-workers-with-wrangler-1)
|
|
136
|
-
- [Documentation](https://docs.upstash.com/redis/tutorials/cloudflare_workers_with_redis)
|
|
137
|
-
|
|
138
|
-
#### Fastly
|
|
139
|
-
|
|
140
|
-
Fastly introduces a concept called
|
|
141
|
-
[backend](https://developer.fastly.com/reference/api/services/backend/). You
|
|
142
|
-
need to configure a backend in your `fastly.toml`. An example can be found
|
|
143
|
-
[here](https://github.com/upstash/upstash-redis/blob/main/examples/fastly/fastly.toml).
|
|
144
|
-
Until the fastly api stabilizes we recommend creating an instance manually:
|
|
145
|
-
|
|
146
|
-
```ts
|
|
147
|
-
import { Redis } from "@upstash/redis/fastly"
|
|
148
|
-
|
|
149
|
-
const redis = new Redis({
|
|
150
|
-
url: <UPSTASH_REDIS_REST_URL>,
|
|
151
|
-
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
152
|
-
backend: <BACKEND_NAME>,
|
|
153
|
-
})
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
- [Code example](https://github.com/upstash/upstash-redis/tree/main/examples/fastly)
|
|
157
|
-
- [Documentation](https://blog.upstash.com/fastly-compute-edge-with-redi)
|
|
158
|
-
|
|
159
|
-
#### Deno
|
|
160
|
-
|
|
161
|
-
Examples: [Deno Deploy](https://deno.com/deploy),
|
|
162
|
-
[Netlify Edge](https://www.netlify.com/products/edge/)
|
|
163
|
-
|
|
164
|
-
```ts
|
|
165
|
-
import { Redis } from "https://deno.land/x/upstash_redis/mod.ts"
|
|
166
|
-
|
|
167
|
-
const redis = new Redis({
|
|
168
|
-
url: <UPSTASH_REDIS_REST_URL>,
|
|
169
|
-
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
// or
|
|
173
|
-
const redis = Redis.fromEnv();
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
### Working with types
|
|
177
|
-
|
|
178
|
-
Most commands allow you to provide a type to make working with typescript
|
|
179
|
-
easier.
|
|
180
|
-
|
|
181
|
-
```ts
|
|
182
|
-
const data = await redis.get<MyCustomType>("key");
|
|
183
|
-
// data is typed as `MyCustomType`
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
## Migrating to v1
|
|
187
|
-
|
|
188
|
-
### Explicit authentication
|
|
189
|
-
|
|
190
|
-
The library is no longer automatically trying to load connection secrets from
|
|
191
|
-
environment variables. You must either supply them yourself:
|
|
46
|
+
## Basic Usage:
|
|
192
47
|
|
|
193
48
|
```ts
|
|
194
49
|
import { Redis } from "@upstash/redis"
|
|
@@ -197,146 +52,52 @@ const redis = new Redis({
|
|
|
197
52
|
url: <UPSTASH_REDIS_REST_URL>,
|
|
198
53
|
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
199
54
|
})
|
|
200
|
-
```
|
|
201
55
|
|
|
202
|
-
|
|
56
|
+
// string
|
|
57
|
+
await redis.set('key', 'value');
|
|
58
|
+
let data = await redis.get('key');
|
|
59
|
+
console.log(data)
|
|
203
60
|
|
|
204
|
-
|
|
205
|
-
// Nodejs
|
|
206
|
-
import { Redis } from "@upstash/redis";
|
|
207
|
-
const redis = Redis.fromEnv();
|
|
208
|
-
```
|
|
61
|
+
await redis.set('key2', 'value2', {ex: 1});
|
|
209
62
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
```
|
|
63
|
+
// sorted set
|
|
64
|
+
await redis.zadd('scores', { score: 1, member: 'team1' })
|
|
65
|
+
data = await redis.zrange('scores', 0, 100 )
|
|
66
|
+
console.log(data)
|
|
215
67
|
|
|
216
|
-
|
|
68
|
+
// list
|
|
69
|
+
await redis.lpush('elements', 'magnesium')
|
|
70
|
+
data = await redis.lrange('elements', 0, 100 )
|
|
71
|
+
console.log(data)
|
|
217
72
|
|
|
218
|
-
|
|
73
|
+
// hash
|
|
74
|
+
await redis.hset('people', {name: 'joe'})
|
|
75
|
+
data = await redis.hget('people', 'name' )
|
|
76
|
+
console.log(data)
|
|
219
77
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
throw new Error(error);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// new
|
|
228
|
-
const data = await redis.set("key", "value"); // error is thrown automatically
|
|
78
|
+
// sets
|
|
79
|
+
await redis.sadd('animals', 'cat')
|
|
80
|
+
data = await redis.spop('animals', 1)
|
|
81
|
+
console.log(data)
|
|
229
82
|
```
|
|
230
83
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
`v1.0.0` introduces redis pipelines. Pipelining commands allows you to send a
|
|
234
|
-
single http request with multiple commands.
|
|
235
|
-
|
|
236
|
-
```ts
|
|
237
|
-
import { Redis } from "@upstash/redis";
|
|
238
|
-
|
|
239
|
-
const redis = new Redis({
|
|
240
|
-
/* auth */
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
const p = redis.pipeline();
|
|
84
|
+
### Upgrading to v1.4.0 **(ReferenceError: fetch is not defined)**
|
|
244
85
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
// or inline:
|
|
251
|
-
p.hset("key2", "field", { hello: "world" }).hvals("key2");
|
|
252
|
-
|
|
253
|
-
// Execute the pipeline once you are done building it:
|
|
254
|
-
// `exec` returns an array where each element represents the response of a command in the pipeline.
|
|
255
|
-
// You can optionally provide a type like this to get a typed response.
|
|
256
|
-
const res = await p.exec<[Type1, Type2, Type3]>();
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
For more information about pipelines using REST see
|
|
260
|
-
[here](https://blog.upstash.com/pipeline).
|
|
261
|
-
|
|
262
|
-
### Advanced
|
|
263
|
-
|
|
264
|
-
A low level `Command` class can be imported from `@upstash/redis` in case you
|
|
265
|
-
need more control about types and or (de)serialization.
|
|
266
|
-
|
|
267
|
-
By default all objects you are storing in redis are serialized using
|
|
268
|
-
`JSON.stringify` and recursively deserialized as well. Here's an example how you
|
|
269
|
-
could customize that behaviour. Keep in mind that you need to provide a `fetch`
|
|
270
|
-
polyfill if you are running on nodejs. We recommend
|
|
271
|
-
[isomorphic-fetch](https://www.npmjs.com/package/isomorphic-fetch).
|
|
272
|
-
|
|
273
|
-
```ts
|
|
274
|
-
import { Command } from "@upstash/redis/commands"
|
|
275
|
-
import { HttpClient } from "@upstash/redis/http"
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* TData represents what the user will enter or receive,
|
|
279
|
-
* TResult is the raw data returned from upstash, which may need to be
|
|
280
|
-
* transformed or parsed.
|
|
281
|
-
*/
|
|
282
|
-
const deserialize: (raw: TResult) => TData = ...
|
|
283
|
-
|
|
284
|
-
class CustomGetCommand<TData, TResult> extends Command<TData | null, TResult | null> {
|
|
285
|
-
constructor(key: string, ) {
|
|
286
|
-
super(["get", key], { deserialize })
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
const client = new HttpClient({
|
|
291
|
-
baseUrl: <UPSTASH_REDIS_REST_URL>,
|
|
292
|
-
headers: {
|
|
293
|
-
authorization: `Bearer ${<UPSTASH_REDIS_REST_TOKEN>}`,
|
|
294
|
-
},
|
|
295
|
-
})
|
|
296
|
-
|
|
297
|
-
const res = new CustomGetCommand("key").exec(client)
|
|
298
|
-
```
|
|
299
|
-
|
|
300
|
-
### Additional information
|
|
301
|
-
|
|
302
|
-
#### `keepalive`
|
|
303
|
-
|
|
304
|
-
`@upstash/redis` is capable of reusing connections where possible to minimize
|
|
305
|
-
latency. Connections can be reused if the client is stored in memory and not
|
|
306
|
-
initialized with every new function invocation. The easiest way to achieve this
|
|
307
|
-
is by creating the client outside of your handler and adding an https agent:
|
|
308
|
-
|
|
309
|
-
```ts
|
|
310
|
-
// Nextjs api route
|
|
311
|
-
import { Redis } from "@upstash/redis";
|
|
312
|
-
import https from "https";
|
|
313
|
-
|
|
314
|
-
const redis = Redis.fromEnv({
|
|
315
|
-
agent: new https.Agent({ keepAlive: true }),
|
|
316
|
-
});
|
|
86
|
+
If you are running on nodejs v17 and earlier, `fetch` will not be natively
|
|
87
|
+
supported. Platforms like Vercel, Netlify, Deno, Fastly etc. provide a polyfill
|
|
88
|
+
for you. But if you are running on bare node, you need to either specify a
|
|
89
|
+
polyfill yourself or change the import path to:
|
|
317
90
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
}
|
|
91
|
+
```typescript
|
|
92
|
+
import { Redis } from "@upstash/redis/with-fetch";
|
|
321
93
|
```
|
|
322
94
|
|
|
323
|
-
|
|
324
|
-
initialized and the previously established connection to upstash is reused.
|
|
325
|
-
|
|
326
|
-
#### Javascript MAX_SAFE_INTEGER
|
|
327
|
-
|
|
328
|
-
Javascript can not handle numbers larger than `2^53 -1` safely and would return
|
|
329
|
-
wrong results when trying to deserialize them. In these cases the default
|
|
330
|
-
deserializer will return them as string instead. This might cause a mismatch
|
|
331
|
-
with your custom types.
|
|
332
|
-
|
|
333
|
-
```ts
|
|
334
|
-
await redis.set("key", "101600000000150081467");
|
|
335
|
-
const res = await redis<number>("get");
|
|
336
|
-
```
|
|
95
|
+
### Upgrading from v0.2.0?
|
|
337
96
|
|
|
338
|
-
|
|
339
|
-
|
|
97
|
+
Please read the
|
|
98
|
+
[migration guide](https://docs.upstash.com/redis/sdks/javascriptsdk/migration).
|
|
99
|
+
For further explanation we wrote a
|
|
100
|
+
[blog post](https://blog.upstash.com/upstash-redis-sdk-v1).
|
|
340
101
|
|
|
341
102
|
## Docs
|
|
342
103
|
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"main": "./script/platforms/nodejs.js",
|
|
4
4
|
"types": "./types/platforms/nodejs.d.ts",
|
|
5
5
|
"name": "@upstash/redis",
|
|
6
|
-
"version": "v0.0.0-ci.
|
|
6
|
+
"version": "v0.0.0-ci.ba39ba1e",
|
|
7
7
|
"description": "An HTTP/REST based Redis client built on top of Upstash REST API.",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|