|
| 1 | +# Node Redis: Examples |
| 2 | + |
| 3 | +This folder contains example scripts showing how to use Node Redis in different scenarios. |
| 4 | + |
| 5 | +|File Name |Description | |
| 6 | +|------------------------|------------------------------------| |
| 7 | +|`connect-as-acl-user.js`|Connect to Redis 6 using an ACL user| |
| 8 | + |
| 9 | +## Contributing |
| 10 | + |
| 11 | +We'd love to see more examples here. If you have an idea that you'd like to see included here, submit a Pull Request and we'll be sure to review it! Don't forget to check out our [contributing guide](../CONTRIBUTING.md). |
| 12 | + |
| 13 | +### Coding Guidelines for Examples |
| 14 | + |
| 15 | +When adding a new example, please follow these guidelines: |
| 16 | + |
| 17 | +* Add your code in a single JavaScript file per example, directly in the `examples` folder |
| 18 | +* Do not introduce other dependencies in your example |
| 19 | +* Give your `.js` file a meaningful name using `-` separators e.g. `adding-to-a-stream.js` |
| 20 | +* Indent your code using 2 spaces |
| 21 | +* Use the single line `//` comment style and comment your code |
| 22 | +* Add a comment at the top of your `.js` file describing what your example does |
| 23 | +* Add a comment at the top of your `.js` file describing any Redis commands that need to be run to set up data for your example (try and keep this minimal) |
| 24 | +* Use semicolons |
| 25 | +* Use `async` and `await` |
| 26 | +* Use single quotes, `'hello'` not `"hello"` |
| 27 | +* Place your example code in a single `async` function where possible, named according to the file name e.g. `add-to-stream.js` would contain `const addtoStream = async () => { ... };`, and call this function at the end of the file e.g. `addToStream();` |
| 28 | +* Unless your example requires a connection string, assume Redis is on the default localhost port 6379 with no password |
| 29 | +* Use meaningful example data, let's not use `foo`, `bar`, `baz` etc! |
| 30 | +* Leave on empty line at the end of your `.js` file |
| 31 | +* Update this `README.md` file to add your example to the table |
| 32 | + |
| 33 | +Use [connect-as-acl-user.js](connect-as-acl-user.js) as a guide to develop a well formatted example script. |
| 34 | + |
| 35 | +### Example Template |
| 36 | + |
| 37 | +Here's a starter template for adding a new example, imagine this is stored in `do-something.js`: |
| 38 | + |
| 39 | +```javascript |
| 40 | +// This comment should describe what the example does |
| 41 | +// and can extend to multiple lines. |
| 42 | + |
| 43 | +// Set up the data in redis-cli using these commands: |
| 44 | +// <add your command(s) here, one per line> |
| 45 | + |
| 46 | +import { createClient } from 'redis'; |
| 47 | + |
| 48 | +const doSomething = async () => { |
| 49 | + const client = createClient(); |
| 50 | + |
| 51 | + await client.connect(); |
| 52 | + |
| 53 | + // Add your example code here... |
| 54 | + |
| 55 | + await client.quit(); |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +doSomething(); |
| 60 | +``` |
0 commit comments