document

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: 5 Imported by: 1

Documentation

Overview

Package document provides generic methods to interact with Firestore documents.

A Fuego document provides useful functions allowing to easily manipulate Firestore documents.

Usage

Creating a document is straightforward:

	type User struct {
		FirstName       string              `firestore:"FirstName"`
		LastName        string              `firestore:"LastName"`
		EmailAddress    string              `firestore:"EmailAddress"`
		Address         string              `firestore:"Address"`
		Tokens          map[string]string   `firestore:"Tokens"`
	}

	err := fuego.Document("users", "jsmith").Create(ctx, user)
    if err != nil {
        panic(err)
    }

In some cases, you may want to create a document and let Firebase generated a unique ID for you. Fuego supports this:

err := fuego.DocumentWithGeneratedID("users").Create(ctx, user)

Retrieving a document as even simpler:

user := User{}
err := fuego.Document("users", "jsmith").Retrieve(ctx, &user)

You also may want to only check if a given document exists without providing a struct:

// Note: false will be returned if an error occurs as well
value := fuego.Document("users", "jsmith").Exists(ctx)

Fields - All types

Fuego also allow to easily manipulate specific fields of a document (e.g. retrieve, update, increment, ...) It supports all the fields offered by Firestore at the exception of geopoints and references.

Usage

Retrieving a field is almost as simple as retrieving a document:

value, err := fuego.Document("users", "jsmith").String("FirstName").Retrieve(ctx)

Updating a field:

err := fuego.Document("users", "jsmith").String("FirstName").Update(ctx, "Mike")

Fields - Numbers

Numbers are stored in Firestore as int64. Fuego provides operations that are frequently performed with number fields.

For instance, you may want to increment or decrement a number field. With Fuego, this can be done in one single operatipn. It uses Transactions underneath.

err := fuego.Document("users", "jsmith").Number(Age).Increment(ctx)
// or
err := fuego.Document("users", "ben_button").Number(Age).Decrement(ctx)

Fields - Arrays

As you may know, dealing with arrays in Firestore documents can be somewhat of a burden. When updating an array field, the entire field is overridden. Using Fuego, you can decide to either override or append to the field:

err := fuego.Document("users", "jsmith").
Array("Address").
Override(ctx, []interface{}{"4th Floor"})

// or...

err := fuego.Document("users", "jsmith").
Array("Address").
Append(ctx, []interface{}{"4th Floor"})

Of course, you can also retrieve an array as follow:

values, err := fuego.Document("users", "jsmith").Array("Address").Retrieve(ctx)
if err != nil {
	panic(err)
}

// Note the required type assertion
fmt.Println("First Element: ", values[0].(string))

Fields - Timestamp

Timestamp fields work the same way as the other fields except that a timezone (IANA Time Zone) needs to be provided at retrieval:

val, err := fuego.Document("users", "jsmith").Timestamp("LastSeenAt").Retrieve(ctx, "America/Los_Angeles")

Package document provides generic methods to interact with Firestore documents.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDocumentNotExist indicates that the requested document doesn't exist.
	ErrDocumentNotExist = errors.New("document: doesn't exist")

	// ErrFieldRetrieve indicates that the requested field value could not be retrieved.
	ErrFieldRetrieve = errors.New("field: couldn't retrieve the field value")
)

Functions

This section is empty.

Types

type Array

type Array struct {

	// Document is the underlying document (incl. ID and ref).
	Document Document

	// Name is the name of the field.
	Name string
	// contains filtered or unexported fields
}

Array represents a document field of type Array.

func (*Array) Append

func (f *Array) Append(ctx context.Context, data []interface{}) error

Append will append the provided data to the existing data (if any) of an Array field.

The update will be executed inside a transaction.

values, err := fuego.Document("users", "jsmith").Array("Address").Append(ctx, []interface{}{"More info"})

func (*Array) Override

func (f *Array) Override(ctx context.Context, data []interface{}) error

Override will override the existing data (if any) of an Array field.

values, err := fuego.Document("users", "jsmith").Array("Address").Override(ctx, []interface{}{"New Street", "New Building"})

func (*Array) Retrieve

func (f *Array) Retrieve(ctx context.Context) ([]interface{}, error)

Retrieve returns the content of a specific field for a given document.

values, err := fuego.Document("users", "jsmith").Array("Address").Retrieve(ctx)

type ArrayField

type ArrayField interface {

	// Retrieve returns the value of a specific field containing an array.
	//
	// note : the returned data will require a type assertion.
	Retrieve(ctx context.Context) ([]interface{}, error)

	// Append will append the provided data to the existing data (if any) of an Array field.
	Append(ctx context.Context, data []interface{}) error

	// Override will override the existing data (if any) of an Array field.
	// Note: this is the default behaviour with Firestore.
	Override(ctx context.Context, data []interface{}) error
}

ArrayField provides the necessary to interact with a Firestore document field of type Array.

type Boolean

