foon

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2019 License: MIT Imports: 24 Imported by: 0

README

foon

Foon is a wrapper library for Firestore Native API and which provides an autocaching interface to the Google App Engine. Foon can use like mjibson/goon.

Usage

Struct

Below is an example struct which are stored to Firestore Document (in "Users" Collection).

// Collection
type User struct {
	__kind    string    `foon:"collection,Users"` // Specifies Collection Name
	ID        string    `foon:"id"` // When the document was put into Firestore, this field stores Document ID automaticary.
	Name      string    `firestore:"name"`
	CreatedAt time.Time `foon:"createdAt" firestore:"createdAt"` // When the document was put into Firestore, this field stores stored datetime.
	UpdatedAt time.Time `foon:"updatedAt" firestore:"updatedAt"` // When the document was put or updated, this field modified automaticary.
}

// Sub Collection
type Device struct {
	__kind     string    `foon:"collection,Devices"`
	ID         string    `foon:"id"`
	Parent     *foon.Key `foon:"parent"`
	DeviceName string    `firestore:"deviceName"`
}
Get Document

Following are examples to get document from "User" collection which are stored in Firestore.

user := &User{ ID: "user001" }
f := foon.Must(appEngineContext)
if err := f.Get(user); err != nil {
    log.Warningf(appEngineContext, "failed to get user.")
}

e.g If Devices are the Sub Collection from User Document, followings are usage to get some Device document.

user := &User{ ID: "user001" }
key := foon.NewKey(user)

device := &Device{ID: "device001", Parent: key }
f := foon.Must(appEngineContext)
f.Get(device)
Put Document

Following are examples.

f := foon.Must(appEngineContext)
user := &User{ Name: "username01" }

// if you don't specify id, foon stores random id automaticary.
if err := f.Insert(user); err == nil {
    // store sub collection
    device := &Device{ ID: "device01", Parent: foon.NewKey(user) }
    f.Put(device)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewParentField

func NewParentField(src interface{}) *parentField

func NotFound

func NotFound(err error) bool

Types

type CacheKey

type CacheKey string
const (
	InstanceCache   CacheKey = "foon"
	CollectionCache CacheKey = "foon/collections"
	MetadataCache   CacheKey = "foon/metadata"
	GroupDataCache  CacheKey = "foon/matadata/group"
)

func (CacheKey) CreateCollectionURIByKey added in v0.10.0

func (c CacheKey) CreateCollectionURIByKey(key *Key) IURI

func (CacheKey) CreateURIByKey

func (c CacheKey) CreateURIByKey(key *Key) IURI

type CacheMetadata

type CacheMetadata struct {
	Item *CacheMetadataItem
	// contains filtered or unexported fields
}

* クエリ結果などを保持するためのキャッシュ (関連のPut時には全削除される)

func LoadGroupMetaData added in v0.10.0

func LoadGroupMetaData(cache *FirestoreCache, key *Key) *CacheMetadata

func LoadMetadata

func LoadMetadata(cache *FirestoreCache, key *Key) *CacheMetadata

func (*CacheMetadata) DeleteAll

func (c *CacheMetadata) DeleteAll() error

func (*CacheMetadata) Has

func (c *CacheMetadata) Has(key IURI) bool

func (*CacheMetadata) Load

func (c *CacheMetadata) Load(key IURI, src interface{}) error

func (*CacheMetadata) Put

func (c *CacheMetadata) Put(uri IURI, src interface{}) error

func (*CacheMetadata) PutMulti

func (c *CacheMetadata) PutMulti(datas []MetadataItem) error

func (*CacheMetadata) Save

func (c *CacheMetadata) Save() error

type CacheMetadataItem

type CacheMetadataItem struct {
	MemcachePath string
	Data         []string
}

type CacheResult

type CacheResult struct {
	Key      *Key
	Src      interface{}
	HasCache bool
}

* キャッシュを取得する際の結果

type CacheURI

type CacheURI string

func (CacheURI) URI

func (c CacheURI) URI() string

type ConditionURI

type ConditionURI string

func (ConditionURI) URI

func (c ConditionURI) URI() string

type Conditions

type Conditions struct {
	Queries Queries
	// contains filtered or unexported fields
}

func NewConditions

func NewConditions() *Conditions

func (*Conditions) CollectionGroup added in v0.10.0

func (w *Conditions) CollectionGroup(collectionId string) *Conditions

func (*Conditions) CollectionGroupWithKey added in v0.10.0

func (w *Conditions) CollectionGroupWithKey(key *Key) *Conditions

func (Conditions) CursorURI added in v0.10.0

func (c Conditions) CursorURI(key *Key) IURI

func (Conditions) HasConditions

func (c Conditions) HasConditions() bool

func (Conditions) HasNoConditions

func (c Conditions) HasNoConditions() bool

func (Conditions) Hash

func (c Conditions) Hash() string

func (*Conditions) Limit

func (w *Conditions) Limit(limit int) *Conditions

func (*Conditions) Offset

func (w *Conditions) Offset(offset int) *Conditions

func (*Conditions) OrderBy

func (w *Conditions) OrderBy(column string, direction firestore.Direction) *Conditions

func (Conditions) Query

func (c Conditions) Query(query firestore.Query, f *Foon) (firestore.Query, error)

func (*Conditions) StartAfter added in v0.10.0

func (w *Conditions) StartAfter(cursor *Cursor) *Conditions

func (Conditions) String

func (c Conditions) String() string

func (Conditions) URI

func (c Conditions) URI(key *Key) IURI

func (*Conditions) Where

func (w *Conditions) Where(column string, operation string, value interface{}) *Conditions

type Cursor added in v0.10.0

type Cursor struct {
	ID     string
	Path   string
	Orders []CursorOrder
}

* DocumentSnapshot使いづらいので粒度を上げるためのカーソル作成

func NewCursor added in v0.10.0

func NewCursor(cursor string) (*Cursor, error)

func NewCursorWithSeed added in v0.10.0

func NewCursorWithSeed(cursor string, seed []byte) (*Cursor, error)

func (*Cursor) AddField added in v0.10.0

func (c *Cursor) AddField(column string, direction firestore.Direction)

func (Cursor) NewCursorWithOrders added in v0.10.0

func (c Cursor) NewCursorWithOrders() *Cursor

func (Cursor) String added in v0.10.0

func (c Cursor) String() string

func (Cursor) StringWithSeed added in v0.10.0

func (c Cursor) StringWithSeed(seed []byte) string

type CursorOrder added in v0.10.0

type CursorOrder struct {
	FieldName string
	Direction firestore.Direction
}

type CursorQuery added in v0.10.0

type CursorQuery interface {
	Queryer(query firestore.Query, cursor *Cursor, f *Foon) (firestore.Query, error)
	Hash(cursor *Cursor) string
}

type FirestoreCache

type FirestoreCache struct {
	context.Context
	// contains filtered or unexported fields
}

* Memcacheを扱う

func NewCache

func NewCache(ctx context.Context, logger Logger) *FirestoreCache

func (*FirestoreCache) Delete

func (c *FirestoreCache) Delete(info *Key) error

func (*FirestoreCache) DeleteCache

func (c *FirestoreCache) DeleteCache(path string) error

func (*FirestoreCache) DeleteMulti

func (c *FirestoreCache) DeleteMulti(keys []*Key) error

func (*FirestoreCache) Get

func (c *FirestoreCache) Get(info *Key, src interface{}) error

func (*FirestoreCache) GetCache

func (c *FirestoreCache) GetCache(path string, src interface{}) error

func (*FirestoreCache) GetEntity

func (c *FirestoreCache) GetEntity(src interface{}) error

func (*FirestoreCache) GetMulti

func (c *FirestoreCache) GetMulti(results map[string]*CacheResult) error

func (*FirestoreCache) Put

func (c *FirestoreCache) Put(info *Key, src interface{}) error

func (*FirestoreCache) PutCache

func (c *FirestoreCache) PutCache(path string, src interface{}) error

func (*FirestoreCache) PutEntity

func (c *FirestoreCache) PutEntity(src interface{}) error

func (*FirestoreCache) PutMulti

func (c *FirestoreCache) PutMulti(results []*KeyAndData) error

type FirestoreClient

type FirestoreClient interface {
	Get(dr *firestore.DocumentRef) (*firestore.DocumentSnapshot, error)
	GetAll(drs []*firestore.DocumentRef) ([]*firestore.DocumentSnapshot, error)
	Create(dr *firestore.DocumentRef, data interface{}) error
	Set(dr *firestore.DocumentRef, data interface{}, opts ...firestore.SetOption) error
	Delete(dr *firestore.DocumentRef, opts ...firestore.Precondition) error
	Update(dr *firestore.DocumentRef, data []firestore.Update, opts ...firestore.Precondition) error
	Documents(q firestore.Query) *firestore.DocumentIterator
	Batch() (*firestore.WriteBatch, error)
	RunTransaction(fn func(ctx context.Context, fs *firestore.Transaction) error, opts ...firestore.TransactionOption) error
	Client() *firestore.Client
}

type FirestoreClientImpl

type FirestoreClientImpl struct {
	// contains filtered or unexported fields
}

func (*FirestoreClientImpl) Batch

func (*FirestoreClientImpl) Client

func (f *FirestoreClientImpl) Client() *firestore.Client

func (*FirestoreClientImpl) Create

func (f *FirestoreClientImpl) Create(dr *firestore.DocumentRef, data interface{}) error

func (*FirestoreClientImpl) Delete

func (*FirestoreClientImpl) Documents

func (*FirestoreClientImpl) Get

func (*FirestoreClientImpl) GetAll

func (*FirestoreClientImpl) RunTransaction

func (f *FirestoreClientImpl) RunTransaction(fn func(ctx context.Context, fs *firestore.Transaction) error, opts ...firestore.TransactionOption) error

func (*FirestoreClientImpl) Set

func (f *FirestoreClientImpl) Set(dr *firestore.DocumentRef, data interface{}, opts ...firestore.SetOption) error

func (*FirestoreClientImpl) Update

type FirestoreTransactionClient

type FirestoreTransactionClient struct {
	// contains filtered or unexported fields
}

func (*FirestoreTransactionClient) Batch

func (*FirestoreTransactionClient) Client

func (*FirestoreTransactionClient) Create

func (f *FirestoreTransactionClient) Create(dr *firestore.DocumentRef, data interface{}) error

func (*FirestoreTransactionClient) Delete

func (*FirestoreTransactionClient) Documents

func (*FirestoreTransactionClient) Get

func (*FirestoreTransactionClient) GetAll

func (*FirestoreTransactionClient) RunTransaction

func (*FirestoreTransactionClient) Set

func (f *FirestoreTransactionClient) Set(dr *firestore.DocumentRef, data interface{}, opts ...firestore.SetOption) error

func (*FirestoreTransactionClient) Update

type Foon

type Foon struct {
	context.Context
	// contains filtered or unexported fields
}

func Must

func Must(ctx context.Context) *Foon

func New

func New(ctx context.Context) (*Foon, error)

func NewStoreWithProjectID

func NewStoreWithProjectID(ctx context.Context, projectID string) (*Foon, error)

func (*Foon) Batch

func (s *Foon) Batch() (WriteBatch, error)

func (*Foon) Delete

func (s *Foon) Delete(src interface{}) error

func (*Foon) Get

func (s *Foon) Get(src interface{}) error

func (*Foon) GetAll

func (s *Foon) GetAll(key *Key, src interface{}) error

func (*Foon) GetByKey added in v0.10.0

func (s *Foon) GetByKey(key *Key, src interface{}) error

func (*Foon) GetByQuery

func (s *Foon) GetByQuery(key *Key, src interface{}, conditions *Conditions) error

func (*Foon) GetByQueryWithoutCache

func (s *Foon) GetByQueryWithoutCache(key *Key, src interface{}, conditions *Conditions) error

func (*Foon) GetGroupByQuery added in v0.10.0

func (s *Foon) GetGroupByQuery(src interface{}, conditions *Conditions) error

func (*Foon) GetMulti

func (s *Foon) GetMulti(src interface{}) error

func (*Foon) GetMultiWithoutCache

func (s *Foon) GetMultiWithoutCache(src interface{}) error

func (*Foon) GetWithoutCache

func (s *Foon) GetWithoutCache(src interface{}) error

func (*Foon) Insert

func (s *Foon) Insert(src interface{}) error

func (*Foon) InsertMulti

func (s *Foon) InsertMulti(slices interface{}) error

func (*Foon) LastCursor added in v0.10.0

func (s *Foon) LastCursor() string

func (*Foon) Put

func (s *Foon) Put(src interface{}) error

func (*Foon) PutMulti

func (s *Foon) PutMulti(slices interface{}) error

func (*Foon) RunInTransaction

func (s *Foon) RunInTransaction(fn func(f *Foon) error, options ...firestore.TransactionOption) error

func (*Foon) SetLogger

func (s *Foon) SetLogger(logger Logger)

type FoonError

type FoonError string
const (
	NoSuchDocument FoonError = "NoSuchEntity"
	InvalidId      FoonError = "InvalidID"
)

func (FoonError) Error

func (f FoonError) Error() string

func (FoonError) Is

func (f FoonError) Is(err error) bool

func (FoonError) IsNot

func (f FoonError) IsNot(err error) bool

type IURI

type IURI interface {
	URI() string
}

type Key

type Key struct {
	ParentPath string
	Collection string
	ID         string
}

func KeyError

func KeyError(src interface{}) (*Key, error)

func NewKey

func NewKey(src interface{}) *Key

func NewKeyWithPath

func NewKeyWithPath(fullPath string) *Key

func (Key) CollectionPath

func (k Key) CollectionPath() string

func (Key) CreateCollectionRef

func (k Key) CreateCollectionRef(client *firestore.Client) *firestore.CollectionRef

func (Key) CreateDocumentRef

func (k Key) CreateDocumentRef(client *firestore.Client) *firestore.DocumentRef

func (Key) CreateGroupCollectionRef added in v0.10.0

func (k Key) CreateGroupCollectionRef(client *firestore.Client) *firestore.CollectionGroupRef

func (Key) Equals

func (k Key) Equals(other *Key) bool

func (*Key) EqualsEntity added in v0.10.0

func (k *Key) EqualsEntity(src interface{}) bool

func (Key) HasUniqueID

func (k Key) HasUniqueID() bool

func (*Key) Inject added in v0.10.0

func (k *Key) Inject(src interface{}) error

func (*Key) IsSameKind added in v0.10.0

func (k *Key) IsSameKind(src interface{}) bool

func (*Key) ParentKey added in v0.10.0

func (k *Key) ParentKey() *Key

func (Key) Path

func (k Key) Path() string

func (*Key) SamePath

func (k *Key) SamePath(ref *firestore.DocumentRef) bool

func (Key) SelfPath

func (k Key) SelfPath() string

func (Key) URI

func (k Key) URI() string

func (*Key) Update

func (k *Key) Update(fullPath string)

type KeyAndData

type KeyAndData struct {
	Key *Key
	Src interface{}
}

type Limit

type Limit int

func (Limit) Hash

func (w Limit) Hash() string

func (Limit) Order

func (w Limit) Order() int

func (Limit) Queryer

func (w Limit) Queryer(query firestore.Query) firestore.Query

type Logger

type Logger interface {
	Trace(message string)
	Warning(message string)
}

type MetadataItem

type MetadataItem struct {
	Key  IURI
	Data interface{}
}

type Offset

type Offset int

func (Offset) Hash

func (w Offset) Hash() string

func (Offset) Order

func (w Offset) Order() int

func (Offset) Queryer

func (w Offset) Queryer(query firestore.Query) firestore.Query

type Order

type Order struct {
	Column    string
	Direction firestore.Direction
}

func (Order) Hash

func (w Order) Hash() string

func (Order) Order

func (w Order) Order() int

func (Order) Queryer

func (w Order) Queryer(query firestore.Query) firestore.Query

type Queries

type Queries []Query

func (Queries) Len

func (q Queries) Len() int

func (Queries) Less

func (q Queries) Less(i, j int) bool

func (Queries) Swap

func (q Queries) Swap(i, j int)

type Query

type Query interface {
	Queryer(query firestore.Query) firestore.Query
	Hash() string
	Order() int
}

type StartAfter added in v0.10.0

type StartAfter struct {
}

func (StartAfter) Hash added in v0.10.0

func (w StartAfter) Hash(cursor *Cursor) string

func (StartAfter) Queryer added in v0.10.0

func (w StartAfter) Queryer(query firestore.Query, cursor *Cursor, f *Foon) (firestore.Query, error)

type Where

type Where struct {
	Column    string
	Operation string
	Value     interface{}
}

func (Where) Hash

func (w Where) Hash() string

func (Where) Order

func (w Where) Order() int

func (Where) Queryer

func (w Where) Queryer(query firestore.Query) firestore.Query

type WriteBatch

type WriteBatch interface {
	Create(data interface{}) WriteBatch
	Set(data interface{}, opts ...firestore.SetOption) WriteBatch
	Delete(key *Key, opts ...firestore.Precondition) WriteBatch
	Commit() error
}

type WriteBatchImpl

type WriteBatchImpl struct {
	// contains filtered or unexported fields
}

func (*WriteBatchImpl) Commit

func (b *WriteBatchImpl) Commit() error

func (*WriteBatchImpl) Create

func (b *WriteBatchImpl) Create(data interface{}) WriteBatch

func (*WriteBatchImpl) Delete

func (b *WriteBatchImpl) Delete(key *Key, opts ...firestore.Precondition) WriteBatch

func (*WriteBatchImpl) Set

func (b *WriteBatchImpl) Set(data interface{}, opts ...firestore.SetOption) WriteBatch

Jump to

Keyboard shortcuts

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