|
| 1 | +--- |
| 2 | +id: firestore |
| 3 | +title: Firestore |
| 4 | +--- |
| 5 | + |
| 6 | + |
| 7 | +[](https://gofiber.io/discord) |
| 8 | + |
| 9 | + |
| 10 | +A Firestore storage driver using [cloud.google.com/go/firestore](https://pkg.go.dev/cloud.google.com/go/firestore). |
| 11 | + |
| 12 | +**Note:** If no credentials are provided, the driver uses Application Default Credentials (ADC) or the `GOOGLE_APPLICATION_CREDENTIALS` environment variable. If credentials are provided via config (`Credentials` or `CredentialsPath`), those take precedence. See: [Google Cloud Authentication Guide](https://cloud.google.com/docs/authentication) |
| 13 | + |
| 14 | +### Table of Contents |
| 15 | + |
| 16 | +- [Signatures](#signatures) |
| 17 | +- [Installation](#installation) |
| 18 | +- [Examples](#examples) |
| 19 | +- [Config](#config) |
| 20 | +- [Default Config](#default-config) |
| 21 | + |
| 22 | +### Signatures |
| 23 | + |
| 24 | +```go |
| 25 | +func New(config ...Config) *Storage |
| 26 | +func NewFromConnection(client *firestore.Client, collection string) *Storage |
| 27 | +func (s *Storage) Get(key string) ([]byte, error) |
| 28 | +func (s *Storage) GetWithContext(ctx context.Context, key string) ([]byte, error) |
| 29 | +func (s *Storage) Set(key string, val []byte, exp time.Duration) error |
| 30 | +func (s *Storage) SetWithContext(ctx context.Context, key string, val []byte, exp time.Duration) error |
| 31 | +func (s *Storage) Delete(key string) error |
| 32 | +func (s *Storage) DeleteWithContext(ctx context.Context, key string) error |
| 33 | +func (s *Storage) Reset() error |
| 34 | +func (s *Storage) ResetWithContext(ctx context.Context) error |
| 35 | +func (s *Storage) Close() error |
| 36 | +func (s *Storage) Conn() *firestore.Client |
| 37 | +``` |
| 38 | + |
| 39 | +### Installation |
| 40 | + |
| 41 | +Firestore is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet: |
| 42 | + |
| 43 | +```bash |
| 44 | +go mod init github.com/<user>/<repo> |
| 45 | +``` |
| 46 | + |
| 47 | +And then install the firestore implementation: |
| 48 | + |
| 49 | +```bash |
| 50 | +go get github.com/gofiber/storage/firestore |
| 51 | +``` |
| 52 | + |
| 53 | +### Examples |
| 54 | + |
| 55 | +Import the storage package. |
| 56 | + |
| 57 | +```go |
| 58 | +import firestorage "github.com/gofiber/storage/firestore" |
| 59 | +``` |
| 60 | + |
| 61 | +You can use the following possibilities to create a storage: |
| 62 | + |
| 63 | +```go |
| 64 | +// Initialize with Application Default Credentials |
| 65 | +store := firestorage.New(firestorage.Config{ |
| 66 | + ProjectID: "my-gcp-project", |
| 67 | +}) |
| 68 | + |
| 69 | +// Initialize with service account JSON file |
| 70 | +store := firestorage.New(firestorage.Config{ |
| 71 | + ProjectID: "my-gcp-project", |
| 72 | + CredentialsPath: "/path/to/service-account-key.json", |
| 73 | + Collection: "sessions", |
| 74 | +}) |
| 75 | + |
| 76 | +// Initialize with embedded credentials JSON |
| 77 | +store := firestorage.New(firestorage.Config{ |
| 78 | + ProjectID: "my-gcp-project", |
| 79 | + Credentials: `{"type": "service_account", ...}`, |
| 80 | +}) |
| 81 | + |
| 82 | +// Initialize with custom timeout |
| 83 | +store := firestorage.New(firestorage.Config{ |
| 84 | + ProjectID: "my-gcp-project", |
| 85 | + Collection: "fiber_storage", |
| 86 | + RequestTimeout: 10 * time.Second, |
| 87 | + Reset: false, |
| 88 | +}) |
| 89 | +``` |
| 90 | + |
| 91 | +#### Using an Existing Firestore Client |
| 92 | + |
| 93 | +If you already have a Firestore client configured in your application, you can create a Storage instance directly from that client: |
| 94 | + |
| 95 | +```go |
| 96 | +import ( |
| 97 | + "cloud.google.com/go/firestore" |
| 98 | + "context" |
| 99 | + firestorage "github.com/gofiber/storage/firestore" |
| 100 | +) |
| 101 | + |
| 102 | +ctx := context.Background() |
| 103 | +client, err := firestore.NewClient(ctx, "my-gcp-project") |
| 104 | +if err != nil { |
| 105 | + panic(err) |
| 106 | +} |
| 107 | + |
| 108 | +store := firestorage.NewFromConnection(client, "my_collection") |
| 109 | +``` |
| 110 | + |
| 111 | +### Config |
| 112 | + |
| 113 | +```go |
| 114 | +type Config struct { |
| 115 | + // ProjectID is the Google Cloud project ID |
| 116 | + // Required. Will panic if empty |
| 117 | + ProjectID string |
| 118 | + |
| 119 | + // Collection name where data will be stored |
| 120 | + // |
| 121 | + // Optional. Default is "fiber_storage" |
| 122 | + Collection string |
| 123 | + |
| 124 | + // CredentialsPath is the path to the service account JSON key file |
| 125 | + // If not provided, Application Default Credentials (ADC) will be used |
| 126 | + // |
| 127 | + // Optional. Default is "" |
| 128 | + CredentialsPath string |
| 129 | + |
| 130 | + // Credentials is a JSON string with service account credentials |
| 131 | + // Takes precedence over CredentialsPath if both are provided |
| 132 | + // |
| 133 | + // Optional. Default is "" |
| 134 | + Credentials string |
| 135 | + |
| 136 | + // RequestTimeout is the timeout for Firestore requests |
| 137 | + // |
| 138 | + // Optional. Default is 10 seconds |
| 139 | + RequestTimeout time.Duration |
| 140 | + |
| 141 | + // Reset clears all documents in the collection on initialization |
| 142 | + // |
| 143 | + // Optional. Default is false |
| 144 | + Reset bool |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +### Default Config |
| 149 | + |
| 150 | +```go |
| 151 | +var ConfigDefault = Config{ |
| 152 | + Collection: "fiber_storage", |
| 153 | + RequestTimeout: 10 * time.Second, |
| 154 | + Reset: false, |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +### Additional Resources |
| 159 | + |
| 160 | +- [Firestore Go SDK Documentation](https://pkg.go.dev/cloud.google.com/go/firestore) |
| 161 | +- [Firebase Console](https://console.firebase.google.com) |
| 162 | +- [Google Cloud Firestore Documentation](https://cloud.google.com/firestore/docs) |
| 163 | +- [Google Cloud Authentication Guide](https://cloud.google.com/docs/authentication) |
| 164 | +- [Firestore Quotas and Limits](https://cloud.google.com/firestore/quotas) |
0 commit comments