def delete(self, entity: T, transaction: Transaction = None): collection_ref = self.client.collection(self.path) document_ref = collection_ref.document(entity.id) if transaction: transaction.delete(document_ref) else: document_ref.delete()
def delete(self, collection_path: str, id: str, transaction: Transaction = None): collection_ref = self.client.collection(collection_path) document_ref = collection_ref.document(id) if transaction: transaction.delete(document_ref) else: document_ref.delete()
def create_tr(transaction: Transaction, word_create: models.WordCreate, tags: List[models.TagAct], taught: str, collect_word: str, collect_unknown: str, collect_session: str): """ 新規単語の追加で呼び出すトランザクション処理 """ # unknownsコレクションにある場合は削除する docs = db.collection(collect_unknown).where("word", "==", word_create.word).stream() for doc in docs: transaction.delete(doc._reference) system_service.dec_unknown() set_like = 0 set_tags = [] set_tags_cnt = {} for tag in tags: set_tags.append(tag["text"]) set_tags_cnt[tag["text"]] = firestore.Increment(1) set_like = set_like + tag["pnt"] # wordsコレクションに追加 word_data = models.WordAll(**word_create.dict()).dict() word_data["taught"] = taught word_data["like"] = set_like word_data["tags"] = set_tags word_data["tags_cnt"] = set_tags_cnt word_data["cnt"] = firestore.Increment(1) word_data["index"] = system_service.get_system_data()["word_cnt"] word_ref = db.collection(collect_word).document() transaction.set(word_ref, word_data) # sessionsコレクション更新 session_ref = db.collection(collect_session).document(document_id=taught) session_data = { "teach_words": firestore.ArrayUnion([word_create.word]), "teach_refs": firestore.ArrayUnion([word_ref]), "teach_cnt": firestore.Increment(1), } transaction.set(session_ref, session_data, merge=True) return word_ref