Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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:

Screenshot 2021-05-24 at 11.14.11.png

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:

Screenshot 2021-05-24 at 12.33.05.png

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:

Screenshot 2021-05-24 at 12.35.11.png

Yes, the row was deleted.

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

See you in the next article.

1 comment

Comment

Log in or Sign up to comment
M Amine
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 30, 2021

Great article @Alexey Matveev you are really helping the community with this. Keep up the good work. 

Are you offering (or willing to offer) a training on Atlassian App dev? 

TAGS
AUG Leaders

Atlassian Community Events