dataprovider

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2024 License: MIT Imports: 7 Imported by: 0

README

CI and Test Generate SBOM Go Report Card Go Reference

Dataprovider

dataprovider is a module that provides a uniform interface to interact behind the scene with many databases and configurations. On this design is used the jmoiron/sqlx package that provides a set of extensions on top of the excellent built-in database/sql package.

The dataprovider in memory and sqlite uses modernc.org/sqlite package that is a CGO free SQLite3 driver for Go.

Working on

  • Database Migration
  • Database Connection
  • Database Transaction
  • Database Query
  • Database QueryRow
  • Database QueryRowx

Supported databases

  • Memory
  • SQLite3
  • MySQL
  • PostgreSQL
  • SQL Server
  • Oracle
  • CockroachDB
  • ClickHouse
  • Cassandra
  • MongoDB
  • Redis
  • InfluxDB
  • Elasticsearch
  • BigQuery
  • Google Cloud Firestore
  • Google Cloud Spanner
  • Google Cloud Datastore

How to use

go get github.com/dyammarcano/dataprovider

Need to build with the tag mysql, postgres, or oracle to use the specific database. Default driver is sqlite in memory mode all data is lost when the program ends.

go build -tags mysql
go build -tags postgres
go build -tags oracle

Example of initialization

package main

import "github.com/dyammarcano/dataprovider"

func main() {
	// Create a config with driver name to initialize the data provider
	opts := dataprovider.NewOptions(
		dataprovider.WithDriver(
			dataprovider.SQLiteDataProviderName), 
			dataprovider.WithConnectionString("file:test.sqlite3?cache=shared"),
		)
	
	var provider = dataprovider.Must(dataprovider.NewDataProvider(opts))

	// Initialize the database
	query := "CREATE TABLE IF NOT EXISTS ...;"
	if err := provider.InitializeDatabase(query); err != nil {
		panic(err)
	}

	// Get the connection and use it as sqlx.DB or sql.DB
	conn := provider.GetConnection()
}

Example of usage

package main

import (
	"encoding/json"
	"github.com/dyammarcano/dataprovider"
	"os"
)

type User struct {
	Id        int64  `json:"id" db:"id"`
	FirstName string `json:"first_name" db:"first_name"`
	LastName  string `json:"last_name" db:"last_name"`
	Email     string `json:"email" db:"email"`
	Gender    string `json:"gender" db:"gender"`
	IpAddress string `json:"ip_address" db:"ip_address"`
	City      string `json:"city" db:"city"`
}

func main() {
	// Create a config with driver name to initialize the data provider
	opts := dataprovider.NewOptions(
		dataprovider.WithDriver(
			dataprovider.MemoryDataProviderName),
		dataprovider.WithConnectionString("file:test.sqlite3?cache=shared"),
	)

	var provider = dataprovider.Must(dataprovider.NewDataProvider(opts))

	// Initialize the database
	query := "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, first_name TEXT, last_name TEXT, email TEXT, gender TEXT, ip_address TEXT, city TEXT);"
	if err := provider.InitializeDatabase(query); err != nil {
		panic(err)
	}

	// Get the connection
	conn := provider.GetConnection()

	// Begin a transaction
	tx := conn.MustBegin()

	// Insert data
	tx.MustExec("insert into users (first_name, last_name, email, gender, ip_address, city) values ('Marcus', 'Bengefield', 'mbengefield0@vistaprint.com', 'Male', '83.121.11.105', 'Miura');")
	tx.MustExec("insert into users (first_name, last_name, email, gender, ip_address, city) values ('Brandise', 'Mateuszczyk', 'bmateuszczyk1@vistaprint.com', 'Female', '131.187.209.233', 'Dalududalu');")
	tx.MustExec("insert into users (first_name, last_name, email, gender, ip_address, city) values ('Ray', 'Ginnaly', 'rginnaly2@merriam-webster.com', 'Male', '76.71.94.89', 'Al Baqāliţah');")

	// Commit the transaction
	if err = tx.Commit(); err != nil {
		panic(err)
	}

	// Query the data
	var users []User

	query = "SELECT * FROM users"
	rows, err := conn.Queryx(query)
	if err != nil {
		panic(err)
	}

	// Scan the data
	for rows.Next() {
		var user User
		if err = rows.StructScan(&user); err != nil {
			panic(err)
		}
		users = append(users, user)
	}

	// Print the data
	if err = json.NewEncoder(os.Stdout).Encode(users); err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

View Source
const (
	// OracleDatabaseProviderName defines the name for Oracle database Provider
	OracleDatabaseProviderName = provider.OracleDatabaseProviderName

	// SQLiteDataProviderName defines the name for SQLite database Provider
	SQLiteDataProviderName = provider.SQLiteDataProviderName

	// MySQLDatabaseProviderName defines the name for MySQL database Provider
	MySQLDatabaseProviderName = provider.MySQLDatabaseProviderName

	// PostgreSQLDatabaseProviderName defines the name for PostgreSQL database Provider
	PostgreSQLDatabaseProviderName = provider.PostgreSQLDatabaseProviderName

	// MemoryDataProviderName defines the name for memory provider using SQLite in-memory database Provider
	MemoryDataProviderName = provider.MemoryDataProviderName
)

Variables

This section is empty.

Functions

func GetQueryFromFile

func GetQueryFromFile(filename string) (string, error)

func NewOptions added in v0.1.0

func NewOptions(opts ...OptionFunc) *provider.Options

Types

type OptionFunc added in v0.1.0

type OptionFunc func(*provider.Options)

func WithConnectionString added in v0.1.0

func WithConnectionString(connectionString string) OptionFunc

func WithContext added in v0.1.0

func WithContext(ctx context.Context) OptionFunc

func WithDriver added in v0.1.0

func WithDriver(driver provider.NamedProvider) OptionFunc

func WithHost added in v0.1.0

func WithHost(host string) OptionFunc

func WithName added in v0.1.0

func WithName(name string) OptionFunc

func WithPassword added in v0.1.0

func WithPassword(password string) OptionFunc

func WithPoolSize added in v0.1.0

func WithPoolSize(poolSize int) OptionFunc

func WithPort added in v0.1.0

func WithPort(port int) OptionFunc

func WithSQLTablesPrefix added in v0.1.0

func WithSQLTablesPrefix(sqlTablesPrefix string) OptionFunc

func WithSchema added in v0.1.0

func WithSchema(schema string) OptionFunc

func WithUsername added in v0.1.0

func WithUsername(username string) OptionFunc

type Provider

type Provider interface {
	// Disconnect disconnects from the data provider
	Disconnect() error

	// GetConnection returns the connection to the data provider
	GetConnection() *sqlx.DB

	// CheckAvailability checks if the data provider is available
	CheckAvailability() error

	// ReconnectDatabase reconnects to the database
	ReconnectDatabase() error

	// InitializeDatabase initializes the database
	InitializeDatabase(schema string) error

	// MigrateDatabase migrates the database to the latest version
	MigrateDatabase() migration.MigrationProvider

	// RevertDatabase reverts the database to the specified version
	RevertDatabase(targetVersion int) error

	// ResetDatabase resets the database
	ResetDatabase() error

	// GetProviderStatus returns the status of the provider
	GetProviderStatus() Status
}

func Must added in v0.1.0

func Must(provider Provider, err error) Provider

Must panics if the error is not nil

Otherwise, it returns the provider instance with the corresponding implementation

func NewDataProvider

func NewDataProvider(options *provider.Options) (Provider, error)

NewDataProvider creates a new data provider instance

type Status added in v0.1.0

type Status = provider.Status

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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