1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Allow context store name to be provided in the key

For nodes that get/set context, when multiple stores are configured
they will not know to parse the store name from the key. So they
will pass the store name in the key, such as #:(store)::key.

Currently that will cause that full string to be used as the key
and the default context store used - which is wrong.

The code now parses out the store name from the key if it is set -
athough if the call to get/set does include the store argument, it
will take precedence.

This only applies when the key is a string - it doesn't apply when
an array of keys is provided.
This commit is contained in:
Nick O'Leary 2018-09-14 23:21:05 +01:00
parent 66ee27c5fa
commit a8ec032553
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 53 additions and 16 deletions

View File

@ -225,18 +225,28 @@ function createContext(id,seed) {
get: {
value: function(key, storage, callback) {
var context;
if (!storage && !callback) {
context = stores["_"];
if (!callback && typeof storage === 'function') {
callback = storage;
storage = undefined;
}
if (callback && typeof callback !== 'function'){
throw new Error("Callback must be a function");
}
if (!Array.isArray(key)) {
var keyParts = util.parseContextStore(key);
key = keyParts.key;
if (!storage) {
storage = keyParts.store || "_";
}
} else {
if (typeof storage === 'function') {
callback = storage;
if (!storage) {
storage = "_";
}
if (callback && typeof callback !== 'function'){
throw new Error("Callback must be a function");
}
context = getContextStorage(storage);
}
context = getContextStorage(storage);
if (callback) {
if (!seed) {
context.get(scope,key,callback);
@ -270,18 +280,28 @@ function createContext(id,seed) {
set: {
value: function(key, value, storage, callback) {
var context;
if (!storage && !callback) {
context = stores["_"];
if (!callback && typeof storage === 'function') {
callback = storage;
storage = undefined;
}
if (callback && typeof callback !== 'function'){
throw new Error("Callback must be a function");
}
if (!Array.isArray(key)) {
var keyParts = util.parseContextStore(key);
key = keyParts.key;
if (!storage) {
storage = keyParts.store || "_";
}
} else {
if (typeof storage === 'function') {
callback = storage;
if (!storage) {
storage = "_";
}
if (callback && typeof callback !== 'function') {
throw new Error("Callback must be a function");
}
context = getContextStorage(storage);
}
context = getContextStorage(storage);
context.set(scope, key, value, callback);
}
},

View File

@ -499,6 +499,23 @@ describe('context', function() {
done();
}).catch(done);
});
it('should allow the store name to be provide in the key', function(done) {
Context.init({contextStorage:contextDefaultStorage});
Context.load().then(function(){
var context = Context.get("1","flow");
var cb = function(){done("An error occurred")}
context.set("#:(test)::foo","bar");
context.get("#:(test)::foo");
stubGet2.called.should.be.false();
stubSet2.called.should.be.false();
stubSet.calledWithExactly("1:flow","foo","bar",undefined).should.be.true();
stubGet.calledWith("1:flow","foo").should.be.true();
done();
}).catch(done);
});
it('should use default as the alias of other context', function(done) {
Context.init({contextStorage:contextAlias});
Context.load().then(function(){