Skip to main content

A python interface to handle connection to a mongod instance

Project description

# Mongo Adapter

[![Build Status][travis-img]][travis-url]

A python implementation of a mongo adapter.

The idea is to make a base class that handles the connection to a mongod instance.
It is nice to handle the connection in the same way for different python projects that involves a mongo database.

The Mongo Adapter takes a client as init argument and then we connect to a database with `setup(database)`, forunately `mongo_adapter` can handle the client part as well.


from mongo_adapter import MongoAdapter, get_client

db_name = 'test'
client = get_client()
adapter = MongoAdapter(client)

assert adapter.db == db_name

## Intended usage

The intended usage is here illustrated with an example

from mongo_adapter import MongoAdapter, get_client

class LibraryAdapter(MongoAdapter):
def setup(self, database='library'):
"""Overrides the basic setup method"""
self.db = database

self.books_collection =
self.user_collection =

def add_book(self, title, author):
"""Add a book to the books collection"""
result = self.books_collection.insert_one(
'title': title,
'author': author
return result

if __name__ == '__main__':
client = get_client()
adapter = LibraryAdapter(client, database='library')

adapter.add_book('Moby Dick', 'Herman Melville')


## API

### Client


def check_connection(client):
"""Check if the mongod process is running



def get_client(host='localhost', port=27017, username=None, password=None,
uri=None, mongodb=None, timeout=20):
"""Get a client to the mongo database

host(str): Host of database
port(int): Port of database
timeout(int): How long should the client try to connect




### Adapter

class MongoAdapter(object):
"""Adapter for communicating with a mongo database"""
def __init__(self, client=None, db_name=None):
self.client = client
self.db = None
self.db_name = None
if (db_name and client):

def init_app(self, app):
"""Setup via Flask"""
host = app.config.get('MONGO_HOST', 'localhost')
port = app.config.get('MONGO_PORT', 27017)
db_name = app.config['MONGO_DBNAME']"connecting to database: %s:%s/%s", host, port, db_name)
self.setup(db_name=db_name, db=app.extensions['pymongo']['MONGO'][1])

def setup(self, db_name, db=None):
"""Setup connection to a database

if db:
self.db = db
self.db_name =
self.db = self.client[db_name]
self.db_name = db_name


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for mongo-adapter, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size mongo_adapter-0.1.1-py2.py3-none-any.whl (7.6 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size mongo_adapter-0.1.1.tar.gz (5.4 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page