collection

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2020 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Package collection provides generic methods to interact with Firestore collections.

Package collection provides generic methods to interact with Firestore collections.

A Fuego collection provides useful functions allowing to easily manipulate Firestore collections.

Usage

Retrieving a collection is straightforward:

users, err := fuego.Collection("users").Retrieve(ctx, &User{})
if err != nil {
	panic(err)
}

// Note the required type assertion
fmt.Println("FirstName: ", users[0].(*User).FirstName) // prints John

When it comes to performing more complex queries, it just works like the firestore client. Fuego's collection struct embeds a firestore.Query (https://firebase.google.com/docs/firestore/query-data/queries). This allow to directly use its methods:

collection := fuego.Collection("users")
query := collection.Where("FirstName", "==", "John").Limit(50)
users, err := collection.RetrieveWith(ctx, &User{}, query)

Fuego also provide with the ability to set a value for a field for all documents within a given collection:

err := fuego.Collection("users").SetForAll(ctx, "Premium", true) // Yay!

Or simply delete all documents in the collection:

err := fuego.Collection("users").DeleteAll(ctx)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

type Collection interface {

	// Retrieve retrieve all the documents from a collection.
	//
	// note: the `sample` is a pointer to an initialized struct of the expected type.
	Retrieve(ctx context.Context, sample interface{}) ([]interface{}, error)

	// RetrieveWith retrieve documents from a collection using the provided Query.
	RetrieveWith(ctx context.Context, sample interface{}, query firestore.Query) ([]interface{}, error)

	// SetForAll sets a field with a given value for ALL documents in the collection.
	//
	// Note: uses Batched writes
	SetForAll(ctx context.Context, fieldName string, fieldValue interface{}) error

	// DeleteAll removes all items from the collection.
	//
	// Use precautiously ;)
	DeleteAll(ctx context.Context) error
}

Collection provides the necessary to interact with a Firestore collection.

type FirestoreCollection

type FirestoreCollection struct {

	// Ref (firestore.CollectionRef) is a reference to the collection.
	Ref *firestore.CollectionRef

	// Query (firestore.Query) is embedded so its methods can conveniently be used directly.
	firestore.Query
	// contains filtered or unexported fields
}

FirestoreCollection provides features related to Firestore collections.

func New

New creates and returns a new FirestoreCollection.

func (*FirestoreCollection) DeleteAll

func (c *FirestoreCollection) DeleteAll(ctx context.Context) error

DeleteAll removes all items from the collection.

err := fuego.Collection("users").DeleteAll(ctx)

func (*FirestoreCollection) Retrieve

func (c *FirestoreCollection) Retrieve(ctx context.Context, sample interface{}) ([]interface{}, error)

Retrieve retrieve all the documents from a collection.

values, err := fuego.Collection("users").Retrieve(ctx, &User{})

func (*FirestoreCollection) RetrieveWith

func (c *FirestoreCollection) RetrieveWith(ctx context.Context, sample interface{}, query firestore.Query) ([]interface{}, error)

RetrieveWith retrieve documents from a collection using the provided Query.

values, err := fuego.Collection("users").RetrieveWith(ctx, &User{}, query)

func (*FirestoreCollection) SetForAll

func (c *FirestoreCollection) SetForAll(ctx context.Context, fieldName string, fieldValue interface{}) error

SetForAll will set a field with a given value for ALL documents in the collection.

err := fuego.Collection("users").SetForAll(ctx, "NewField", "NewValue")

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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