| Crates.io | firestore-manager |
| lib.rs | firestore-manager |
| version | 0.1.0 |
| created_at | 2026-01-16 09:20:11.123192+00 |
| updated_at | 2026-01-16 09:20:11.123192+00 |
| description | A comprehensive Rust library for managing Google Cloud Firestore |
| homepage | |
| repository | https://github.com/yourusername/firestore-manager |
| max_upload_size | |
| id | 2048298 |
| size | 182,623 |
Rust library สำหรับจัดการ Google Cloud Firestore อย่างง่ายและมีประสิทธิภาพ พร้อมฟังก์ชันครบครันสำหรับการทำงานกับ Firestore
GOOGLE_APPLICATION_CREDENTIALS ให้ชี้ไปที่ JSON key fileexport GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-key.json"
เพิ่ม dependency ใน Cargo.toml:
[dependencies]
firestore-manager = "0.1.0"
tokio = { version = "1.35", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
use firestore_manager::FirestoreManager;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let manager = FirestoreManager::new("your-project-id").await?;
Ok(())
}
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct User {
name: String,
email: String,
age: u32,
}
let collections = manager.list_collections().await?;
for collection in collections {
println!("Collection: {}", collection);
}
let user = User {
name: "John Doe".to_string(),
email: "john@example.com".to_string(),
age: 30,
};
let doc_id = manager.add_document("users", None, &user).await?;
println!("Document ID: {}", doc_id);
let doc_id = manager.add_document("users", Some("user_123"), &user).await?;
let users: Vec<(String, User)> = manager.get_collection("users").await?;
for (id, user) in users {
println!("ID: {} -> Name: {}", id, user.name);
}
let user: User = manager.get_document("users", "user_123").await?;
println!("User: {:?}", user);
let updated_user = User {
name: "Jane Doe".to_string(),
email: "jane@example.com".to_string(),
age: 28,
};
manager.update_document("users", "user_123", &updated_user).await?;
use std::collections::HashMap;
let mut fields = HashMap::new();
fields.insert("email".to_string(), serde_json::json!("newemail@example.com"));
fields.insert("age".to_string(), serde_json::json!(31));
manager.update_fields("users", "user_123", fields).await?;
manager.delete_document("users", "user_123").await?;
ดู examples/basic_usage.rs สำหรับตัวอย่างการใช้งานแบบครบถ้วน:
# ตั้งค่า environment variables
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/key.json"
export GOOGLE_CLOUD_PROJECT="your-project-id"
# รัน example
cargo run --example basic_usage
firestore-manager/
├── Cargo.toml
├── README.md
├── src/
│ └── lib.rs # Main library code
└── examples/
└── basic_usage.rs # Usage examples
FirestoreManagernew(project_id: &str) -> Result<Self, FirestoreError>สร้าง instance ใหม่ของ FirestoreManager
list_collections() -> Result<Vec<String>, FirestoreError>แสดงรายชื่อ collections ทั้งหมดในระดับ root
get_collection<T>(collection_name: &str) -> Result<Vec<(String, T)>, FirestoreError>ดึงข้อมูลทั้งหมดจาก collection โดยคืนค่าเป็น Vec ของ (document_id, data)
add_document<T>(collection_name: &str, document_id: Option<&str>, data: &T) -> Result<String, FirestoreError>เพิ่ม document ใหม่ คืนค่า document ID
update_document<T>(collection_name: &str, document_id: &str, data: &T) -> Result<(), FirestoreError>อัพเดท document ทั้งหมด
update_fields(collection_name: &str, document_id: &str, fields: HashMap<String, Value>) -> Result<(), FirestoreError>อัพเดทเฉพาะ fields ที่ระบุ
delete_document(collection_name: &str, document_id: &str) -> Result<(), FirestoreError>ลบ document
get_document<T>(collection_name: &str, document_id: &str) -> Result<T, FirestoreError>ดึงข้อมูล document เฉพาะด้วย ID
Library ใช้ custom error type FirestoreError:
match manager.get_document::<User>("users", "user_123").await {
Ok(user) => println!("Found: {:?}", user),
Err(FirestoreError::DocumentNotFound(id)) => {
println!("Document {} not found", id);
}
Err(e) => println!("Error: {}", e),
}
Error types ที่รองรับ:
ConnectionError - ปัญหาการเชื่อมต่อCollectionNotFound - ไม่พบ collectionDocumentNotFound - ไม่พบ documentSerializationError - ปัญหาการ serialize/deserializeOperationFailed - การทำงานล้มเหลวLibrary รองรับการ authentication ผ่าน:
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account-key.json"
# Run tests
cargo test
# Run with logging
RUST_LOG=debug cargo test
# Run specific example
cargo run --example basic_usage
Serialize และ Deserialize เสมอResult และ handle errors อย่างเหมาะสมFirestoreManager instance เพียงครั้งเดียวและใช้ซ้ำtokio runtime สำหรับ async operationsContributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.
หากพบปัญหาหรือมีคำถาม:
examples/ directoryMade with ❤️ for the Rust community