type Boolean struct {

	// Document is the underlying document (incl. ID and ref).
	Document Document

	// Name is the name of the field.
	Name string
}

Boolean represents a document field of type Boolean.

func (*Boolean) Retrieve

func (f *Boolean) Retrieve(ctx context.Context) (bool, error)

Retrieve returns the content of a specific field for a given document.

val, err := fuego.Document("users", "jsmith").Boolean("Premium").Retrieve(ctx)

func (*Boolean) Update

func (f *Boolean) Update(ctx context.Context, with bool) error

Update updates the value of a specific field of type Boolean.

err := fuego.Document("users", "jsmith").Boolean("Premium").Update(ctx, true)

type BooleanField

type BooleanField interface {

	// Retrieve returns the value of a specific field containing a Boolean (bool).
	Retrieve(ctx context.Context) (bool, error)

	// Update updates the value of a specific field containing a Boolean (bool).
	Update(ctx context.Context, with bool) error
}

BooleanField provides the necessary to interact with a Firestore document field of type Boolean.

type Document

type Document interface {

	// Create a document.
	Create(ctx context.Context, from interface{}) error

	// Retrieve populate the destination passed as parameter.
	//
	// note: the `to` parameter has to be a pointer.
	Retrieve(ctx context.Context, to interface{}) error

	// Exists returns true if the document exists, false otherwise.
	//
	// note: if an error occurs, false is also returned.
	Exists(ctx context.Context) bool

	// Delete removes a document from Firestore.
	Delete(ctx context.Context) error

	// Array returns a specific Array field.
	Array(name string) *Array

	// String returns a specific String field.
	String(name string) *String

	// Number returns a specific Number field.
	Number(name string) *Number

	// Boolean returns a specific Boolean field.
	Boolean(name string) *Boolean

	// Map returns a specific Map field.
	Map(name string) *Map

	// Timestamp returns a specific Timestamp field.
	Timestamp(name string) *Timestamp

	// GetDocumentRef returns a Document Reference (DocumentRef).
	GetDocumentRef() *firestore.DocumentRef

	// InBatch returns true if a WriteBatch has been started, false otherwise.
	InBatch() bool

	// Batch returns the pointer to the WriteBatch (if any), nil oherwise.
	Batch() *firestore.WriteBatch
}

Document provides the necessary to interact with a Firestore document.

type FirestoreDocument

type FirestoreDocument struct {

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

	// ID is the ID of the document
	ID string
	// contains filtered or unexported fields
}

FirestoreDocument provides features related to Firestore documents.

func New

func New(fs *firestore.Client, path, documentID string, wb *firestore.WriteBatch) *FirestoreDocument

New creates and returns a new FirestoreDocument.

func (*FirestoreDocument) Array

func (d *FirestoreDocument) Array(name string) *Array

Array returns a new Array.

func (*FirestoreDocument) Batch

Batch returns the pointer to the WriteBatch (if any), nil oherwise.

func (*FirestoreDocument) Boolean

func (d *FirestoreDocument) Boolean(name string) *Boolean

Boolean returns a new Boolean.

func (*FirestoreDocument) Create

func (d *FirestoreDocument) Create(ctx context.Context, from interface{}) error

Create a document in Firestore.

func (*FirestoreDocument) Delete

func (d *FirestoreDocument) Delete(ctx context.Context) error

Delete removes a document from Firestore.

func (*FirestoreDocument) Exists

func (d *FirestoreDocument) Exists(ctx context.Context) bool

Exists returns true if a given document exists, false otherwise.

func (*FirestoreDocument) GetDocumentRef

func (d *FirestoreDocument) GetDocumentRef() *firestore.DocumentRef

GetDocumentRef returns a document reference.

func (*FirestoreDocument) InBatch

func (d *FirestoreDocument) InBatch() bool

InBatch returns true if a WriteBatch has been started, false otherwise.

func (*FirestoreDocument) Map

func (d *FirestoreDocument) Map(name string) *Map

Map returns a new Map.

func (*FirestoreDocument) Number

func (d *FirestoreDocument) Number(name string) *Number

Number returns a new Number.

func (*FirestoreDocument) Retrieve

func (d *FirestoreDocument) Retrieve(ctx context.Context, to interface{}) error

Retrieve a document from Firestore.

to: the destination must be a pointer.

func (*FirestoreDocument) String

func (d *FirestoreDocument) String(name string) *String

String returns a new String.

func (*FirestoreDocument) Timestamp

func (d *FirestoreDocument) Timestamp(name string) *Timestamp

Timestamp returns a new Timestamp.

type Map

type Map struct {

	// Document is the underlying document (incl. ID and ref).
	Document Document

	// Name is the name of the field.
	Name string
	// contains filtered or unexported fields
}

Map represents a document field of type Map.

func (*Map) Merge

func (f *Map) Merge(ctx context.Context, data map[string]interface{}) error

Merge merges the value of a specific Map field.

func (*Map) Override

func (f *Map) Override(ctx context.Context, data map[string]interface{}) error

