Skip to main content

mangODM - simple async ODM for mongodb.

Project description

MANGO - simple async ODM for MongoDB.

User Guide

Installation

You can install mangodm via pip.

pip install mangodm

Initialization

Firstly need to connect to MongoDB.

from mangodm import connect_to_mongo

MONGODB_CONNECTION_URL = ""
DATABASE_NAME = ""

async def main():
	await connect_to_mongo(MONGODB_CONNECTION_URL, DATABASE_NAME)

Don't forgot to close connection.

from mangodm import connect_to_mongo, close_connection

MONGODB_CONNECTION_URL = ""
DATABASE_NAME = ""

def befor_down():
	close_connection()

  

async def main():
	await connect_to_mongo(MONGODB_CONNECTION_URL, DATABASE_NAME)

Defining Collection

Inherit Document class and describe its fields to defining new collection.

from mangodm import Document

class User(Document):
	name: str
	age: int

Before start to use collection you need to register it.

from mangodm import Document

class User(Document):
	name: str
	age: int

User.register_collection() # IMPORTANT

Also you can add special configuration to collection. To do this add subclass Config.

Config Parameters

  • collection_name - collection name in mongoDB.
  • excludeFields - list of names fields which don't saving in DB.
  • excludeFieldsResponse - list of names fields which don't include in response.
from mangodm import Document

class User(Document):
	name: str
	password: str
	age: int
	points: int

	class Config:
		collection_name = "users"
		excludeFields = ["points"]
		excludeFieldsResponse: ["password"]

User.register_collection()

Create new document

For create new document just create new object of collection class and call create() method from it.

from mangodm import Document

class User(Document):
	name: str
	age: int

User.register_collection()

async def main():
	mike = User(name="Mike", age=23)
	await mike.create()

After creating you can get document id. ID is str type.

async def main():
	mike = User(name="Mike", age=23)
	await mike.create()
	print(mike.id)

Get documents from MongoDB

To get one document use method - get. Parameters for this method is filter for finding document. Parameter convert to JSON and put to MongoDB method find_one.

Method return class object or none if querying document doesn't exist.

from mangodm import Document

class User(Document):
	name: str
	age: int

User.register_collection()

async def main():
	mike = await User.get(name="Mike")
		if mike:
			print(mike.id)
	
	anna = await User.get(id="6561a3851492b7e9d9a9533e")
	if anna:
		print(anna.age)

To get all documets which suitable for finding filter use method - find. This method has same paramrtrs how method get. find method return list of class objects, list can be empty.

from mangodm import Document

class User(Document):
	name: str
	age: int

User.register_collection()

async def main():
	users = await User.find() # get all documents if no parametrs
	for user in users:
		print(user.id)

Updating document

To update editing use method update.

from mangodm import Document

class User(Document):
	name: str
	age: int
	
User.register_collection()

async def main():
	mike = await User.get(name="Mike")
	if mike:
		print(mike.id)
		mike.age = 24
		await mike.update()

Deleting document

To delete document use method delete.

from mangodm import Document

class User(Document):
	name: str
	age: int

User.register_collection()

async def main():
	mike = await User.get(name="Mike")
	if mike:
		await mike.delete()

Response documents

You can convert your document to JSON. For this use method to_response().

from mangodm import Document

class User(Document):
	name: str
	age: int

User.register_collection()

async def main():
	mike = await User.get(name="Mike")
	if mike:
		print(mike.to_response())

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

mangodm-0.1.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mangodm-0.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file mangodm-0.1.1.tar.gz.

File metadata

  • Download URL: mangodm-0.1.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for mangodm-0.1.1.tar.gz
Algorithm Hash digest
SHA256 79ce28b6b04f7ec4a519a67b47b2f88ab9f3bfd447188e17151673d7b4c0b072
MD5 4cde672c3eeeb01d6fed1b26c45fb35f
BLAKE2b-256 ec464ec5aa76c8f49731fea49d4fb5f957c930099e1fe50380c2c0d9f84cee29

See more details on using hashes here.

File details

Details for the file mangodm-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mangodm-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for mangodm-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 99dcdd34a2d5c16d08f617e3da51517575f9613024b8594f05f6188f50855187
MD5 ad4252edbc0f5f4f32963b313ccfcfa3
BLAKE2b-256 9b47880112f476fb44562bb8b2f0b7fd19fa5109415094d616e5a487461a4e03

See more details on using hashes here.

Supported by

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