-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathdt-json.js
425 lines (363 loc) · 13.5 KB
/
dt-json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// EXAMPLE: json_tutorial
// HIDE_START
import assert from 'assert';
import {
createClient
} from 'redis';
const client = await createClient();
await client.connect();
// HIDE_END
// REMOVE_START
await client.flushDb();
// REMOVE_END
// STEP_START set_get
const res1 = await client.json.set("bike", "$", '"Hyperion"');
console.log(res1); // OK
const res2 = await client.json.get("bike", "$");
console.log(res2); // "Hyperion"
const res3 = await client.json.type("bike", "$");
console.log(res3); // [ 'string' ]
// STEP_END
// REMOVE_START
assert.equal(res2, '"Hyperion"');
// REMOVE_END
// STEP_START str
const res4 = await client.json.strLen("bike", "$");
console.log(res4) // [10]
const res5 = await client.json.strAppend("bike", '" (Enduro bikes)"');
console.log(res5) // 27
const res6 = await client.json.get("bike", "$");
console.log(res6) // ['"Hyperion"" (Enduro bikes)"']
// STEP_END
// REMOVE_START
assert.equal(res6, ['"Hyperion"" (Enduro bikes)"']);
// REMOVE_END
// STEP_START num
const res7 = await client.json.set("crashes", "$", 0);
console.log(res7) // OK
const res8 = await client.json.numIncrBy("crashes", "$", 1);
console.log(res8) // [1]
const res9 = await client.json.numIncrBy("crashes", "$", 1.5);
console.log(res9) // [2.5]
const res10 = await client.json.numIncrBy("crashes", "$", -0.75);
console.log(res10) // [1.75]
// STEP_END
// REMOVE_START
assert.deepEqual(res10, [1.75])
// REMOVE_END
// STEP_START arr
const res11 = await client.json.set("newbike", "$", ["Deimos", {"crashes": 0 }, null]);
console.log(res11); // OK
const res12 = await client.json.get("newbike", "$");
console.log(res12); // [ 'Deimos', { crashes: 0 }, null ]
const res13 = await client.json.get("newbike", "$[1].crashes");
console.log(res13); // [ 'Deimos', { crashes: 0 }, null ]
const res14 = await client.json.del("newbike", "$.[-1]");
console.log(res14); // [1]
const res15 = await client.json.get("newbike", "$");
console.log(res15); // [ 'Deimos', { crashes: 0 } ]
// STEP_END
// REMOVE_START
assert.deepEqual(res15, ["Deimos", {
"crashes": 0
}]);
// REMOVE_END
// STEP_START arr2
const res16 = await client.json.set("riders", "$", []);
console.log(res16); // OK
const res17 = await client.json.arrAppend("riders", "$", "Norem");
console.log(res17); // [1]
const res18 = await client.json.get("riders", "$");
console.log(res18); // [ 'Norem' ]
const res19 = await client.json.arrInsert("riders", "$", 1, "Prickett", "Royse", "Castilla");
console.log(res19); // [4]
const res20 = await client.json.get("riders", "$");
console.log(res20); // [ 'Norem', 'Prickett', 'Royse', 'Castilla' ]
const res21 = await client.json.arrTrim("riders", "$", 1, 1);
console.log(res21); // [1]
const res22 = await client.json.get("riders", "$");
console.log(res22); // [ 'Prickett' ]
const res23 = await client.json.arrPop("riders", "$");
console.log(res23); // [ 'Prickett' ]
const res24 = await client.json.arrPop("riders", "$");
console.log(res24); // [null]
// STEP_END
// REMOVE_START
assert.deepEqual(res24, [null]);
// REMOVE_END
// STEP_START obj
const res25 = await client.json.set(
"bike:1", "$", {
"model": "Deimos",
"brand": "Ergonom",
"price": 4972
}
);
console.log(res25); // OK
const res26 = await client.json.objLen("bike:1", "$");
console.log(res26); // [3]
const res27 = await client.json.objKeys("bike:1", "$");
console.log(res27); // [['model', 'brand', 'price']]
// STEP_END
// REMOVE_START
assert.deepEqual(res27, [
["model", "brand", "price"]
]);
// REMOVE_END
// STEP_START set_bikes
// HIDE_START
const inventoryJSON = {
"inventory": {
"mountain_bikes": [{
"id": "bike:1",
"model": "Phoebe",
"description": "This is a mid-travel trail slayer that is a fantastic daily driver or one bike quiver. The Shimano Claris 8-speed groupset gives plenty of gear range to tackle hills and there\u2019s room for mudguards and a rack too. This is the bike for the rider who wants trail manners with low fuss ownership.",
"price": 1920,
"specs": {
"material": "carbon",
"weight": 13.1
},
"colors": ["black", "silver"],
},
{
"id": "bike:2",
"model": "Quaoar",
"description": "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and teaawait client. All in all it's an impressive package for the price, making it very competitive.",
"price": 2072,
"specs": {
"material": "aluminium",
"weight": 7.9
},
"colors": ["black", "white"],
},
{
"id": "bike:3",
"model": "Weywot",
"description": "This bike gives kids aged six years and older a durable and uberlight mountain bike for their first experience on tracks and easy cruising through forests and fields. A set of powerful Shimano hydraulic disc brakes provide ample stopping ability. If you're after a budget option, this is one of the best bikes you could get.",
"price": 3264,
"specs": {
"material": "alloy",
"weight": 13.8
},
},
],
"commuter_bikes": [{
"id": "bike:4",
"model": "Salacia",
"description": "This bike is a great option for anyone who just wants a bike to get about on With a slick-shifting Claris gears from Shimano\u2019s, this is a bike which doesn\u2019t break the bank and delivers craved performance. It\u2019s for the rider who wants both efficiency and capability.",
"price": 1475,
"specs": {
"material": "aluminium",
"weight": 16.6
},
"colors": ["black", "silver"],
},
{
"id": "bike:5",
"model": "Mimas",
"description": "A real joy to ride, this bike got very high scores in last years Bike of the year report. The carefully crafted 50-34 tooth chainset and 11-32 tooth cassette give an easy-on-the-legs bottom gear for climbing, and the high-quality Vittoria Zaffiro tires give balance and grip.It includes a low-step frame , our memory foam seat, bump-resistant shocks and conveniently placed thumb throttle. Put it all together and you get a bike that helps redefine what can be done for this price.",
"price": 3941,
"specs": {
"material": "alloy",
"weight": 11.6
},
},
],
}
};
// HIDE_END
const res28 = await client.json.set("bikes:inventory", "$", inventoryJSON);
console.log(res28); // OK
// STEP_END
// STEP_START get_bikes
const res29 = await client.json.get("bikes:inventory", {
path: "$.inventory.*"
});
console.log(res29);
/*
[
[
{
id: 'bike:1',
model: 'Phoebe',
description: 'This is a mid-travel trail slayer that is a fantastic daily driver or one bike quiver. The Shimano Claris 8-speed groupset gives plenty of gear range to tackle hills and there’s room for mudguards and a rack too. This is the bike for the rider who wants trail manners with low fuss ownership.',
price: 1920,
specs: [Object],
colors: [Array]
},
{
id: 'bike:2',
model: 'Quaoar',
description: "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and teaawait client. All in all it's an impressive package for the price, making it very competitive.",
price: 2072,
specs: [Object],
colors: [Array]
},
{
id: 'bike:3',
model: 'Weywot',
description: "This bike gives kids aged six years and older a durable and uberlight mountain bike for their first experience on tracks and easy cruising through forests and fields. A set of powerful Shimano hydraulic disc brakes provide ample stopping ability. If you're after a budget option, this is one of the best bikes you could get.",
price: 3264,
specs: [Object]
}
],
[
{
id: 'bike:4',
model: 'Salacia',
description: 'This bike is a great option for anyone who just wants a bike to get about on With a slick-shifting Claris gears from Shimano’s, this is a bike which doesn’t break the bank and delivers craved performance. It’s for the rider who wants both efficiency and capability.',
price: 1475,
specs: [Object],
colors: [Array]
},
{
id: 'bike:5',
model: 'Mimas',
description: 'A real joy to ride, this bike got very high scores in last years Bike of the year report. The carefully crafted 50-34 tooth chainset and 11-32 tooth cassette give an easy-on-the-legs bottom gear for climbing, and the high-quality Vittoria Zaffiro tires give balance and grip.It includes a low-step frame , our memory foam seat, bump-resistant shocks and conveniently placed thumb throttle. Put it all together and you get a bike that helps redefine what can be done for this price.',
price: 3941,
specs: [Object]
}
]
]
*/
// STEP_END
// STEP_START get_mtnbikes
const res30 = await client.json.get("bikes:inventory", {
path: "$.inventory.mountain_bikes[*].model"
});
console.log(res30); // ['Phoebe', 'Quaoar', 'Weywot']
const res31 = await client.json.get("bikes:inventory", {
path: '$.inventory["mountain_bikes"][*].model'
});
console.log(res31); // ['Phoebe', 'Quaoar', 'Weywot']
const res32 = await client.json.get("bikes:inventory", {
path: "$..mountain_bikes[*].model"
});
console.log(res32); // ['Phoebe', 'Quaoar', 'Weywot']
// STEP_END
// REMOVE_START
assert.deepEqual(res30, ["Phoebe", "Quaoar", "Weywot"]);
assert.deepEqual(res31, ["Phoebe", "Quaoar", "Weywot"]);
assert.deepEqual(res32, ["Phoebe", "Quaoar", "Weywot"]);
// REMOVE_END
// STEP_START get_models
const res33 = await client.json.get("bikes:inventory", {
path: "$..model"
});
console.log(res33); // ['Phoebe', 'Quaoar', 'Weywot', 'Salacia', 'Mimas']
// STEP_END
// REMOVE_START
assert.deepEqual(res33, ["Phoebe", "Quaoar", "Weywot", "Salacia", "Mimas"]);
// REMOVE_END
// STEP_START get2mtnbikes
const res34 = await client.json.get("bikes:inventory", {
path: "$..mountain_bikes[0:2].model"
});
console.log(res34); // ['Phoebe', 'Quaoar']
// STEP_END
// REMOVE_START
assert.deepEqual(res34, ["Phoebe", "Quaoar"]);
// REMOVE_END
// STEP_START filter1
const res35 = await client.json.get("bikes:inventory", {
path: "$..mountain_bikes[?(@.price < 3000 && @.specs.weight < 10)]"
});
console.log(res35);
/*
[
{
id: 'bike:2',
model: 'Quaoar',
description: "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and teaawait client. All in all it's an impressive package for the price, making it very competitive.",
price: 2072,
specs: { material: 'aluminium', weight: 7.9 },
colors: [ 'black', 'white' ]
}
]
*/
// STEP_END
// STEP_START filter2
// names of bikes made from an alloy
const res36 = await client.json.get("bikes:inventory", {
path: "$..[?(@.specs.material == 'alloy')].model"
});
console.log(res36); // ['Weywot', 'Mimas']
// STEP_END
// REMOVE_START
assert.deepEqual(res36, ["Weywot", "Mimas"]);
// REMOVE_END
// STEP_START filter3
const res37 = await client.json.get("bikes:inventory", {
path: "$..[?(@.specs.material =~ '(?i)al')].model"
});
console.log(res37); // ['Quaoar', 'Weywot', 'Salacia', 'Mimas']
// STEP_END
// REMOVE_START
assert.deepEqual(res37, ["Quaoar", "Weywot", "Salacia", "Mimas"]);
// REMOVE_END
// STEP_START filter4
const res37a = await client.json.set(
'bikes:inventory',
'$.inventory.mountain_bikes[0].regex_pat',
'(?i)al'
);
const res37b = await client.json.set(
'bikes:inventory',
'$.inventory.mountain_bikes[1].regex_pat',
'(?i)al'
);
const res37c = await client.json.set(
'bikes:inventory',
'$.inventory.mountain_bikes[2].regex_pat',
'(?i)al'
);
const res37d = await client.json.get(
'bikes:inventory',
'$.inventory.mountain_bikes[?(@.specs.material =~ @.regex_pat)].model'
);
console.log(res37d); // ['Quaoar', 'Weywot']
// STEP_END
// STEP_START update_bikes
const res38 = await client.json.get("bikes:inventory", {
path: "$..price"
});
console.log(res38); // [1920, 2072, 3264, 1475, 3941]
const res39 = await client.json.numIncrBy("bikes:inventory", "$..price", -100);
console.log(res39); // [1820, 1972, 3164, 1375, 3841]
const res40 = await client.json.numIncrBy("bikes:inventory", "$..price", 100);
console.log(res40); // [1920, 2072, 3264, 1475, 3941]
// STEP_END
// REMOVE_START
assert.deepEqual(res40.sort(), [1475, 1920, 2072, 3264, 3941]);
// REMOVE_END
// STEP_START update_filters1
const res40a = await client.json.set(
'bikes:inventory',
'$.inventory.*[?(@.price<2000)].price',
1500
);
// Get all prices from the inventory
const res40b = await client.json.get(
'bikes:inventory',
'$..price'
);
console.log(res40b); // [1500, 2072, 3264, 1500, 3941]
// STEP_END
// STEP_START update_filters2
const res41 = await client.json.arrAppend(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].colors", "pink"
);
console.log(res41); // [3, 3]
const res42 = await client.json.get("bikes:inventory", {
path: "$..[*].colors"
});
console.log(res42); // [['black', 'silver', 'pink'], ['black', 'white'], ['black', 'silver', 'pink']]
// STEP_END
// REMOVE_START
assert.deepEqual(res42, [
["black", "silver", "pink"],
["black", "white"],
["black", "silver", "pink"],
]);
await client.quit();
// REMOVE_END