# 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:
```python
# 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
```python
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](http://mypy-lang.org/) will accept!
#### Fields Example
```python
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
```python
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
```python
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
```python
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
```python
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
```
Raw data
{
"_id": null,
"home_page": "https://github.com/jerber/magicdb_new",
"name": "magicdb",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "firestore,pydantic,orm,python orm firestore,firebase",
"author": "Jeremy Berman",
"author_email": "jerber@sas.upenn.edu",
"download_url": "https://files.pythonhosted.org/packages/c6/0a/546b10040ca97b342158b29621ff4eeec70de194fcbe3443b24acffd570b/magicdb-0.2.115.tar.gz",
"platform": "",
"description": "# MagicDB\nA fully typed Firestore ORM for python -- the easiest way to store data.\n\nMagicDB inherets from Pydantic, so you get all the power of Pydantic models with the functionality of Firestore: https://pydantic-docs.helpmanual.io/.\n\n## Instalation\n```\npip install magicdb\n```\n\n## Initialize the DB\nMagicDB is initialized via a Firestore service account json which you download from your Firebase console.\nOnce 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:\n\n```python\n# 1)\n# You can set the env variable from the terminal too: export GOOGLE_APPLICATION_CREDENTIALS=\"path/to/my-service-account.json\"\nimport os\nos.environ[\"GOOGLE_APPLICATION_CREDENTIALS\"] = \"path/to/my-service-account.json\"\n\n# OR\n\n# 2)\nimport magicdb\nmagicdb.connect(from_file=\"path/to/my-service-account.json\")\n```\n\n## Example\n```python\nfrom magicdb.Models import MagicModel\n\nclass Salesman(MagicModel):\n name: str = None\n company: str = None\n\ns = Salesman()\ns.name = 'Jim'\ns.save()\n\n# Get Salesman\ns = Salesman.collection.get(s.id)\nprint(s.name) # Jim\n```\n\n## Fields\nUse any type [mypy](http://mypy-lang.org/) will accept!\n\n#### Fields Example\n```python\nfrom datetime import datetime\n\nclass Manager(MagicModel):\n\tname: str\n\tage: int\n\tcompany: str = 'Dunder Mifflin'\n\tstartedWorkingAt: datetime = None\n\n# m = Manager(name='Michael Scott', age=44) # you must pass in the required fields on initializing the object.\nm.age = 45\nm.save() # Success! New doc in collection \"manager\" as: { name: Michael Scott, age: 45, company: Dunder Mifflin }\n\nm = Manager(name='Dwight Schrute') # Exception since age is required but not given\n```\n\nYou can also add other Objects as a field.\n\n### NestedModel Example\n```python\nclass Dog(MagicModel):\n\tage: int\n\towner: Manager\n\ndog = Dog()\ndog.age = 3\ndog.owner = Manager(name='Robert California', age=59)\ndog.save()\nprint(dog)\n\n```\n\n\n## Collections\nThe collection name for a class defaults to the class' name in lowercase. To set the collection name, use the `Meta` class.\n\n### Meta Example\n\n```python\nclass Student(MagicModel):\n\tname: str = None\n\tschool: str = 'UPenn'\n\n\tclass Meta:\n\t\tcollection_name = 'students'\n\n\ns = Student(name='Amy Gutman')\ns.save() # creates a new document in the \"students\" collection\nprint(s) # name='Amy Gutman' school='UPenn'\n```\n\nYou can also inheret classes.\n\n### Inheritance Example\n```python\nclass ExchangeStudent(Student):\n\toriginalCountry: str\n\n\tclass Meta:\n\t\tcollection_name = 'exchangeStudents'\n\ne = ExchangeStudent(originalCountry='UK')\nprint(e.school) # UPenn\ne.save()\nprint(e) # name=None school='UPenn' originalCountry='UK'\n```\n\n## Queries\nYou can make queries with the same syntax you would using the Python firebase-admin SDK. But FireORM returns the objects.\n\n### Queries Example\n```python\n\ne = ExchangeStudent(originalCountry='UK')\nprint(e.school) # UPenn\ne.save()\nprint(e) # name=None school='UPenn' originalCountry='UK'\n\nmanagers = Manager.collection.where('name', '==', 'Michael Scott').limit(1).stream()\nprint(managers) # [Manager(name='Michael Scott', age=45, company='Dunder Mifflin', startedWorkingAt=None)]\nprint(managers[0].id)\nmanager = Manager.collection.get('0mIWZ8FfgQzBanCllqsV')\nprint(manager) # name='Michael Scott' age=45 company='Dunder Mifflin' startedWorkingAt=None\n```\n",
"bugtrack_url": null,
"license": "",
"summary": "",
"version": "0.2.115",
"split_keywords": [
"firestore",
"pydantic",
"orm",
"python orm firestore",
"firebase"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "07f70e7294c5c2abc8024d0e47c25640",
"sha256": "0b6bafbf4bc0def7b276416e224048b2d062a2b89ca5c795c99c9493cdf0f58c"
},
"downloads": -1,
"filename": "magicdb-0.2.115-py3-none-any.whl",
"has_sig": false,
"md5_digest": "07f70e7294c5c2abc8024d0e47c25640",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 27314,
"upload_time": "2021-01-25T01:57:18",
"upload_time_iso_8601": "2021-01-25T01:57:18.141134Z",
"url": "https://files.pythonhosted.org/packages/37/fd/9bd27c329dca2b42709b6bc82e8098175c82551cf7131724663393c2e905/magicdb-0.2.115-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "987c4de3fdff75dcf9247ff49dc0cc99",
"sha256": "d0fd32480f2f68c0305d6f854c61f24904fde6c5ce7d542863f94e2449c06e6a"
},
"downloads": -1,
"filename": "magicdb-0.2.115.tar.gz",
"has_sig": false,
"md5_digest": "987c4de3fdff75dcf9247ff49dc0cc99",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 18709,
"upload_time": "2021-01-25T01:57:19",
"upload_time_iso_8601": "2021-01-25T01:57:19.113337Z",
"url": "https://files.pythonhosted.org/packages/c6/0a/546b10040ca97b342158b29621ff4eeec70de194fcbe3443b24acffd570b/magicdb-0.2.115.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-01-25 01:57:19",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": null,
"github_project": "jerber",
"error": "Could not fetch GitHub repository",
"lcname": "magicdb"
}