Override simply update (override) the field with a given Map.

func (*Map) Retrieve

func (f *Map) Retrieve(ctx context.Context) (map[string]interface{}, error)

Retrieve returns the content of a specific field for a given document.

type MapField

type MapField interface {

	// Retrieve returns the value of a specific field containing a map.
	//
	// note : the returned values will require type assertion.
	Retrieve(ctx context.Context) (map[string]interface{}, error)

	// Merge will merge the provided data with the existing data (if any) of a Map field.
	// Note: this is the default behaviour with Firestore.
	Merge(ctx context.Context, data map[string]interface{}) error

	// Override will override the existing data (if any)  withthe provided data.
	Override(ctx context.Context, data map[string]interface{}) error
}

MapField provides the necessary to interact with a Firestore document field of type Map.

type Number

type Number struct {

	// Document is the underlying document (incl. ID and ref).
	Document Document

	// Name is the name of the field.
	Name string
	// contains filtered or unexported fields
}

Number represents a document field of type Number.

func (*Number) Decrement

func (f *Number) Decrement(ctx context.Context) error

Decrement the value of a specific field of type Number.

The update will be executed inside a transaction. If the field doesn't exist, it will be set to 0.

err := fuego.Document("users", "jsmith").Number("Age").Decrement(ctx)

func (*Number) Increment

func (f *Number) Increment(ctx context.Context) error

Increment the value of a specific field of type Number.

The update will be executed inside a transaction. If the field doesn't exist, it will be set to 1.

err := fuego.Document("users", "jsmith").Number("Age").Increment(ctx)

func (*Number) Retrieve

func (f *Number) Retrieve(ctx context.Context) (int64, error)

Retrieve returns the content of a specific field for a given document.

nb, err := fuego.Document("users", "jsmith").Number("Age").Retrieve(ctx)

func (*Number) Update

func (f *Number) Update(ctx context.Context, with int64) error

Update the value of a specific field of type Number.

err := fuego.Document("users", "jsmith").Number("Age").Update(ctx, 42).

type NumberField

type NumberField interface {

	// Retrieve returns the value of a specific field containing a number (int64).
	Retrieve(ctx context.Context) (int64, error)

	// Update the value of a specific field containing a number (int64).
	Update(ctx context.Context, with int64) error

	// Increment the value of a specific field containing a number (int64).
	// If the field doesn't exist, it will be set to 1.
	Increment(ctx context.Context) error

	// Decrement the value of a specific field containing a number (int64).
	// If the field doesn't exist, it will be set to 0.
	Decrement(ctx context.Context) error
}

NumberField provides the necessary to interact with a Firestore document field of type Number.

type String

type String struct {

	// Document is the underlying document (incl. ID and ref).
	Document Document

	// Name is the name of the field.
	Name string
}

String represents a document field of type String.

func (*String) Retrieve

func (f *String) Retrieve(ctx context.Context) (string, error)

Retrieve returns the content of a specific field for a given document.

str, err := fuego.Document("users", "jsmith").String("FirstName").Retrieve(ctx)

func (*String) Update

func (f *String) Update(ctx context.Context, with string) error

Update updates the value of a specific field of type String.

err := fuego.Document("users", "jsmith").String("FirstName").Update(ctx, "Jane")

type StringField

type StringField interface {

	// Retrieve returns the value of a specific field containing an string.
	Retrieve(ctx context.Context) (string, error)

	// Update updates the value of a specific field containing a string.
	Update(ctx context.Context, with string) error
}

StringField provides the necessary to interact with a Firestore document field of type String.

type Timestamp

type Timestamp struct {

	// Document is the underlying document (incl. ID and ref).
	Document Document

	// Name is the name of the field.
	Name string
}

Timestamp represents a document field of type Timestamp.

func (*Timestamp) Retrieve

func (f *Timestamp) Retrieve(ctx context.Context, location string) (time.Time, error)

Retrieve returns the content of a specific field for a given document.

location needs to be a value from the IANA Time Zone database A time.Time zero value will be returned if an error occurs.

val, err := fuego.Document("users", "jsmith").Timestamp("LastSeenAt").Retrieve(ctx, "America/Los_Angeles")

func (*Timestamp) Update

func (f *Timestamp) Update(ctx context.Context, with time.Time) error

Update updates the value of a specific field of type Timestamp.

err := fuego.Document("users", "jsmith").Timestamp("LastSeenAt").Update(ctx, time.Now())

type TimestampField

type TimestampField interface {

	// Retrieve returns the value of a specific field containing a timestamp (time.Time).
	//
	// location needs to be a value from the IANA Time Zone database (e.g. "America/Los_Angeles")
	// A time.Time zero value will be returned if an error occurs.
	Retrieve(ctx context.Context, location string) (time.Time, error)

	// Update updates the value of a specific field containing a timestamp (time.Time).
	Update(ctx context.Context, with time.Time) error
}

TimestampField provides the necessary to interact with a Firestore document field of type Timestamp.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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