Skip to main content

A basic ORM like interface for mongodb in python that uses dataclasses.

Project description

Mongoclass

A basic ORM like interface for mongodb in python that uses dataclasses.

Installation

To get started, install mongoclass using pip like so.

pip install -U mongoclass

Getting Started

This section will explain the basics of how to use mongoclass. After reading this, read the API Reference for more information.

from mongoclass import MongoClassClient
client = MongoClassClient("mongoclass", "localhost:27017")

This will create a MongoClassClient instance that exposes the features of mongoclass. MongoClassClient inherits from pymongo.MongoClient so you can also use it like you'd normally use pymongo.

Schemas

To create a schema (or a preferred term, mongoclass), this is all you have to do.

from dataclasses import dataclass

@client.mongoclass()
@dataclass
class User:
    name: str
    email: str
    phone: int
    country: str = "US"

This creates a User mongoclass that belongs in the user collection inside the default database. To create an actual User object and have it be inserted in the database, create an instance of User and call the .insert() method like so

john = User("John Dee", "johndee@gmail.com", 5821)
insert_result = john.insert()

The first line creates the user John Dee with the provided information. Notice how we didn't need to provide a country, that is because country defaults to US.

The second line inserts it to the user collection in the default database and then returns a pymongo.InsertOneResult

As an alternative to having to call .insert(), you can pass _insert=True to User() which will automatically insert as soon as the object is initialized. You do loose the ability to receive the pymongo.InsertOneResult

For the remaining guide and full documentation, click here

LICENSE

MIT License

Copyright (c) 2022 Philippe Mathew

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

mongoclass-1.6.tar.gz (10.6 kB view details)

Uploaded Source

File details

Details for the file mongoclass-1.6.tar.gz.

File metadata

  • Download URL: mongoclass-1.6.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for mongoclass-1.6.tar.gz
Algorithm Hash digest
SHA256 5aeb1669d63d96548dc8cfa6f06edf0ac01cf141a62d83208f0f458188667330
MD5 5cefe0ee51dd85db94420f5d8db65e5e
BLAKE2b-256 9d6a2b705bead2d0155768a811cffca61c494fb5c55fac8a92b8bf494693b619

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