Documentation
¶
Index ¶
- func ApplyConfig(db *sql.DB, config *MySqlDBConfig)
- func BeginPostgres(db *sql.DB) (*sql.Tx, error)
- func CheckDatabase(database *DatabaseConfig, defaults DatabaseConfig)
- func CheckHostname(database *DatabaseConfig, defaults string)
- func CheckName(database *DatabaseConfig, defaults string)
- func CheckPassword(database *DatabaseConfig, defaults string)
- func CheckPort(database *DatabaseConfig, defaults string)
- func CheckProtocol(database *DatabaseConfig, defaults string)
- func CheckType(database *DatabaseConfig, defaults string)
- func CheckUsername(database *DatabaseConfig, defaults string)
- func CloseMySQL(db *sql.DB)
- func ClosePostgres(db *sql.DB) error
- func ConnectMongoDB(config MongoDBConfig) (*mongo.Client, error)
- func CreateMySqlTable(db *sql.DB, tableName string, s interface{}) error
- func DisconnectMongoDB(client *mongo.Client) error
- func ExecPostgres(db *sql.DB, query string, args ...interface{}) (sql.Result, error)
- func ExecuteSQL(db *sql.DB, query string, args ...interface{}) (sql.Result, error)
- func MapToStruct(m map[string]interface{}, s interface{}) error
- func MongoDBOptions(config MongoDBConfig) *options.ClientOptions
- func MongoDBPing(client *mongo.Client) error
- func MySqlConn(dbType string, dsn string) (*sql.DB, error)
- func MySqlPingDB(db *sql.DB) error
- func MySqlString(config *MySqlDBConfig) string
- func NewDBConnection(config *MySqlDBConfig) (*sql.DB, error)
- func NewRedisClient(config RedisConfig) *redis.Client
- func OpenPostgres(connString string) (*sql.DB, error)
- func PingPostgres(db *sql.DB) error
- func PostgresString(config DatabaseConfig) string
- func QueryPostgres(db *sql.DB, query string, args ...interface{}) (*sql.Rows, error)
- func QueryRowPostgres(db *sql.DB, query string, args ...interface{}) *sql.Row
- func QuerySQL(db *sql.DB, query string, args ...interface{}) ([]map[string]interface{}, error)
- func RedisClose(client *redis.Client) error
- func RedisDelete(client *redis.Client, key string) error
- func RedisExists(client *redis.Client, key string) (int64, error)
- func RedisGet(client *redis.Client, key string) (interface{}, error)
- func RedisHExists(client *redis.Client, key string, field string) (bool, error)
- func RedisHGet(client *redis.Client, key string, field string) (interface{}, error)
- func RedisHSet(client *redis.Client, key string, field string, value interface{}) error
- func RedisOptions(config RedisConfig) *redis.Options
- func RedisPing(client *redis.Client) error
- func RedisSAdd(client *redis.Client, key string, member interface{}) error
- func Set(client *redis.Client, key string, value interface{}) error
- func SetupPostgres(config DatabaseConfig) (*sql.DB, error)
- func WithDBConnection(config *MySqlDBConfig, tableName string, s interface{}, f func(*sql.DB) error) error
- type DatabaseConfig
- type MongoDBConfig
- type MySqlDBConfig
- type RedisConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyConfig ¶
func ApplyConfig(db *sql.DB, config *MySqlDBConfig)
ApplyConfig sets the connection pool settings for a *sql.DB instance using the provided ConnectionConfig.
func BeginPostgres ¶
BeginPostgres starts a new transaction. Transactions are useful for ensuring that a series of queries are executed as a single unit of work, and can be rolled back if any of the queries fail.
func CheckDatabase ¶
func CheckDatabase(database *DatabaseConfig, defaults DatabaseConfig)
CheckDatabase checks if the Type, Protocol, Hostname, Port, Name, User, and Password fields in the given ConnectionConfig are set from the DATABASE_TYPE, DATABASE_PROTOCOL, DATABASE_HOST, DATABASE_PORT, DATABASE_NAME, DATABASE_USER, and DATABASE_PASSWORD environment variables. If they are not set, it sets them to the given default values.
func CheckHostname ¶
func CheckHostname(database *DatabaseConfig, defaults string)
CheckHostname checks if the Hostname field in the given ConnectionConfig is set from the DATABASE_HOST environment variable. If it is not set, it sets it to the given default value.
func CheckName ¶
func CheckName(database *DatabaseConfig, defaults string)
CheckName checks if the Name field in the given ConnectionConfig is set from the DATABASE_NAME environment variable. If it is not set, it sets it to the given default value.
func CheckPassword ¶
func CheckPassword(database *DatabaseConfig, defaults string)
CheckPassword checks if the Password field in the given ConnectionConfig is set from the DATABASE_PASSWORD environment variable. If it is not set, it sets it to the given default value.
func CheckPort ¶
func CheckPort(database *DatabaseConfig, defaults string)
CheckPort checks if the Port field in the given ConnectionConfig is set from the DATABASE_PORT environment variable. If it is not set, it sets it to the given default value.
func CheckProtocol ¶
func CheckProtocol(database *DatabaseConfig, defaults string)
CheckProtocol checks if the Protocol field in the given ConnectionConfig is set from the DATABASE_PROTOCOL environment variable. If it is not set, it sets it to the given default value.
func CheckType ¶
func CheckType(database *DatabaseConfig, defaults string)
CheckType checks if the Type field in the given ConnectionConfig is set from the DATABASE_TYPE environment variable. If it is not set, it sets it to the given default value.
func CheckUsername ¶
func CheckUsername(database *DatabaseConfig, defaults string)
CheckUsername checks if the User field in the given ConnectionConfig is set from the DATABASE_USER environment variable. If it is not set, it sets it to the given default value.
func CloseMySQL ¶
func ClosePostgres ¶
ClosePostgres closes the connection to the Postgres database.
func ConnectMongoDB ¶
func ConnectMongoDB(config MongoDBConfig) (*mongo.Client, error)
ConnectMongoDB establishes a connection to a MongoDB server.
func CreateMySqlTable ¶
CreateMySqlTable creates a new table in the database based on the provided struct.
func DisconnectMongoDB ¶
DisconnectMongoDB closes the connection to a MongoDB server.
func ExecPostgres ¶
ExecPostgres executes a query that does not return rows (e.g. INSERT, UPDATE, DELETE).
func ExecuteSQL ¶
ExecuteSQL is a generic function that can be used to execute any SQL query.
func MapToStruct ¶
MapToStruct converts a map[string]interface{} to a struct. The map keys must match the struct field names.
func MongoDBOptions ¶
func MongoDBOptions(config MongoDBConfig) *options.ClientOptions
MongoDBOptions creates a *options.ClientOptions struct using a MongoDBConfig struct.
func MongoDBPing ¶
MongoDBPing pings the MongoDB server to test the connection.
func MySqlPingDB ¶
MySqlPingDB pings the database to check the connection.
func MySqlString ¶
func MySqlString(config *MySqlDBConfig) string
MySqlString builds connection string for connecting to a MySQL database using the provided ConnectionConfig.
func NewDBConnection ¶
func NewDBConnection(config *MySqlDBConfig) (*sql.DB, error)
NewDBConnection establishes a connection to the MySQL database specified in the provided ConnectionConfig.
func NewRedisClient ¶
func NewRedisClient(config RedisConfig) *redis.Client
NewRedisClient creates a new Redis client using the provided configuration.
func OpenPostgres ¶
OpenPostgres opens a new connection to a Postgres database using the provided connection string.
func PingPostgres ¶
PingPostgres pings the Postgres database to test the connection.
func PostgresString ¶
func PostgresString(config DatabaseConfig) string
PostgresString returns a connection string for a Postgres database.
func QueryPostgres ¶
QueryPostgres executes a query that returns rows (e.g. SELECT).
func QueryRowPostgres ¶
QueryRowPostgres executes a query that returns a single row. It is useful when you are expecting a single row as the result of a query (e.g. SELECT COUNT(*) FROM table).
func QuerySQL ¶
QuerySQL is a generic function that can be used to execute any SELECT SQL query. It returns a slice of map[string]interface{}, with each map representing a row in the result set. The keys of the map are the column names, and the values are the column values for that row.
func RedisClose ¶
RedisClose closes the connection to the Redis server.
func RedisDelete ¶
RedisDelete deletes a key in Redis.
func RedisExists ¶
RedisExists checks if a key exists in Redis.
func RedisHExists ¶
RedisHExists checks if a field exists in a hash in Redis.
func RedisOptions ¶
func RedisOptions(config RedisConfig) *redis.Options
RedisOptions creates a *redis.Options struct using a RedisConfig struct.
func SetupPostgres ¶
func SetupPostgres(config DatabaseConfig) (*sql.DB, error)
SetupPostgres sets up a connection to a Postgres database using the provided configuration.
func WithDBConnection ¶
func WithDBConnection(config *MySqlDBConfig, tableName string, s interface{}, f func(*sql.DB) error) error
WithDBConnection establishes a connection to the database, creates the specified table if it doesn't exist, and then closes the connection when the provided function is finished executing.
Types ¶
type DatabaseConfig ¶
type DatabaseConfig struct { Type string Protocol string Hostname string Port string Name string Username string Password string }
func DefaultMongoDB ¶
func DefaultMongoDB() DatabaseConfig
func DefaultMySql ¶
func DefaultMySql() DatabaseConfig
DefaultMysql returns a DatabaseConfig struct with default values for the fields.
func DefaultPostgres ¶
func DefaultPostgres() DatabaseConfig
func DefaultRedis ¶
func DefaultRedis() DatabaseConfig
func GetDatabase ¶
func GetDatabase(defaults DatabaseConfig) DatabaseConfig
GetDatabase returns a ConnectionConfig struct with the Type, Protocol, Hostname, Port, Name, User, and Password fields set from the DATABASE_TYPE, DATABASE_PROTOCOL, DATABASE_HOST, DATABASE_PORT, DATABASE_NAME, DATABASE_USER, and DATABASE_PASSWORD environment variables. If they are not set, it sets them to the given default values.
type MongoDBConfig ¶
type MongoDBConfig struct { DatabaseConfig URI string Database string }
MongoDBConfig holds the configuration for a MongoDB connection.
func DefaultMongoDBConfig ¶
func DefaultMongoDBConfig() MongoDBConfig
DefaultMongoDBConfig returns a default configuration for a MongoDB connection.
type MySqlDBConfig ¶
type MySqlDBConfig struct { DatabaseConfig DBName string Timeout time.Duration MaxIdleConns int MaxOpenConns int ConnMaxLifetime time.Duration ConnMaxIdleTime time.Duration }
MySqlDBConfig is a struct that contains the configuration for the database connection.
func DefaultMySqlConf ¶
func DefaultMySqlConf() *MySqlDBConfig
DefaultMySqlConf returns a struct with default values for the fields.
type RedisConfig ¶
type RedisConfig struct { DatabaseConfig Address string DB int PoolSize int }
func DefaultRedisConfig ¶
func DefaultRedisConfig() RedisConfig
DefaultRedisConfig returns a default configuration for a Redis connection.