Skip to content

Interacting with MongoDB using Python has never been more straightforward and empowering! πŸ’ͺ

License

Notifications You must be signed in to change notification settings

MajidRaimi/tymongo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

TyMongo πŸπŸ“¦

PyMongo + Pydantic = TyMongo 🀩

TyMongo is a Python package designed to simplify your interaction with MongoDB by leveraging the power of pymongo for database connectivity and pydantic for efficient modeling and type creation through classes.

Features

  • Seamless connection to MongoDB using pymongo. 🌐
  • Data modeling and type creation with pydantic classes. 🧱
  • Automatic data validation and type conversion. βœ…
  • Automatic data serialization and deserialization. πŸ“¦

Installation πŸ“₯

To install TyMongo, simply use pip:

pip install tymongo

Setup πŸ› οΈ

All you really need to do is to create .env file in your project root directory and add the following variables:

DATABASE_URL=your_database_url
DATABASE_NAME=your_database_name

And now you're ready to go! πŸš€

Usage πŸ“

Creating a Model

To create a model, simply inherit from TyMongoModel and define your fields:

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

    class Config:
        collection = "users"
  • Will work 100% like BaseModel from pydantic, but with some extra features. 🀩
  • Make sure you define inner class Config and set collection to the name of the collection you want to use in your database. πŸ“
  • You don't have to define _id field, it will be automatically created for you. πŸ†”

Creating a User

user = User(name="Majid", age=21)
# or
user_dict = {"name": "Majid", "age": 21}
user = User(**user_dict)
  • You can pass any value to any field, it will be automatically converted to the type you defined. πŸ”„
  • You can also pass a dictionary to the model, it will be automatically converted to the model. πŸ”„

Saving a User

user.save()

Now you have a new user in your database! πŸŽ‰

Editing a User

user.age = 22
user.save()

Now your user's age is 22 in your database! πŸŽ‰

Deleting a User

user.delete()

Now your user is deleted from your database! πŸŽ‰

Extra Features 🀩

Querying & Fields Validation

query = {"age": 22}
users = User.find(query)

Just like pymongo, you can pass a query to find method and it will return a list of all the users that match the query as a list of models, just make sure the dictionary keys match the model fields. πŸ”‘

query = {
    'x': 'some value',
    'age': 21,
}
users = User.find(query)

This will raise an error because x is not a field in the model. ❌

Get a Single Document by ID

user = User.get("xxxxxxxxxxxxxxxxxxxxxxxx")

Get a Single Document by Query

q = {
    'name' : 'Majid'
}
user = User.find_one(q)

What if I want to use pymongo directly? πŸ€”

You can access the collection object directly from the model:

collection = User.__collection__()
users = collection.find({"age": 22})

License

TyMongo is licensed under the terms of the MIT License.

Contributing 🀝

Contact πŸ“§

Acknowledgements πŸ™

Thank you for using TyMongo! πŸ™Œ happy coding! πŸš€

About

Interacting with MongoDB using Python has never been more straightforward and empowering! πŸ’ͺ

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages