Documentation ¶
Index ¶
- Constants
- Variables
- func CacheHandler(next http.HandlerFunc) http.HandlerFunc
- func CacheMiddleware(next http.Handler) http.Handler
- type Cache
- type EntityMap
- type FSClient
- func (fsc *FSClient) DefaultFromDBMapperFunc(inKey string, inVal interface{}) (mt mapper.MappingType, outKey string, outVal interface{})
- func (fsc *FSClient) DefaultToDBMapperFunc(inKey string, inVal interface{}) (mt mapper.MappingType, outKey string, outVal interface{})
- func (fsc *FSClient) DoInTransaction(ctx context.Context, f func(tctx context.Context) error) error
- func (fsc *FSClient) NewRequest() *Request
- func (fsc *FSClient) SetCache(cache Cache)
- type FutureFunc
- type NotFoundError
- type Request
- func (req *Request) CreateEntities(ctx context.Context, entities interface{}) FutureFunc
- func (req *Request) DeleteEntities(ctx context.Context, entities interface{}) FutureFunc
- func (req *Request) GetEntities(ctx context.Context, entities interface{}) func() ([]interface{}, error)
- func (req *Request) GetID(entity interface{}) string
- func (req *Request) GetParent(entity interface{}) interface{}
- func (req *Request) QueryEntities(ctx context.Context, query firestore.Query, toSlicePtr interface{}) FutureFunc
- func (req *Request) SetID(entity interface{}, id string)
- func (req *Request) SetLoadPaths(paths ...string) *Request
- func (req *Request) SetMapperFunc(mapperFunc mapperFunc) *Request
- func (req *Request) ToCollection(entity interface{}) *firestore.CollectionRef
- func (req *Request) ToRef(entity interface{}) *firestore.DocumentRef
- func (req *Request) UpdateEntities(ctx context.Context, entities interface{}) FutureFunc
Constants ¶
const AllEntities = "ALL"
AllEntities loads all paths on the struct see: SetLoadPaths
Variables ¶
var ( // SessionCacheKey is the key for the session map in the context SessionCacheKey = contextKey("sessionCache") // ErrCacheMiss returned on a cache miss ErrCacheMiss = errors.New("not found in cache") )
Functions ¶
func CacheHandler ¶
func CacheHandler(next http.HandlerFunc) http.HandlerFunc
CacheHandler should be used on the mux chain to support session cache. So getting the same entity several times will only generate on DB hit
Types ¶
type Cache ¶
type Cache interface { Get(ctx context.Context, key string) (EntityMap, error) GetMulti(ctx context.Context, keys []string) (map[string]EntityMap, error) Set(ctx context.Context, key string, item EntityMap) error SetMulti(ctx context.Context, items map[string]EntityMap) error Delete(ctx context.Context, key string) error DeleteMulti(ctx context.Context, keys []string) error }
Cache can be used to implement custom caching
type FSClient ¶
type FSClient struct { Client *firestore.Client MapToDB *mapper.Mapper MapFromDB *mapper.Mapper IDKey, ParentKey string Cache *cacheWrapper IsEntity func(i interface{}) bool }
FSClient is the client used to perform the CRUD actions
func New ¶
New creates a firestorm client. Supply the names of the id and parent fields of your model structs Leave parent blank if sub-collections are not used.
func (*FSClient) DefaultFromDBMapperFunc ¶
func (fsc *FSClient) DefaultFromDBMapperFunc(inKey string, inVal interface{}) (mt mapper.MappingType, outKey string, outVal interface{})
DefaultFromDBMapperFunc default mapper that maps firestore fields and values to entity fields and values
func (*FSClient) DefaultToDBMapperFunc ¶
func (fsc *FSClient) DefaultToDBMapperFunc(inKey string, inVal interface{}) (mt mapper.MappingType, outKey string, outVal interface{})
DefaultToDBMapperFunc default mapper that maps entity fields and values to be firestore fields and values
func (*FSClient) DoInTransaction ¶
DoInTransaction wraps any updates that needs to run in a transaction. Use the transaction context tctx for any calls that need to be part of the transaction. Do reads before writes as required by firestore
func (*FSClient) NewRequest ¶
NewRequest creates a new CRUD Request to firestore
type FutureFunc ¶
type FutureFunc func() error
FutureFunc is a function that when called blocks until the result is ready
type NotFoundError ¶
type NotFoundError struct { // Refs contains the references not found Refs map[string]*firestore.DocumentRef }
NotFoundError is returned when any of the entities are not found in firestore The error can be ignored if dangling references is not a problem
func (NotFoundError) Error ¶
func (e NotFoundError) Error() string
type Request ¶
type Request struct { FSC *FSClient // contains filtered or unexported fields }
Request a request builder for querying firestore
func (*Request) CreateEntities ¶
func (req *Request) CreateEntities(ctx context.Context, entities interface{}) FutureFunc
CreateEntities creates the entities and auto creates the id if left empty. Supply either a struct or a slice as value or reference.
func (*Request) DeleteEntities ¶
func (req *Request) DeleteEntities(ctx context.Context, entities interface{}) FutureFunc
DeleteEntities deletes the entities. Supply either a struct or a slice as value or reference.
func (*Request) GetEntities ¶
func (req *Request) GetEntities(ctx context.Context, entities interface{}) func() ([]interface{}, error)
GetEntities reads the entities from the database by their id. Supply either a pointer to a struct or pointer to a slice. Returns a slice containing the found entities and an error if some entities are not found.
func (*Request) GetID ¶
GetID gets the id of the entity. It panics if the entity does not have an ID field.
func (*Request) GetParent ¶
func (req *Request) GetParent(entity interface{}) interface{}
GetParent gets the patent of the entity
func (*Request) QueryEntities ¶
func (req *Request) QueryEntities(ctx context.Context, query firestore.Query, toSlicePtr interface{}) FutureFunc
QueryEntities query for entities. Supply a reference to a slice for the result
func (*Request) SetLoadPaths ¶
SetLoadPaths adds the paths (refs) to load for the entity. Eg. to load a users grandmother: 'mother.mother' To load all refs on the struct use firestorm.AllEntities See examples: https://github.com/jschoedt/go-firestorm/blob/master/tests/integration_test.go
func (*Request) SetMapperFunc ¶
SetMapperFunc is called before the map is saved to firestore. This can be used to modify the map before it is saved
func (*Request) ToCollection ¶
func (req *Request) ToCollection(entity interface{}) *firestore.CollectionRef
ToCollection creates a firestore CollectionRef to the entity
func (*Request) ToRef ¶
func (req *Request) ToRef(entity interface{}) *firestore.DocumentRef
ToRef creates a firestore DocumentRef for the entity
func (*Request) UpdateEntities ¶
func (req *Request) UpdateEntities(ctx context.Context, entities interface{}) FutureFunc
UpdateEntities updates the entities. Supply either a struct or a slice as value or reference.