store

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2020 License: MIT Imports: 8 Imported by: 0

README

postdog - Store plugin

This plugin stores sent letters in a database and provides functions to query those letters.

Example

package main

import (
	"context"

	"github.com/bounoable/postdog"
	"github.com/bounoable/postdog/letter"
	"github.com/bounoable/postdog/plugin/store"
	"github.com/bounoable/postdog/plugin/store/memorystore"
)

func main() {
  po := postdog.New(
		postdog.WithPlugin(
			store.Plugin(
				memorystore.New(), // in-memory store
			),
		),
	)

	err := po.Send(context.Background(), letter.Write(
		letter.Text("Hello."),
	))
}

Disable insertion

You can disable the insertion of letters into the database through the context:

package main

import (
	"context"

	"github.com/bounoable/postdog"
	"github.com/bounoable/postdog/letter"
	"github.com/bounoable/postdog/plugin/store"
	"github.com/bounoable/postdog/plugin/store/memorystore"
)

func main() {
  po := postdog.New(
		postdog.WithPlugin(
			store.Plugin(
				memorystore.New(), // in-memory store
			),
		),
	)

	// disable storage for this context
  ctx := store.Disable(context.Background())

	err := po.Send(ctx, letter.Write(
		letter.Text("Hello."),
	))
}

Query letters

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/bounoable/postdog"
	"github.com/bounoable/postdog/letter"
	"github.com/bounoable/postdog/plugin/store"
	"github.com/bounoable/postdog/plugin/store/memorystore"
	"github.com/bounoable/postdog/plugin/store/query"
)

func main() {
  memstore := memorystore.New( /* fill the store with letters */ ) // in-memory store

	_ = postdog.New(
		postdog.WithPlugin(store.Plugin(memstore)),
	)

	ctx := context.Background()

	cur, err := query.Run(
		ctx,
		memstore,
		query.Subject("order", "offer"), // subject must contain "order" or "offer"
		query.SentBetween(time.Now().AddDate(0, 0, -7), time.Now()), // letter must have been sent in the past 7 days
		query.Sort(query.SortBySendDate, query.SortDesc),            // sort descending by send date
		// see the `query` package for more query options
	)
	if err != nil {
		panic(err)
	}
	defer cur.Close(ctx) // close the cursor after use

	for cur.Next(ctx) {
		let := cur.Current()
		fmt.Println(let)
	}

	if cur.Err() != nil {
		panic(cur.Err())
	}
}

Documentation

Overview

Package store provides a storage plugin for sent letters.

Example
package main

import (
	"context"

	"github.com/bounoable/postdog"
	"github.com/bounoable/postdog/letter"
	"github.com/bounoable/postdog/plugin/store"
	"github.com/bounoable/postdog/plugin/store/memorystore"
)

func main() {
	po := postdog.New(
		postdog.WithPlugin(
			store.Plugin(
				memorystore.New(), // in-memory store
			),
		),
	)

	err := po.Send(context.Background(), letter.Write(
		letter.Text("Hello."),
	))

	_ = err
}
Output:

Example (Disable)
package main

import (
	"context"

	"github.com/bounoable/postdog"
	"github.com/bounoable/postdog/letter"
	"github.com/bounoable/postdog/plugin/store"
	"github.com/bounoable/postdog/plugin/store/memorystore"
)

func main() {
	po := postdog.New(
		postdog.WithPlugin(
			store.Plugin(
				memorystore.New(), // in-memory store
			),
		),
	)

	// disable storage for this context
	ctx := store.Disable(context.Background())

	err := po.Send(ctx, letter.Write(
		letter.Text("Hello."),
	))

	_ = err
}
Output:

Example (Query)
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/bounoable/postdog"
	"github.com/bounoable/postdog/plugin/store"
	"github.com/bounoable/postdog/plugin/store/memorystore"
	"github.com/bounoable/postdog/plugin/store/query"
)

func main() {
	memstore := memorystore.New( /* fill the store with letters */ ) // in-memory store

	_ = postdog.New(
		postdog.WithPlugin(store.Plugin(memstore)),
	)

	ctx := context.Background()

	cur, err := query.Run(
		ctx,
		memstore,
		query.Subject("order", "offer"), // subject must contain "order" or "offer"
		query.SentBetween(time.Now().AddDate(0, 0, -7), time.Now()), // letter must have been sent in the past 7 days
		query.Sort(query.SortBySendDate, query.SortDesc),            // sort descending by send date
		// see the `query` package for more query options
	)
	if err != nil {
		panic(err)
	}
	defer cur.Close(ctx) // close the cursor after use

	for cur.Next(ctx) {
		let := cur.Current()
		fmt.Println(let)
	}

	if cur.Err() != nil {
		panic(cur.Err())
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Name is the plugin name.
	Name = "store"
)

Functions

func AutowirePlugin added in v0.4.0

func AutowirePlugin(ctx context.Context, cfg map[string]interface{}) (postdog.Plugin, error)

AutowirePlugin creates the store plugin from the given cfg.

func Disable

func Disable(ctx context.Context) context.Context

Disable disables the insertion of letters for ctx.

func Disabled

func Disabled(ctx context.Context) bool

Disabled determines if the insertion of letters is disabled for ctx.

func Enable

func Enable(ctx context.Context) context.Context

Enable (re)enables the insertion of letters for ctx.

func Enabled

func Enabled(ctx context.Context) bool

Enabled determines if the insertion of letters is enabled for ctx.

func Plugin

func Plugin(store Store) postdog.PluginFunc

Plugin is the install function for the store plugin. It hooks into the postdog.AfterSend hook and inserts all sent letters into the store implementation.

func Register added in v0.4.0

func Register(cfg *autowire.Config)

Register registers the plugin in the autowire config.

func RegisterProvider added in v0.4.0

func RegisterProvider(name string, factory Factory)

RegisterProvider globally registers the store factory for the given store provider name for autowiring.

Types

type Factory added in v0.4.0

type Factory interface {
	CreateStore(context.Context, map[string]interface{}) (Store, error)
}

Factory is a store factory.

type FactoryFunc added in v0.4.0

type FactoryFunc func(context.Context, map[string]interface{}) (Store, error)

FactoryFunc allows functions to be used as a Factory.

func (FactoryFunc) CreateStore added in v0.4.0

func (fn FactoryFunc) CreateStore(ctx context.Context, cfg map[string]interface{}) (Store, error)

CreateStore creates the store from the given cfg.

type Letter

type Letter struct {
	letter.Letter
	SentAt    time.Time
	SendError string
}

Letter adds additional info to a letter.Letter.

func (Letter) String added in v0.2.0

func (let Letter) String() string

type Store

type Store interface {
	Insert(context.Context, Letter) error
}

Store inserts letters into a repository.

type UnregisteredProviderError added in v0.4.0

type UnregisteredProviderError struct {
	Name string
}

UnregisteredProviderError means the autowire config defines the plugin with an unregistered store provider name.

func (UnregisteredProviderError) Error added in v0.4.0

func (err UnregisteredProviderError) Error() string

Directories

Path Synopsis
Package mock_store is a generated GoMock package.
Package mock_store is a generated GoMock package.
Package mongostore provides the mongodb store implementation.
Package mongostore provides the mongodb store implementation.
Package query provides functions to query letters from a store.
Package query provides functions to query letters from a store.
mock_query
Package mock_query is a generated GoMock package.
Package mock_query is a generated GoMock package.
Package storetest provides testing utilities that can be used to test store implementations.
Package storetest provides testing utilities that can be used to test store implementations.

Jump to

Keyboard shortcuts

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