Categorygithub.com/ankorstore/yokai-contrib/fxgomysqlserver
modulepackage
1.0.0
Repository: https://github.com/ankorstore/yokai-contrib.git
Documentation: pkg.go.dev

# README

Fx Go MySQL Server Module

ci go report codecov Deps PkgGoDev

Fx module for Go Mysql Server.

Overview

This module integrates an embedded Go MySQL server into your Yokai application.

This is made for development / testing purposes, not to replace a real MySQL server for production applications.

It can be configured to accept connections:

  • via TCP
  • via a socket
  • via memory

Make sure to acknowledge the underlying vendor limitations.

Installation

First install the module:

go get github.com/ankorstore/yokai-contrib/fxgomysqlserver

Then activate it in your application bootstrapper:

// internal/bootstrap.go
package internal

import (
	"github.com/ankorstore/yokai/fxcore"
	"github.com/ankorstore/yokai-contrib/fxgomysqlserver"
)

var Bootstrapper = fxcore.NewBootstrapper().WithOptions(
	// load fxgomysqlserver module
	fxgomysqlserver.FxGoMySQLServerModule,
	// ...
)

Configuration

Configuration reference:

# ./configs/config.yaml
app:
  name: app
  env: dev
  version: 0.1.0
  debug: true
modules:
  gomysqlserver:
    config:
      transport: tcp            # transport to use: tcp, socket or memory (tcp by default)
      socket: /tmp/mysql.sock   # socket path (/tmp/mysql.sock by default)
      user: user                # database user name (user by default)
      password: password        # database user password (password by default)
      host: localhost           # database host (localhost by default)
      port: 3306                # database port (3306 by default)
      database: db              # database name (db by default)
    log:
      enabled: true             # to enable server logs
    trace:
      enabled: true             # to enable server traces

Usage

TCP transport

You can configure the server to accept TCP connections:

# ./configs/config.yaml
modules:
  gomysqlserver:
    config:
      transport: tcp
      user: user
      password: password
      host: localhost
      port: 3306
      database: db

And then connect with:

import "database/sql"

db, _ := sql.Open("mysql", "user:password@tcp(localhost:3306)/db")

Socket transport

You can configure the server to accept socket connections:

# ./configs/config.yaml
modules:
  gomysqlserver:
    config:
      transport: socket
      socket: /tmp/mysql.sock 
      user: user
      password: password
      database: db

And then connect with:

import "database/sql"

db, _ := sql.Open("mysql", "user:password@unix(/tmp/mysql.sock)/db")

Memory transport

You can configure the server to accept memory connections:

# ./configs/config.yaml
modules:
  gomysqlserver:
    config:
      transport: memory
      user: user
      password: password
      database: db

And then connect with:

import "database/sql"

db, _ := sql.Open("mysql", "user:password@memory(bufconn)/db")

Testing

The memory transport avoids to open TCP ports or sockets, making it particularly lean and useful for testing purposes.

In you test configuration:

# ./configs/config.test.yaml
modules:
  gomysqlserver:
    config:
      transport: memory

In you application bootstrapper:

// internal/bootstrap.go
package internal

import (
	"testing"
	
	"github.com/ankorstore/yokai-contrib/fxgomysqlserver"
)

// RunTest starts the application in test mode, with an optional list of [fx.Option].
func RunTest(tb testing.TB, options ...fx.Option) {
	tb.Helper()

	// ...

	Bootstrapper.RunTestApp(
		tb,
		// enable and start the server
		fxgomysqlserver.FxGoMySQLServerModule,
		// apply per test options
		fx.Options(options...),
	)
}

You can check the tests of this module to get testing examples.

# Packages

No description provided by the author
No description provided by the author

# Functions

NewFxGoMySQLServer returns a new [sqle.Server] instance.
NewFxGoMySQLServerConfig returns a new [config.GoMySQLServerConfig] instance.
NewFxGoMySQLServerModuleInfo returns a new [FxGoMySQLServerModuleInfo].

# Constants

ModuleName is the module name.

# Variables

FxGoMySQLServerModule is the [Fx] go-mysql-server module.

# Structs

FxGoMySQLServerConfigParam allows injection of the required dependencies in [NewGoMySQLServerConfig].
FxGoMySQLServerModuleInfo is a module info collector for gomysqlserver.
FxGoMySQLServerParam allows injection of the required dependencies in [NewFxGoMySQLServer].