ã¯ããã«
Golang 㧠FireStore ä¸ã«ãã¼ã¿ãç»é²ããããæ¹ãåãããªãã£ãã®ã§ããã®å¯¾å¿ã¡ã¢ã§ãã
Â
äºåæºå
ï¼ï¼Firebase Project ãä½æãã
Firebase ã«ãã°ã¤ã³ãã¦ä»»æã®ããã¸ã§ã¯ããä½æãã¾ãã
firebase.google.com ãã®å¾ãå·¦ã¡ãã¥ã¼ããããã¹ã¦ã®ãããã¯ãã->ãCloud FIrestoreããé¸æã
ç»é¢ã®æ示ã«å¾ã£ã¦ãFIrestore ã使ç¨ããæºåãå®äºããã¾ãã
Â
ï¼ï¼Firebase Admin SDK ãã Firestore ã«ã¢ã¯ã»ã¹ã§ããããã«ç§å¯éµãç»é²ãã
ç¶ãã¦ãä¸è¨ã®ãµã³ãã«ã³ã¼ãã«ããããã«ãFirebase Admin SDK 㧠Firestore ã«ã¤ãªããããã«ç§å¯éµãç»é²ãã¦ããã¾ãã
Â
Google Cloud ã³ã³ã½ã¼ã«ã«ãã°ã¤ã³ãã¦ãããã¸ã§ã¯ãã¯ä¸è¨æé ã§ä½æããããã¸ã§ã¯ããé¸æãã¾ãã
ãµã¼ãã¹ã¢ã«ã¦ã³ããã¼ã¸ã«ã¢ã¯ã»ã¹ããã¨ãããã©ã«ã㧠firebase-adminsdk ã®ãµã¼ãã¹ã¢ã«ã¦ã³ããç»é²ããã¦ããã¨æãã®ã§ããã®ã¢ã«ã¦ã³ããé¸æ -> ãã¼ -> éµã追å ã¨é²ãã§ç§å¯éµãä½æãã¾ãã
ç»é¢ã®æ示ã«å¾ã£ã¦ãæ¨å¥¨ããã¦ããJSON å½¢å¼ã§ä½æããã¨JSONãã¡ã¤ã«ããã¦ã³ãã¼ãããã¾ãã
Â
å®è£
ããã¾ã§ã§äºåæºåãå®äºããã®ã§ãå®è£ ãé²ãã¦ããã¾ãã
firestore.go
package db import ( "context" "log" "os" "sync" "cloud.google.com/go/firestore" firebase "firebase.google.com/go" "google.golang.org/api/option" ) var ( clientInstance *firestore.Client clientOnce sync.Once closeOnce sync.Once ) func InitializeFirestoreClient() { clientOnce.Do(func() { ctx := context.Background() sa := option.WithCredentialsFile(os.Getenv("GCLOUD_CREDENTIAL_FILE_PATH")) app, err := firebase.NewApp(ctx, nil, sa) if err != nil { log.Fatalf("error initilizing app: %v", err) } clientInstance, err = app.Firestore(ctx) if err != nil { log.Fatalf("error initializing Firestoer client: %v", err) } }) } func CloseFirestoreClient() { closeOnce.Do(func() { err := clientInstance.Close() if err != nil { log.Printf("error closing Firestore client: %v", err) } }) } // ãã¹ããã¼ã¿ç»é² func PrepareTestData() { records := []struct { Text string Num int }{ {Text: "text1", Num: 1}, } ctx := context.Background() for _, record := range records { _, _, err := clientInstance.Collection("records").Add(ctx, record) if err != nil { log.Printf("Failed to adding record: %v", err) } else { log.Printf("Successfully added record: %+v", record) } } }
- Firestore ã©ã¤ãã©ãªã import ããInitializeFirestoreClient ã§åæåãã¾ã
- åæåã¯ï¼åº¦ã ãè¡ããã°ããã®ã§ãsync.Once ã§è¡ã£ã¦ãã¾ã
- äºåæºåã§ãã¦ã³ãã¼ãããJSONãã¡ã¤ã«ãæå®ãã¦ãGCLOUD_CREDENTIAL_FILE_PATH ã®ç°å¢å¤æ°ãããã¹ãåå¾ãã¦èªã¿è¾¼ãã§ãã¾ããï¼ç°å¢å¤æ°èªã¿è¾¼ã¿é¨åã®ã³ã¼ãã¯å¾è¿°ï¼
Â
environment.go
package configs import ( "log" "os" "github.com/joho/godotenv" ) func LoadEnv() error { err := godotenv.Load("./configs/.env") if err != nil { log.Fatalf("error loading env file: %v", err) return err } gcloud_file_path := os.Getenv("GCLOUD_CREDENTIAL_FILE_PATH") log.Printf("GCLOUD_CREDENTIAL_FILE_PATH: %v", gcloud_file_path) return nil }
Â
- ç°å¢å¤æ°ã®èªã¿è¾¼ã¿ã«ã¯ godotenv ã使ç¨ãã¦ãã¾ã
- .env ã«ä¸è¨ã®ãããªå½¢å¼ã§ç°å¢å¤æ°ãå®ç¾©ãã¾ã
Â
main.go
package main import ( "backend/configs" "backend/internal/db" ) func main() { err := configs.LoadEnv() if err != nil { panic("error loading env file") } db.InitializeFirestoreClient() defer db.CloseFirestoreClient() db.PrepareTestData() }
- ããã¾ã§ã®å¦çãå¼ã³åºããPrepareTestData é¢æ°ãå¼ã³åºã㦠Firestore ã«ç»é²ãè¡ã£ã¦ãã¾ã
åä½ç¢ºèª
å®è¡å
Â
main.go å®è¡å¾
Â
çµããã«
Firestore ã«æ¥ç¶ããã¾ã§ã®äºåæºåã®é¨åã§å°ãæéåãã¾ããããã·ã³ãã«ãªå®è£ 㧠Go ãã Firestore ã«æ¥ç¶ã§ãããã¨ããããã¾ããã
ä»åã¯ç»é²ã ãã§ããããåç §ãæ´æ°ãåé¤ãªã©ã試ãã¦ããããã§ãã