As part of my series of posts about the components needed to build a Multi Tenant Node-RED system in this post I’ll talk about writing a plugin to store the users flow in the database rather than on disk.
There are a number of existing Storage plugins, the default local filessystem, the CloudantDB plugin that is used with Node-RED in the IBM Cloud.
I’m going to use MongoDB as the backend storage and the Mongoose library to wrap the reading/writing to the database (I’ll be reusing the Mongoose schema definiations later in the Authentication plugin and the app to manager Node-RED instances).
The documentation from the Storage API can be found here. There are a number of methods that a plugin needs to provide:
init()
This sets up the plugin, it reads the settings and then opens the connection to the database.
init: function(nrSettings) {
settings = nrSettings.mongodbSettings || {};
if (!settings) {
var err = Promise.reject("No MongoDB settings for flow storage found");
err.catch(err => {});
return err;
}
appname = settings.appname;
return new Promise(function(resolve, reject){
mongoose.connect(settings.mongoURI, mongoose_options)
.then(() => {
resolve();
})
.catch(err => {
reject(err);
});
})
},
getFlows()/saveFlows(flows)
Here we retrieve/save the flow to the database. If there isn’t a current flow (such as the first time the instance is run) we need to return an empty array ([]
)
getFlows: function() {
return new Promise(function(resolve, reject) {
Flows.findOne({appname: appname}, function(err, flows){
if (err) {
reject(err);
} else {
if (flows){
resolve(flows.flow);
} else {
resolve([]);
}
}
})
})
},
saveFlows: function(flows) {
return new Promise(function(resolve, reject) {
Flows.findOneAndUpdate({appname: appname},{flow: flows}, {upsert: true}, function(err,flow){
if (err) {
reject(err)
} else {
resolve();
}
})
})
},
The upsert: true
in the options passed to findOneAndUpdatet()
triggers an insert if there isn’t an existing matching document.
getCredentials/saveCredentials(credentials)
Here we had to convert the credentials object to a string because MongoDB doesn’t like root object keys that start with a $
(the encrypted credentials string is held in the $_
entry in the object.
getCredentials: function() {
return new Promise(function(resolve, reject) {
Credentials.findOne({appname: appname}, function(err, creds){
if (err) {
reject(err);
} else {
if (creds){
resolve(JSON.parse(creds.credentials));
} else {
resolve({});
}
}
})
})
},
saveCredentials: function(credentials) {
return new Promise(function(resolve, reject) {
Credentials.findOneAndUpdate({appname: appname},{credentials: JSON.stringify(credentials)}, {upsert: true}, function(err,credentials){
if (err) {
reject(err)
} else {
resolve();
}
})
})
},
getSessions()/saveSessions(sessions)/getSettings()/saveSettings(settings)
These are pretty much just carbon copies of the getFlows()
/saveFlows(flows)
functions since they are just storing/retrieving a single JSON object.
getLibraryEntry(type,name)/saveLibraryEntry(type,name,meta,body)
saveLibraryEntry(type,name,meta,body)
is pretty standard with a little bit of name manipulation to make it look more like a file path.
getLibraryEntry(type,name,meta,body)
needs a bit more work as we need to build the directory structure as well as being able to return the actual file content.
getLibraryEntry: function(type,name) {
if (name == "") {
name = "/"
} else if (name.substr(0,1) != "/") {
name = "/" + name
}
return new Promise(function(resolve,reject) {
Library.findOne({appname: appname, type: type, name: name}, function(err, file){
if (err) {
reject(err);
} else if (file) {
resolve(file.body);
} else {
var reg = new RegExp('^' + name , "");
Library.find({appname: appname, type: type, name: reg }, function(err, fileList){
if (err) {
reject(err)
} else {
var dirs = [];
var files = [];
for (var i=0; i<fileList.length; i++) {
var n = fileList[i].name;
n = n.replace(name, "");
if (n.indexOf('/') == -1) {
var f = fileList[i].meta;
f.fn = n;
files.push(f);
} else {
n = n.substr(0,n.indexOf('/'))
dirs.push(n);
}
}
dirs = dirs.concat(files);
resolve(dirs);
}
})
}
})
});
},
saveLibraryEntry: function(type,name,meta,body) {
return new Promise(function(resolve,reject) {
var p = name.split("/"); // strip multiple slash
p = p.filter(Boolean);
name = p.slice(0, p.length).join("/")
if (name != "" && name.substr(0, 1) != "/") {
name = "/" + name;
}
Library.findOneAndUpdate({appname: appname, name: name},
{name:name, meta:meta, body:body, type: type},
{upsert: true, useFindAndModify: false},
function(err, library){
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
Using the plugin
To use the plugin you need to include it in the settings.js
file. This is normally found in the userDir (the location of this file is logged when Node-RED starts up).
...
storageModule: require('node-red-contrib-storage-mongodb'),
mongodbSettings: {
mongoURI: "mongodb://localhost/nodered",
appname: "r1"
},
...
The mongodbSettings
object contains the URI for the database and the appname
is a unique identifier for this Node-RED instance allowing the same database to be used for multiple instances of Node-RED.
Source code
You can find the code for this module here and it’s hosted on npmjs here