Skip to content

Commit e317d79

Browse files
committed
node js cheat sheet (streams, json, search and query)
1 parent 36ec9fe commit e317d79

File tree

3 files changed

+410
-85
lines changed

3 files changed

+410
-85
lines changed

docs/howtos/quick-start/cheat-sheets/json.mdx

Lines changed: 123 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import TabItem from '@theme/TabItem';
2929
</tr>
3030
<tr>
3131
<td>JSON.SET</td>
32-
<td>JSON.SET key path json [NX | XX]</td>
32+
<td>JSON.SET key path value</td>
3333
<td>
3434
<pre>
3535
<code>{`
@@ -42,14 +42,13 @@ JSON.SET employee_profile:1 . '{"name":"Alice"}'
4242
<tr>
4343
<td colspan="4">
4444
<em>Description:</em> Sets JSON value at path in key.
45+
<em>Time Complexity:</em> O(M+N) where M is the original size and N is the
46+
new size
4547
</td>
4648
</tr>
4749
<tr>
4850
<td>JSON.GET</td>
49-
<td>
50-
JSON.GET key [INDENT indentation-string] [NEWLINE line-break-string]
51-
[SPACE space-string] [path]
52-
</td>
51+
<td>JSON.GET key [path [path ...]]</td>
5352
<td>
5453
<pre>
5554
<code>{`
@@ -68,6 +67,9 @@ JSON.GET employee_profile:1
6867
<tr>
6968
<td colspan="4">
7069
<em>Description:</em> Returns the JSON value at path in key.
70+
<em>Time Complexity:</em> O(N) when path is evaluated to a single value where
71+
N is the size of the value, O(N) when path is evaluated to multiple values,
72+
where N is the size of the key
7173
</td>
7274
</tr>
7375
<tr>
@@ -86,6 +88,9 @@ JSON.NUMINCRBY employee_profile:1 .age 5
8688
<tr>
8789
<td colspan="4">
8890
<em>Description:</em> Increments a number inside a JSON document.
91+
<em>Time Complexity:</em> O(1) when path is evaluated to a single value,
92+
O(N) when path is evaluated to multiple values, where N is the size of the
93+
key
8994
</td>
9095
</tr>
9196
<tr>
@@ -103,7 +108,9 @@ JSON.OBJKEYS employee_profile:1
103108
<tr>
104109
<td colspan="4">
105110
<em>Description:</em> Return the keys in the object that's referenced by
106-
path
111+
path. <em>Time Complexity:</em> O(N) when path is evaluated to a single
112+
value, where N is the number of keys in the object, O(N) when path is
113+
evaluated to multiple values, where N is the size of the key
107114
</td>
108115
</tr>
109116
<tr>
@@ -121,12 +128,14 @@ JSON.OBJLEN employee_profile:1
121128
<tr>
122129
<td colspan="4">
123130
<em>Description:</em> Report the number of keys in the JSON object at
124-
path in key
131+
path in key. <em>Time Complexity:</em> O(1) when path is evaluated to a
132+
single value, O(N) when path is evaluated to multiple values, where N is
133+
the size of the key
125134
</td>
126135
</tr>
127136
<tr>
128137
<td>JSON.ARRAPPEND</td>
129-
<td>JSON.ARRAPPEND key path json...</td>
138+
<td>JSON.ARRAPPEND key [path] value [value ...]</td>
130139
<td>
131140
<pre>
132141
<code>{`
@@ -135,17 +144,19 @@ JSON.ARRAPPEND employee_profile:1 .colors '"yellow"'
135144
`}</code>
136145
</pre>
137146
</td>
138-
<td>(integer) 3</td>
147+
<td>(integer) 4</td>
139148
</tr>
140149
<tr>
141150
<td colspan="4">
142-
<em>Description:</em> Appends the specified JSON value(s) to the JSON
143-
array at the specified path.
151+
<em>Description:</em> Append the json values into the array at path
152+
after the last element in it. <em>Time Complexity:</em> O(1) for each
153+
value added, O(N) for multiple values added where N is the size of the
154+
key
144155
</td>
145156
</tr>
146157
<tr>
147158
<td>JSON.ARRINSERT</td>
148-
<td>JSON.ARRINSERT key path index json...</td>
159+
<td>JSON.ARRINSERT key path index value [value ...]</td>
149160
<td>
150161
<pre>
151162
<code>{`
@@ -157,13 +168,16 @@ JSON.ARRINSERT employee_profile:1 .colors 2 '"purple"'
157168
</tr>
158169
<tr>
159170
<td colspan="4">
160-
<em>Description:</em> Inserts the JSON value(s) in the JSON array at the
161-
specified path.
171+
<em>Description:</em> Insert the json values into the array at path
172+
before the index (shifts to the right). <em>Time Complexity:</em> O(N)
173+
when path is evaluated to a single value where N is the size of the
174+
array, O(N) when path is evaluated to multiple values, where N is the
175+
size of the key
162176
</td>
163177
</tr>
164178
<tr>
165179
<td>JSON.ARRINDEX</td>
166-
<td>JSON.ARRINDEX key path json [start [stop]]</td>
180+
<td>JSON.ARRINDEX key path value [start [stop]]</td>
167181
<td>
168182
<pre>
169183
<code>{`
@@ -176,7 +190,9 @@ JSON.ARRINDEX employee_profile:1 .colors '"purple"'
176190
<tr>
177191
<td colspan="4">
178192
<em>Description:</em> Searches for the first occurrence of a JSON value
179-
in an array.
193+
in an array. <em>Time Complexity:</em> O(N) when path is evaluated to a
194+
single value where N is the size of the array, O(N) when path is
195+
evaluated to multiple values, where N is the size of the key
180196
</td>
181197
</tr>
182198
</tbody>
@@ -186,6 +202,97 @@ JSON.ARRINDEX employee_profile:1 .colors '"purple"'
186202

187203
<TabItem value="NODE_JS">
188204

205+
```js
206+
/*
207+
JSON.SET key path value
208+
Sets JSON value at path in key.
209+
O(M+N) where M is the original size and N is the new size
210+
*/
211+
const setResult = await client.json.set('employee_profile:1', '.', {
212+
name: 'Alice',
213+
});
214+
console.log(setResult); // OK
215+
216+
/*
217+
JSON.GET key [path [path ...]]
218+
Returns the JSON value at path in key.
219+
O(N) when path is evaluated to a single value where N is the size of the value, O(N) when path is evaluated to multiple values, where N is the size of the key
220+
*/
221+
const getResult = await client.json.get('employee_profile:1');
222+
console.log(getResult); // { name: 'Alice' }
223+
224+
/*
225+
JSON.NUMINCRBY key path number
226+
Increments a number inside a JSON document.
227+
O(1) when path is evaluated to a single value, O(N) when path is evaluated to multiple values, where N is the size of the key
228+
*/
229+
await client.json.set('employee_profile:1', '.age', 30);
230+
const incrementResult = await client.json.numIncrBy(
231+
'employee_profile:1',
232+
'.age',
233+
5,
234+
);
235+
console.log(incrementResult); // 35
236+
237+
/*
238+
JSON.OBJKEYS key [path]
239+
Return the keys in the object that's referenced by path
240+
O(N) when path is evaluated to a single value, where N is the number of keys in the object, O(N) when path is evaluated to multiple values, where N is the size of the key
241+
*/
242+
const objKeysResult = await client.json.objKeys('employee_profile:1');
243+
console.log(objKeysResult); // [ 'name', 'age' ]
244+
245+
/*
246+
JSON.OBJLEN key [path]
247+
Report the number of keys in the JSON object at path in key
248+
O(1) when path is evaluated to a single value, O(N) when path is evaluated to multiple values, where N is the size of the key
249+
*/
250+
const objLenResult = await client.json.objLen('employee_profile:1');
251+
console.log(objLenResult); // 2
252+
253+
/*
254+
JSON.ARRAPPEND key [path] value [value ...]
255+
Append the json values into the array at path after the last element in it
256+
O(1) for each value added, O(N) for multiple values added where N is the size of the key
257+
*/
258+
await client.json.set('employee_profile:1', '.colors', [
259+
'red',
260+
'green',
261+
'blue',
262+
]);
263+
const arrAppendResult = await client.json.arrAppend(
264+
'employee_profile:1',
265+
'.colors',
266+
'yellow',
267+
);
268+
console.log(arrAppendResult); // 4
269+
270+
/*
271+
JSON.ARRINSERT key path index value [value ...]
272+
Insert the json values into the array at path before the index (shifts to the right)
273+
O(N) when path is evaluated to a single value where N is the size of the array, O(N) when path is evaluated to multiple values, where N is the size of the key
274+
*/
275+
const arrInsertResult = await client.json.arrInsert(
276+
'employee_profile:1',
277+
'.colors',
278+
2,
279+
'purple',
280+
);
281+
console.log(arrInsertResult); // 5
282+
283+
/*
284+
JSON.ARRINDEX key path json [start [stop]]
285+
Searches for the first occurrence of a JSON value in an array.
286+
O(N) when path is evaluated to a single value where N is the size of the array, O(N) when path is evaluated to multiple values, where N is the size of the key
287+
*/
288+
const arrIndexResult = await client.json.arrIndex(
289+
'employee_profile:1',
290+
'.colors',
291+
'purple',
292+
);
293+
console.log(arrIndexResult); // 2
294+
```
295+
189296
</TabItem>
190297

191298
</Tabs>

0 commit comments

Comments
 (0)