metastore

package module
v1.12.2 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Meta Store

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/dracory/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

View Source
const COLUMN_CREATED_AT = "created_at"
View Source
const COLUMN_DELETED_AT = "deleted_at"
View Source
const COLUMN_ID = "id"
View Source
const COLUMN_META_KEY = "meta_key"
View Source
const COLUMN_META_VALUE = "meta_value"
View Source
const COLUMN_OBJECT_ID = "object_id"
View Source
const COLUMN_OBJECT_TYPE = "object_type"
View Source
const COLUMN_UPDATED_AT = "updated_at"

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
	AutomigrateEnabled bool
	DebugEnabled       bool
}

type Store

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

Store defines a meta store

func (*Store) EnableDebug

func (st *Store) EnableDebug(debug bool)

EnableDebug enables or disables debug mode

func (*Store) FindByKey

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

FindByKey finds a meta entry 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) GetDB

func (st *Store) GetDB() *sql.DB

GetDB returns the database connection

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) GetMetaTableName

func (st *Store) GetMetaTableName() string

GetMetaTableName returns the meta table name

func (*Store) IsAutomigrateEnabled

func (st *Store) IsAutomigrateEnabled() bool

IsAutomigrateEnabled returns whether automigrate is enabled

func (*Store) MigrateDown added in v1.5.0

func (st *Store) MigrateDown(ctx context.Context, tx ...*sql.Tx) error

MigrateDown drops the meta table

func (*Store) MigrateUp added in v1.5.0

func (st *Store) MigrateUp(ctx context.Context, tx ...*sql.Tx) error

MigrateUp creates the meta table

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

type StoreInterface

type StoreInterface interface {
	// MigrateDown drops the meta table
	MigrateDown(ctx context.Context, tx ...*sql.Tx) error

	// MigrateUp creates the meta table
	MigrateUp(ctx context.Context, tx ...*sql.Tx) error

	// EnableDebug enables or disables debug logging
	EnableDebug(debug bool)

	// FindByKey finds a meta entry by object type, object ID, and key
	FindByKey(objectType string, objectID string, key string) (*Meta, error)

	// Get retrieves a value by object type, object ID, and key
	Get(objectType string, objectID string, key string, valueDefault string) (string, error)

	// GetJSON retrieves and unmarshals a JSON value by object type, object ID, and key
	GetJSON(objectType string, objectID string, key string, valueDefault interface{}) (interface{}, error)

	// Remove deletes a meta entry by object type, object ID, and key
	Remove(objectType string, objectID string, key string) error

	// Set stores a string value for a given object type, object ID, and key
	Set(objectType string, objectID string, key string, value string) error

	// SetJSON marshals and stores a JSON value for a given object type, object ID, and key
	SetJSON(objectType string, objectID string, key string, value interface{}) error

	// GetMetaTableName returns the meta table name
	GetMetaTableName() string

	// GetDB returns the database connection
	GetDB() *sql.DB

	// IsAutomigrateEnabled returns whether automigrate is enabled
	IsAutomigrateEnabled() bool
}

StoreInterface defines the interface for meta store operations

func NewStore

func NewStore(opts NewStoreOptions) (StoreInterface, error)

NewStore creates a new meta store

Jump to

Keyboard shortcuts

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