Read and write to the database!

Plugin Development
  • A few questions!

    • Is there a standard way to write an read data from the database?
    • Does it matter if I use mongodb or redis ie does some plugin only support mongo?
    • Nodebb database layout for plugin data. Any good standard that I should use? How to structure data the NodeBB way?

    I prefer to use as much builtin functions as possible to skit reiventing the wheel again 😉

    Is it better to learn node.js first? I only have skills for the LAMP stack 😞

  • What's your objective in the end?

    To read/write on the database (whichever you're using), with NodeBB I'd suggest you to use the "abstract layer" they developed. You can import src/database.js from the sources and use the native functions implemented in nodebb.

    To see how they structure data, I'd suggest you to go check the code of their official plugins (or directly of the platform core).

    You should definitely learn Node first, IMO.

  • Unless you are doing something specific never write directly to the document store (its not as db as you know it) for one thing is not the best-structured store owing to the fact they support multiple.

    As @Giggiux says use the abstraction layer this allows your plugin to work with both mongo and redis and it also allows NodeBB to change the structure of the doc store or even add more and your plugin will just work.

  • Is there any good exampels for using the abstraction layer "src/database.js". Or do i need to learn from looking into the code only?

    I want to make a movie/games/database plugin for nodeBB 🙂

  • @Jenkler IMO the best example is their code!

    You could replicate and modify what in NodeBB are categories as groups (Movie/games/tv show), and what in NodeBB are topics as actual elements (a movie, a tv-series, a game). The best examples of code are their one and you can find them in /src 🙂

    In order to use these things in your plugin, check official plugins code ^^

    In my opinion is the best way to learn 😉

Suggested Topics

  • 0 Votes
    2 Posts
  • 0 Votes
    10 Posts

    topics.getUnreadData doesn't take an array of pids. You should use Topics.hasReadTopics(tids, uid);

  • 0 Votes
    2 Posts

    Hello everyone,

    I am trying to save a nested JSON array into my database.

    I init my database entry as following:

    Database.getObject('nodebb-plugin-europahaus-addon-spotify', function (data) { if (!data) { //Init as default and save to database data = { currentTrack: "", playlistSongs: [], requestedSongs: [], playback: false } Database.setObject('nodebb-plugin-europahaus-addon-spotify', data, function(err) { if (err) console.error(err); }); });

    which adds the data perfectly fine.

    Now I want to edit one of the fields.

    Therefore, I use the following code:

    songs = []; //Add songs and information tracks.forEach(function(track) { var artistsString = ""; track.track.artists.forEach(function(artist) { artistsString += + ", "; }); artistsString = artistsString.slice(0, -2); songs.push({ track: { id:, name:, artists: artistsString, uri: track.track.uri } }); }); //Save data in database // //Save JSON as string since NodeBB won't allow saving arrays Database.setObjectField('nodebb-plugin-europahaus-addon-spotify', 'playlistSongs', songs, function(err) { if (err) console.error(err); callback(null, {can: true, tracks: songs}); });

    When accessing my database, no value has changed. Even when replacing the array by a simple string, no update occurs in the database.

    Thank you for your help!
    Kind regards.

  • 0 Votes
    16 Posts

    @djensen47 yep I noticed that after reading your OP more carefully. The intention of my bot was a 100% configurable bot that would be able to perform actions based on certain events happening and possibly having filters applied to it. It would be waaaay overkill for your use case.

    It works in theory but I lost motivation and no one seemed interested in it at the time. If it was actually finished it could easily perform tasks like what you're writing a separate plugin for.

  • 0 Votes
    1 Posts

    First of all, I'm new to NodeJS development. I'm liking it so far (except for a few annoyances here and there).

    I am extending development of nodebb-plugin-import-ipboard (forked and will send a PR once it's done) to support a bunch of stuff the current plugin does not currently support, for instance:

    IPB replies which are in a special blockquote format. Spoiler blocks Mentions A few more things...

    On my nodebb-plugin-import-ipboard/index.js file, which I'm requiring from a manual test.js file (so it's not running inside NodeBB!), I placed a:
    var db = require('nodebb-plugin-import/server/db');

    But when I try to use it like this:
    var imported_post = db.getObject("_imported_post:1566");
    I just get "undefined".
    I can see on the debugger variables Redis is loaded (but I don't know if it was loaded correctly.)

    I also tried loading data instead of db:
    var data = require('nodebb-plugin-import/server/data');
    var imported_post = data.getImportedPost("1566");

    And I just get undefined.
    Can someone tell me what I'm doing wrong?