这是indexloc提供的服务,不要输入任何密码

firestore

package
v0.0.0-...-5b2ba2d Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2025 License: GPL-3.0 Imports: 28 Imported by: 0

Documentation

Overview

Package firestore provides functionality for interacting with Google Cloud Firestore database

Index

Constants

View Source
const DocumentCreated = "google.cloud.firestore.v1.created"
View Source
const DocumentDeleted = "google.cloud.firestore.v1.deleted"
View Source
const DocumentUpdated = "google.cloud.firestore.v1.updated"
View Source
const DocumentWritten = "google.cloud.firestore.v1.written"
View Source
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

View Source
var All = NewProjection("*")

All returns all fields of the document

View Source
var BulkWriterError = errorx.IllegalState.NewSubtype("Bulk Writer Error")
View Source
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

View Source
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 Client

func Client(ctx context.Context, database DatabaseName) (*fs.Client, error)

Client creates a new Firestore client for the specified database.

func ClosingWhenDoneSeq

func ClosingWhenDoneSeq[T any](seq iter.Seq[T], client *fs.Client) iter.Seq[T]

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

func ClosingWhenDoneSeq2[K, V any](seq2 iter.Seq2[K, V], client *fs.Client) iter.Seq2[K, V]

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 Count

func Count(ctx context.Context, query fs.Query) int64

Count returns the number of documents that match the given query.

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

func DeleteCollection(ctx context.Context, client *fs.Client, path string) error

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

func DocSnapShotSeq2ToType[V any](it iter.Seq2[string, *fs.DocumentSnapshot]) iter.Seq2[string, *V]

DocSnapShotSeq2ToType converts a Seq2 of DocumentSnapshots to a Seq2 of type V.

func DocSnapShotSeqToType

func DocSnapShotSeqToType[R any](it iter.Seq[*fs.DocumentSnapshot]) iter.Seq[*R]

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 Exists

func Exists(err error) bool

Exists checks if the given error is not a Firestore "not found" error.

func IsAlreadyExists

func IsAlreadyExists(err error) bool

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

func IsNotFound(err error) bool

IsNotFound checks if the given error is a Firestore "not found" error.

func MapToUpdates

func MapToUpdates(m map[string]interface{}) []fs.Update

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 (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

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.

type QueryOp

type QueryOp string

The op argument must be one of "==", "!=", "<", "<=", ">", ">=", "array-contains", "array-contains-any", "in" or "not-in" QueryOp represents a Firestore query operation

func (QueryOp) String

func (q QueryOp) String() string

String returns the string representation of the QueryOp

type WherePredicate

type WherePredicate func(q fs.Query) fs.Query

WherePredicate is a function type that applies filtering conditions to a Firestore query

Jump to

Keyboard shortcuts

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