models

package
v0.0.0-...-c6e3130 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2022 License: Apache-2.0 Imports: 5 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

Functions

This section is empty.

Types

type Candy

type Candy struct {
	Name string `bson:"name"`
}

func (Candy) MarshalJSON

func (c Candy) MarshalJSON() ([]byte, error)

type DB

type DB interface {
	// Get gets the value for the given key and loads it into val.
	// returns the value and nil if it was found, nil and ErrNotFound if it
	// was not found and another appropriate error otherwise
	Get(key string, val Model) error
	// GetAllKeys gets all the keys in the database. returns nil and an appropriate
	// error if the keys couldn't be fetched
	GetAllKeys() ([]string, error)
	// Set sets the value for the given existing key. returns nil if the key
	// already existed and was successfully set, ErrNotFound if the key didn't
	// already exist, and another appropriate error otherwise
	Set(key string, val Model) error
	// Upsert sets or creates the value for the existing key. returns true and nil
	// if the key was created on this call, false and nil if the key was not created
	// but still successfully updated, and false and the appropriate error otherwise
	Upsert(key string, val Model) (bool, error)
}

DB is the interface to the database, a flat key-value store. The idea here is to have multiple different DB implementations. This codebase provides an in-memory database and a (untested) mongo database

func NewInMemoryDB

func NewInMemoryDB() DB

type Model

type Model interface {
	json.Marshaler
}

type MongoDB

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

MongoDB is a DB implementation that talks to an external MongoDB server. Note: this is UNTESTED code, only to be used as a getting started reference. make sure you test it thouroughly against a real MongoDB server before using it in production

func NewMongoDB

func NewMongoDB(urlStr string) (*MongoDB, error)

NewMongoDB connects to an external MongoDB server and returns a DB implementation that fronts that connection. call Close() on the returned value when done.

func (*MongoDB) Close

func (m *MongoDB) Close()

Close releases the underlying connections. always call this when completely done with operations, not before.

func (*MongoDB) Get

func (m *MongoDB) Get(key string, val Model) error

Get is the interface implementation

func (*MongoDB) GetAllKeys

func (m *MongoDB) GetAllKeys() ([]string, error)

func (*MongoDB) Set

func (m *MongoDB) Set(key string, val Model) error

Set is the interface implementation

func (*MongoDB) Upsert

func (m *MongoDB) Upsert(key string, val Model) (bool, error)

Upsert is the interface implementation

Jump to

Keyboard shortcuts

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