storage

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2022 License: MIT Imports: 10 Imported by: 0

README

Storage

The firestore (Google Firebase database) is used as the persistent storage

Model (next model) is a interface for custom structure with user data. It stores user data and specifies in which collection is stored. It also implements the method of creating an instance of itself.

The model can be standard or with a custom ID.

Standart Model

Example of a standard model

type Event struct {
    StdModel // This is a required field for standard storage models

    // user fields
    EventName string
    EventAt time.Time
    ...
}

Any user storage model must

  • method that specifies of model storage collection name CollectionName() string
  • method of creating a model instance NewModel() StoreModel

So we add two methods

var _ Model = (*Event)(nil)
var EventModel = (*Event)(nil)

func (*Event) NewModel() StoreModel {
	return &Event{}
}


func (t *Event) CollectionName() string {
	return "<firestore collection name>"
}

Done. The model can be used in Storage. Because it implements the interface Model.

ctx := context.TODO()
s := NewStorage(/*TODO*/)

// event := (*Event)(nil).NewModel().(*Event)
// event.SetModelID("123)
// or
// event := EventModel.NewModel().(*Event)
// event.SetModelID("123)
// or
// event := &Event{StdModel: StdModel{PrimaryData: PrimaryData{ID: "123"}}}
// or
// event := NewWithID((*Event)(nil), "123")
// or

event := NewWithID(EventModel, "123")

// get model by primary ID
s.GetModel(ctx, event)

// change
event.EventAt = time.Now().UTC()

// apply changes
s.UpsertModel(ctx, event)

// remove if exists model
if event.Exsits() {
    s.DeleteModel(ctx, event)
}

Custom ID Model

type Event struct {
    ModelCustomID // This is a required field for storage models with custom logic for ID
    ...
}

And in addition to the mandatory fields must ba added

  • implement ModelID() string
  • implement SetModelID(string)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound                = errors.New("not found")
	ErrAlreadyExists           = errors.New("already exists")
	ErrInvalidModelEmptyID     = errors.New("invalid model: empty ID")
	ErrStorageModelMustBeEmpty = errors.New("storage: model must be empty")
)

Functions

func DocRef

func DocRef(db *firestore.Client, model Model) *firestore.DocumentRef

DocRef returns the firestore.DocumentRef based on model.

func LoadDocAndPopulate

func LoadDocAndPopulate(ctx context.Context, docRef *firestore.DocumentRef, model Model) error

LoadDocAndPopulate looks up firestore document by ID in the store and uses to set fields in model.

func PopulateModelFrom

func PopulateModelFrom(model Model, doc *firestore.DocumentSnapshot) error

PopulateModelFrom set fields in model from firestore document.

Types

type CustomModelID

type CustomModelID interface {
	ModelID() string
	SetModelID(in string)
}

A helper interface for custom storage models with a custom implementation of model ID storage.

type DocumentRef

type DocumentRef = firestore.DocumentRef

type Model

type Model interface {
	// returns new empy model
	NewModel() Model

	// Exists returns true if model (after retrieving from the database) existing in database.
	Exists() bool
	ModelID() string
	SetModelID(in string)
	// CollectionName returns name of the collection (in firestore) where stored model.
	CollectionName() string
	// ModelUpdatedAt returns update datetime in the database.
	// if model is exists returns valid value
	// if the model does not exist returns empty time.Time (time.Time{}.IsZero() == true)
	ModelUpdatedAt() time.Time
	// ModelUpdatedAt returns create datetime in the database.
	// if model is exists returns valid value
	// if the model does not exist returns empty time.Time (time.Time{}.IsZero() == true)
	ModelCreatedAt() time.Time
	// contains filtered or unexported methods
}

Model for custom structure with user data. It stores user data and specifies in which collection is stored. It also implements the method of creating an instance of itself.

func IterateAllDocsAndStop

func IterateAllDocsAndStop(iter *firestore.DocumentIterator, kind Model) ([]Model, error)

IterateAllDocsAndStop iterate through the records and to populate in model.

func NewWithID

func NewWithID(kind Model, id string) Model

helper method

type ModelCustomID

type ModelCustomID struct {
	ServiceData
}

type ModelImpl

type ModelImpl interface {
	// CollectionName returns name of the collection (in firestore) where stored model.
	CollectionName() string
	NewModel() Model
}

A helper interface specifying the requirements for custom storage models.

type PrimaryData

type PrimaryData struct {
	ID string `firestore:"-"`
}

func (*PrimaryData) ModelID

func (m *PrimaryData) ModelID() string

func (*PrimaryData) SetModelID

func (m *PrimaryData) SetModelID(in string)

type ServiceData

type ServiceData struct {
	Doc *firestore.DocumentSnapshot `firestore:"-"`
}

Implements a mandatory interface for any storage model.

func (*ServiceData) Exists

func (m *ServiceData) Exists() bool

func (*ServiceData) ModelCreatedAt

func (m *ServiceData) ModelCreatedAt() time.Time

func (*ServiceData) ModelUpdatedAt

func (m *ServiceData) ModelUpdatedAt() time.Time

type StdModel

type StdModel struct {
	ServiceData
	PrimaryData
}

type Storage

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

func NewStorage

func NewStorage(db *firestore.Client) *Storage

func (*Storage) DeleteModel

func (s *Storage) DeleteModel(ctx context.Context, model Model) error

DeleteModel deletes the model.

func (*Storage) DocRef

func (s *Storage) DocRef(model Model) *DocumentRef

func (*Storage) FirestoreClient

func (s *Storage) FirestoreClient() *firestore.Client

func (*Storage) GetModel

func (s *Storage) GetModel(ctx context.Context, model Model) error

GetModel looks up model by ID in the store and populate to model.

func (*Storage) Iterate

func (s *Storage) Iterate(iter *firestore.DocumentIterator, factory Model) []Model

Iterate iterate through the records and to populate in model.

func (*Storage) LoadToModel

func (s *Storage) LoadToModel(ctx context.Context, docRef *firestore.DocumentRef, model Model) error

LoadToModel looks up firestore document by ID in the store and uses to set fields in model.

func (*Storage) UpsertIfNotExists

func (s *Storage) UpsertIfNotExists(ctx context.Context, model Model) error

UpsertIfNotExists helper method for to perform update if the model does not exist in the storage. WARN: Model.Exsits() returns false after successfully upsert

func (*Storage) UpsertModel

func (s *Storage) UpsertModel(ctx context.Context, model Model) error

UpsertModel updates or creates a model in the store. WARN: Model.Exsits() returns false after successfully upsert

type Timestamp

type Timestamp = timestamp.Timestamp

Jump to

Keyboard shortcuts

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