Skip to main content

Asynchronous MongoDB Driver Wrapper For Rust MongoDB crate

Project description

Ruson-ODM

Ruson-ODM is an asynchronous object-document mapper for Python 3.6+. It is designed to be a lightweight and easy-to-use ODM for MongoDB.

Usage

Using Ruson-ODM is easy and requires only two setup steps: setup and inheritance. After these two steps are complete, you can start using Ruson-ODM to query, insert, update and deleted documents from the database.

Inheriting RusonDoc

RusonDoc is the interface that your classes will use to interact with the ODM. To use it, you must create a class that inherits from it.

import asyncio
from ruson.core.config import Config
from ruson.core.instance import Ruson
from ruson.core.ruson_doc import RusonDoc


class User(RusonDoc):
    email: str

Note that subclasses can override super class attributes like the "id" field.

Setup

To use the ruson ODM, you need to first create a Config instance with the necessary parameters for your database connection (connection_name is optional). With the config object created, you can then setup a Ruson connection using the singleton Ruson.

async def setup_ruson():
    config = Config(database_uri="mongodb://localhost:27017", database_name="test", connection_name="default")
    await Ruson.create_connection(config)

Querying the database

Once the Ruson connection is setup, you can start querying the database. Your classes that inherited from RusonDoc can now use the find, find_one and find_many methods to query the database.

By default, Ruson will not format your data. You can use the formatter parameter to pass a callable to format the response from the database.

async def formatter(value: dict):
    # This function does not need to be async, it is just an
    # example to show that async functions are also supported
    return value["email"]


async def find():
    email = "test@example.com"

    # Return iterator over matched users
    users = await User(email=email).find(many=True)

    # Consumes iterator and transform into a list
    users_list = await users.tolist()

    print("Find method")
    print(f"Users found with email: '{email}'", len(users_list))
    print("Users", list(users_list[0].items()))
    print()


async def find_one():
    user_email = await User.find_one({"email": "test@example.com"}, formatter=formatter)
    print("Find one method")
    print("User email", user_email)
    print()


async def find_many():
    users_emails = await User.find_many(formatter=formatter)

    print("Find many method")
    # Iteration asynchronously through the results
    async for email in users_emails:
        print(email)

    print()

Inserting into the database

To insert a document into the database, you can use the insert_one method either with an instance or a class.

async def insert_one():
    print("Insert one method")

    # Insert using class method
    user = User(email="test@example.com")
    result = await User.insert_one(user)
    print("Inserted id", result.inserted_id)

    # Insert using instance method
    another = User(email="another@example.com")
    result = await another.insert()
    print("Inserted id", result.inserted_id)

    print()

To insert multiple documents into the database, you can use the insert_many method either with a class.

async def insert_many():
    users = [
        User(email="test1@example.com"),
        User(email="test2@example.com"),
        User(email="test3@example.com"),
        User(email="test4@example.com"),
    ]
    result = await User.insert_many(users)

    print("Insert many method")
    print("Inserted ids", list(map(str, result.inserted_ids)))
    print()

Updating records in the database

To update a record in the database, you can use the update_one method. This method takes two parameters, the filter and the update. The filter is used to find the document to update and the update is the data to update the document with. It can be used either with the class method or the instance method.

async def update_one():
    result = await User.update_one(
        {"$set": {"email": "updated@example.com"}}, {"email": "test1@example.com"}
    )

    print("Update one method")
    print("Matched count", result.matched_count)
    print("Modified count", result.modified_count)
    print()

Deleting records from the database

To delete a record from the database, you can use the delete_one method. This method takes a filter as a parameter and can be used either with the class method or the instance method.

async def delete():
    # Delete only the first match using "many=False"
    result = await User(email="test4@example.com").delete(many=False)

    print("Delete method")
    print("Deleted count", result.deleted_count)
    print()


async def delete_one():
    # Delete class method
    result = await User.delete_one({"email": "test3@example.com"})

    print("Delete one method")
    print("Deleted count", result.deleted_count)
    print()

It is also possible to delete multiple records from the database using the delete_many method. This method takes a filter as a parameter and can be used either with the class method or the instance method delete combined with the flag many=True.

async def delete_many():
    # Delete many using class method
    result = await User.delete_many({"email": {"$regex": "^test"}})

    print("Delete many method")
    print("Deleted count", result.deleted_count)
    print()

