python fireo

pythonのfireoの概要と使い方

タグ fireo firebase


pythonのfireoの概要と使い方

概要

  • gcpのfirestoreをpythonから操作するためのライブラリ(ORM)
  • firestoreのnative modeのみサポート
  • 明示的にデータベース名を指定することもできる
  • レコードのキーは collection_name + id で決定される

インストール

$ pip install fireo

使い方

データベースの設定

  • 明示的にデータベースを指定しない場合は(default)が使用される
import fireo
from google.cloud import firestore

fireo.connection(client=firestore.Client(database="mydb"))

モデルの定義

from fireo.models import Model
from fireo.fields import IDField, DateTime, TextField

class Item(Model):
    class Meta:
        collection_name = "items"
    id: str = IDField() # type: ignore
    name: str = TextField() # type: ignore
    timestamp = DateTime(auto=True) # type: ignore

データの存在の確認

if Item.collection.get("id"):
    print("exist")
else:
    print("not exist")

データの追加

item = Item()
item.id = "id"
item.name = "test"
item.save()

データの取得

item = Item.collection.get("id")

データの更新

item = Item.collection.get("id")
item.name = "test2"
item.save(merge=True)

データの削除

item = Item.collection.delete("id")

クエリ

items = Item.collection.filter("name", "==", "test").fetch()

参考