Asynchronous MongoDB Driver Wrapper For Rust MongoDB crate
Project description
Ruson-ODM
Ruson-ODM is an asynchronous object-document mapper for Python 3.11+. It is designed to be a lightweight and easy-to-use ODM for MongoDB.
Installation
Ruson-ODM is available to be installed from PyPi with:
pip install ruson
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
Built Distributions
File details
Details for the file ruson-1.0.6.tar.gz
.
File metadata
- Download URL: ruson-1.0.6.tar.gz
- Upload date:
- Size: 42.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75021b099bc425c96f0e5713620b80843fc653df89de8165c6bc59d3059eb2a6 |
|
MD5 | 57c89e0145ffeb1b66c15fd00120fcdc |
|
BLAKE2b-256 | 4527278a1218492809cfaf3894ab8ba84f6db466592f1603c7b0ff2cf292ec58 |
File details
Details for the file ruson-1.0.6-cp312-none-win_amd64.whl
.
File metadata
- Download URL: ruson-1.0.6-cp312-none-win_amd64.whl
- Upload date:
- Size: 4.7 MB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8c33caef2d326ba6423b22f747f5351be63183019ac621be994ded699007110 |
|
MD5 | 86bbedbe78ab32ada5593cac37a43e56 |
|
BLAKE2b-256 | 71a6fba05139ee79af4ac57bdc768bcdc489aa880c2f1050e3693e9682405568 |
File details
Details for the file ruson-1.0.6-cp312-none-win32.whl
.
File metadata
- Download URL: ruson-1.0.6-cp312-none-win32.whl
- Upload date:
- Size: 4.0 MB
- Tags: CPython 3.12, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01d40862c07e7ffd9305909a3af8d2dc019f01d3779edacff02767f60191d631 |
|
MD5 | e3e8c472b5a1cde33dd099baae3d69e5 |
|
BLAKE2b-256 | 1cbfbcd8b5979c0eed3a320321286be442d880ff16d4e34fd6e9f7837a645566 |
File details
Details for the file ruson-1.0.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: ruson-1.0.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 6.5 MB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0af5a65e50a9825be8f005ab3c863e46894f11585ae666447fc1142e01add229 |
|
MD5 | c5de3ef0ae785971b2c870aa3bff5bb1 |
|
BLAKE2b-256 | deec4657102da797c3696e4db1800a2796e35c4f8940cdec27f78ab102f8fc75 |
File details
Details for the file ruson-1.0.6-cp312-cp312-manylinux_2_12_i686.manylinux2010_i686.whl
.
File metadata
- Download URL: ruson-1.0.6-cp312-cp312-manylinux_2_12_i686.manylinux2010_i686.whl
- Upload date:
- Size: 6.9 MB
- Tags: CPython 3.12, manylinux: glibc 2.12+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e89c497ba6d4de80da93ab4e95560eb9b30b9e430e447a7957ccd66a451813ae |
|
MD5 | 7890c707a35f5804c10b831d5198b6da |
|
BLAKE2b-256 | 1e75e87efa25694c2ceddea74dc90151dfbe3299fd830c8aca21d29cb9773874 |
File details
Details for the file ruson-1.0.6-cp312-cp312-macosx_11_0_arm64.whl
.
File metadata
- Download URL: ruson-1.0.6-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 4.7 MB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71f12fb7761fcfd0f39edc8811a27ceff033cbe15403db0b1ab8dd224efed2e4 |
|
MD5 | 16004bf67bd109f4a68c57852d7fb892 |
|
BLAKE2b-256 | 29a2019b46c3945c18c57d238c36fdcb00ce233a921500db054793a3ea673ff7 |
File details
Details for the file ruson-1.0.6-cp312-cp312-macosx_10_12_x86_64.whl
.
File metadata
- Download URL: ruson-1.0.6-cp312-cp312-macosx_10_12_x86_64.whl
- Upload date:
- Size: 5.0 MB
- Tags: CPython 3.12, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97e3e761db36a885ba191798ac32251939a73a4f9f5905deaf56eafd5264ec50 |
|
MD5 | f4f4d541c9ff23b3115d9fce70b1a579 |
|
BLAKE2b-256 | 90dad7a4419215d93cc539e98487716498e5952c7543c26c0a8c5c44236c5678 |
File details
Details for the file ruson-1.0.6-cp311-none-win_amd64.whl
.
File metadata
- Download URL: ruson-1.0.6-cp311-none-win_amd64.whl
- Upload date:
- Size: 4.8 MB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68fd36e2b98d6578b3f1e726e9fe0b9cfe9a17b0f7464f638048aa64b7a2f074 |
|
MD5 | a6c7ea04493dc882cca87b55f6ebc2a1 |
|
BLAKE2b-256 | b3a17e5bba5231ab4a17e06d50a9e969a6ebdfa183980600f5a4db28f4e5d353 |
File details
Details for the file ruson-1.0.6-cp311-none-win32.whl
.
File metadata
- Download URL: ruson-1.0.6-cp311-none-win32.whl
- Upload date:
- Size: 4.0 MB
- Tags: CPython 3.11, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72565836a3e23fb800fe83c32464c20610f9ddf6b2197c12352b70bfbab92793 |
|
MD5 | db7af58c5fabeb519bed2465b9fd5859 |
|
BLAKE2b-256 | 1a4a65f5416466757693a12da75aab0575880709804d9fd88acfa05246b061c9 |
File details
Details for the file ruson-1.0.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: ruson-1.0.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 6.5 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25f775c1d5e375995ccfc6e3cc5aef227dc2ae39a619483e5b8a8c99a74a898d |
|
MD5 | da65a72dd8c55380680700c37f31e68b |
|
BLAKE2b-256 | 1c274e996826ee6f9c994883832408d3aacb83c2b717ced362cff63ccd8b0850 |
File details
Details for the file ruson-1.0.6-cp311-cp311-manylinux_2_12_i686.manylinux2010_i686.whl
.
File metadata
- Download URL: ruson-1.0.6-cp311-cp311-manylinux_2_12_i686.manylinux2010_i686.whl
- Upload date:
- Size: 6.9 MB
- Tags: CPython 3.11, manylinux: glibc 2.12+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d512b74dff984a3d50c86351d693d0c859c989cccce5503ed607a7bc6ba5cfee |
|
MD5 | 48d191327cf91863f881bc3c13697f78 |
|
BLAKE2b-256 | f445976dc9aeb468797e8daa4453a36eaf43e2881069bb0017cddf66bca58bfc |
File details
Details for the file ruson-1.0.6-cp311-cp311-macosx_11_0_arm64.whl
.
File metadata
- Download URL: ruson-1.0.6-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 4.7 MB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0452c9e310168c8b7252f2cc3109fc8f6179a50376bc3db7d0bdc047468090ad |
|
MD5 | dfb717b872c16112af7804b699e3e886 |
|
BLAKE2b-256 | c09074a1f29f591dfbbc94019e33426207ed67ba9b1ae6de5361373c7cf0657f |
File details
Details for the file ruson-1.0.6-cp311-cp311-macosx_10_12_x86_64.whl
.
File metadata
- Download URL: ruson-1.0.6-cp311-cp311-macosx_10_12_x86_64.whl
- Upload date:
- Size: 5.0 MB
- Tags: CPython 3.11, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba19e06f653fe647a960152663f94420a989219fdd93e47af2b6a619eedf449c |
|
MD5 | 2a05096fefa93d87c64bb9b574b6f586 |
|
BLAKE2b-256 | caee9006d1e7e0bba5ed112a51f4a85f3338059321e6a789b0341fb6720598bc |