Skip to main content

Typed ORM for python.

Project description

MagicDB

The easiest way to store data.

Instalation

pip install magicdb

Example

from magicdb.Models import MagicModel

class Salesman(MagicModel):
    name: str = None
    company: str = None

s = Salesman()
s.name = 'Jim'
s.save()

# Get Salesman
s = Salesman.collection.get(s.id)
print(s.name) # Jim

Fields

Use any type mypy will accept!

Fields Example

from datetime import datetime

class Manager(MagicModel):
	name: str
	age: int
	company: str = 'Dunder Mifflin'
	startedWorkingAt: datetime = None

# m = Manager(name='Michael Scott', age=44)  # you must pass in the required fields on initializing the object.
m.age = 45
m.save()  # Success! New doc in collection "manager" as: { name: Michael Scott, age: 45, company: Dunder Mifflin }

m = Manager(name='Dwight Schrute') # Exception since age is required but not given

You can also add other Objects as a field.

NestedModel Example

class Dog(MagicModel):
	age: int
	owner: Manager

dog = Dog()
dog.age = 3
dog.owner = Manager(name='Robert California', age=59)
dog.save()
print(dog)

Collections

The collection name for a class defaults to the class' name in lowercase. To set the collection name, use the Meta class.

Meta Example

class Student(MagicModel):
	name: str = None
	school: str = 'UPenn'

	class Meta:
		collection_name = 'students'


s = Student(name='Amy Gutman')
s.save()  # creates a new document in the "students" collection
print(s)  # name='Amy Gutman' school='UPenn'

You can also inheret classes.

Inheritance Example

class ExchangeStudent(Student):
	originalCountry: str

	class Meta:
		collection_name = 'exchangeStudents'

e = ExchangeStudent(originalCountry='UK')
print(e.school)  # UPenn
e.save()
print(e)  # name=None school='UPenn' originalCountry='UK'

Queries

You can make queries with the same syntax you would using the Python firebase-admin SDK. But FireORM returns the objects.

Queries Example

e = ExchangeStudent(originalCountry='UK')
print(e.school)  # UPenn
e.save()
print(e)  # name=None school='UPenn' originalCountry='UK'

managers = Manager.collection.where('name', '==', 'Michael Scott').limit(1).stream()
print(managers) # [Manager(name='Michael Scott', age=45, company='Dunder Mifflin', startedWorkingAt=None)]
print(managers[0].id)
manager = Manager.collection.get('0mIWZ8FfgQzBanCllqsV')
print(manager) # name='Michael Scott' age=45 company='Dunder Mifflin' startedWorkingAt=None

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

magicdb-0.0.6.tar.gz (9.2 kB view hashes)

Uploaded Source

Built Distribution

magicdb-0.0.6-py3-none-any.whl (9.7 kB view hashes)

Uploaded Python 3

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