模型概念在google cloud firestore上的实现
mantle-firestore的Python项目详细描述
地幔火库模型(β)
在google cloud firestore上实现模型概念。 它试图使开发人员与firestore的交互尽可能简单。
模型概念在google cloud firestore上的实现
在google cloud firestore上实现模型概念。 它试图使开发人员与firestore的交互尽可能简单。
pip install mantle-firestore
"""This code creates a model with some possible scenarios when working with db.FirestoreModelAssume a case of mantle.studio where a user can have- Single login- Be in multiple projects- Have an account in each project"""frommantle.firestoreimportModel,SERVER_TIMESTAMPfrommantleimportdbclassUser(Model):user_name=db.TextProperty(length=16,required=True)email=db.TextProperty(required=True)full_name=db.TextProperty(required=True)password=db.TextProperty(required=False)date_registered=db.DateTimeProperty(default=SERVER_TIMESTAMP)__sub_collection__="user_data"classProject(Model):name=db.TextProperty(required=True)logo=db.BytesProperty()defcreate_account(self,user,roles=None):# Since account is a sub-collection of an enterprise,# the parent enterprise must be providedifrolesisNone:roles=["admin"]account=Account(__parent__=self,user=user,roles=roles)account.put()returnaccountclassAccount(Model):__sub_collection__=Projectuser=db.ReferenceProperty(User)roles=db.ListProperty(field_type=db.TextProperty())date_added=db.DateTimeProperty(auto_add_now=True)last_updated=db.DateTimeProperty(auto_now=True)# Then we canuser=User(user_name="john",email="john@doe.fam",password="123456")user.full_name="John Doe"user.put()# Get an existing userjohn=User.get(user.id)# Query usersuser2=User.query().equal("email","jane@doe.fam")[0]users=User.query()for_userinusers:print(_user.email)