Skip to main content
Version: firestore_v0.x.x

Firestore

Release Discord Test

A Firestore storage driver using cloud.google.com/go/firestore.

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

Table of Contents

Signatures

func New(config ...Config) *Storage
func NewFromConnection(client *firestore.Client, collection string) *Storage
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) GetWithContext(ctx context.Context, key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) SetWithContext(ctx context.Context, key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) DeleteWithContext(ctx context.Context, key string) error
func (s *Storage) Reset() error
func (s *Storage) ResetWithContext(ctx context.Context) error
func (s *Storage) Close() error
func (s *Storage) Conn() *firestore.Client

Installation

Firestore is tested on the 2 last Go versions with support for modules. So make sure to initialize one first if you didn't do that yet:

go mod init github.com/<user>/<repo>

And then install the firestore implementation:

go get github.com/gofiber/storage/firestore

Examples

Import the storage package.

import firestorage "github.com/gofiber/storage/firestore"

You can use the following possibilities to create a storage:

// Initialize with Application Default Credentials
store := firestorage.New(firestorage.Config{
ProjectID: "my-gcp-project",
})

// Initialize with service account JSON file
store := firestorage.New(firestorage.Config{
ProjectID: "my-gcp-project",
CredentialsPath: "/path/to/service-account-key.json",
Collection: "sessions",
})

// Initialize with embedded credentials JSON
store := firestorage.New(firestorage.Config{
ProjectID: "my-gcp-project",
Credentials: `{"type": "service_account", ...}`,
})

// Initialize with custom timeout
store := firestorage.New(firestorage.Config{
ProjectID: "my-gcp-project",
Collection: "fiber_storage",
RequestTimeout: 10 * time.Second,
Reset: false,
})

Using an Existing Firestore Client

If you already have a Firestore client configured in your application, you can create a Storage instance directly from that client:

import (
"cloud.google.com/go/firestore"
"context"
firestorage "github.com/gofiber/storage/firestore"
)

ctx := context.Background()
client, err := firestore.NewClient(ctx, "my-gcp-project")
if err != nil {
panic(err)
}

store := firestorage.NewFromConnection(client, "my_collection")

Config

type Config struct {
// ProjectID is the Google Cloud project ID
// Required. Will panic if empty
ProjectID string

// Collection name where data will be stored
//
// Optional. Default is "fiber_storage"
Collection string

// CredentialsPath is the path to the service account JSON key file
// If not provided, Application Default Credentials (ADC) will be used
//
// Optional. Default is ""
CredentialsPath string

// Credentials is a JSON string with service account credentials
// Takes precedence over CredentialsPath if both are provided
//
// Optional. Default is ""
Credentials string

// RequestTimeout is the timeout for Firestore requests
//
// Optional. Default is 10 seconds
RequestTimeout time.Duration

// Reset clears all documents in the collection on initialization
//
// Optional. Default is false
Reset bool
}

Default Config

var ConfigDefault = Config{
Collection: "fiber_storage",
RequestTimeout: 10 * time.Second,
Reset: false,
}

Additional Resources