Skip to main content

No project description provided

Project description

MagicDB

A fully typed Firestore ORM for python -- the easiest way to store data.

MagicDB inherets from Pydantic, so you get all the power of Pydantic models with the functionality of Firestore: https://pydantic-docs.helpmanual.io/.

Instalation

pip install magicdb

Initialize the DB

MagicDB is initialized via a Firestore service account json which you download from your Firebase console. Once you have the json, you must tell MagicDB where it is, either by 1) setting the GOOGLE_APPLICATION_CREDENTIALS environment variable to the json path, or by 2) calling magicdb.connect with the path:

# 1)
# You can set the env variable from the terminal too: export GOOGLE_APPLICATION_CREDENTIALS="path/to/my-service-account.json"
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/my-service-account.json"

# OR

# 2)
import magicdb
magicdb.connect(from_file="path/to/my-service-account.json")

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.2.49.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

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

magicdb-0.2.49-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file magicdb-0.2.49.tar.gz.

File metadata

  • Download URL: magicdb-0.2.49.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.2 Darwin/19.6.0

File hashes

Hashes for magicdb-0.2.49.tar.gz
Algorithm Hash digest
SHA256 770c0030c509e4714c1d3d80b12857ac6dcf834bfa6430524d527c121c13e22e
MD5 dc9cb3b249f5150e7a171c9c28dd2ed4
BLAKE2b-256 a481cd677f76c9b43551a3218ae2f8fa87ad953e2955c9b6cfa1b2e9a77feac0

See more details on using hashes here.

File details

Details for the file magicdb-0.2.49-py3-none-any.whl.

File metadata

  • Download URL: magicdb-0.2.49-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.2 Darwin/19.6.0

File hashes

Hashes for magicdb-0.2.49-py3-none-any.whl
Algorithm Hash digest
SHA256 d68f03d14074365a13b0fb659b6e7d726a6cb320ddd38de3e0e1699386a20eee
MD5 2f67428ee0196bf0627ceac368fe225e
BLAKE2b-256 825bcb3c529bde68c38aece4e093b5d8977efb3a306d4dd43c4c6ce9a76079ff

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