To run the examples

async def main():
    await setup_ruson()
    await insert_one()
    await insert_many()
    await find()
    await find_one()
    await find_many()
    await update_one()
    await delete()
    await delete_one()
    await delete_many()


if __name__ == "__main__":
    asyncio.run(main())

Other supported methods

Ruson

  • get_client
  • get_config
  • create_session

Client

  • database
  • default_database
  • list_databases
  • create_session
  • shutdown

Database

  • collection
  • list_collections
  • drop

Collection

  • upsert
  • count_documents
  • distinct
  • create_index
  • list_indexes
  • drop_indexes
  • drop

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

ruson-1.0.1.tar.gz (40.2 kB view details)

Uploaded Source

Built Distributions

ruson-1.0.1-cp312-none-win_amd64.whl (4.9 MB view details)

Uploaded CPython 3.12 Windows x86-64

ruson-1.0.1-cp312-none-win32.whl (4.2 MB view details)

Uploaded CPython 3.12 Windows x86

ruson-1.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.1 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

ruson-1.0.1-cp312-cp312-manylinux_2_12_i686.manylinux2010_i686.whl (7.5 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.12+ i686

ruson-1.0.1-cp312-cp312-macosx_11_0_arm64.whl (4.9 MB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

ruson-1.0.1-cp312-cp312-macosx_10_7_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.12 macOS 10.7+ x86-64

ruson-1.0.1-cp311-none-win_amd64.whl (4.9 MB view details)

Uploaded CPython 3.11 Windows x86-64

ruson-1.0.1-cp311-none-win32.whl (4.2 MB view details)

Uploaded CPython 3.11 Windows x86

ruson-1.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.1 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

ruson-1.0.1-cp311-cp311-manylinux_2_12_i686.manylinux2010_i686.whl (7.5 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.12+ i686

ruson-1.0.1-cp311-cp311-macosx_11_0_arm64.whl (4.9 MB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

ruson-1.0.1-cp311-cp311-macosx_10_7_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.11 macOS 10.7+ x86-64

File details

Details for the file ruson-1.0.1.tar.gz.

File metadata

  • Download URL: ruson-1.0.1.tar.gz
  • Upload date:
  • Size: 40.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.3.2

File hashes

Hashes for ruson-1.0.1.tar.gz
Algorithm Hash digest
SHA256 74a2014f68bda0652839fe83e77727e6ae7c49706a33fd8e73607b013d4a68ea
MD5 dd3bc0ed54eedd6828fb712fc370002f
BLAKE2b-256 de38a1317c9ffd85fe6fd986cbf366c89185e0851ade00dc52a0c2ea33558c12

See more details on using hashes here.

File details

Details for the file ruson-1.0.1-cp312-none-win_amd64.whl.

File metadata

  • Download URL: ruson-1.0.1-cp312-none-win_amd64.whl
  • Upload date:
  • Size: 4.9 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.3.2

File hashes

Hashes for ruson-1.0.1-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 25938ffaacc9a231cb932c546f176e08f80ffd4fab3f655bc3106181711cb332
MD5 53e2043146ef88cc1bad38d264937296
BLAKE2b-256 49ad3d18ac353185f6a9102ad7567b518788ae58f2ac482bcd560eebbc9d075e

See more details on using hashes here.

File details

Details for the file ruson-1.0.1-cp312-none-win32.whl.

File metadata

  • Download URL: ruson-1.0.1-cp312-none-win32.whl
  • Upload date:
  • Size: 4.2 MB
  • Tags: CPython 3.12, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.3.2

File hashes

Hashes for ruson-1.0.1-cp312-none-win32.whl
Algorithm Hash digest
SHA256 f23fd862b654b3e4703d17d255ab96c650275b59be65275253b2ed3b445cc319
MD5 e115b7f612ce05fd739eeb399cc43676
BLAKE2b-256 5c5647af0dc649250aa24f8a2a4f9efd75caf99e6a8da02d65ac2180e8503993

See more details on using hashes here.

File details

Details for the file ruson-1.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ruson-1.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2017f7591d7b512983f0503763c6861a094aa15703e80abddf0aa6943ad79701
MD5 2421fd96517840293ef2bf8cb028ed11
BLAKE2b-256 a5b37e75992af3cb7094c06bc7f32f69c7248812cf42f5a451676aa4bcd25933

See more details on using hashes here.

File details

Details for the file ruson-1.0.1-cp312-cp312-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for ruson-1.0.1-cp312-cp312-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 da25ca9b31563337d7d98721c8167899639ba7423b5f90c6a38a453a520e9fdc
MD5 4597957d1edc96f72b3d14d866e174d8
BLAKE2b-256 e96714cdcdde0aa15f73bc35551ab65280df2975385c1b9e685e55c7f69e8262

See more details on using hashes here.

File details

Details for the file ruson-1.0.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ruson-1.0.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 33ff54df9047ed2f1074be4651f7af7b2ad4825c87692c0ce24984f671afc399
MD5 61f490905a1001708770adb0766775f3
BLAKE2b-256 64192c01df3df781a7dba120d046a88af53657b3c12d2b8d159e62e094f96bd9

See more details on using hashes here.

File details

Details for the file ruson-1.0.1-cp312-cp312-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for ruson-1.0.1-cp312-cp312-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 79cd0420c4861ccc6a61058788da723153f60238e5e27cc620d467cd96877277
MD5 75cb2326e894761dc211742db5f55268
BLAKE2b-256 87a8323a1e0dcf5ed078c2951e87e06a02582d534d54734dbce0787f35a27058

See more details on using hashes here.

File details

Details for the file ruson-1.0.1-cp311-none-win_amd64.whl.

File metadata

  • Download URL: ruson-1.0.1-cp311-none-win_amd64.whl
  • Upload date:
  • Size: 4.9 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.3.2

File hashes

Hashes for ruson-1.0.1-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 12cdca310451ff0603e4f8056a5811132bb109e64e3d739031388461ba9045d9
MD5 bfedb1dbf3a870edeb47b461a8f17395
BLAKE2b-256 0361d2af36477fdbf662fbcc38577631f9f77da2b586c8db9442c64ea2c4c9ab

See more details on using hashes here.

File details

Details for the file ruson-1.0.1-cp311-none-win32.whl.

File metadata

  • Download URL: ruson-1.0.1-cp311-none-win32.whl
  • Upload date:
  • Size: 4.2 MB
  • Tags: CPython 3.11, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.3.2

File hashes

Hashes for ruson-1.0.1-cp311-none-win32.whl
Algorithm Hash digest
SHA256 9bc510a9f5055f59d8d316478fffd8e67e04d94af64d3170df5edca7d1a4d9af
MD5 a2bf215ddf5fc7a4948c8d2b441fc5e6
BLAKE2b-256 2c05c1a5f3e1f4bb9d64753661b727a350e16c0f85ad5861bd0c9cdf8f8745e3

See more details on using hashes here.

File details

Details for the file ruson-1.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ruson-1.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 33f62d1eeb72f68bca8fc65fca9676b46eb89579a93ac451d9097411a4413c52
MD5 19a6caa2e50fa62075b2ff18bec3f75f
BLAKE2b-256 3a2e5dafbe5d4b4e24bdfca772fd6a536c63b3b55b85de966e4219df27cfdb1e

See more details on using hashes here.

File details

Details for the file ruson-1.0.1-cp311-cp311-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for ruson-1.0.1-cp311-cp311-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 062d8bcb94b2ef9dd64d896775e9073dc55d7e3466bdc337e390a8d0f02a82a3
MD5 612ab6c40c1df7b8c7c73a26fd08654e
BLAKE2b-256 7eb875f4c36f9699bdfee3304e0232c6327d0ca626cb26ca03e63bae356af09a

See more details on using hashes here.

File details

Details for the file ruson-1.0.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ruson-1.0.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bb26549c1ea3855fe7317900d5f410f172fd8db8e9b4c0f1551c42de0da4e6f2
MD5 fb33bcfae6f957a9357c1a5495379f2e
BLAKE2b-256 62fc849d8412c3bb575943bf8672b3ffed962003e589edb53cfcec2816004e32

See more details on using hashes here.

File details

Details for the file ruson-1.0.1-cp311-cp311-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for ruson-1.0.1-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 67b84f12ad35603a92177964745eb65fa75a6b8eaeb87a4cdca0c63ad6ceec61
MD5 8179ab1d4db90428561b8f61d83b979e
BLAKE2b-256 284f1190cefebdb1a73e8391a26944bc7a8a282f3bc96ae531629a3fad88ae67

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