Skip to content

Commit 3279caf

Browse files
committed
Add Redis.io code examples
- Add set & get example - Use bash script to test doc tests - Add package.json to doctests - Add search-quickstart
1 parent d65a641 commit 3279caf

File tree

6 files changed

+365
-0
lines changed

6 files changed

+365
-0
lines changed

.github/workflows/doctests.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Documentation Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
doctests:
14+
runs-on: ubuntu-latest
15+
services:
16+
redis-stack:
17+
image: redis/redis-stack-server:latest
18+
options: >-
19+
--health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
20+
ports:
21+
- 6379:6379
22+
steps:
23+
- uses: actions/checkout@v3
24+
- uses: actions/setup-node@v2.3.0
25+
with:
26+
node-version: 18
27+
- name: Install Packages
28+
run: npm ci
29+
- name: Build
30+
run: npm run build-all
31+
- run: |
32+
sudo apt update
33+
sudo apt install -y redis-tools
34+
- name: install doctest deps
35+
run: |
36+
npm install
37+
working-directory: doctests
38+
- name: run tests
39+
run: |
40+
sh run_examples.sh
41+
working-directory: doctests

doctests/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Command examples for redis.io
2+
3+
## Setup
4+
5+
To set up the examples folder so that you can run an example / develop one of your own:
6+
7+
```
8+
$ git clone https://github.com/redis/node-redis.git
9+
$ cd node-redis
10+
$ npm install -ws && npm run build-all
11+
$ cd doctests
12+
$ npm install
13+
```
14+
15+
## How to add examples
16+
17+
Create regular node file in the current folder with meaningful name. It makes sense prefix example files with
18+
command category (e.g. string, set, list, hash, etc) to make navigation in the folder easier.
19+
20+
### Special markup
21+
22+
See https://github.com/redis-stack/redis-stack-website#readme for more details.
23+
24+
## How to test the examples
25+
26+
Just include necessary assertions in the example file and run
27+
```bash
28+
sh doctests/run_examples.sh
29+
```
30+
to test all examples in the current folder.
31+
32+
See `tests.js` for more details.

doctests/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "node-redis-doctests",
3+
"version": "1.0.0",
4+
"description": "Code examples for redis.io",
5+
"main": "index.js",
6+
"private": true,
7+
"type": "module",
8+
"dependencies": {
9+
"redis": "../"
10+
}
11+
}
12+

doctests/run_examples.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
3+
4+
basepath=`readlink -f $1`
5+
if [ $? -ne 0 ]; then
6+
basepath=`readlink -f $(dirname $0)`
7+
fi
8+
echo "No path specified, using ${basepath}"
9+
10+
set -e
11+
cd ${basepath}
12+
for i in `ls ${basepath}/*.js`; do
13+
redis-cli flushdb
14+
node $i
15+
done

