aedstorm

package module
v0.0.0-...-ab2917f Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2023 License: MIT Imports: 12 Imported by: 0

README

GoDoc Build Status codecov

This is an ORM like package which makes working with App Engine datastore entities in go a bit easier. The name aedstorm stands for App Engine DataSTore ORM.

It makes working with the datastore and basic data structs quite simple.

Example

import (
	"encoding/json"
	"net/http"

	"google.golang.org/appengine"
	aedstorm "github.com/rubanbydesign/go-aedstorm"
)

type MyData struct {
	ID, Value string
}

func myHandler(w http.ResponseWriter, r *http.Request) {

	ctx := appengine.NewContext(r)

	// Save a new entity.
	d := &MyData{ID: "foo", Value: "bar"}
	if err := aedstorm.NewModel(&d).WithContext(ctx).Save(); err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	dd := &MyData{ID: "foo"}
	if err := aedstorm.NewModel(&dd).WithContext(ctx).Load(); err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	// dd.Value  will now equal d.Value after the load.
	w.Header.Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(&dd)
}

I'm planning on adding more documentation in the future, including some more advanced interfaces which you can implement on a model, and queries, but for now this should give you an idea how easy it is to get going.

Planned improvements

  • Better documentation, more basic examples
  • More documentation around current interfaces which can be implemented
  • Add support for loading/saving related entities

Documentation

Overview

Package aedstorm an ORM like functions which makes working with App Engine datastore entities in go a bit easier

Index

Constants

View Source
const (
	// TagName is the tag name where we look for custom tag values, like "id"
	TagName = "datastore"
)

Variables

View Source
var (
	ErrNoModel      = errors.New("Model is not loaded")
	ErrNoID         = errors.New("Model has no ID")
	ErrNoContext    = errors.New("No net/context was loaded")
	ErrNilModel     = errors.New("Model is nil")
	ErrModelInvalid = errors.New("Model must be a struct pointer")
)

Standardized error messages

Functions

func Copy

func Copy(srcVal interface{}, dstVal interface{}) error

Copy copies one interface into the other doing type checking to make sure it's safe. If it cannot be copied, an error is returned.

func SetMockQueryResult

func SetMockQueryResult(data interface{}, err error, keys []*datastore.Key)

SetMockQueryResult sets the query GetAll() result to be the values given

Types

type DataModel

type DataModel struct {
	sync.Mutex
	// contains filtered or unexported fields
}

DataModel is a ORM styled structure for saving and loading entities

func NewModel

func NewModel(m Model) *DataModel

NewModel returns an initialized data model

func (*DataModel) Cache

func (dm *DataModel) Cache() error

Cache caches the entity in memcache

func (*DataModel) Context

func (dm *DataModel) Context() context.Context

Context returns the internal net/context

func (*DataModel) Delete

func (dm *DataModel) Delete() error

Delete deletes the entity from the datastore and cache

func (*DataModel) ID

func (dm *DataModel) ID() string

ID returns the underlying data struct's unique ID. If the supplied struct implements this interface, then it's result will be that of the model's EntityID() function. Otherwise, a new random uuid v4 will be used.

func (*DataModel) Key

func (dm *DataModel) Key() *datastore.Key

Key returns the datastore key

func (*DataModel) Load

func (dm *DataModel) Load() error

Load loads the entity from the datastore. Must have an ID for this to work.

func (*DataModel) Save

func (dm *DataModel) Save() error

Save writes the entity to the datastore

func (*DataModel) Uncache

func (dm *DataModel) Uncache() error

Uncache removes the cached model from cache

func (*DataModel) WithContext

func (dm *DataModel) WithContext(ctx context.Context) *DataModel

WithContext sets the internal context for use in future operations

type EntityError

type EntityError interface {
	Error() error
}

EntityError is an interface which returns an error. When the model's struct implements this, it's called before saving the struct to the datastore. It can be used for verification of the data in the model, etc.

type EntityID

type EntityID interface {
	GetID() string
}

EntityID is an interface returns the int64 ID for the datastore struct. If the supplied struct implements this interface, then it's result will be the ID of the struct in the datastore. Otherwise, a new random uuid v4 will be used

type EntityName

type EntityName interface {
	Entity() string
}

EntityName is an interface which defines the entity name of the data to be stored in the datastore. If a struct implements this interface, then it will be saved with this entity name. If not, the name of the struct itself will be used.

type Model

type Model interface{}

Model is an interface for datastore entities. User-implemented structs must implement this interface for things to work.

type OnCache

type OnCache interface {
	Cache() error
}

OnCache is an interface which defines a callback which is run after a entity is successfully cached.

type OnDelete

type OnDelete interface {
	Delete() error
}

OnDelete is an interface which defines a callback which is run after a entity is successfully deleted.

type OnSave

type OnSave interface {
	Save() error
}

OnSave is an interface which defines a callback which is run after a entity is successfully saved. It's run parallel with the caching method, so there's no guarantee that the model is already in memcache when it's called. Instead, if you need to rely on it being in memcache, implement the OnCache interface.

type OnUncache

type OnUncache interface {
	Uncache() error
}

OnUncache is an interface which defines a callback which is run after a entity is successfully removed from cache.

type Query

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

Query is a struct which implements a subset of the "datastore.Query" interface and is mockable

func NewQuery

func NewQuery(m interface{}) *Query

NewQuery returns a new query based off the type of m. If m implements the EntityName interface, it uses that for an entity name, otherwise it uses the name of the struct itself

func (*Query) Count

func (q *Query) Count(ctx context.Context) (int, error)

Count matches the "datastore.Query".Count interface

func (*Query) Filter

func (q *Query) Filter(filterStr string, value interface{}) *Query

Filter implements the "datastore.Query".Filter interface

func (*Query) GetAll

func (q *Query) GetAll(ctx context.Context, out interface{}) ([]*datastore.Key, error)

GetAll matches the "datastore.Query".GetAll interface

func (*Query) KeysOnly

func (q *Query) KeysOnly() *Query

func (*Query) Limit

func (q *Query) Limit(num int) *Query

func (*Query) Offset

func (q *Query) Offset(num int) *Query

func (*Query) Order

func (q *Query) Order(fieldName string) *Query

Order returns a derivative query with a field-based sort order. Orders are applied in the order they are added. The default order is ascending; to sort in descending order prefix the fieldName with a minus sign (-).

type SetID

type SetID interface {
	SetID(string)
}

SetID is an interface, which if defined, allows the model to set it's own ID.

type UUID

type UUID [16]byte

UUID is a v4 Universally unique identifier

func NewUUID

func NewUUID() (*UUID, error)

NewUUID creates a new uuid v4

func (*UUID) String

func (u *UUID) String() string

Jump to

Keyboard shortcuts

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