Documentation
¶
Overview ¶
Package firestore provides functionality for interacting with Google Cloud Firestore database
Index ¶
- Constants
- Variables
- func Client(ctx context.Context, database DatabaseName) (*fs.Client, error)
- func ClosingWhenDoneSeq[T any](seq iter.Seq[T], client *fs.Client) iter.Seq[T]
- func ClosingWhenDoneSeq2[K, V any](seq2 iter.Seq2[K, V], client *fs.Client) iter.Seq2[K, V]
- func CollectionIterToSeq(ci *fs.CollectionIterator) iter.Seq[*fs.CollectionRef]
- func Count(ctx context.Context, query fs.Query) int64
- func CountDuplicateDocumentIds(ctx context.Context, databaseName DatabaseName, collectionPath string) (iter.Seq2[string, int], error)
- func DeleteCollection(ctx context.Context, client *fs.Client, path string) error
- func DocRefIDKeyer() containers.Keyer[fs.DocumentRef]
- func DocSnapShotKeyer() containers.Keyer[fs.DocumentSnapshot]
- func DocSnapShotSeq2ToType[V any](it iter.Seq2[string, *fs.DocumentSnapshot]) iter.Seq2[string, *V]
- func DocSnapShotSeqToType[R any](it iter.Seq[*fs.DocumentSnapshot]) iter.Seq[*R]
- func DocSnapShotToType[T any](dss *fs.DocumentSnapshot) (*T, error)
- func DocumentIterToTypeSeq[T any](di *fs.DocumentIterator) iter.Seq[*T]
- func DocumentIteratorToSeq(dsi *fs.DocumentIterator) iter.Seq[*fs.DocumentSnapshot]
- func DocumentIteratorToSeq2(dsi *fs.DocumentIterator) iter.Seq2[string, *fs.DocumentSnapshot]
- func Exists(err error) bool
- func IsAlreadyExists(err error) bool
- func IsNotFound(err error) bool
- func MapToUpdates(m map[string]interface{}) []fs.Update
- func NewCollectionStore[T any](database DatabaseName, collection string, keyerFunc containers.Keyer[T]) *collectionStore[T]
- type BulkStoreErrorHandling
- type CollectionName
- type CollectionStore
- type DatabaseName
- type Projection
- type Queries
- type Query
- type QueryOp
- type WherePredicate
Constants ¶
const DocumentCreated = "google.cloud.firestore.v1.created"
const DocumentDeleted = "google.cloud.firestore.v1.deleted"
const DocumentUpdated = "google.cloud.firestore.v1.updated"
const DocumentWritten = "google.cloud.firestore.v1.written"
const MAX_BULK_WRITE_SIZE = 500
MAX_BULK_WRITE_SIZE defines the maximum number of operations that can be performed in a single bulk write [maximum number of field transformations that can be performed on a single document in a Commit operation or in a transaction] : https://firebase.google.com/docs/firestore/quotas
Variables ¶
var All = NewProjection("*")
All returns all fields of the document
var BulkWriterError = errorx.IllegalState.NewSubtype("Bulk Writer Error")
var OnlyDocumentId = NewProjection()
OnlyDocumentId returns only the document ID (as in doc.Ref.ID) not the "id" that might be on the document if you want only that document id use NewProjection
var QueryOps = struct { Equals QueryOp NotEquals QueryOp LessThan QueryOp LessThanOrEqual QueryOp GreaterThan QueryOp GreaterThanOrEquals QueryOp ArrayContains QueryOp ArrayContainsAny QueryOp In QueryOp NotIn QueryOp }{ Equals: "==", NotEquals: "!=", LessThan: "<", LessThanOrEqual: "<=", GreaterThan: ">", GreaterThanOrEquals: ">=", ArrayContains: "array-contains", ArrayContainsAny: "array-contains-any", In: "in", NotIn: "not-in", }
Functions ¶
func ClosingWhenDoneSeq ¶
ClosingWhenDoneSeq wraps the provided iter.Seq and ensures that fs.Client.Close() is called after the last item is provided by the Seq.
func ClosingWhenDoneSeq2 ¶
ClosingWhenDoneSeq2 wraps the provided iter.Seq2 and ensures that fs.Client.Close() is called after the last item is provided by the Seq2.
func CollectionIterToSeq ¶
func CollectionIterToSeq(ci *fs.CollectionIterator) iter.Seq[*fs.CollectionRef]
CollectionIterToSeq converts a Firestore CollectionIterator to an iter.Seq of CollectionRefs. It handles the iteration and error handling internally, providing a simplified interface to process collections. Returns an iter.Seq that yields *fs.CollectionRef values.
func CountDuplicateDocumentIds ¶
func CountDuplicateDocumentIds(ctx context.Context, databaseName DatabaseName, collectionPath string) (iter.Seq2[string, int], error)
CountDuplicateDocumentIds finds and reports duplicate document IDs within a Firestore collection. It takes the Firestore client and the collection path as input. It returns an iter.Seq2[string,int] where keys are duplicate document Ids and values are the number of occurrences. If no duplicates are found, it returns an empty iter.Seq2[string,int. Returns an error if one occurs.
func DeleteCollection ¶
DeleteCollection deletes all documents in a Firestore collection using a BulkWriter. It processes documents in parallel batches of size MAX_BULK_WRITE_SIZE.
ctx is the context for the operation client is the Firestore client to use path is the path to the collection to delete
Returns an error if any document deletion fails, nil on success
func DocRefIDKeyer ¶
func DocRefIDKeyer() containers.Keyer[fs.DocumentRef]
DocRefIDKeyer returns a Keyer function that extracts the ID of a DocumentRef as a string. This is useful for creating maps or sets keyed by DocumentRef IDs.
func DocSnapShotKeyer ¶
func DocSnapShotKeyer() containers.Keyer[fs.DocumentSnapshot]
DocSnapShotKeyer returns a Keyer function that extracts the ID of a DocumentSnapshot as a string.
func DocSnapShotSeq2ToType ¶
DocSnapShotSeq2ToType converts a Seq2 of DocumentSnapshots to a Seq2 of type V.
func DocSnapShotSeqToType ¶
DocSnapShotSeqToType converts a Seq of DocumentSnapshots to a Seq of type R. Deprecated: Use DocumentIterToTypeSeq which automatically wraps the fs.DocumentIterator and more efficient data processing. This delegates to an unexported implementation, so this can be deleted in a future release.
func DocSnapShotToType ¶
func DocSnapShotToType[T any](dss *fs.DocumentSnapshot) (*T, error)
DocSnapShotToType unmarshals a Firestore DocumentSnapshot into a struct of type T.
func DocumentIterToTypeSeq ¶
func DocumentIterToTypeSeq[T any](di *fs.DocumentIterator) iter.Seq[*T]
DocumentIterToTypeSeq converts a Firestore DocumentIterator to an iter.Seq of type T. It first converts the DocumentIterator to DocumentSnapshots using DocumentIteratorToSeq, then converts those snapshots to type T using DocSnapShotSeqToType. Type parameter T should be the target type to unmarshal the documents into. Returns an iter.Seq that yields pointers to values of type T.
func DocumentIteratorToSeq ¶
func DocumentIteratorToSeq(dsi *fs.DocumentIterator) iter.Seq[*fs.DocumentSnapshot]
DocumentIteratorToSeq converts a firestore.Iterator to an iter.Seq. value is a pointer to the type V
func DocumentIteratorToSeq2 ¶
func DocumentIteratorToSeq2(dsi *fs.DocumentIterator) iter.Seq2[string, *fs.DocumentSnapshot]
DocumentIteratorToSeq2 converts a firestore.Iterator to an iter.Seq2. doc.Ref.ID is used as the "key" or first value, second value is a pointer to the type V
func IsAlreadyExists ¶
IsAlreadyExists checks if the given error is a Firestore "already exists" error. It returns true if the error is not nil and its status code indicates the resource already exists.
func IsNotFound ¶
IsNotFound checks if the given error is a Firestore "not found" error.
func MapToUpdates ¶
MapToUpdates converts a map to a slice of Firestore Update structs.
func NewCollectionStore ¶
func NewCollectionStore[T any](database DatabaseName, collection string, keyerFunc containers.Keyer[T]) *collectionStore[T]
NewCollectionStore creates a new collectionStore for a given database, collection, and keyer function.
Types ¶
type BulkStoreErrorHandling ¶
type BulkStoreErrorHandling string
const ( FAIL_ON_FIRST_ERROR BulkStoreErrorHandling = "fail_on_first_error" COLLECT_ERRORS BulkStoreErrorHandling = "collect_errors" )
func (BulkStoreErrorHandling) ErrGroup ¶
func (bs BulkStoreErrorHandling) ErrGroup(ctx context.Context) *errgroup.Group
func (BulkStoreErrorHandling) String ¶
func (bs BulkStoreErrorHandling) String() string
type CollectionName ¶
type CollectionName string
CollectionName represents the name of a Firestore collection
type CollectionStore ¶
type CollectionStore[T any] interface { All() iter.Seq2[string, *T] Load(id string) (*T, error) Find(where WherePredicate, selectPaths Projection) iter.Seq[*T] Store(v *T) (*T, error) BulkStore(iter iter.Seq[*T], errorHandling BulkStoreErrorHandling) error Remove(id string) error BulkRemove(iter iter.Seq[string], errorHandling BulkStoreErrorHandling) error }
type DatabaseName ¶
type DatabaseName string
DatabaseName represents the name of a Firestore database
const DEFAULT DatabaseName = fs.DefaultDatabaseID
type Projection ¶
type Projection struct {
// contains filtered or unexported fields
}
Projection defines the fields to be returned in a query result
func NewProjection ¶
func NewProjection(paths ...string) Projection
NewProjection creates a new projection instance for querying specific fields from Firestore documents. The paths parameter accepts dot-separated paths that specify the fields to select. For example, "user.address.street" will be split into a FieldPath ["user", "address", "street"]. Each field path is converted to a fs.FieldPath internally for use in Firestore queries.
func (Projection) String ¶
func (proj Projection) String() string
type Queries ¶
type Queries interface { ByKey(key string) *byKeyQuery BeforeTime(field string, before time.Time) *timeBeforeQuery AfterTime(field string, after time.Time) *timeAfterQuery TimeBetween(field string, start time.Time, end time.Time) *timeBetweenQuery TimeEquals(field string, value time.Time) *timeEqualsQuery StringEquals(field string, value string) *stringEqualsQuery IntEquals(field string, value int) *intEqualsQuery BoolEquals(field string, value bool) *boolEqualsQuery }
Queries interface defines the methods for creating query instances.
type Query ¶
type Query interface {
Execute(ctx context.Context) (*fs.DocumentIterator, error)
}
Query interface defines the Execute method for all query types.
func NewQuery ¶
func NewQuery(collection *fs.CollectionRef) Query
NewQuery creates a new Query instance for the specified Firestore collection.