mariadb

package module
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2025 License: MIT Imports: 26 Imported by: 0

README

MariaDB Adapter Package

This package provides a MariaDB adapter for the Frameless. It allows you to interact with MariaDB databases using a standardized interface.

Features

  • Connection management: Establish and manage connections to MariaDB databases.
  • CRUD operations: Perform create, read, update, and delete operations on MariaDB tables.
  • Transaction support: Use transactions to ensure atomicity and consistency of database operations.
  • Migration support: Use migrations to manage schema changes and versioning of your database.
  • use MariaDB as caching backend

Getting Started

To use this package, you need to install it using Go's package manager:

go get go.llib.dev/frameless/adapter/mariadb

Then, import the package in your Go program:

import "go.llib.dev/frameless/adapter/mariadb"

Create a connection to a MariaDB database using the Connect function:

conn, err := mariadb.Connect("user:password@tcp(localhost:3306)/database")

Use the Repository type to perform CRUD operations on a table:

repo := mariadb.Repository[Entity, ID]{
    Connection: conn,
    Mapping:    EntityMapping(),
}

// Create an entity
entity := Entity{Name: "John Doe"}
err := repo.Create(context.Background(), &entity)

License

This package is licensed under the MIT License.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSON

func JSON[T any](ptr *T) flsql.DTO

func MakeMigrator

func MakeMigrator(conn Connection, namespace string, steps migration.Steps[Connection]) migration.Migrator[Connection]

func Timestamp

func Timestamp(ptr *time.Time) flsql.DTO

Timestamp is a MySQL DTO Model for the timestamp type mapping. Use it from your scan and argument mapping.

Types

type CacheRepository

type CacheRepository[ENT, ID any] struct {
	Connection Connection
	// ID [required] is unique identifier.  the table name prefix used to create the cache repository tables.
	//
	// Example:
	// 		ID: "foo"
	// 			-> "foo_cache_entities"
	//
	ID string
	// JSONDTOM [optional] is the mapping between an ENT type and a JSON DTO type,
	// which is used to encode entities within the entity repository.
	// This mapping is important because if the entity type changes during refactoring,
	// the previously cached data can still be correctly decoded using the JSON DTO.
	// This means you won’t need to delete cached data or worry about data corruption.
	// It provides a safeguard, ensuring smooth transitions without affecting stored data.
	JSONDTOM dtokit.Mapper[ENT]
	// IDA is the ID accessor, that explains how the ID field of the ENT can be accessed.
	IDA extid.Accessor[ENT, ID]
	// IDM is the mapping between ID and the string type which is used in the CacheRepository tables to represent the ID value.
	// If the ID is a string type, then this field can be ignored.
	IDM dtokit.MapperTo[ID, string]
}

CacheRepository is a generic implementation for using mysql as a caching backend with `frameless/pkg/cache.Cache`. CacheRepository implements `cache.Repository[ENT,ID]`

func (CacheRepository[ENT, ID]) BeginTx

func (r CacheRepository[ENT, ID]) BeginTx(ctx context.Context) (context.Context, error)

func (CacheRepository[ENT, ID]) CommitTx

func (r CacheRepository[ENT, ID]) CommitTx(ctx context.Context) error

func (CacheRepository[ENT, ID]) Entities

func (r CacheRepository[ENT, ID]) Entities() cache.EntityRepository[ENT, ID]

func (CacheRepository[ENT, ID]) Hits

func (r CacheRepository[ENT, ID]) Hits() cache.HitRepository[ID]

func (CacheRepository[ENT, ID]) Migrate

func (r CacheRepository[ENT, ID]) Migrate(ctx context.Context) error

func (CacheRepository[ENT, ID]) RollbackTx

func (r CacheRepository[ENT, ID]) RollbackTx(ctx context.Context) error

type Connection

type Connection struct {
	flsql.ConnectionAdapter[sql.DB, sql.Tx]
}

func Connect

func Connect(dsn string) (Connection, error)

type Locker added in v0.4.0

type Locker struct {
	Name       string
	Connection Connection
}

Locker is a MariaDB-based shared mutex implementation.

Example
package main

import (
	"context"
	"os"

	"go.llib.dev/frameless/adapter/mariadb"
)

func main() {
	cm, err := mariadb.Connect(os.Getenv("DATABASE_URL"))
	if err != nil {
		panic(err)
	}

	l := mariadb.Locker{
		Name:       "my-lock",
		Connection: cm,
	}

	ctx, err := l.Lock(context.Background())
	if err != nil {
		panic(err)
	}

	if err := l.Unlock(ctx); err != nil {
		panic(err)
	}
}

func (Locker) Lock added in v0.4.0

func (l Locker) Lock(ctx context.Context) (context.Context, error)

func (Locker) Migrate added in v0.4.0

func (l Locker) Migrate(ctx context.Context) error

func (Locker) TryLock added in v0.5.0

func (l Locker) TryLock(ctx context.Context) (_ context.Context, _ bool, rErr error)

