Add ability to delete context values from sidebar

This commit is contained in:
Nick O'Leary
2018-11-12 17:04:22 +00:00
parent 1c66c88f95
commit bfd98f3767
9 changed files with 225 additions and 16 deletions

View File

@@ -20,7 +20,6 @@
var runtime;
// TODO: move runtime/util to util/index
var util = require("@node-red/util").util;
function exportContextStore(scope,ctx, store, result, callback) {
@@ -152,5 +151,104 @@ var api = module.exports = {
resolve({});
}
})
},
/**
* Gets the info of an individual node set
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {String} opts.scope - the scope of the context
* @param {String} opts.id - the id of the context
* @param {String} opts.store - the context store
* @param {String} opts.key - the context key
* @return {Promise} - the node information
* @memberof RED.nodes
*/
delete: function(opts) {
return new Promise(function(resolve,reject) {
var scope = opts.scope;
var id = opts.id;
var store = opts.store;
var key = opts.key;
var availableStores = runtime.nodes.listContextStores();
//{ default: 'default', stores: [ 'default', 'file' ] }
if (store && availableStores.stores.indexOf(store) === -1) {
runtime.log.audit({event: "context.get",scope:scope,id:id,store:store,key:key,error:"not_found"});
var err = new Error();
err.code = "not_found";
err.status = 404;
return reject(err);
}
var ctx;
if (scope === 'global') {
ctx = runtime.nodes.getContext('global');
} else if (scope === 'flow') {
ctx = runtime.nodes.getContext(id);
} else if (scope === 'node') {
var node = runtime.nodes.getNode(id);
if (node) {
ctx = node.context();
}
}
if (ctx) {
if (key) {
store = store || availableStores.default;
ctx.set(key,undefined,store,function(err) {
runtime.log.audit({event: "context.delete",scope:scope,id:id,store:store,key:key});
resolve();
});
return;
} else {
// TODO: support deleting whole context
runtime.log.audit({event: "context.get",scope:scope,id:id,store:store,key:key,error:"not_found"});
var err = new Error();
err.code = "not_found";
err.status = 404;
return reject(err);
// var stores;
// if (!store) {
// stores = availableStores.stores;
// } else {
// stores = [store];
// }
//
// var result = {};
// var c = stores.length;
// var errorReported = false;
// stores.forEach(function(store) {
// exportContextStore(scope,ctx,store,result,function(err) {
// if (err) {
// // TODO: proper error reporting
// if (!errorReported) {
// errorReported = true;
// runtime.log.audit({event: "context.delete",scope:scope,id:id,store:store,key:key,error:"unexpected_error"});
// var err = new Error();
// err.code = "unexpected_error";
// err.status = 400;
// return reject(err);
// }
//
// return;
// }
// c--;
// if (c === 0) {
// if (!errorReported) {
// runtime.log.audit({event: "context.get",scope:scope,id:id,store:store,key:key});
// resolve(result);
// }
// }
// });
// })
}
} else {
runtime.log.audit({event: "context.delete",scope:scope,id:id,store:store,key:key});
resolve();
}
});
}
}