使用google的cloud firestore无模式数据库创建模型类的包装器
firestore-model的Python项目详细描述
Firestore型号
快速简单地为google的cloud firestore无模式数据库创建模型类。
概述
本项目旨在为在新提供的python 3.7app engine环境中使用cloud firestore提供与ndb客户端库在python 2.7appengine中使用cloud datastore相似的便利性和实用性。
示例
import firestore_model
from google.firestore import firestore
from filestore_model import Model, Query
# initialize the database connection globally for Firestore Model
firestore_model.db = firestore.Client()
# Define a data structure for a User
@dataclass
class User(Model):
first_name:str
last_name:str
occupation:str
# Create a new user, pass save = True to automagically save the model object
u1 = User.make(
first_name='Sonic',
last_name='Brown',
occupation='circus dog'
save=True
)
# Fetch all users that match a given query
users = User.query([
('occupation', 'circus dog'),
('created', '>', 1540776978)
]
).get()
# Iterate through the results of the query
for u in users:
print(u.id, u.created, u.first_name, u.last_name, u.occupation)