การติดตั้ง firebase lib สำหรับ Python3
หมายเหตุ: คำสั่งในการติดตั้ง lib ในบทความนี้ เป็นของระบบปฏิบัติการ Windows
พิมพ์คำสั่งนี้ใน command เพื่อ ติดตั้ง Firebase Admin SDK
โค้ด: เลือกทั้งหมด
pip install --upgrade firebase-admin
การตั้งค่า แอปPython3 ของเรา
เข้าไปที่ตั้งค่าใน project setting โดย setting(รูปฟันเฟือง) >> project setting
จากนั้น Service accounts >> Firebase Admin SDK >> Python >> Generate new private key ระวัง: ห้ามไม่ให้ไฟล์นี้ ออกสู่สาธารณะ
สร้างไฟล์ Python ขึ้นมา 1 ไฟล์ ชื่ออะไรก็ได้ แล้วใส่โค้ดตามนี้เลย แต่แก้ตรง path ตาม direactory ของตัวเอง (ถ้าให้แนะนำเอาไว้ที่เดียวกับไฟล์ python, path จะได้สั้นๆหน่อย copy ชื่อไฟล์เอา .json มาด้วย เอามาแทนที่ 'private-key.json' อย่าลืมใส่ ' ' ด้วย)
- เอาไว้ที่เดียวกับไฟล์ python
- copy ชื่อไฟล์เอา .json มาด้วย
- เอามาแทนที่ 'private-key.json' อย่าลืมใส่ ' ' ด้วย
โค้ด: เลือกทั้งหมด
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
# Use a service account
cred = credentials.Certificate('private-key.json') ## edit your path here
firebase_admin.initialize_app(cred)
db = firestore.client()
การกำหนดสิทธ์การเข้าถึงฐานข้อมูล
ให้เข้าไปที่ Cloud Firestore >> Rule แล้วแก้ให้เหมือนในรูป
โค้ดในรูป
โค้ด: เลือกทั้งหมด
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true ;
}
}
}
การอ่านข้อมูล จาก Cloud Firestore
โค้ดนี้อ่านข้อมูลจาก collection ชื่อ users แล้วเอามาวนลูป for เพื่อแปลงเป็นข้อมูลเป็น dictionary และ แสดงข้อมูล ทุกๆ record
โค้ด: เลือกทั้งหมด
users_ref = db.collection('users') # get the users's collection reference
docs = users_ref.stream() # from users's collection reference make it be Generators type
for doc in docs: # for each record from users's collection
print(f'{doc.id} => {doc.to_dict()}') # show document id & dictionary of all field for 1 record {field_name: value}
ตัวอย่างผลลัพธ์
สรุป
Firebase Cloud Firestore มีความยืดหยุ่นมาก แต่ถ้าออกแบบไม่ดี จะทำให้การอ่าน , ค้นหา, ลบ , เปลี่ยนแปลง ทำได้ยากกว่าเดิม
ไม่ควรสร้าง collection ซ้อน collection ชีวิตที่ง่ายจะกลายเป็นชีวิตที่ยากแทน
อ้างอิง
https://firebase.google.com/docs/firestore/quickstart#python
https://www.youtube.com/watch?v=N0j6Fe2vAK4&t=1928s
https://youtu.be/yylnC3dr_no
https://stories.sellsuki.co.th/cloud-firestore-%E0%B8%84%E0%B8%B8%E0%B8%93%E0%B8%84%E0%B9%88%E0%B8%B2%E0%B8%97%E0%B8%B5%E0%B9%88%E0%B8%84%E0%B8%B8%E0%B8%93%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%84%E0%B8%A7%E0%B8%A3-b471d9b926e3
https://www.mindphp.com/forums/viewtopic.php?p=233438#p233438
https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/3921-what-is-firebase-backend.html
https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2578-key-feature-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html
https://www.mindphp.com/forums/viewtopic.php?p=233325#p233325