Skip to main content

An improved package that provides mongodb connectivity. Not compatible with pyramid_mongo or pyramid_mongodb

Project description

Pyramid Mongodb

A simple library to integrate mongodb into your pyramid application. Comes with a debugtoolbar.

Features

  • Supports multiple databases
  • Configuration only setup
  • Integrated debugtoolbar with:
    • Shows db response times
    • explain() for cursor results
    • Connection information
    • Database and collection stats
  • Avoids recreating and closing MongoClient on every request.

Setup

pip install pyramid_mongodb2

Add the following to your application's ini file, (include pyramid_mongodb2:MongoToolbar in debugtoolbar.includes if you want to debug):

[app:main]
mongo_uri = mongodb://username:password@mongodb.host.com:27017/authdb
mongo_dbs = 
    foo
    bar
    baz-quux
    foo-test = foo
pyramid.includes =
    pyramid_mako    
    pyramid_debugtoolbar
    pyramid_mongodb2
debugtoolbar.includes =
    pyramid_mongodb2:MongoToolbar

The code will use config.add_request_method() to add a Database object to your requests, where each database is accessible by db_database_name, as defined in your configuration.

Note: database names with hyphens in them will be converted to underscores, that is database baz-quux will be accessible by request.db_baz_quux.

When doing foo-test = foo, the mongodb database with name foo-test will be assigned to request.db_foo. This helps when testing so that you can use a separate database for development, testing and production without changing your application code, or if you just want to alias a database name.

In your code where you can access request, you now have the following variables:

request.db
request.db_foo
request.db_bar
request.db_baz_quux
request.db_foo

request.db is the MongoClient object, should you ever need it.

In your view code, you can do this:

from pyramid.view import view_config

@view_config(route_name='home', renderer="templates/landing.mako")
def my_view(request):
    return {
        'some_data': request.db_foo.some_collection.find({'a': {'$gte': 5}}, {'_id': False}),
        'other_data': request.db_bar.visitors.insert_one({'person': request.remote_addr}),
    }

Debugging

With debugging enabled, all queries will be logged in request.query_log, when the debugtoolbar is opened, you can then view the execution time and explain() of cursor results. You can also see connection settings and stats for databases and collections.

Screenshots

Here's what the toolbar looks like in action:

Clicking the database or collection name will take you to the relevant section of the collections tab. Clicking the operation name will take you to its pymongo documentation. debug1

Clicking the explain button will show you the explain() result for a cursor. debug2 You can view detailed connection information here, clicking the field name will take you to the pymongo documentation for that field. debug3 This page show dbstats for all connected databases used in this request and their collections. debug4 Here we can see the use of multiple databases in a single project. debug5

Project details


Download files

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

Source Distribution

pyramid_mongodb2-1.6.2.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

pyramid_mongodb2-1.6.2-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file pyramid_mongodb2-1.6.2.tar.gz.

File metadata

  • Download URL: pyramid_mongodb2-1.6.2.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.19.4 CPython/3.6.3

File hashes

Hashes for pyramid_mongodb2-1.6.2.tar.gz
Algorithm Hash digest
SHA256 58ce3bc34ba358a17896a232012e7125104e740bc4b8cf277cc0f861b4a2806c
MD5 7a0395dd30524a59dadfb1714d2076d2
BLAKE2b-256 9081d37d070a238126db30b2a566e16745f5948b28cceefd234fd3a9a7890af9

See more details on using hashes here.

File details

Details for the file pyramid_mongodb2-1.6.2-py3-none-any.whl.

File metadata

  • Download URL: pyramid_mongodb2-1.6.2-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.25.0 CPython/3.6.2

File hashes

Hashes for pyramid_mongodb2-1.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c9a99202b67aaee44ba83413080012706597d6166386ead5cb46727e70cec1c9
MD5 7e84be3ac0d4d8cc51c2ca06565d135a
BLAKE2b-256 1c4b98e6b437d8f15f13b373655c8a32511a90308b1533d9c9925bf41ea1823c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page