atlassian-connect-express 7: working with Postgres

Hello!

In this article we will talk how to connect Postgres to our app.

The code will be based on this article.

You can find the source code for the article here.

You can find a video for this article here.

Our app by default stores information about all clients who installed our app. By default this data is stored in an in-memory database which means if we restart our app all registration data of our clients will be lost. It can be acceptable during development but it is definitely not acceptable during production mode.

That is why in this article we will see how to connect our app to Postgres.

I created a Postgres database with the following command:

docker run -p 5432:5432 --name tutorial_db -e POSTGRES_PASSWORD=password -d postgres

And then I created a database called “app”:

Right now we have the following configuration for our database in the backend/config.json file:

"store": {
            "adapter": "sequelize",
            "dialect": "sqlite3",
            "type": "memory"
        },

As you can see we use an in-memory database. Now let’s change the configuration to this one:

"store": {
            "type": "postgres",
            "url": "$DATABASE_URL"
        },

Now we need to set the $DATABASE_URL parameter. We can do it by setting the DATABASE_URL environment variable, but for the sake of simplicity I will replace $DATABASE_URL with a database url:

 "url": "postgres://postgres:password@localhost:5432/app"

Now we need to add the pg dependency to our app and install it:

npm install --save-dev pg

Now let’s start our app:

npm start

It started successfuly.

If you look at the app database you will see that a new table called AddonSettings has been created:

Right now the table is empty. Let’s install our app to Bitbucket and have a look what will change.

Here is how the table looks after I installed my app:

You can see that we have a new line with the “clientInfo” key. This line was added by atlassian-connect-express and contains information about the client which lets atlassian-connect-express identify and validate requests from the client.

That is all for the article. See you next time.

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

One thought on “atlassian-connect-express 7: working with Postgres

Leave a Reply

%d bloggers like this:

Spelling error report

The following text will be sent to our editors: