metastore

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2024 License: MIT Imports: 15 Imported by: 0

README

Meta Store Open in Gitpod

Tests Status Go Report Card PkgGoDev

Meta stores meta information for any object to a database table.

Store to database additional information to anything using metas (key - value) pairs

Installation

go get -u github.com/gouniverse/metastore

Table Schema

The following schema is used for the database.

meta
id String, UniqueId
object_type String (100)
object_id String (40)
meta_key String (255)
meta_value Long Text
created_at DateTime
updated_at DateTime
deleted_at DateTime

Setup

metaStore, err := metastore.NewStore(metastore.NewStoreOptions{
	DB:                 databaseInstance,
	MetaTableName:      "my_meta",
	AutomigrateEnabled: true,
	DebugEnabled:       false,
})

if err != nil {
    t.Fatal("Error at AutoMigrate", err.Error())
}

Usage

  • Set a meta values (for user with ID 1)
metaStore.Set("user", "1", "verified", "yes")
metaStore.Set("user", "1", "verified_at", "2021-03-12")
  • Get meta values (for user with ID 1), if not found a default value is returned
log.Println(metaStore.Get("user", "1", "verified", ""))
log.Println(metaStore.Get("user", "1", "verified_at", ""))

Changelog

2022.12.07 - Changed setup to use struct

2022.12.07 - Updated dependencies, fixed package name

2022.01.02 - Removed GORM dependency

2021.12.29 - Added tests badge

2021.12.29 - Added tests

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Meta

type Meta struct {
	ID         string     `db:"id"`
	ObjectType string     `db:"object_type"`
	ObjectID   string     `db:"object_id"`
	Key        string     `db:"meta_key"`
	Value      string     `db:"meta_value"`
	CreatedAt  time.Time  `db:"created_at"`
	UpdatedAt  time.Time  `db:"updated_at"`
	DeletedAt  *time.Time `db:"deleted_at"`
}

Meta type

type NewStoreOptions

type NewStoreOptions struct {
	MetaTableName      string
	DB                 *sql.DB
	DbDriverName       string
	AutomigrateEnabled bool
	DebugEnabled       bool
}

type Store

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

Store defines a session store

func NewStore

func NewStore(opts NewStoreOptions) (*Store, error)

NewStore creates a new entity store

func (*Store) AutoMigrate

func (st *Store) AutoMigrate() error

AutoMigrate auto migrate

func (*Store) DriverName

func (st *Store) DriverName(db *sql.DB) string

DriverName finds the driver name from database

func (*Store) EnableDebug

func (st *Store) EnableDebug(debug bool)

EnableDebug - enables the debug option

func (*Store) FindByKey

func (st *Store) FindByKey(objectType string, objectID string, key string) (*Meta, error)

FindByKey finds a cache by key

func (*Store) Get

func (st *Store) Get(objectType string, objectID string, key string, valueDefault string) (string, error)

Get gets a key from cache

func (*Store) GetJSON

func (st *Store) GetJSON(objectType string, objectID string, key string, valueDefault interface{}) (interface{}, error)

GetJSON gets a JSON key from cache

func (*Store) Remove

func (st *Store) Remove(objectType string, objectID string, key string) error

Remove deletes a meta key

func (*Store) Set

func (st *Store) Set(objectType string, objectID string, key string, value string) error

Set sets new key value pair

func (*Store) SetJSON

func (st *Store) SetJSON(objectType string, objectID string, key string, value interface{}) error

SetJSON sets new key value pair

func (*Store) SqlCreateTable

func (st *Store) SqlCreateTable() string

SqlCreateTable returns a SQL string for creating the setting table

type StoreOption

type StoreOption func(*Store)

StoreOption options for the cache store

Jump to

Keyboard shortcuts

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