datastore

package
v0.2.30 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2022 License: Apache-2.0 Imports: 30 Imported by: 0

README

Quickstart on Datastore's

PostgreSQL
CREATE DATABASE bux;
CREATE USER buxdblocaluser WITH ENCRYPTED PASSWORD '_LocalSecretPassForDb456_';
GRANT ALL PRIVILEGES ON DATABASE bux TO buxdblocaluser;
ALTER ROLE buxdblocaluser SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;
GRANT CREATE ON SCHEMA public TO buxdblocaluser;
GRANT ALL ON SCHEMA public TO buxdblocaluser;
SQLite
Set a database path (IE: default_db.db)
MySQL
CREATE DATABASE bux;
DROP USER IF EXISTS 'buxDbLocalUser'@'%';
SET GLOBAL sql_mode = '';
SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
CREATE USER 'buxDbLocalUser'@'%' IDENTIFIED BY '_LocalSecretPassForDb789_';
GRANT USAGE ON `bux`.* TO 'buxDbLocalUser'@'%';
GRANT DELETE ON `bux`.* TO 'buxDbLocalUser'@'%';
GRANT INSERT ON `bux`.* TO 'buxDbLocalUser'@'%';
GRANT SELECT ON `bux`.* TO 'buxDbLocalUser'@'%';
GRANT UPDATE ON `bux`.* TO 'buxDbLocalUser'@'%';
GRANT CREATE ON `bux`.* TO 'buxDbLocalUser'@'%';
GRANT INDEX ON `bux`.* TO 'buxDbLocalUser'@'%';
GRANT ALTER ON `bux`.* TO 'buxDbLocalUser'@'%';
GRANT REFERENCES ON `bux`.* TO 'buxDbLocalUser'@'%';
GRANT DROP ON `bux`.* TO 'buxDbLocalUser'@'%';
GRANT LOCK TABLES ON `bux`.* TO 'buxDbLocalUser'@'%';
GRANT CREATE TEMPORARY TABLES ON `bux`.* TO 'buxDbLocalUser'@'%';
GRANT TRIGGER ON `bux`.* TO 'buxDbLocalUser'@'%';
GRANT SUPER ON *.* TO 'buxDbLocalUser'@'%';
FLUSH PRIVILEGES;
MongoDB
No setup required

Documentation

Overview

Package datastore is the database service abstraction layer

Index

Constants

View Source
const (

	// SortDesc will sort descending
	SortDesc = "desc"

	// SortAsc will sort ascending
	SortAsc = "asc"
)

Defaults for library functionality

View Source
const (
	Postgres = "postgres"
	JSON     = "JSON"
	JSONB    = "JSONB"
)

index creation constants

Variables

View Source
var ErrDuplicateKey = errors.New("duplicate key")

ErrDuplicateKey error when a record is inserted and conflicts with an existing record

View Source
var ErrNoResults = errors.New("no results found")

ErrNoResults error when no results are found

View Source
var ErrNoSourceFound = errors.New("no source database found in all given configurations")

ErrNoSourceFound is when no source database is found in all given configurations

View Source
var ErrUnknownCollection = errors.New("could not determine collection name from model")

ErrUnknownCollection is thrown when the collection can not be found using the model/name

View Source
var ErrUnknownSQL = errors.New("unknown sql implementation")

ErrUnknownSQL is an error when using a SQL engine that is not known for indexes and migrations

View Source
var ErrUnsupportedDriver = errors.New("sql driver unsupported")

ErrUnsupportedDriver is when the given SQL driver is not determined to be known or supported

View Source
var ErrUnsupportedEngine = errors.New("unsupported datastore engine")

ErrUnsupportedEngine is used when the engine given is not a known datastore engine

View Source
var SQLDatabases = []Engine{
	MySQL,
	PostgreSQL,
	SQLite,
}

SQLDatabases is the list of supported SQL databases (via GORM)

Functions

func BuxWhere

func BuxWhere(tx buxWhereInterface, conditions map[string]interface{}, engine Engine) interface{}

BuxWhere add conditions

func IsSQLEngine

func IsSQLEngine(e Engine) bool

IsSQLEngine check whether the string already is in the slice

func MarshalQueryParams added in v0.2.14

func MarshalQueryParams(m QueryParams) graphql.Marshaler

MarshalQueryParams will marshal the custom type

Types

type Client

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

Client is the datastore client (configuration)

func (*Client) AutoMigrateDatabase

func (c *Client) AutoMigrateDatabase(ctx context.Context, models ...interface{}) error

AutoMigrateDatabase will detect the engine and migrate as needed

func (*Client) Close

func (c *Client) Close(ctx context.Context) error

Close will terminate (close) the datastore and any open connections

func (*Client) CreateInBatches added in v0.2.14

func (c *Client) CreateInBatches(
	ctx context.Context,
	models interface{},
	batchSize int,
) error

CreateInBatches create all the models given in batches

func (*Client) CreateInBatchesMongo added in v0.2.14

func (c *Client) CreateInBatchesMongo(
	ctx context.Context,
	models interface{},
	batchSize int,
) error

CreateInBatchesMongo insert multiple models vai bulk.Write

func (*Client) Debug

func (c *Client) Debug(on bool)

Debug will set the debug flag

func (*Client) DebugLog

func (c *Client) DebugLog(text string)

DebugLog will display verbose logs

func (*Client) Engine

func (c *Client) Engine() Engine

Engine will return the client's engine

func (*Client) Execute

func (c *Client) Execute(query string) *gorm.DB

Execute a SQL query

func (*Client) GetDatabaseName

func (c *Client) GetDatabaseName() string

GetDatabaseName will return the full database name for the given model name

func (*Client) GetModel

func (c *Client) GetModel(
	ctx context.Context,
	model interface{},
	conditions map[string]interface{},
	timeout time.Duration,
	forceWriteDB bool,
) error

GetModel will get a model from the datastore

func (*Client) GetModelCount added in v0.2.18

func (c *Client) GetModelCount(
	ctx context.Context,
	model interface{},
	conditions map[string]interface{},
	timeout time.Duration,
) (int64, error)

GetModelCount will return a count of the model matching conditions

func (*Client) GetModels

func (c *Client) GetModels(
	ctx context.Context,
	models interface{},
	conditions map[string]interface{},
	queryParams *QueryParams,
	fieldResults interface{},
	timeout time.Duration,
) error

GetModels will return a slice of models based on the given conditions

func (*Client) GetModelsAggregate added in v0.2.19

func (c *Client) GetModelsAggregate(ctx context.Context, models interface{},
	conditions map[string]interface{}, aggregateColumn string, timeout time.Duration) (map[string]interface{}, error)

GetModelsAggregate will return an aggregate count of the model matching conditions

func (*Client) GetMongoCollection added in v0.2.14

func (c *Client) GetMongoCollection(
	collectionName string,
) *mongo.Collection

GetMongoCollection will get the mongo collection for the given tableName

func (*Client) GetMongoCollectionByTableName added in v0.2.14

func (c *Client) GetMongoCollectionByTableName(
	tableName string,
) *mongo.Collection

GetMongoCollectionByTableName will get the mongo collection for the given tableName

func (*Client) GetTableName

func (c *Client) GetTableName(modelName string) string

GetTableName will return the full table name for the given model name

func (*Client) HasMigratedModel

func (c *Client) HasMigratedModel(modelType string) bool

HasMigratedModel will return if the model type has been migrated

func (*Client) IncrementModel

func (c *Client) IncrementModel(
	ctx context.Context,
	model interface{},
	fieldName string,
	increment int64,
) (newValue int64, err error)

IncrementModel will increment the given field atomically in the database and return the new value

func (*Client) IndexExists

func (c *Client) IndexExists(tableName, indexName string) (bool, error)

IndexExists check whether the given index exists in the datastore

func (*Client) IndexMetadata

func (c *Client) IndexMetadata(tableName, field string) error

IndexMetadata check and creates the metadata json index

func (*Client) IsAutoMigrate

func (c *Client) IsAutoMigrate() bool

IsAutoMigrate returns whether auto migration is on

func (*Client) IsDebug

func (c *Client) IsDebug() bool

IsDebug will return the debug flag (bool)

func (*Client) IsNewRelicEnabled

func (c *Client) IsNewRelicEnabled() bool

IsNewRelicEnabled will return if new relic is enabled

func (*Client) NewTx

func (c *Client) NewTx(ctx context.Context, fn func(*Transaction) error) error

NewTx will start a new datastore transaction

func (*Client) Raw

func (c *Client) Raw(query string) *gorm.DB

Raw a raw SQL query

func (*Client) SaveModel

func (c *Client) SaveModel(
	ctx context.Context,
	model interface{},
	tx *Transaction,
	newRecord, commitTx bool,
) error

SaveModel will take care of creating or updating a model (primary key based) (abstracting the database)

value is a pointer to the model, IE: &Transaction{}

type ClientInterface

type ClientInterface interface {
	StorageService
	Close(ctx context.Context) error
	Debug(on bool)
	DebugLog(text string)
	Engine() Engine
	GetDatabaseName() string
	GetTableName(modelName string) string
	GetMongoCollection(collectionName string) *mongo.Collection
	GetMongoCollectionByTableName(tableName string) *mongo.Collection
	IsAutoMigrate() bool
	IsDebug() bool
	IsNewRelicEnabled() bool
}

ClientInterface is the Datastore client interface

func NewClient

func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error)

NewClient creates a new client for all Datastore functionality

If no options are given, it will use the defaultClientOptions() ctx may contain a NewRelic txn (or one will be created)

type ClientOps

type ClientOps func(c *clientOptions)

ClientOps allow functional options to be supplied that overwrite default client options.

func WithAutoMigrate

func WithAutoMigrate(migrateModels ...interface{}) ClientOps

WithAutoMigrate will enable auto migrate database mode (given models)

Pointers of structs (IE: &models.Xpub{})

func WithDebugging

func WithDebugging() ClientOps

WithDebugging will enable debugging mode

func WithLogger

func WithLogger(customLogger logger.Interface) ClientOps

WithLogger will set the custom logger interface

func WithMongo

func WithMongo(config *MongoDBConfig) ClientOps

WithMongo will set the datastore to use MongoDB

func WithMongoConnection

func WithMongoConnection(database *mongo.Database, tablePrefix string) ClientOps

WithMongoConnection will set the datastore to use an existing Mongo database connection

func WithNewRelic

func WithNewRelic() ClientOps

WithNewRelic will enable the NewRelic wrapper

func WithSQL

func WithSQL(engine Engine, configs []*SQLConfig) ClientOps

WithSQL will load a datastore using either an SQL database config or existing connection

func WithSQLConnection

func WithSQLConnection(engine Engine, sqlDB *sql.DB, tablePrefix string) ClientOps

WithSQLConnection will set the datastore to an existing connection for MySQL or PostgreSQL

func WithSQLite

func WithSQLite(config *SQLiteConfig) ClientOps

WithSQLite will set the datastore to use SQLite

type CommonConfig

type CommonConfig struct {
	Debug                 bool          `json:"debug" mapstructure:"debug"`                                       // flag for debugging sql queries in logs
	MaxConnectionIdleTime time.Duration `json:"max_connection_idle_time" mapstructure:"max_connection_idle_time"` // 360
	MaxConnectionTime     time.Duration `json:"max_connection_time" mapstructure:"max_connection_time"`           // 60
	MaxIdleConnections    int           `json:"max_idle_connections" mapstructure:"max_idle_connections"`         // 5
	MaxOpenConnections    int           `json:"max_open_connections" mapstructure:"max_open_connections"`         // 5
	TablePrefix           string        `json:"table_prefix" mapstructure:"table_prefix"`                         // pre_users (pre)
}

CommonConfig is the common configuration fields between engines

type DatabaseLogWrapper added in v0.2.14

type DatabaseLogWrapper struct {
	logger.Interface
}

DatabaseLogWrapper is a special wrapper for the GORM logger

func (*DatabaseLogWrapper) LogMode added in v0.2.14

LogMode will set the log level/mode

type Engine

type Engine string

Engine is the different engines that are supported (database)

const (
	Empty      Engine = "empty"
	MongoDB    Engine = "mongodb"
	MySQL      Engine = "mysql"
	PostgreSQL Engine = "postgresql"
	SQLite     Engine = "sqlite"
)

Supported engines (databases)

func (Engine) IsEmpty

func (e Engine) IsEmpty() bool

IsEmpty will return true if the datastore is not set

func (Engine) String

func (e Engine) String() string

String is the string version of engine

type MongoDBConfig

type MongoDBConfig struct {
	CommonConfig       `json:",inline" mapstructure:",squash"` // Common configuration
	DatabaseName       string                                  `json:"database_name" mapstructure:"database_name"` // The database name
	ExistingConnection *mongo.Database                         `json:"-" mapstructure:"-"`                         // Used for existing database connection
	Transactions       bool                                    `json:"transactions" mapstructure:"transactions"`   // If it has transactions
	URI                string                                  `json:"uri" mapstructure:"uri"`                     // The connection string URI
}

