Constructor
new List()
Example
const client = require('redis').createClient();
const types = require('redis-type')(client);
const list = new types.List('my_list', true); // last flag for JSON=true
(async () => {
await list.push({a: 1}, {a: 2}, {a: 3});
console.log(await list.slice()); // get list contents
const lastEl = await list.pop();
console.log(await list.slice()); // [{a: 1}, {a: 2}];
await list.setElementAt(0, lastEl);
console.log(await list.slice()); // [{a: 3}, {a: 2}];
})();
Extends
Methods
length() → {Promise.<Number>}
Get length of a list
- Redis command: LLEN
- JavaScript analogy: Array.length
shift() → {Promise.<(Object|String)>}
Remove and get first element of the list
- Redis command: LPOP
- JavaScript analogy: Array.prototype.shift
Example
(async () => {
await list.push(1, 2, 3); // fill list with values
let first = await list.shift(); // gonna be 1 and list will contain [2, 3]
})()
pop() → {Promise.<(Object|String)>}
Remove and get last element
- Redis command: RPOP
- JavaScript analogy: Array.prototype.pop
Example
(async () => {
await list.push(1, 2, 3); // fill list with values
let last = await list.pop(); // gonna be 3 and list will contain [1, 2]
})()
unshift() → {Promise}
Prepend list with element(s)
- Redis command: LPUSH
- JavaScript analogy: Array.prototype.unshift
Parameters:
Name | Type | Description |
---|---|---|
...els |
String |
Element(s) to push in the beginning |
Example
(async () => {
await list.unshift(1); // list [1]
await list.unshift(3, 2); // list [3, 2, 1] - just like an Array in JS
})()
push() → {Promise.<Number>}
Push element(s) to the end of the list
- Redis command: RPUSH
- JavaScript analogy: Array.prototype.push
Parameters:
Name | Type | Description |
---|---|---|
...els |
String |
Element(s) to push |
Example
(async () => {
await list.push(1, 2, 3, 4); // return value: 4
await list.push(1); // return value: 5
})();
slice(beginopt, endopt) → {Promise.<Array>}
Get range of elements with BEGIN-END arguments
- Redis command: LRANGE
- JavaScript analogy: Array.prototype.slice
Supports just the same interface as Array does.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
begin |
Number |
<optional> |
0 |
Start point index |
end |
Number |
<optional> |
null |
End point index |
Example
(async () => {
await list.push(1, 2, 3, 4, 5);
let one = await list.slice(); // gonna be [1, 2, 3, 4, 5]
let two = await list.slice(1); // gonna be [2, 3, 4, 5]
let three = await list.slice(-2); // gonna be [4, 5]
let four = await list.slice(1, 3) // gonna be [2, 3]
})()
insertAfter(key, el) → {Promise.<Number>}
Insert element after the element with given value (as String)
- Redis command: LINSERT AFTER
- JavaScript analogy: none
Parameters:
Name | Type | Description |
---|---|---|
key |
Number |
Key to insert after |
el |
String |
Element to insert |
Example
(async () => {
await list.push('a', 'c', 'd');
await list.insertAfter('a', 'b'); // returned value: 4, list: [a, b, c, d]
})()
insertBefore(key, el) → {Promise.<Number>}
Insert elenent before the elemnt with given index
- Redis command: LINSERT BEFORE
- JavaScript analogy: none
Parameters:
Name | Type | Description |
---|---|---|
key |
Number |
Key to insert after |
el |
String |
Element to insert |
Example
(async () => {
await list.push('a', 'c', 'd');
await list.insertBefore('c', 'b'); // returned value: 4, list: [a, b, c, d]
})()
getElementAt(index) → {Promise.<(?String|Object)>}
Get element at the given index
- Redis command: LINDEX
- JavaScript analogy: none | or Array[index]
Parameters:
Name | Type | Description |
---|---|---|
index |
Number |
Index to get element at |
Example
(async () => {
await list.push('a', 'b', 'c', 'd');
let el1 = await list.getElementAt(0); // value: 'a'
let el2 = await list.getElementAt(-1); // value: 'd'
let el3 = await list.getElementAt(100); // value: null
})()
setElementAt(index, value) → {Promise.<String>}
Set element at given index
- Redis command: LSET
- JavaScript analogy: none | or Array[index] = value
Parameters:
Name | Type | Description |
---|---|---|
index |
Number |
Index to set el at |
value |
String |
Value to set at given index |
Throws:
-
When index is out of range (AKA greater than list.length())
- Type
- Error
Example
(async () => {
await list.push('a', 'b', 'c', 'd');
await list.setElementAt(0, 'd'); // OK, list is [d, b, c, d]
await list.setElementAt(1, 'd'); // OK, list is [d, d, c, d]
await list.setElementAt(2, 'd'); // OK, list is [d, d, d, d]
await list.setElementAt(100, 'd').catch((e) => console.log(e)); // err: index out of range
})()
trim(begin, end) → {Promise.<String>}
Trim list in the given begin-end range
- Redis command: LTRIM
- JavaScript analogy: none; maybe Array.prototype.slice()
Indexes you specify are included (0, 0) - one element, (0, 1) - 2 elements
Parameters:
Name | Type | Description |
---|---|---|
begin |
Number |
Start index |
end |
Number |
End index |
Example
(async () => {
await list.push(1, 2, 3, 4);
await list.trim(0, -1); // list [1,2,3,4] - the same
await list.trim(-3, -1); // list [2,3,4] - from the end
await list.trim(0, 1); // list [2,3] - from the start
})()
prom(method) → {function}
Return promisified version of Redis client's methods.
Uses util.promisify internally which is most native way of getting Promise. As all Redis commands have name of the key as first argument, we can and do set first argument with name/key of the storage/structure
Parameters:
Name | Type | Description |
---|---|---|
method |
String |
Method to return promisified |
- Inherited From:
- Source:
clear() → {Promise}
Remove any data type from global scope using DEL method and the key used in creation
Command might be used for debug clean-up, development purposes or for production reset of the storage
- Inherited From:
- Source: