docstore

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2024 License: CC0-1.0 Imports: 15 Imported by: 0

README

docstore

Document store with built in cache

Supported storage backend

  • Memory
  • MongoDB
  • SQL (Mysql, Postgres)

Plase see the cache package for supported cache backend

Quick Start

Installation $ go get github.com/diki-haryadi/govega/docstore

Usage

MongoDB

import (
    "fmt"

	"github.com/diki-haryadi/govega/docstore"
    _ "github.com/diki-haryadi/govega/docstore/mongo"
)


mconf := &docstore.Config{
    Database:        "userdb",
    Collection:      "user",
    CacheURL:        "mem://",
    CacheExpiration: 3600 * 24,
    IDField:         "id",
    TimestampField:  "created_at",
    Driver:          "mongo",
    Connection:      database.Client{
        URI:     "mongodb://localhost:27017",
        DB:      "userdb",
        AppName: "test",
    },
}


SQL

import (
    "fmt"

	"github.com/diki-haryadi/govega/docstore"
    _ "github.com/diki-haryadi/govega/docstore/sql"
)


sconf := &docstore.Config{
    Database:        "userdb",
    Collection:      "user",
    CacheURL:        "mem://",
    CacheExpiration: 3600 * 24,
    IDField:         "id",
    TimestampField:  "created_at",
    Driver:          "mysql",
    Connection:      database.DBConfig{
		MasterDSN:     "root:password@(localhost:3306)/outbox?parseTime=true",
		SlaveDSN:      "root:password@(localhost:3306)/outbox?parseTime=true",
		RetryInterval: 5,
		MaxIdleConn:   10,
		MaxConn:       5,
	},
}


Initialize docstore

import (
    "fmt"

	"github.com/diki-haryadi/govega/docstore"
)

func main() {
    conf := &docstore.Config{
		Database:        "userdb",
		Collection:      "user",
		CacheURL:        "mem://",
		CacheExpiration: 3600 * 24,
		IDField:         "id",
		TimestampField:  "created_at",
		Driver:          "memory",
		Connection:      nil,
	}

    store, err := docstore.New(conf)
    if err != nil {
        panic(err)
    }


    ms := docstore.NewMemoryStore("user", "id")
    cache := mem.NewMemoryCache()
    conf := &Config{
		IDField:        "id",
		TimestampField: "created_at",
	}

    store = NewDocstore(ms, cache, conf)

}

Example
package main

import (
    "fmt"

	"github.com/diki-haryadi/govega/cache/mem"
	"github.com/diki-haryadi/govega/docstore"
)

type User struct {
    ID        string    `json:"id"`
    Name      string    `json:"name"`
    Username  string    `json:"username"`
    Age       int       `json:"age"`
    CreatedAt time.Time `json:"created_at"`
}

func main() {
    ms := docstore.NewMemoryStore("user", "id")
    cache := mem.NewMemoryCache()
    conf := &Config{
		IDField:        "id",
		TimestampField: "created_at",
	}

    cs := NewDocstore(ms, cache, conf)

    usr := &User{
		Name:     "sahal",
		Username: "sahalzain",
		Age:      35,
	}

    if err := cs.Create(ctx, usr); err != nil {
        fmt.Println(err)
    }

    var doc User
	if err := cs.Get(ctx, usr.ID, &doc); err != nil {
        fmt.Println(err)
    }
}

Documentation

Index

Constants

View Source
const NotFound = DocstoreError("[docstore] document not found")

Variables

This section is empty.

Functions

func DocstoreTestCRUD

func DocstoreTestCRUD(cs *CachedStore, t *testing.T)

func DriverBulkTest

func DriverBulkTest(d Driver, t *testing.T)

func DriverCRUDTest

func DriverCRUDTest(d Driver, t *testing.T)

func RegisterDriver

func RegisterDriver(name string, fn DriverFactory)

Types

type CachedStore

type CachedStore struct {
	*Config
	// contains filtered or unexported fields
}

func New

func New(config *Config) (*CachedStore, error)

func NewDocstore

func NewDocstore(storage Driver, cache cache.Cache, config *Config) *CachedStore

func (*CachedStore) BulkCreate

func (s *CachedStore) BulkCreate(ctx context.Context, docs interface{}) error

func (*CachedStore) BulkGet

func (s *CachedStore) BulkGet(ctx context.Context, ids, docs interface{}) error

func (*CachedStore) Create

func (s *CachedStore) Create(ctx context.Context, doc interface{}) error

func (*CachedStore) Delete

func (s *CachedStore) Delete(ctx context.Context, id interface{}) error

func (*CachedStore) Find