func (Locker) Unlock added in v0.4.0

func (l Locker) Unlock(ctx context.Context) error

type LockerFactory added in v0.4.0

type LockerFactory[Key comparable] struct {
	Connection Connection
	// Namespace [optional] allows you to make isolation between locks generated with the same key but for a different namesapce.
	Namespace string
}
Example
package main

import (
	"context"
	"log"
	"os"

	"go.llib.dev/frameless/adapter/mariadb"
)

func main() {
	cm, err := mariadb.Connect(os.Getenv("DATABASE_URL"))
	if err != nil {
		log.Fatal(err)
	}

	lockerFactory := mariadb.LockerFactory[string]{Connection: cm}
	if err := lockerFactory.Migrate(context.Background()); err != nil {
		log.Fatal(err)
	}

	locker := lockerFactory.LockerFor("hello world")

	ctx, err := locker.Lock(context.Background())
	if err != nil {
		log.Fatal(err)
	}

	if err := locker.Unlock(ctx); err != nil {
		log.Fatal(err)
	}
}

func (LockerFactory[Key]) LockerFor added in v0.4.0

func (lf LockerFactory[Key]) LockerFor(key Key) guard.Locker

func (LockerFactory[Key]) Migrate added in v0.4.0

func (lf LockerFactory[Key]) Migrate(ctx context.Context) error

func (LockerFactory[Key]) NonBlockingLockerFor added in v0.5.0

func (lf LockerFactory[Key]) NonBlockingLockerFor(key Key) guard.NonBlockingLocker

func (LockerFactory[Key]) Purge added in v0.5.0

func (lf LockerFactory[Key]) Purge(ctx context.Context) error

type Repository

type Repository[ENT, ID any] struct {
	Connection Connection
	Mapping    flsql.Mapping[ENT, ID]
}

Repository implements CRUD operations for a specific entity type in mariadb.

func MakeMigrationStateRepository

func MakeMigrationStateRepository(conn Connection) Repository[migration.State, migration.StateID]

func (Repository[ENT, ID]) BeginTx

func (r Repository[ENT, ID]) BeginTx(ctx context.Context) (context.Context, error)

BeginTx implements the comproto.OnePhaseCommitter interface.

func (Repository[ENT, ID]) CommitTx

func (r Repository[ENT, ID]) CommitTx(ctx context.Context) error

CommitTx implements the comproto.OnePhaseCommitter interface.

func (Repository[ENT, ID]) Create

func (r Repository[ENT, ID]) Create(ctx context.Context, ptr *ENT) (rErr error)

func (Repository[ENT, ID]) DeleteAll

func (r Repository[ENT, ID]) DeleteAll(ctx context.Context) (rErr error)

func (Repository[ENT, ID]) DeleteByID

func (r Repository[ENT, ID]) DeleteByID(ctx context.Context, id ID) (rErr error)

func (Repository[ENT, ID]) FindAll

func (r Repository[ENT, ID]) FindAll(ctx context.Context) iterkit.SeqE[ENT]

func (Repository[ENT, ID]) FindByID

func (r Repository[ENT, ID]) FindByID(ctx context.Context, id ID) (ENT, bool, error)

func (Repository[ENT, ID]) FindByIDs

func (r Repository[ENT, ID]) FindByIDs(ctx context.Context, ids ...ID) iterkit.SeqE[ENT]

func (Repository[ENT, ID]) RollbackTx

func (r Repository[ENT, ID]) RollbackTx(ctx context.Context) error

RollbackTx implements the comproto.OnePhaseCommitter interface.

func (Repository[ENT, ID]) Save added in v0.2.0

func (r Repository[ENT, ID]) Save(ctx context.Context, ptr *ENT) (rErr error)

func (Repository[ENT, ID]) Update

func (r Repository[ENT, ID]) Update(ctx context.Context, ptr *ENT) (rErr error)

type TaskerSchedulerLocks added in v0.4.0

type TaskerSchedulerLocks struct{ Connection Connection }

func (TaskerSchedulerLocks) LockerFor added in v0.4.0

func (TaskerSchedulerLocks) Migrate added in v0.4.0

func (lf TaskerSchedulerLocks) Migrate(ctx context.Context) error

func (TaskerSchedulerLocks) NonBlockingLockerFor added in v0.5.0

func (lf TaskerSchedulerLocks) NonBlockingLockerFor(id tasker.ScheduleID) guard.NonBlockingLocker

type TaskerSchedulerStateRepository added in v0.4.0

type TaskerSchedulerStateRepository struct{ Connection Connection }

func (TaskerSchedulerStateRepository) Create added in v0.4.0

func (TaskerSchedulerStateRepository) DeleteByID added in v0.4.0

func (TaskerSchedulerStateRepository) FindByID added in v0.4.0

func (TaskerSchedulerStateRepository) Migrate added in v0.4.0

func (TaskerSchedulerStateRepository) Update added in v0.4.0

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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