mariadb

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2024 License: MIT Imports: 22 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

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 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) (iterators.Iterator[ENT], error)

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) (iterators.Iterator[ENT], error)

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)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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