Skip to content

Commit 8c4d5ed

Browse files
authored
Added new examples of select and pairs queries. (#109)
1 parent 3c4fb44 commit 8c4d5ed

File tree

3 files changed

+516
-0
lines changed

3 files changed

+516
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ crud.select('customers', {{'<=', 'age', 35}})
347347
**Note**: tuples are sorted by age because space has index `age`.
348348
Otherwise, tuples are sorted by primary key.
349349

350+
See more examples of select queries [here.](https://github.com/tarantool/crud/docs/select.md)
351+
350352
### Pairs
351353

352354
You can iterate across a distributed space using the `crud.pairs` function.
@@ -370,6 +372,8 @@ for _, object in crud.pairs('customers', {{'<=', 'age', 35}}, {use_tomap = true}
370372
end
371373
```
372374

375+
See more examples of pairs queries [here.](https://github.com/tarantool/crud/docs/pairs.md)
376+
373377
### Truncate
374378

375379
```lua

doc/pairs.md

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
# Pairs examples
2+
3+
With ``crud.pairs``, you can iterate across a distributed space.
4+
The arguments are the same as [``crud.select``](https://github.com/tarantool/crud/docs/select.md), except of the ``use_tomap`` parameter.
5+
Below are examples that may help you.
6+
7+
## Getting space
8+
9+
Let's check ``developers`` space contents to make other examples more clear. Just select first 4 values without conditions.
10+
11+
**Example:**
12+
13+
```lua
14+
tuples = {}
15+
for _, tuple in crud.pairs('developers', nil, { first = 4 }) do
16+
table.insert(tuples, tuple)
17+
end
18+
19+
tuples
20+
---
21+
- - - 1 -- id
22+
- 7331 -- bucket_id
23+
- Alexey -- name
24+
- Adams -- surname
25+
- 20 -- age
26+
- - 2
27+
- 899
28+
- Sergey
29+
- Allred
30+
- 21
31+
- - 3
32+
- 9661
33+
- Pavel
34+
- Adams
35+
- 27
36+
- - 4
37+
- 501
38+
- Mikhail
39+
- Liston
40+
- 51
41+
...
42+
```
43+
44+
## ``use_tomap`` parameter
45+
46+
With ``use_tomap`` flag, you can choose to iterate over objects or over tuples.
47+
If ``use_tomap = true``, you will iterate over objects. This parameter is false by default.
48+
49+
**Example:**
50+
51+
```lua
52+
objects = {}
53+
for _, obj in crud.pairs('developers', nil, { use_tomap = true, first = 3 }) do
54+
table.insert(tuples, tuple)
55+
end
56+
57+
objects
58+
---
59+
- - id: 1
60+
bucket_id: 7331
61+
name: Alexey
62+
surname: Adams
63+
age: 20
64+
- id: 2
65+
bucket_id: 899
66+
name: Sergey
67+
surname: Allred
68+
age: 21
69+
- id: 3
70+
bucket_id: 9661
71+
name: Pavel
72+
surname: Adams
73+
age: 27
74+
...
75+
```
76+
77+
## Pagination
78+
79+
``crud.pairs``, like [``crud.select``](https://github.com/tarantool/crud/doc/select#pagination), supports pagination.
80+
To use it, combine the ``first`` and ``after`` parameters.
81+
82+
**Example:**
83+
84+
```lua
85+
tuples = {}
86+
for _, tuple in crud.pairs('developers', nil, { first = 2 }) do
87+
table.insert(tuples, tuple) -- Got first two tuples
88+
end
89+
90+
tuples
91+
---
92+
- - - 1
93+
- 7331
94+
- Alexey
95+
- Adams
96+
- 20
97+
- - 2
98+
- 899
99+
- Sergey
100+
- Allred
101+
- 21
102+
...
103+
new_tuples = {}
104+
for _, tuple in crud.pairs('developers', nil, { after = tuples[2], first = 2 }) do
105+
table.insert(new_tuples, tuple) -- Got next two tuples
106+
end
107+
108+
new_tuples
109+
---
110+
- - - 3
111+
- 9661
112+
- Pavel
113+
- Adams
114+
- 27
115+
- - 4
116+
- 501
117+
- Mikhail
118+
- Liston
119+
- 51
120+
...
121+
```
122+
123+
Note that ``crud.pairs``, unlike ``crud.select``, **doesn't support reverse pagination.**
124+
125+
## Lua Fun
126+
127+
[``Pairs``](https://github.com/tarantool/crud#pairs) is [Lua Fun](https://github.com/luafun/luafun) compatible. Some examples of working with basic functional functions below.
128+
129+
**Filter example:**
130+
131+
```lua
132+
objects = {}
133+
for _, obj in crud.pairs('developers', {{'>=', 'age', 20}}, { use_tomap = true }):filter(function(x) return x.age % 5 == 0 end) do
134+
table.insert(objects, obj)
135+
end
136+
137+
objects
138+
---
139+
- - id: 1
140+
bucket_id: 7331
141+
name: Alexey
142+
surname: Adams
143+
age: 20
144+
...
145+
```
146+
147+
**Reduce (foldl) example:**
148+
149+
```lua
150+
age_sum = crud.pairs('developers', nil, { use_tomap = true }):reduce(function(acc, x) return acc + x.age end, 0)
151+
age_sum
152+
---
153+
- 166
154+
....
155+
```
156+
157+
**Map example:**
158+
159+
```lua
160+
objects = {}
161+
for _, obj in crud.pairs('developers', nil, { use_tomap = true }):map(function(x) return {id = obj.id, name = obj.name, age = obj.age * 2}) do
162+
table.insert(objects, obj)
163+
end
164+
165+
objects
166+
---
167+
- - id: 1
168+
name: Alexey
169+
age: 40
170+
- id: 2
171+
name: Sergey
172+
age: 42
173+
- id: 3
174+
name: Pavel
175+
age: 54
176+
- id: 4
177+
name: Mikhail
178+
age: 102
179+
- id: 5
180+
name: Dmitry
181+
age: 32
182+
- id: 6
183+
name: Alexey
184+
age: 62
185+
...
186+
```
187+
188+
**Take example**:
189+
190+
```lua
191+
tuples = {}
192+
for _, tuple in crud.pairs('developers', {{'>=', 'age', 25}}):take(2) do
193+
table.insert(tuples, tuple)
194+
end
195+
196+
tuples
197+
---
198+
- - - 3
199+
- 9661
200+
- Pavel
201+
- Adams
202+
- 27
203+
- - 4
204+
- 501
205+
- Mikhail
206+
- Liston
207+
- 51
208+
...
209+
```

0 commit comments

Comments
 (0)