MongoDBConfig is the configuration for each MongoDB connection

type QueryParams added in v0.2.14

type QueryParams struct {
	Page          int    `json:"page,omitempty"`
	PageSize      int    `json:"page_size,omitempty"`
	OrderByField  string `json:"order_by_field,omitempty"`
	SortDirection string `json:"sort_direction,omitempty"`
}

QueryParams object to use when limiting and sorting database query results

func UnmarshalQueryParams added in v0.2.14

func UnmarshalQueryParams(v interface{}) (QueryParams, error)

UnmarshalQueryParams will unmarshal the custom type

type SQLConfig

type SQLConfig struct {
	CommonConfig              `json:",inline" mapstructure:",squash"` // Common configuration
	Driver                    string                                  `json:"driver" mapstructure:"driver"`                                             // mysql or postgresql
	ExistingConnection        *sql.DB                                 `json:"-" mapstructure:"-"`                                                       // Used for existing database connection
	Host                      string                                  `json:"host" mapstructure:"host"`                                                 // database host IE: localhost
	Name                      string                                  `json:"name" mapstructure:"name"`                                                 // database-name
	Password                  string                                  `json:"password" mapstructure:"password" encrypted:"true"`                        // user-password
	Port                      string                                  `json:"port" mapstructure:"port"`                                                 // 3306
	Replica                   bool                                    `json:"replica" mapstructure:"replica"`                                           // True if it's a replica (Read-Only)
	SkipInitializeWithVersion bool                                    `json:"skip_initialize_with_version" mapstructure:"skip_initialize_with_version"` // Skip using MySQL in test mode
	TimeZone                  string                                  `json:"time_zone" mapstructure:"time_zone"`                                       // timezone (IE: Asia/Shanghai)
	TxTimeout                 time.Duration                           `json:"tx_timeout" mapstructure:"tx_timeout"`                                     // 5*time.Second
	User                      string                                  `json:"user" mapstructure:"user"`                                                 // database username
}

SQLConfig is the configuration for each SQL connection (mysql or postgresql)

type SQLiteConfig

type SQLiteConfig struct {
	CommonConfig       `json:",inline" mapstructure:",squash"` // Common configuration
	DatabasePath       string                                  `json:"database_path" mapstructure:"database_path"` // Location of a permanent database file (if NOT set, uses temporary memory)
	ExistingConnection gorm.ConnPool                           `json:"-" mapstructure:"-"`                         // Used for existing database connection
	Shared             bool                                    `json:"shared" mapstructure:"shared"`               // Adds a shared param to the connection string
}

SQLiteConfig is the configuration for each SQLite connection

type StorageService

type StorageService interface {
	AutoMigrateDatabase(ctx context.Context, models ...interface{}) error
	CreateInBatches(ctx context.Context, models interface{}, batchSize int) error
	Execute(query string) *gorm.DB
	GetModel(ctx context.Context, model interface{}, conditions map[string]interface{}, timeout time.Duration, forceWriteDB bool) error
	GetModels(ctx context.Context, models interface{}, conditions map[string]interface{}, queryParams *QueryParams,
		fieldResults interface{}, timeout time.Duration) error
	GetModelCount(ctx context.Context, model interface{}, conditions map[string]interface{},
		timeout time.Duration) (int64, error)
	GetModelsAggregate(ctx context.Context, models interface{}, conditions map[string]interface{},
		aggregateColumn string, timeout time.Duration) (map[string]interface{}, error)
	HasMigratedModel(modelType string) bool
	IncrementModel(ctx context.Context, model interface{},
		fieldName string, increment int64) (newValue int64, err error)
	IndexExists(tableName, indexName string) (bool, error)
	IndexMetadata(tableName, field string) error
	NewTx(ctx context.Context, fn func(*Transaction) error) error
	Raw(query string) *gorm.DB
	SaveModel(ctx context.Context, model interface{}, tx *Transaction, newRecord, commitTx bool) error
}

StorageService is the storage related methods

type Transaction

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

Transaction is the internal datastore transaction

func (*Transaction) CanCommit

func (tx *Transaction) CanCommit() bool

CanCommit will return true if it can commit

func (*Transaction) Commit

func (tx *Transaction) Commit() error

Commit will commit the transaction

func (*Transaction) Rollback

func (tx *Transaction) Rollback() error

Rollback the transaction

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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