firetils

package module
v0.0.42 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2024 License: Apache-2.0 Imports: 15 Imported by: 5

README

Firebase utils for golang

Tools that make it easier to work with Google Cloud / Firebase's Firestore.

Go Reference

For instance:

ob := &MyObject{}
err := firetils.Save(ctx, client, "myCollection", ob) // depending on interfaces on the object, it will update timestamps, call PreSave() function, etc

Fetching:

There's several functions here, they will all call populate into a struct and call AfterLoad().

firetils.GetByID
firetils.GetOneByQuery
firetils.GetAllByQuery

Data Handling

Add TimeStamped and Firestored to your objects, eg:

type X struct {
    firetils.Firestored
    firetils.TimeStamped
    firetils.IDed
}

You can do pre-saving and after-loading by adding PreSave(ctx context.Context) and/or AfterLoad(ctx context.Context) function to your models.

Authentication

Authenticate function will validate an auth token.

Or use firetils.FireAuth middleware to do it automatically.

Documentation

Index

Constants

View Source
const (
	TokenContextKey  = contextKey("token")
	UserIDContextKey = contextKey("user_id")
)

Variables

View Source
var (
	InvalidToken = errors.New("Invalid auth token")
	NoToken      = errors.New("No auth token provided")
	AuthClient   *fauth.Client
)

Functions

func Authenticate

func Authenticate(ctx context.Context, firebaseAuth *fauth.Client, w http.ResponseWriter, r *http.Request, hardVerify bool) (*fauth.Token, error)

Authenticate checks the Authorization header for a firebase token

func Collection

func Collection(client *firestore.Client, name string) *firestore.CollectionRef

Collection returns CollectionRef by name

func CreateSession added in v0.0.40

func CreateSession(auth *auth.Client, r *http.Request) (token string, response map[string]any, err error)
	return gotils.WriteObject(w, http.StatusOK, resp)
}

func Delete

func Delete(ctx context.Context, client *firestore.Client, collectionName, id string) error

GetByID get a doc by ID

func EnsureUserDefault added in v0.0.40

func EnsureUserDefault(ctx context.Context, auth *auth.Client, fs *firestore.Client, idToken, usersCollection string) (*auth.UserRecord, error)

This will verify the idToken, fetch the user from firebase auth, then update the 'usersCollection' with, email, name and image IF it doesn't already exist. TODO: Will update those fields if they have changed.

func FireAuth

func FireAuth(next http.Handler) http.Handler

FireAuth middleware to guard endpoints

func GetAllByQuery

func GetAllByQuery(ctx context.Context, q firestore.Query, limit int, ret []interface{}) error

GetAllByQuery generic way to get a list of documents. NOTE: this doesn't seem to work well, best to use GetAllByQuery2. limit param restricts how many are returned. <=0 is all results. ret will be filled with the objects Deprecated: use GetAllByQuery2+

func GetAllByQuery3 added in v0.0.41

func GetAllByQuery3[T StoredAndStamped](ctx context.Context, q firestore.Query, v T) ([]T, error)

func GetByID

func GetByID(ctx context.Context, client *firestore.Client, collectionName, id string, t StoredAndStamped) error

GetByID get a doc by ID

func GetByRef

GetByRef generic way to get a document

func GetOneByQuery

func GetOneByQuery(ctx context.Context, q firestore.Query, t StoredAndIded) error

GetOneByQuery generic way to get a document

func New

func New(ctx context.Context, projectID string, opts []option.ClientOption) (*firebase.App, error)

New creates a new firestore client Call defer client.Close() after this if you can

func NewFireHolder

func NewFireHolder(ctx context.Context, app *firebase.App) *fireHolder

NewFireHolder don't use this yet This is intended to be a way to have multiple firetils instance with different configs, rather than globals

func OptionalAuth

func OptionalAuth(next http.Handler) http.Handler

OptionalAuth this won't guard it, but will set the token in the context if it's there. Will not error out if it's not there.

func Save3 added in v0.0.41

func Save3(ctx context.Context, client *firestore.Client, collection string, v StoredAndStamped) error

func Save3WithOpts added in v0.0.41

func Save3WithOpts(ctx context.Context, client *firestore.Client, collection string, v StoredAndStamped, opts *SaveOptions) error

func SetOwned

func SetOwned(ctx context.Context, o OwnedI)

func Token

func Token(ctx context.Context) *fauth.Token

func UserID

func UserID(ctx context.Context) string

Types

type Firestored

type Firestored struct {
	Ref *firestore.DocumentRef `firestore:"-" json:"-"`
}

func (*Firestored) GetRef

func (f *Firestored) GetRef() *firestore.DocumentRef

func (*Firestored) SetRef

func (f *Firestored) SetRef(ref *firestore.DocumentRef)

