Skip to main content

A simple MongoDB ORM that support types

Project description

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: (for now, only from github)

pip install git+https://github.com/MajidRaimi/tymongo.git/dist/tymongo-0.0.1-py3-none-any.whl

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

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

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(q)

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! 🚀

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

tymongo-0.0.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

tymongo-0.0.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file tymongo-0.0.1.tar.gz.

File metadata

  • Download URL: tymongo-0.0.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for tymongo-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c1de1792196a3e97e0b6b414c8a323ed73d6d705f1ea147c893e25fcdf4b3c0d
MD5 f5d721c248687e35d2bd3018d6536e74
BLAKE2b-256 53de5b0ea15b18c87b73ff99472c71bfa89dd5a46cd3768c553db59128e4e085

See more details on using hashes here.

File details

Details for the file tymongo-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: tymongo-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for tymongo-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ea93930ab94be53d898478e9209019b1d879f403d17eb948050e1eda7b05768e
MD5 3b9dd19f68af4088c490be63ece70bf5
BLAKE2b-256 1e61c6d3bcb563945b2da7f218fb2dd21b8c5b01e5c51b92f190a2559b23a93e

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