Skip to main content

Firestore ORM for python.

Project description

FireORM

The easiest way to use Firestore with python.

Instalation

pip install fireorm

Example

from fireorm.Models import Model
from fireorm.Fields import TextField

class Salesman(Model):
    name = TextField()
    company = TextField()

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

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

Fields

There are 9 types of builtin fields, consistant with Firestore: BooleanField, DateField, ListField, MapField, NullField, ReferenceField, TextField, and NestedModel (which we'll get in a bit).

Each field takes the optional parameters default and required. If the field is not set, it will default to the value of default. If there is no default, the field is not set, and required == True, an Exception will be raised.

Fields Example

class Manager(Model):
	name = TextField(required=True)
	age = NumberField(required=True)
	company = TextField(required=True, default='Dunder Mifflin')
	startedWorkingAt = DateField()

m = Manager(name='Michael Scott') # you can pass in fields or set them later
m.age = 45
m.save() # Success! New doc in collection "manager" as: { name: Michael Scott, age: 45, company: Dunder Mifflin }

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

You can also add a NestedModel which lets you add a defined class as a Field.

NestedModel Example

class Dog(Model):
	age = NumberField()
	owner = Manager(required=True)

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

Collections

The collection name for a class defaults to the class' name in lowercase. To set the collection name, use the Meta class. You can also specify which fields print when printing the class.

Meta Example

class Student(Model):
	name = TextField()
	school = TextField(required=True, default='UPenn')

	class Meta:
		collection_name = 'students'
		fields_to_print = ['name']

s = Student(name='Amy Gutman')
s.save() # creates a new document in the "students" collection
print(s) # <*Student* key: students/9AJ5DeSvzfD04uqyhhpL, id: 9AJ5DeSvzfD04uqyhhpL, name: Amy Gutman>

You can also inheret classes.

Inheritance Example

class ExchangeStudent(Student):
	originalCountry = TextField(required=True)

	class Meta:
		collection_name = 'exchangeStudents'
		fields_to_print = None # when this is None or does not exist, it prints all fields. When it is [] it only prints the defaults (key and id).

e = ExchangeStudent(originalCountry='UK')
print(e.school) # UPenn
e.save()
print(e) # <*ExchangeStudent* key: exchangeStudents/XbGdMjo9x9166MvZ79Zr, id: XbGdMjo9x9166MvZ79Zr, name: None, originalCountry: UK, school: UPenn>

Queries

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

Queries Example

managers = Manager.collection.where('name', '==', 'Michael Scott').limit(1).stream()
print(managers) # [<*Manager* key: manager/Z8S75KU2n7QQnIm2cExy, id: Z8S75KU2n7QQnIm2cExy, age: 45, company: Dunder Mifflin, name: Michael Scott, startedWorkingAt: None>]

manager = Manager.collection.get('Z8S75KU2n7QQnIm2cExy')
print(manager) # <*Manager* key: manager/Z8S75KU2n7QQnIm2cExy, id: Z8S75KU2n7QQnIm2cExy, age: 45, company: Dunder Mifflin, name: Michael Scott, startedWorkingAt: None>

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

fireorm-0.0.7.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

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

fireorm-0.0.7-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file fireorm-0.0.7.tar.gz.

File metadata

  • Download URL: fireorm-0.0.7.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2

File hashes

Hashes for fireorm-0.0.7.tar.gz
Algorithm Hash digest
SHA256 a5a238bb7b7dea10a067139c94a96adeebbdb3e348ccc8acbdc96930cea7f540
MD5 9fe8c9e1c9f7d4e1e6d47ac9108a2db5
BLAKE2b-256 878b413e41bc56d9c14ef503f0dd508b1fef24ec204069f5eb0c07224f6715db

See more details on using hashes here.

File details

Details for the file fireorm-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: fireorm-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2

File hashes

Hashes for fireorm-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 778e43323769d3e8907e35a9468ef025487c59f50704f92ebb18c2d892021302
MD5 404703bde1e41dd0cc49c0ed14a75f58
BLAKE2b-256 f785309304461fde37256ed4d4fd45bb2b83ff8920f204ef3031dbe35a2e040d

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