type FirestoredAndTimeStamped added in v0.0.40

type FirestoredAndTimeStamped struct {
	Firestored
	TimeStamped
	IDed
}

type FirestoredI

type FirestoredI interface {
	GetRef() *firestore.DocumentRef
	SetRef(*firestore.DocumentRef)
}

type IDed

type IDed struct {
	ID string `firestore:"-" json:"id"`
}

func (*IDed) GetID

func (f *IDed) GetID() string

func (*IDed) SetID

func (f *IDed) SetID(id string)

type IDedI

type IDedI interface {
	GetID() string
	SetID(string)
}

type KitchenSink

type KitchenSink interface {
	StoredAndStamped
	OwnedI
}

type Owned

type Owned struct {
	UserID string `firestore:"user_id" json:"user_id"`
}

Owned stores the user_id for the owner of this object

func (*Owned) GetUserID

func (f *Owned) GetUserID() string

func (*Owned) SetUserID

func (f *Owned) SetUserID(id string)

type OwnedBy

type OwnedBy struct {
	UserID string `firestore:"userID" json:"userID"`
}

OwnedBy same as Owned, but camel case version

func (*OwnedBy) GetUserID

func (f *OwnedBy) GetUserID() string

func (*OwnedBy) SetUserID

func (f *OwnedBy) SetUserID(id string)

type OwnedI

type OwnedI interface {
	GetUserID() string
	SetUserID(string)
}

type SaveOptions

type SaveOptions struct {
	SkipOwned bool // won't set userID on the saved object
}

type StoredAndIded

type StoredAndIded interface {
	FirestoredI
	IDedI
}

todo: should FirestoredI just be merged with IdedI ?

func GetOneByQuery2

func GetOneByQuery2(ctx context.Context, q firestore.Query, v StoredAndIded) (StoredAndIded, error)

GetOneByQuery2 generic way to get a document This one returns a new object. Still trying to decide which way I like better...

type StoredAndStamped

type StoredAndStamped interface {
	FirestoredI
	IDedI
	TimestampedI
}

func GetAllByQuery2

func GetAllByQuery2(ctx context.Context, q firestore.Query, v StoredAndStamped) ([]StoredAndStamped, error)

GetAllByQuery2 generic way to get a list of documents, by just passing in the type. `limit` param restricts how many we return. <=0 is all v is an instance of the type of object to be returned, it will not be modified or updated.

func GetByID2

func GetByID2(ctx context.Context, client *firestore.Client, collectionName, id string, v StoredAndStamped) (StoredAndStamped, error)

GetByID2 same as GetByID, but returns new object

func Save

func Save(ctx context.Context, client *firestore.Client, collection string, v StoredAndStamped) (StoredAndStamped, error)

Save sets userID if it's in context, updates timestamps, calls PreSave(), then return object with Ref and ID set.

func Save2

func Save2(ctx context.Context, client *firestore.Client, collection string, v StoredAndStamped, opts *SaveOptions) (StoredAndStamped, error)

Save2 same as Save, but with options

type TimeStamped

type TimeStamped struct {
	UpdatedAt time.Time `firestore:"updatedAt" json:"updatedAt"`
	CreatedAt time.Time `firestore:"createdAt" json:"createdAt"`
}

TimeStamped camel case version

func (*TimeStamped) GetCreatedAt

func (ts *TimeStamped) GetCreatedAt() time.Time

func (*TimeStamped) GetUpdatedAt

func (ts *TimeStamped) GetUpdatedAt() time.Time

func (*TimeStamped) SetCreatedAt

func (ts *TimeStamped) SetCreatedAt(t time.Time)

func (*TimeStamped) SetUpdatedAt

func (ts *TimeStamped) SetUpdatedAt(t time.Time)

type Timestamped

type Timestamped struct {
	UpdatedAt time.Time `firestore:"updated_at" json:"updated_at"`
	CreatedAt time.Time `firestore:"created_at" json:"created_at"`
}

Timestamped snake case version

func (*Timestamped) GetCreatedAt

func (ts *Timestamped) GetCreatedAt() time.Time

func (*Timestamped) GetUpdatedAt

func (ts *Timestamped) GetUpdatedAt() time.Time

func (*Timestamped) SetCreatedAt

func (ts *Timestamped) SetCreatedAt(t time.Time)

func (*Timestamped) SetUpdatedAt

func (ts *Timestamped) SetUpdatedAt(t time.Time)

type TimestampedI

type TimestampedI interface {
	GetCreatedAt() time.Time
	GetUpdatedAt() time.Time
	SetCreatedAt(time.Time)
	SetUpdatedAt(time.Time)
}

func UpdateTimeStamps

func UpdateTimeStamps(obj TimestampedI) TimestampedI

UpdateTimeStamps call this right before storing it in a database

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL