Saving flows into Database

Hi, I have checked out the Storage module. Can anyone help me out if there are any samples of storing flows into postgresql or mongoDB?

I have seen - https://nodered.org/docs/api/storage/methods/#storagesaveflowsflows

Thanks!

Are you saying you want to store the active flow into a database?

There are some examples of Node-RED storage plugins out there - can't put my finger on them right now - but hey do exist.

Yes. I want to store flows into database.

I have found a mongodb plugin but can't seem to understand how to store the flows into the database.

Well you first have to get the flow file. This could be tricky if you don't have the flow file name set in settings.js because Node-RED uses flows_<hostname>.json as the default flows file. That is as long as you do not have 'projects' active.

Once you know the name of the flow, you can use the file node or one of the contrib file nodes to read the file. You can tehn pass that to your database node to save. Don't forget you also need to save the flows_<hostname>_cred.json file also.

Thank you. Isn't there any module to directly do this process from node-red to DB?

What mongodb plugin did you find? Was it a storage plugin or just a regular Node-RED node module?

There is an storage plugin for the Cloudant database here: https://github.com/IBM/node-red-app/blob/master/cloudantStorage.js

It was from this article - https://www.npmjs.com/package/node-red-mongo-storage-plugin
Wasn't useful for me though.

Thanks for sharing the storage plugin.

Why was that module not useful? It's exactly what you asked for - a storage plugin for Node-RED that stores flows in mongo.

I had added the module. But, I didn't understand how to take it forward and use the saveFlows() method to store the flows into mongoDB.

It's a storage plug-in, you don't have to call any method from within your flow (in fact, you can't), that is done by the runtime. It's an alternative to the filesystem-based storage that is used by default.

Did you read and follow the instructions on how to integrate it? You'd have to modify your settings.js for it to work.

I have installed the plugin. I didn't understand the integration part of it as what exactly needs to be edited and how the flows will be stored into my DB. It would be great help if you have any other sample article so I can understand it better.

Afar from the documentation of the module there's just the doc of the Storage API itself.

https://nodered.org/docs/api/storage/

The settings.js that you need to modify resides in your Node-RED user directory, usually .node-red in your user profile.

@poojak could you explain why you want to do this, the fact that you are having difficulties with the fundamentals of using a storage plugin makes me wonder whether you misunderstand something.

I want to implement user wise flow storage in database. I have understood the changes are required in settings.js file. What I am not sure is how the saveFlows() works and how am I supposed to add the flows according to the users into mongoDB.

Node-RED is a single-user system. It runs one set of flows regardless of who has logged in to the editor. The storage layer doesn't know anything about individual users - it just stores the flows being deployed to the runtime.

If you want to just save flows to a database then you can use that plugin you've found and configure it via your settings file exactly as its readme says. That's all you do.

Is there a guide that details how to write a plugin? I'm sure the Documentation is perfect for seasoned JS Devs but I am an amateur and I'm having a bit of trouble finding an example I can refer to.

Thanks

Oodles of Information here and it's a fully working example too - can't get better than that.

Thanks for the reference!

Should I add the var settings { } code block to settings.js in ~/.node-red/ ?

If yes, That settings file does not have a var settings { } code block where I can append the storage module config. Shouldn't there be a line describing the current config? Or should I add one?

Hope I'm not being vague :neutral_face: