Skip to main content

MongoDB with μMongo ODM support for Sanic framework

Features

  • Uses motor_asyncio package for async queries to MongoDB

  • Good integrated with uMongo ODM, so that you can use it easily in your projects

Installation

This package should be installed using pip:

pip install sanic-mongodb-extension

Example

#!/usr/bin/env python3
from sanic import Sanic, response
from sanic_mongodb_ext import MongoDbExtension
from umongo import Instance, Document, MotorAsyncIOInstance
from umongo.fields import StringField


app = Sanic(__name__)
# Configuration for MongoDB and uMongo
app.config.update({
    "MONGODB_DATABASE": "app", # Make ensure that the `app` database is really exists
    "MONGODB_URI": "mongodb://root:root@mongodb:27017",
    # You can also specify custom connection options.
    # For more details check the official docs: https://api.mongodb.com/python/3.7.0/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient
    "MONGODB_CONNECT_OPTIONS": {
        "minPoolSize": 10,
        "maxPoolSize": 50,
    },
    "LAZY_UMONGO": MotorAsyncIOInstance(),
})
# uMongo client is available as `app.mongodb` or `app.extensions['mongodb']`.
# The lazy client will be available as `app.lazy_mongodb` only when the database was specified,
# and which is a great choice for the structured projects.
MongoDbExtension(app)


# Describe the model
@app.lazy_umongo.register
class Artist(Document):
    name = StringField(required=True, allow_none=False)


# And use it later for APIs
@app.route("/")
async def handle(request):
    artist = Artist(name="A new rockstar!")
    await artist.commit()
    return response.json(artist.dump())


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

License

The sanic-mongodb-extension is published under BSD license. For more details read LICENSE file.

Real project examples

Open Matchmaking project:

Metadata

Release files for sanic-mongodb-extension 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for sanic-mongodb-extension 0.6.0
File Interpreter ABI Platform
sanic_mongodb_extension-0.6.0-py3-none-any.whl Python 3 none any Details

Release files / sanic_mongodb_extension-0.6.0-py3-none-any.whl

Download URL sanic_mongodb_extension-0.6.0-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
883e622757b53402acd0e3d33600e0c6e670f3e9048efca631626851c608ae95
BLAKE2b-256 checksum
How to use checksums
9f34e676bbb45488a4d215eddfb820d32a3c7be2811f12e8d9ffd85ed42b5ba0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release history Release notifications | RSS feed

This release

0.6.0 This release

1 release file

0.5.0

1 release file

0.4.0

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page