Communicating with the Database
-
What is considered the best and cleanest way to get and set data to the Redis Database?
For instance I want to add the key nodebb-plugin-test:name "Siber" to the Database. How should I best do that? And then also how do I retrieve that key again from the Database?
-
Here's an example of reading and writing to the config:
https://github.com/psychobunny/nodebb-plugin-admin-samplehttps://github.com/psychobunny/nodebb-plugin-admin-sample/blob/master/static/admin.tpl
Check out
data-field="sample:setting1
if you want to retrieve this anywhere in the server side you can just do
meta.config['sample:setting1']
See https://github.com/psychobunny/nodebb-plugin-blog-comments/ for a real world example of this in use. Good luck
-
If you are going to store more than just simple key values, you should use hashes and sets.
For example if you have an object in javascript that you want to save from your plugin you can do
var myObj = { myField: 1, otherField: 'test' }; db.setObject('nodebb-plugin-test:test', myObj, function(err) { ));
To retrieve that object later on
db.getObject('nodebb-plugin-test:test', function(err, myObj) { console.log(myObj); });
To retrieve just a single field use
db.getObjectField('nodebb-plugin-test:test', 'otherField', callback);
To set a single fielddb.setObjectField('nodebb-plugin-test:test', 'otherField', 5, callback);
You can check out the rss plugin to see how this is done.