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

View File

@ -499,6 +499,23 @@ describe('context', function() {
done(); done();
}).catch(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) { it('should use default as the alias of other context', function(done) {
Context.init({contextStorage:contextAlias}); Context.init({contextStorage:contextAlias});
Context.load().then(function(){ Context.load().then(function(){