persistenceServices

package module
v0.0.0-...-17110eb Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

README

persistenceServices

Sharing out of good will and generally unsupported

... I'm still debating its usefulness so please raise an issue if you wish to contribute and we'll discuss

Thanks, Adrian

Introduction

This module implements a persistence layer, with optional CQRS

  • Google Firestore
  • Google Pubsub
  • [Future] Azure Cosmodb
  • [Future] Azure Event Hub
  • etc

Installation

go get github.com/adeturner/persistenceServices
go test

Implementation

Code

Define a docType, must implement documentType:

type documentType interface {
	String() string
	Topic() string
}

type DocType int

// example doc types
const (
	DOCUMENT_TYPE_USERS DocType = iota
	DOCUMENT_TYPE_ORDERS
	DOCUMENT_TYPE_STOCK
)

// Generic name for the Document, used as the Firestore Collection name
func (d DocType) String() string {
	return [...]string{
		"Users",
		"Orders",
		"Stock",
	}[d]
}

// Returns the eventing topic name; topic must be precreated
func (d DocType) Topic() string {
	return [...]string{
		"usersTopic",
		"ordersTopic",
		"stockTopic",
	}[d]
}

Instantiate the persistenceLayer

var persistenceLayer *persistenceServices.PersistenceLayer
ver err error
d := DOCUMENT_TYPE_USERS

// deprecated
p, err := GetPersistenceLayer(docType)

// new
p, err = GetLayer()
p.SetDocType(docType)

Available functions

func GetPersistenceLayer(docType documentType) (*PersistenceLayer, error) {
func (p *PersistenceLayer) AddDocument(key string, values interface{}) error {
func (p *PersistenceLayer) UpdateDocument(key string, values interface{}) error {
func (p *PersistenceLayer) DeleteDocument(key string, values interface{}) error {
func (p *PersistenceLayer) FindById(key string, values interface{}) (interface{}, error) {
func (p *PersistenceLayer) FindByTags(tags []string, strlimit string, value interface{}, valuesArray interface{}) (interface{}, error) {

Example usage

UUID := uuid.New().String()
u := User{Id: UUID, Name: "SomeName", Tag: "SomeTags"}
err := p.AddDocument(UUID, u)
Setup

Environment variables control the data layer access

export CLOUDEVENT_DOMAIN=mydomain.com // Cloud events will have source {CLOUDEVENT_DOMAIN}/{docType.String}
export DEBUG=false            // if true, debug output on
export USE_FIRESTORE=true     // if true reads will come from here; writes will also go here if USE_CQRS=false
export USE_PUBSUB=true        // if true enables a pubsub connection
export USE_CQRS=false         // if true writes are sent to pubsub
export GCP_PROJECT=myproject  // project for GCP connections
export GOOGLE_APPLICATION_CREDENTIALS=~/secrets/persistenceServices.json  // if testing outside of GCP
Tests
./test/localtests.sh

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventType

type EventType int
const (
	EVENT_TYPE_CREATE EventType = iota
	EVENT_TYPE_UPDATE
	EVENT_TYPE_DELETE
	EVENT_TYPE_NOTIFICATION
	EVENT_TYPE_ERROR
)

func (EventType) String

func (t EventType) String() string

type FirestoreConnection

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

func GetFirestore

func GetFirestore(gcpProjectID string) (*FirestoreConnection, error)

GetFirestore -

func GetFirestoreConnection

func GetFirestoreConnection(gcpProjectID string, collectionStr string) (*FirestoreConnection, error)

GetFirestoreConnection - deprecated

func (*FirestoreConnection) FirestoreAdd

func (f *FirestoreConnection) FirestoreAdd(docId string, s interface{}) error

FirestoreAdd -

func (*FirestoreConnection) FirestoreDelete

func (f *FirestoreConnection) FirestoreDelete(docId string, s interface{}) error

FirestoreDelete -

func (*FirestoreConnection) FirestoreFind

func (f *FirestoreConnection) FirestoreFind(queryParams map[string][]string, value interface{}) (interface{}, error)

FirestoreFind - queryParams - map["db.field"][]values value = pass in a struct, e.g. Source{} loop through the map building a query like db.field1 in ("value[0]", "value[1]", .. "value[n]") && db.field2 in ("value[0]", "value[1]", .. "value[n]") Limitation: create a separate query for each OR condition and merge the query results in your app.

func (*FirestoreConnection) FirestoreFindById

func (f *FirestoreConnection) FirestoreFindById(key string, values interface{}) (interface{}, error)

FirestoreFindById -

func (*FirestoreConnection) FirestoreUpdate

func (f *FirestoreConnection) FirestoreUpdate(docId string, s interface{}) error

FirestoreUpdate -

func (*FirestoreConnection) SetCollection

func (f *FirestoreConnection) SetCollection(collectionStr string)

type PersistenceLayer

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

PersistenceLayer implements hexagonal architecture to hide the PaaS functionality from the APIs

func GetLayer

func GetLayer() (*PersistenceLayer, error)

GetLayer -

func GetPersistenceLayer

func GetPersistenceLayer(docType documentType) (*PersistenceLayer, error)

GetPersistenceLayer - deprecated

func LocalEntry

func LocalEntry(docType documentType) (*PersistenceLayer, error)

func (*PersistenceLayer) AddDocument

func (p *PersistenceLayer) AddDocument(key string, values interface{}) error

AddDocument -

func (*PersistenceLayer) CloudEventDomain

func (p *PersistenceLayer) CloudEventDomain() string

func (*PersistenceLayer) DeleteDocument

func (p *PersistenceLayer) DeleteDocument(key string, values interface{}) error

DeleteDocument -

func (*PersistenceLayer) Find

func (p *PersistenceLayer) Find(queryParams map[string][]string, value interface{}) (valuesArray interface{}, err error)

Find -

func (*PersistenceLayer) FindById

func (p *PersistenceLayer) FindById(key string, values interface{}) (interface{}, error)

FindById -

func (*PersistenceLayer) GetConnection

func (p *PersistenceLayer) GetConnection() (err error)

GetConnection -

func (*PersistenceLayer) Initialise

func (p *PersistenceLayer) Initialise() error

Initialise -

func (*PersistenceLayer) Publish

func (p *PersistenceLayer) Publish(eventType EventType, subject string, values interface{}) error

Publish -

func (*PersistenceLayer) SetDocType

func (p *PersistenceLayer) SetDocType(docType documentType)

SetDocType - Note that this only impacts Firestore

func (*PersistenceLayer) UpdateDocument

func (p *PersistenceLayer) UpdateDocument(key string, values interface{}) error

UpdateDocument -

type PubsubConnection

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

func GetPubsubConnection

func GetPubsubConnection(gcpProjectID string) (*PubsubConnection, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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