@upstash/redis 1.6.0 → 1.6.1-next.1

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 (2) hide show
  1. package/README.md +3 -316
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -23,324 +23,11 @@ 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
- ## Quick Start
45
-
46
- ### Install
47
-
48
- #### npm
49
-
50
- ```bash
51
- npm install @upstash/redis
52
- ```
53
-
54
- #### Deno
55
-
56
- ```ts
57
- import { Redis } from "https://deno.land/x/upstash_redis/mod.ts";
58
- ```
59
-
60
- ### Create database
61
-
62
- Create a new redis database on [upstash](https://console.upstash.com/)
63
-
64
- ### Environments
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:
191
-
192
- ```ts
193
- import { Redis } from "@upstash/redis"
194
-
195
- const redis = new Redis({
196
- url: <UPSTASH_REDIS_REST_URL>,
197
- token: <UPSTASH_REDIS_REST_TOKEN>,
198
- })
199
- ```
200
-
201
- Or use one of the static constructors to load from environment variables:
202
-
203
- ```ts
204
- // Nodejs
205
- import { Redis } from "@upstash/redis";
206
- const redis = Redis.fromEnv();
207
- ```
208
-
209
- ```ts
210
- // or when deploying to cloudflare workers
211
- import { Redis } from "@upstash/redis/cloudflare";
212
- const redis = Redis.fromEnv();
213
- ```
214
-
215
- ### Error handling
216
-
217
- Errors are now thrown automatically instead of being returned to you.
218
-
219
- ```ts
220
- // old
221
- const { data, error } = await set("key", "value");
222
- if (error) {
223
- throw new Error(error);
224
- }
225
-
226
- // new
227
- const data = await redis.set("key", "value"); // error is thrown automatically
228
- ```
229
-
230
- ## Pipeline
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();
243
-
244
- // Now you can chain multiple commands to create your pipeline:
245
-
246
- p.set("key", 2);
247
- p.incr("key");
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
- });
316
-
317
- export default async function (req, res) {
318
- // use redis here
319
- }
320
- ```
321
-
322
- Whenever your hot lambda receives a new request the client is already
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
- ```
336
-
337
- In this example `res` will still be a string despite the type annotation. Please
338
- keep that in mind and adjust accordingly.
339
-
340
26
  ## Docs
341
27
 
342
- See [the documentation](https://docs.upstash.com/features/javascriptsdk) for
343
- details.
28
+ See
29
+ [the documentation](https://docs.upstash.com/redis/sdks/javascriptsdk/getstarted)
30
+ for details.
344
31
 
345
32
  ## Contributing
346
33
 
package/package.json CHANGED
@@ -3,7 +3,6 @@
3
3
  "main": "./script/platforms/nodejs.js",
4
4
  "types": "./types/platforms/nodejs.d.ts",
5
5
  "name": "@upstash/redis",
6
- "version": "v1.6.0",
7
6
  "description": "An HTTP/REST based Redis client built on top of Upstash REST API.",
8
7
  "repository": {
9
8
  "type": "git",
@@ -97,5 +96,6 @@
97
96
  "require": "./script/platforms/node_with_fetch.js",
98
97
  "types": "./types/platforms/node_with_fetch.d.ts"
99
98
  }
100
- }
99
+ },
100
+ "version": "1.6.1-next.1"
101
101
  }