Documentation
¶
Index ¶
- Constants
- Variables
- func Authenticate(ctx context.Context, firebaseAuth *fauth.Client, w http.ResponseWriter, ...) (*fauth.Token, error)
- func Collection(client *firestore.Client, name string) *firestore.CollectionRef
- func CreateSession(auth *auth.Client, r *http.Request) (token string, response map[string]any, err error)
- func Delete(ctx context.Context, client *firestore.Client, collectionName, id string) error
- func EnsureUserDefault(ctx context.Context, auth *auth.Client, fs *firestore.Client, ...) (*auth.UserRecord, error)
- func FireAuth(next http.Handler) http.Handler
- func GetAllByQuery(ctx context.Context, q firestore.Query, limit int, ret []interface{}) error
- func GetAllByQuery3[T StoredAndStamped](ctx context.Context, q firestore.Query, v T) ([]T, error)
- func GetByID(ctx context.Context, client *firestore.Client, collectionName, id string, ...) error
- func GetByRef(ctx context.Context, client *firestore.Client, ref *firestore.DocumentRef, ...) error
- func GetOneByQuery(ctx context.Context, q firestore.Query, t StoredAndIded) error
- func New(ctx context.Context, projectID string, opts []option.ClientOption) (*firebase.App, error)
- func NewFireHolder(ctx context.Context, app *firebase.App) *fireHolder
- func OptionalAuth(next http.Handler) http.Handler
- func Save3(ctx context.Context, client *firestore.Client, collection string, ...) error
- func Save3WithOpts(ctx context.Context, client *firestore.Client, collection string, ...) error
- func SetOwned(ctx context.Context, o OwnedI)
- func Token(ctx context.Context) *fauth.Token
- func UserID(ctx context.Context) string
- type Firestored
- type FirestoredAndTimeStamped
- type FirestoredI
- type IDed
- type IDedI
- type KitchenSink
- type Owned
- type OwnedBy
- type OwnedI
- type SaveOptions
- type StoredAndIded
- type StoredAndStamped
- func GetAllByQuery2(ctx context.Context, q firestore.Query, v StoredAndStamped) ([]StoredAndStamped, error)
- func GetByID2(ctx context.Context, client *firestore.Client, collectionName, id string, ...) (StoredAndStamped, error)
- func Save(ctx context.Context, client *firestore.Client, collection string, ...) (StoredAndStamped, error)
- func Save2(ctx context.Context, client *firestore.Client, collection string, ...) (StoredAndStamped, error)
- type TimeStamped
- type Timestamped
- type TimestampedI
Constants ¶
const ( TokenContextKey = contextKey("token") UserIDContextKey = contextKey("user_id") )
Variables ¶
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 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 GetAllByQuery ¶
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 GetByID ¶
func GetByID(ctx context.Context, client *firestore.Client, collectionName, id string, t StoredAndStamped) error
GetByID get a doc by ID
func GetByRef ¶
func GetByRef(ctx context.Context, client *firestore.Client, ref *firestore.DocumentRef, t StoredAndStamped) error
GetByRef generic way to get a document
func GetOneByQuery ¶
GetOneByQuery generic way to get a document
func NewFireHolder ¶
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 ¶
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 Save3WithOpts ¶ added in v0.0.41
func Save3WithOpts(ctx context.Context, client *firestore.Client, collection string, v StoredAndStamped, opts *SaveOptions) error
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 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
type OwnedBy ¶
type OwnedBy struct {
UserID string `firestore:"userID" json:"userID"`
}
OwnedBy same as Owned, but camel case version
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