func (s *CachedStore) Find(ctx context.Context, query *QueryOpt, docs interface{}) error

func (*CachedStore) Get

func (s *CachedStore) Get(ctx context.Context, id, doc interface{}) error

func (*CachedStore) GetCache

func (s *CachedStore) GetCache() cache.Cache

func (*CachedStore) Increment

func (s *CachedStore) Increment(ctx context.Context, id interface{}, fieldName string, value int) error

func (*CachedStore) Migrate

func (s *CachedStore) Migrate(ctx context.Context, config interface{}) error

func (*CachedStore) Replace

func (s *CachedStore) Replace(ctx context.Context, doc interface{}) error

func (*CachedStore) Update

func (s *CachedStore) Update(ctx context.Context, doc interface{}) error

func (*CachedStore) UpdateField

func (s *CachedStore) UpdateField(ctx context.Context, id interface{}, key string, value interface{}) error

type Config

type Config struct {
	Database        string      `json:"db,omitempty"`
	Collection      string      `json:"collection,omitempty"`
	CacheURL        string      `json:"cache_url,omitempty"`
	CacheExpiration int         `json:"cache_expiration,omitempty"`
	IDField         string      `json:"id_field,omitempty"`
	TimestampField  string      `json:"timestamp_field,omitempty"`
	Driver          string      `json:"driver,omitempty"`
	Connection      interface{} `json:"connection,omitempty"`
}

type DocstoreError

type DocstoreError string

func (DocstoreError) Error

func (e DocstoreError) Error() string

type Driver

type Driver interface {
	//GetID(doc interface{}) (interface{}, error)
	Create(ctx context.Context, doc interface{}) error
	Update(ctx context.Context, id, doc interface{}, replace bool) error
	UpdateField(ctx context.Context, id interface{}, fields []Field) error
	Increment(ctx context.Context, id interface{}, key string, value int) error
	GetIncrement(ctx context.Context, id interface{}, key string, value int, doc interface{}) error
	Delete(ctx context.Context, id interface{}) error
	Get(ctx context.Context, id interface{}, doc interface{}) error
	Find(ctx context.Context, query *QueryOpt, docs interface{}) error
	BulkCreate(ctx context.Context, docs []interface{}) error
	BulkGet(ctx context.Context, ids []interface{}, docs interface{}) error
	Migrate(ctx context.Context, config interface{}) error
}

func GetDriver

func GetDriver(config *Config) (Driver, error)

func MemoryStoreFactory

func MemoryStoreFactory(config *Config) (Driver, error)

type DriverFactory

type DriverFactory func(config *Config) (Driver, error)

type Field

type Field struct {
	Name  string
	Value interface{}
}

type FilterOpt

type FilterOpt struct {
	Field string
	Value interface{}
	Ops   string
}

FilterOpt filter option

type MemoryStore

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

func NewMemoryStore

func NewMemoryStore(name, idField string) *MemoryStore

func (*MemoryStore) BulkCreate

func (m *MemoryStore) BulkCreate(ctx context.Context, docs []interface{}) error

func (*MemoryStore) BulkGet

func (m *MemoryStore) BulkGet(ctx context.Context, ids []interface{}, docs interface{}) error

func (*MemoryStore) Create

func (m *MemoryStore) Create(ctx context.Context, doc interface{}) error

func (*MemoryStore) Delete

func (m *MemoryStore) Delete(ctx context.Context, id interface{}) error

func (*MemoryStore) Find

func (m *MemoryStore) Find(ctx context.Context, query *QueryOpt, docs interface{}) error

func (*MemoryStore) Get

func (m *MemoryStore) Get(ctx context.Context, id interface{}, doc interface{}) error

func (*MemoryStore) GetIncrement

func (m *MemoryStore) GetIncrement(ctx context.Context, id interface{}, key string, value int, doc interface{}) error

func (*MemoryStore) Increment

func (m *MemoryStore) Increment(ctx context.Context, id interface{}, key string, value int) error

func (*MemoryStore) Migrate

func (m *MemoryStore) Migrate(ctx context.Context, config interface{}) error

func (*MemoryStore) Update

func (m *MemoryStore) Update(ctx context.Context, id, doc interface{}, replace bool) error

func (*MemoryStore) UpdateField

func (m *MemoryStore) UpdateField(ctx context.Context, id interface{}, fields []Field) error

type QueryOpt

type QueryOpt struct {
	Limit    int
	Skip     int
	Page     int
	OrderBy  string
	IsAscend bool
	Filter   []FilterOpt
}

QueryOpt query option

func (*QueryOpt) AddFilter

func (q *QueryOpt) AddFilter(filter FilterOpt) *QueryOpt

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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