Simple task management rest-api with Flask and ArangoDB

omert
patron-labs
Published in
4 min readDec 19, 2019

ArangoDB is multi model database which supports ACID operations, queries, joins and graphs. It is open source and backed by ArangoDB GmbH.

You can check more details here about ArangoDB. You can check comparisons with MongoDB, Neo4j and Cassandra in their website as well.

Btw, here is my twitter (twitter/omertaban_en), I share well crafted stories from my product development journeys. Let’s connect and engage.

Well, okay are you ready ?

We will develop a REST API for a task management system. Our system has has people, tasks, task categories and relations between them . I want to show how you can reach a multimodel database flexibility and speed without losing pre-defined structures and schemas which we are used to them from SQL databases.

ArangoDB has also query language called AQL which allows us to run sql-like queries in a nosql database .

I will use a python driver called python-arango from joowani and another library arango-orm from Kashif Iftikhar. Both libraries are open source.

This project’s source codes also available in my github.

ArangoDB Database Installation :

You can refer to this document for installing ArangoDB to your computer. If you don’t want go through installation, you can use docker image as well. I will continue with docker image. You can use following command to create a arangodb 3.5.3 container at your end.

docker run -p 8529:8529 -e ARANGO_ROOT_PASSWORD=openSesame arangodb/arangodb:3.5.3
.
.
.
.
ArangoDB (version 3.5.3 [linux]) is ready for business. Have fun!

ArangoDB user interface should be running at http://localhost:8529 or at your docker container. User interface is the one of the good things I like about ArangoDB. It has also command line called arangosh but you don’t have to be familiar with command line to use database. You can create databases, collections, graphs and you can browser data through UI. You can even browse some statistics and performance reports.

Now we have database running, let’s talk about collections (‘tables’) and relations (‘joins’).

Database name: task_db

Collections :

You can consider collections as tables if you are coming from SQL databases background, we have 3 collections in this application.

  1. people
  2. categories
  3. tasks

Relations :

Relations are joins between collections, we have assignee_relation between tasks and people and category_relation between categories and tasks

  1. tasks ->(assignee_relation)->people
  2. categories->(category_relation)->tasks

Project structure of the product is like below.

Let’s dig into code and see how can we use arangodb with flask framework.

models.py:

I would like to start with models.py which actually where data structures are present . I have mentioned before we have 3 collections, I have explained the use of these collections below.

People stores information of individuals which will be assigned to the tasks. Categories helps us to categorize our tasks. One task will have one category only .
Tasks are where the work descriptions and due dates will be stored.
Category Relation is join collection between Categories and Tasks
Assignee Relation is join collection between People and Tasks

__collection__ and _key variables are required for ArangoDB models, __collection__ represents collection name in database, _key is the unique identifier for the objects. We don’t have to specify _key value for each object ArangoDB will create by itself, but if you would like to give identifier you are able to do that.

__init__.py :

I have initialized flask app and database components in this file.

We need to check if tasks_db exist or not. If tasks_db doesn’t exist we will create one and initialize. To check our tasks_db we have initialize _system db first and create our specific database through _system db.

After tasks_db created we use code below to sync collections and relations between our models and database.

db.create_all([Task_Graph, People, Categories, Tasks])

You can create edit collection, relations and graphs by ArangoDB UI at http://127.0.0.1:8529 as well, I prefer to get it done by code.

api.py :

Here we have endpoints for rest-api. It handles CRUD functions for people, categories, tasks. You can see crud and query functions done with arango-orm particularly.

Please check how relations are being used within task endpoints. I have attached the insomnia document at github repo, you can test api endpoints and use for your further developments.

app.py :

Runs the flask app at localhost:5000

Further Developments:

This project contains only back-end of a simple task management system, you can connect this with an user interface built by any js framework.

Authentication layer is not included in the code, a token based authentication would be good I think.

If you got any questions, I would like to see them in comments.

Thanks for reading, claps are appreciated.

Ömer Taban
twitter/omertaban_en

--

--

omert
patron-labs

I write about my journeys in programming and entrepreneurship.