Good work on creating a bunch of storage adapters, will make many folks happy.
I was looking through the codebase and I have a question about the set functionality.
I assume that the BaseContext set
method ...
set(scope, key, value, callback) {
return this.queue.enqueue(async () => {
if (!key) { throw new Error('Key is not defined'); }
if (!callback || typeof callback !== 'function') { throw new Error('Callback is not a function'); }
if (!Array.isArray(key)) { key = [key]; }
if (!Array.isArray(value)) { value = [value]; }
if (key.length !== value.length) { throw new Error('Number of values don\'t match number of keys'); }
... is called when I do something similar to ...
flow.set("responses", content, () => { node.send(msg) })
... in a function node?
What I don't understand is the conversion to arrays of key
and value
in this case.
In my example above, the key is "responses"
(i.e. string) and value is content
which is an array. Now your code would convert the key to ["responses"]
and content
remains an array. Then the lengths are compared but since content
is an array of length > 1, an error would be thrown.
But what I was trying to do (and what works with the memory storage of NR) is assign the responses key an array as value and not assign a collection of keys a single value each.
I understand that the underlying storage may or may not support arrays as values but that should not cause an error or if it does, then it should be something like "array values not supported by storage mechanism XYZ".
What am I missing here?
EDIT:
Having a looking at the existing redis store:
Redis.prototype.set = function (scope, key, value, callback) {
if (callback && typeof callback !== 'function') {
throw new Error('Callback must be a function');
}
try {
if (!Array.isArray(key)) {
key = [key];
value = [value];
} else if (!Array.isArray(value)) {
// key is an array, but value is not - wrap it as an array
value = [value];
}
the difference seems to be that if the key isn't an array, then it becomes an array and the value is wrapped in an extra array.
So in my example it becomes ["responses"]
and [[... content array ...]]
- which do have the same length of one.