atlassian-connect-express 9: add your own settings to the database

Hello!

In this article we will see how to add your own settings to the database.

This article is based on this one.

You can find the source code for the article here.

You can find a video for this article here.

Let’s say your application depends on some settings which are set by your application. Either you save it automatically or a user sets the settings in some of the pages of your application.

First of all, always consider to store the settings for your app in the Jira, Confluence or Bitbucket itself. On the date of writing this article it is not possible to store settings in Bitbucket itself. That is why we need to store settings in our database.

There are a couple of ways to do it. I will show you the simplest one.

When our app is installed the AddonSettings table is created:

This table contains 4 fields: id, clientKey, key and val. By default atlassian-connect-express saves necessary to the framework data in rows where the key equals to clientInfo.

That is why to add your own settings you can add rows with your own key. Let’s say we call our key “appSettings”.

The val columns should be always a json.

Now let’s add our own row on app installation. We should change the code in backend/app.js:

addon.on('host_settings_saved', function(clientKey, data) {
    console.log('host_settings_saved')
    console.log(clientKey)
    console.log(data)
});

to this one:

addon.on('host_settings_saved', function(clientKey, data) {
    console.log('host_settings_saved')
    addon.settings.set('appSettings', {settings1: "settings1 value"}, clientKey)
        .then(() => {
            addon.settings.get('appSettings', clientKey)
                .then((settings) => {
                    console.log(settings)
                })
        })
});

In this code we first create a new row with the appSettings key and then read it and log the result into console.

Now we need to delete the row if we uninstall our app. That is why let’s make the uninstalled endpoint look like this:

    app.post('/uninstalled', addon.authenticate(), function (req, res) {
        addon.settings.del('appSettings', req.context.clientKey)
            .then(() => res.send(200))
    })

We delete the row and then return the success status code.

Now let’s try to install our app again.

Here is the AddonSettings table:

We can see that the row with the appSettings key was added.

Now let’s examine the logs and see if we can read the data from the row:

{ settings1: 'settings1 value' }

Yes, we are able to read the values.

Now let’s uninstall and see if the row was deleted:

Yes, the row was deleted.

Everything works as expected. That is all for the article.

See you in the next article.

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

One thought on “atlassian-connect-express 9: add your own settings to the database

Leave a Reply

%d bloggers like this:

Spelling error report

The following text will be sent to our editors: