@upstash/redis 1.6.0 → 1.7.0
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 +36 -274
- package/esm/pkg/commands/zrange.js +6 -0
- package/package.json +1 -1
- package/script/pkg/commands/zrange.js +6 -0
- package/types/pkg/commands/zrange.d.ts +7 -0
package/README.md
CHANGED
|
@@ -11,9 +11,9 @@ It is the only connectionless (HTTP based) Redis client and designed for:
|
|
|
11
11
|
|
|
12
12
|
- Serverless functions (AWS Lambda ...)
|
|
13
13
|
- Cloudflare Workers (see
|
|
14
|
-
[the example](https://github.com/upstash/upstash-redis/tree/
|
|
14
|
+
[the example](https://github.com/upstash/upstash-redis/tree/main/examples/cloudflare-workers))
|
|
15
15
|
- Fastly Compute@Edge (see
|
|
16
|
-
[the example](https://github.com/upstash/upstash-redis/tree/
|
|
16
|
+
[the example](https://github.com/upstash/upstash-redis/tree/main/examples/fastly))
|
|
17
17
|
- Next.js, Jamstack ...
|
|
18
18
|
- Client side web/mobile applications
|
|
19
19
|
- WebAssembly
|
|
@@ -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,133 +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 service worker](https://github.com/upstash/upstash-redis/tree/main/examples/cloudflare-workers)
|
|
134
|
-
- [Code example module worker](https://github.com/upstash/upstash-redis/tree/main/examples/cloudflare-workers-modules)
|
|
135
|
-
- [Documentation](https://docs.upstash.com/redis/tutorials/cloudflare_workers_with_redis)
|
|
136
|
-
|
|
137
|
-
#### Fastly
|
|
138
|
-
|
|
139
|
-
Fastly introduces a concept called
|
|
140
|
-
[backend](https://developer.fastly.com/reference/api/services/backend/). You
|
|
141
|
-
need to configure a backend in your `fastly.toml`. An example can be found
|
|
142
|
-
[here](https://github.com/upstash/upstash-redis/blob/main/examples/fastly/fastly.toml).
|
|
143
|
-
Until the fastly api stabilizes we recommend creating an instance manually:
|
|
144
|
-
|
|
145
|
-
```ts
|
|
146
|
-
import { Redis } from "@upstash/redis/fastly"
|
|
147
|
-
|
|
148
|
-
const redis = new Redis({
|
|
149
|
-
url: <UPSTASH_REDIS_REST_URL>,
|
|
150
|
-
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
151
|
-
backend: <BACKEND_NAME>,
|
|
152
|
-
})
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
- [Code example](https://github.com/upstash/upstash-redis/tree/main/examples/fastly)
|
|
156
|
-
- [Documentation](https://blog.upstash.com/fastly-compute-edge-with-redi)
|
|
157
|
-
|
|
158
|
-
#### Deno
|
|
159
|
-
|
|
160
|
-
Examples: [Deno Deploy](https://deno.com/deploy),
|
|
161
|
-
[Netlify Edge](https://www.netlify.com/products/edge/)
|
|
162
|
-
|
|
163
|
-
```ts
|
|
164
|
-
import { Redis } from "https://deno.land/x/upstash_redis/mod.ts"
|
|
165
|
-
|
|
166
|
-
const redis = new Redis({
|
|
167
|
-
url: <UPSTASH_REDIS_REST_URL>,
|
|
168
|
-
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
169
|
-
})
|
|
170
|
-
|
|
171
|
-
// or
|
|
172
|
-
const redis = Redis.fromEnv();
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
### Working with types
|
|
176
|
-
|
|
177
|
-
Most commands allow you to provide a type to make working with typescript
|
|
178
|
-
easier.
|
|
179
|
-
|
|
180
|
-
```ts
|
|
181
|
-
const data = await redis.get<MyCustomType>("key");
|
|
182
|
-
// data is typed as `MyCustomType`
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
## Migrating to v1
|
|
186
|
-
|
|
187
|
-
### Explicit authentication
|
|
188
|
-
|
|
189
|
-
The library is no longer automatically trying to load connection secrets from
|
|
190
|
-
environment variables. You must either supply them yourself:
|
|
46
|
+
## Basic Usage:
|
|
191
47
|
|
|
192
48
|
```ts
|
|
193
49
|
import { Redis } from "@upstash/redis"
|
|
@@ -196,146 +52,52 @@ const redis = new Redis({
|
|
|
196
52
|
url: <UPSTASH_REDIS_REST_URL>,
|
|
197
53
|
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
198
54
|
})
|
|
199
|
-
```
|
|
200
55
|
|
|
201
|
-
|
|
56
|
+
// string
|
|
57
|
+
await redis.set('key', 'value');
|
|
58
|
+
let data = await redis.get('key');
|
|
59
|
+
console.log(data)
|
|
202
60
|
|
|
203
|
-
|
|
204
|
-
// Nodejs
|
|
205
|
-
import { Redis } from "@upstash/redis";
|
|
206
|
-
const redis = Redis.fromEnv();
|
|
207
|
-
```
|
|
61
|
+
await redis.set('key2', 'value2', {ex: 1});
|
|
208
62
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
```
|
|
63
|
+
// sorted set
|
|
64
|
+
await redis.zadd('scores', { score: 1, member: 'team1' })
|
|
65
|
+
data = await redis.zrange('scores', 0, 100 )
|
|
66
|
+
console.log(data)
|
|
214
67
|
|
|
215
|
-
|
|
68
|
+
// list
|
|
69
|
+
await redis.lpush('elements', 'magnesium')
|
|
70
|
+
data = await redis.lrange('elements', 0, 100 )
|
|
71
|
+
console.log(data)
|
|
216
72
|
|
|
217
|
-
|
|
73
|
+
// hash
|
|
74
|
+
await redis.hset('people', {name: 'joe'})
|
|
75
|
+
data = await redis.hget('people', 'name' )
|
|
76
|
+
console.log(data)
|
|
218
77
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
throw new Error(error);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
// new
|
|
227
|
-
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)
|
|
228
82
|
```
|
|
229
83
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
`v1.0.0` introduces redis pipelines. Pipelining commands allows you to send a
|
|
233
|
-
single http request with multiple commands.
|
|
234
|
-
|
|
235
|
-
```ts
|
|
236
|
-
import { Redis } from "@upstash/redis";
|
|
237
|
-
|
|
238
|
-
const redis = new Redis({
|
|
239
|
-
/* auth */
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
const p = redis.pipeline();
|
|
84
|
+
### Upgrading to v1.4.0 **(ReferenceError: fetch is not defined)**
|
|
243
85
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
// or inline:
|
|
250
|
-
p.hset("key2", "field", { hello: "world" }).hvals("key2");
|
|
251
|
-
|
|
252
|
-
// Execute the pipeline once you are done building it:
|
|
253
|
-
// `exec` returns an array where each element represents the response of a command in the pipeline.
|
|
254
|
-
// You can optionally provide a type like this to get a typed response.
|
|
255
|
-
const res = await p.exec<[Type1, Type2, Type3]>();
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
For more information about pipelines using REST see
|
|
259
|
-
[here](https://blog.upstash.com/pipeline).
|
|
260
|
-
|
|
261
|
-
### Advanced
|
|
262
|
-
|
|
263
|
-
A low level `Command` class can be imported from `@upstash/redis` in case you
|
|
264
|
-
need more control about types and or (de)serialization.
|
|
265
|
-
|
|
266
|
-
By default all objects you are storing in redis are serialized using
|
|
267
|
-
`JSON.stringify` and recursively deserialized as well. Here's an example how you
|
|
268
|
-
could customize that behaviour. Keep in mind that you need to provide a `fetch`
|
|
269
|
-
polyfill if you are running on nodejs. We recommend
|
|
270
|
-
[isomorphic-fetch](https://www.npmjs.com/package/isomorphic-fetch).
|
|
271
|
-
|
|
272
|
-
```ts
|
|
273
|
-
import { Command } from "@upstash/redis/commands"
|
|
274
|
-
import { HttpClient } from "@upstash/redis/http"
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* TData represents what the user will enter or receive,
|
|
278
|
-
* TResult is the raw data returned from upstash, which may need to be
|
|
279
|
-
* transformed or parsed.
|
|
280
|
-
*/
|
|
281
|
-
const deserialize: (raw: TResult) => TData = ...
|
|
282
|
-
|
|
283
|
-
class CustomGetCommand<TData, TResult> extends Command<TData | null, TResult | null> {
|
|
284
|
-
constructor(key: string, ) {
|
|
285
|
-
super(["get", key], { deserialize })
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const client = new HttpClient({
|
|
290
|
-
baseUrl: <UPSTASH_REDIS_REST_URL>,
|
|
291
|
-
headers: {
|
|
292
|
-
authorization: `Bearer ${<UPSTASH_REDIS_REST_TOKEN>}`,
|
|
293
|
-
},
|
|
294
|
-
})
|
|
295
|
-
|
|
296
|
-
const res = new CustomGetCommand("key").exec(client)
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
### Additional information
|
|
300
|
-
|
|
301
|
-
#### `keepalive`
|
|
302
|
-
|
|
303
|
-
`@upstash/redis` is capable of reusing connections where possible to minimize
|
|
304
|
-
latency. Connections can be reused if the client is stored in memory and not
|
|
305
|
-
initialized with every new function invocation. The easiest way to achieve this
|
|
306
|
-
is by creating the client outside of your handler and adding an https agent:
|
|
307
|
-
|
|
308
|
-
```ts
|
|
309
|
-
// Nextjs api route
|
|
310
|
-
import { Redis } from "@upstash/redis";
|
|
311
|
-
import https from "https";
|
|
312
|
-
|
|
313
|
-
const redis = Redis.fromEnv({
|
|
314
|
-
agent: new https.Agent({ keepAlive: true }),
|
|
315
|
-
});
|
|
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:
|
|
316
90
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
}
|
|
91
|
+
```typescript
|
|
92
|
+
import { Redis } from "@upstash/redis/with-fetch";
|
|
320
93
|
```
|
|
321
94
|
|
|
322
|
-
|
|
323
|
-
initialized and the previously established connection to upstash is reused.
|
|
324
|
-
|
|
325
|
-
#### Javascript MAX_SAFE_INTEGER
|
|
326
|
-
|
|
327
|
-
Javascript can not handle numbers larger than `2^53 -1` safely and would return
|
|
328
|
-
wrong results when trying to deserialize them. In these cases the default
|
|
329
|
-
deserializer will return them as string instead. This might cause a mismatch
|
|
330
|
-
with your custom types.
|
|
331
|
-
|
|
332
|
-
```ts
|
|
333
|
-
await redis.set("key", "101600000000150081467");
|
|
334
|
-
const res = await redis<number>("get");
|
|
335
|
-
```
|
|
95
|
+
### Upgrading from v0.2.0?
|
|
336
96
|
|
|
337
|
-
|
|
338
|
-
|
|
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).
|
|
339
101
|
|
|
340
102
|
## Docs
|
|
341
103
|
|
|
@@ -12,6 +12,12 @@ export class ZRangeCommand extends Command {
|
|
|
12
12
|
if (opts?.byLex) {
|
|
13
13
|
command.push("bylex");
|
|
14
14
|
}
|
|
15
|
+
if (opts?.rev) {
|
|
16
|
+
command.push("rev");
|
|
17
|
+
}
|
|
18
|
+
if (typeof opts?.count !== "undefined" && typeof opts?.offset !== "undefined") {
|
|
19
|
+
command.push("limit", opts.offset, opts.count);
|
|
20
|
+
}
|
|
15
21
|
if (opts?.withScores) {
|
|
16
22
|
command.push("withscores");
|
|
17
23
|
}
|
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": "v1.
|
|
6
|
+
"version": "v1.7.0",
|
|
7
7
|
"description": "An HTTP/REST based Redis client built on top of Upstash REST API.",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
@@ -15,6 +15,12 @@ class ZRangeCommand extends command_js_1.Command {
|
|
|
15
15
|
if (opts?.byLex) {
|
|
16
16
|
command.push("bylex");
|
|
17
17
|
}
|
|
18
|
+
if (opts?.rev) {
|
|
19
|
+
command.push("rev");
|
|
20
|
+
}
|
|
21
|
+
if (typeof opts?.count !== "undefined" && typeof opts?.offset !== "undefined") {
|
|
22
|
+
command.push("limit", opts.offset, opts.count);
|
|
23
|
+
}
|
|
18
24
|
if (opts?.withScores) {
|
|
19
25
|
command.push("withscores");
|
|
20
26
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Command, CommandOptions } from "./command.js";
|
|
2
2
|
export declare type ZRangeCommandOptions = {
|
|
3
3
|
withScores?: boolean;
|
|
4
|
+
rev?: boolean;
|
|
4
5
|
} & ({
|
|
5
6
|
byScore: true;
|
|
6
7
|
byLex?: never;
|
|
@@ -10,6 +11,12 @@ export declare type ZRangeCommandOptions = {
|
|
|
10
11
|
} | {
|
|
11
12
|
byScore?: never;
|
|
12
13
|
byLex?: never;
|
|
14
|
+
}) & ({
|
|
15
|
+
offset: number;
|
|
16
|
+
count: number;
|
|
17
|
+
} | {
|
|
18
|
+
offset?: never;
|
|
19
|
+
count?: never;
|
|
13
20
|
});
|
|
14
21
|
/**
|
|
15
22
|
* @see https://redis.io/commands/zrange
|