node-red-contrib-cache-database
node-red-contrib-cache-database
This was something I had to implement in a personal project (yeah my house is getting full of sensors and actuators) to get some reference and configuration data about those devices.
The main idea behind this was to get the information from a database (SQL or NoSQL...I chose MongoDB) at the first call and cache it to the remaining.
Import the code to your Node-RED and configure the MongoDB component.
In this code I created a database called cache-db and one collection called cache-collection. The documents must have at least two properties: key and value.
Have fun!
[{"id":"811f43f8.3def3","type":"subflow","name":"CACHE FLOW","info":"","in":[{"x":80,"y":120,"wires":[{"id":"aea57b24.958668"}]}],"out":[{"x":1180,"y":120,"wires":[{"id":"6ccff4fe.aa373c","port":0}]}]},{"id":"c13323c0.6fffe","type":"switch","z":"811f43f8.3def3","name":"IF","property":"payload","propertyType":"msg","rules":[{"t":"nnull"},{"t":"null"}],"checkall":"true","outputs":2,"x":490,"y":120,"wires":[["6ccff4fe.aa373c"],["156cd322.53888d"]]},{"id":"aea57b24.958668","type":"Cache in","z":"811f43f8.3def3","name":"CACHE READER","cache":"e2a21528.907068","keyType":"msg","keyProperty":"topic","valueType":"msg","valueProperty":"payload","useString":true,"x":270,"y":120,"wires":[["c13323c0.6fffe"]]},{"id":"6ccff4fe.aa373c","type":"join","z":"811f43f8.3def3","name":"JOIN","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"5","count":"1","x":910,"y":120,"wires":[[]]},{"id":"6758c473.8bc0cc","type":"Cache out","z":"811f43f8.3def3","name":"CACHE WRITER","cache":"e2a21528.907068","keyType":"msg","keyProperty":"topic","valueType":"msg","valueProperty":"payload","ttlType":"msg","ttlProperty":"0","useString":false,"x":1270,"y":360,"wires":[]},{"id":"31383373.101c2c","type":"mongodb3 in","z":"811f43f8.3def3","service":"_ext_","configNode":"7fbc657b.28848c","name":"Find value by key on MongoDB","collection":"cache-collection","operation":"findOne","x":850,"y":260,"wires":[["ffefbc3a.485a7"]]},{"id":"156cd322.53888d","type":"function","z":"811f43f8.3def3","name":"QUERY","func":"msg.payload = { key : msg.topic };\nreturn msg;","outputs":1,"noerr":0,"x":600,"y":260,"wires":[["31383373.101c2c"]]},{"id":"ffefbc3a.485a7","type":"function","z":"811f43f8.3def3","name":"RESPONSE","func":"msg.payload = msg.payload.value;\nreturn msg;","outputs":1,"noerr":0,"x":1090,"y":260,"wires":[["6ccff4fe.aa373c","6758c473.8bc0cc"]]},{"id":"e2a21528.907068","type":"Cache","z":"","name":"CACHE","defaultTtl":"0","checkPeriod":"0"},{"id":"7fbc657b.28848c","type":"mongodb3","z":"","uri":"mongodb://localhost/cache-db","name":"MongoDB (cache-db)","options":"","parallelism":"-1"},{"id":"717a80f9.83289","type":"inject","z":"7b5f2788.09ef28","name":"GET VALUE BY KEY '001'","topic":"001","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":210,"y":140,"wires":[["f283ada7.44892"]]},{"id":"6a53d1d0.94b7d","type":"debug","z":"7b5f2788.09ef28","name":"OUTPUT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":720,"y":180,"wires":[]},{"id":"100d7234.46520e","type":"inject","z":"7b5f2788.09ef28","name":"GET VALUE BY KEY '002'","topic":"002","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":210,"y":220,"wires":[["f283ada7.44892"]]},{"id":"f283ada7.44892","type":"subflow:811f43f8.3def3","z":"7b5f2788.09ef28","name":"","x":520,"y":180,"wires":[["6a53d1d0.94b7d"]]}]