doctests/search-quickstart.js

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
// EXAMPLE: search_quickstart
2+
// REMOVE_START
3+
import assert from "assert";
4+
// REMOVE_END
5+
// HIDE_START
6+
import {AggregateGroupByReducers, AggregateSteps, createClient, SchemaFieldTypes} from 'redis';
7+
// HIDE_END
8+
// STEP_START connect
9+
const client = createClient();
10+
client.on('error', err => console.log('Redis Client Error', err));
11+
12+
await client.connect();
13+
// STEP_END
14+
15+
// STEP_START data_sample
16+
let bicycle1 = {
17+
"brand": "Diaz Ltd",
18+
"model": "Dealer Sl",
19+
"price": 7315.58,
20+
"description": "The Diaz Ltd Dealer Sl is a reliable choice" +
21+
" for urban cycling. The Diaz Ltd Dealer Sl " +
22+
"is a comfortable choice for urban cycling.",
23+
"condition": "used",
24+
}
25+
// STEP_END
26+
let bicycles = [
27+
bicycle1,
28+
{
29+
"brand": "Bridges Group",
30+
"model": "Project Pro",
31+
"price": 3610.82,
32+
"description": "This mountain bike is perfect for mountain biking. The Bridges Group Project Pro is a responsive choice for mountain biking.",
33+
"condition": "used",
34+
},
35+
{
36+
"brand": "Vega, Cole and Miller",
37+
"model": "Group Advanced",
38+
"price": 8961.42,
39+
"description": "The Vega, Cole and Miller Group Advanced provides a excellent ride. With its fast carbon frame and 24 gears, this bicycle is perfect for any terrain.",
40+
"condition": "used",
41+
},
42+
{
43+
"brand": "Powell-Montgomery",
44+
"model": "Angle Race",
45+
"price": 4050.27,
46+
"description": "The Powell-Montgomery Angle Race is a smooth choice for road cycling. The Powell-Montgomery Angle Race provides a durable ride.",
47+
"condition": "used",
48+
},
49+
{
50+
"brand": "Gill-Lewis",
51+
"model": "Action Evo",
52+
"price": 283.68,
53+
"description": "The Gill-Lewis Action Evo provides a smooth ride. The Gill-Lewis Action Evo provides a excellent ride.",
54+
"condition": "used",
55+
},
56+
{
57+
"brand": "Rodriguez-Guerrero",
58+
"model": "Drama Comp",
59+
"price": 4462.55,
60+
"description": "This kids bike is perfect for young riders. With its excellent aluminum frame and 12 gears, this bicycle is perfect for any terrain.",
61+
"condition": "new",
62+
},
63+
{
64+
"brand": "Moore PLC",
65+
"model": "Award Race",
66+
"price": 3790.76,
67+
"description": "This olive folding bike features a carbon frame and 27.5 inch wheels. This folding bike is perfect for compact storage and transportation.",
68+
"condition": "new",
69+
},
70+
{
71+
"brand": "Hall, Haley and Hayes",
72+
"model": "Weekend Plus",
73+
"price": 2008.4,
74+
"description": "The Hall, Haley and Hayes Weekend Plus provides a comfortable ride. This blue kids bike features a steel frame and 29.0 inch wheels.",
75+
"condition": "new",
76+
},
77+
{
78+
"brand": "Peck-Carson",
79+
"model": "Sun Hybrid",
80+
"price": 9874.95,
81+
"description": "With its comfortable aluminum frame and 25 gears, this bicycle is perfect for any terrain. The Peck-Carson Sun Hybrid provides a comfortable ride.",
82+
"condition": "new",
83+
},
84+
{
85+
"brand": "Fowler Ltd",
86+
"model": "Weekend Trail",
87+
"price": 3833.71,
88+
"description": "The Fowler Ltd Letter Trail is a comfortable choice for transporting cargo. This cargo bike is perfect for transporting cargo.",
89+
"condition": "refurbished",
90+
},
91+
]
92+
// STEP_START define_index
93+
let schema = {
94+
'$.brand': {
95+
type: SchemaFieldTypes.TEXT,
96+
sortable: true,
97+
AS: 'brand'
98+
},
99+
'$.model': {
100+
type: SchemaFieldTypes.TEXT,
101+
AS: 'model'
102+
},
103+
'$.description': {
104+
type: SchemaFieldTypes.TEXT,
105+
AS: 'description'
106+
},
107+
'$.price': {
108+
type: SchemaFieldTypes.NUMERIC,
109+
AS: 'price'
110+
},
111+
'$.condition': {
112+
type: SchemaFieldTypes.TAG,
113+
AS: 'condition'
114+
},
115+
}
116+
// STEP_END
117+
118+
// STEP_START create_index
119+
try {
120+
await client.ft.create('idx:bicycle', schema, {
121+
ON: 'JSON',
122+
PREFIX: 'bicycle:'
123+
});
124+
} catch (e) {
125+
if (e.message === 'Index already exists') {
126+
console.log('Index exists already, skipped creation.');
127+
} else {
128+
// Something went wrong, perhaps RediSearch isn't installed...
129+
console.error(e);
130+
process.exit(1);
131+
}
132+
}
133+
// STEP_END
134+
135+
// STEP_START add_documents
136+
for (let i = 0; i < bicycles.length; i++) {
137+
await client.json.set(`bicycle:${i}`, '$', bicycles[i]);
138+
}
139+
// STEP_END
140+
141+
// STEP_START query_single_term_and_num_range
142+
let result = await client.ft.search(
143+
'idx:bicycle',
144+
'folding @price:[1000 4000]'
145+
);
146+
147+
console.log(JSON.stringify(result, null, 2));
148+
/*
149+
{
150+
"total": 1,
151+
"documents": [
152+
{
153+
"id": "bicycle:6",
154+
"value": {
155+
"brand": "Moore PLC",
156+
"model": "Award Race",
157+
"price": 3790.76,
158+
"description": "This olive folding bike features a carbon frame and 27.5 inch wheels. This folding bike is perfect for compact storage and transportation.",
159+
"condition": "new"
160+
}
161+
}
162+
]
163+
}
164+
*/
165+
// STEP_END
166+
// REMOVE_START
167+
assert.equal(result.documents[0].id, "bicycle:6");
168+
// REMOVE_END
169+
170+
// STEP_START query_single_term_limit_fields
171+
result = await client.ft.search(
172+
'idx:bicycle',
173+
'cargo',
174+
{
175+
RETURN: ['$.price']
176+
}
177+
);
178+
179+
console.log(JSON.stringify(result, null, 2));
180+
/*
181+
{
182+
"total": 1,
183+
"documents": [
184+
{
185+
"id": "bicycle:9",
186+
"value": {
187+
"$.price": "3833.71"
188+
}
189+
}
190+
]
191+
}
192+
*/
193+
// STEP_END
194+
// REMOVE_START
195+
assert.equal(result.documents[0].id, "bicycle:9");
196+
// REMOVE_END
197+
198+
// STEP_START simple_aggregation
199+
result = await client.ft.aggregate('idx:bicycle', '*', {
200+
STEPS: [
201+
{
202+
type: AggregateSteps.GROUPBY,
203+
properties: ['@condition'],
204+
REDUCE: [
205+
{
206+
type: AggregateGroupByReducers.COUNT,
207+
AS: 'count'
208+
}
209+
]
210+
}
211+
]
212+
})
213+
214+
console.log(JSON.stringify(result, null, 2));
215+
/*
216+
{
217+
"total": 3,
218+
"results": [
219+
{
220+
"condition": "refurbished",
221+
"count": "1"
222+
},
223+
{
224+
"condition": "used",
225+
"count": "5"
226+
},
227+
{
228+
"condition": "new",
229+
"count": "4"
230+
}
231+
]
232+
}
233+
*/
234+
// STEP_END
235+
// REMOVE_START
236+
assert.equal(result.total, 3);
237+
// REMOVE_END
238+
239+
await client.quit();

doctests/string-set-get-example.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// EXAMPLE: set_and_get
2+
// REMOVE_START
3+
import assert from "assert";
4+
// REMOVE_END
5+
6+
// HIDE_START
7+
import { createClient } from 'redis';
8+
9+
const client = createClient();
10+
11+
client.on('error', err => console.log('Redis Client Error', err));
12+
13+
await client.connect();
14+
15+
// HIDE_END
16+
await client.set('bike:1', 'Process 134');
17+
const value = await client.get('bike:1');
18+
console.log(value);
19+
// returns 'Process 134'
20+
//REMOVE_START
21+
assert.equal(value, 'Process 134');
22+
//REMOVE_END
23+
24+
// HIDE_START
25+
await client.quit();
26+
// HIDE_END

0 commit comments

Comments
 (0)