A bootstrap for a microservice based on Flask with MongoDB

Starting a new project is a common task in microservices architecture. To do this it’s better to have a some template. I put my version to the flask-mongoengine-bootstrap repository. The key point are:

  • very basic, only flask, flask-mongoengine and structlog in requirements
  • configuration through environment variables
  • configured logging in JSON format
  • marking log records with request_id
  • possibility to run development version make dev and tests make test through docker
  • a template for Makefile
  • examples of model, api route and tests

If you’ll use it, do not forget to change SECRET_KEY.

Migrations for MongoEngine

| Comments

A MongoDB collection does not have a strict schema, the documents in one collection can have different fields. But in MongoEngine the schema is provided on an application level and there is a validation. So if you’ll remove a field from the Document and it’s still exists in the database, there will be a mongoengine.errors.FieldDoesNotExist exception. If there is no need in the schema validation, a dynamic schema can be used.

So when there is a strict schema, it’s good to have a tool to make an updates for the documents in the collection when something changes.

A simple CRUD app with Django and Mongoengine

| Comments

There are several possibilities to use MongoDB in Django:

Django MongoDB uses django-nonrel, which is a fork based on Django 1.3.
I don’t like this idea, because now Django 1.5 is ready to out. Beetween Mongoengine and MongoKit, I like more Mongoengine. There are several comparative articles:

So I created a simple CRUD app using Mongoengine. The model definition in Mongoengine looks like in Django.