Categorygithub.com/gavrylenkoIvan/go-data
modulepackage
0.0.0-20230724172915-1c9f3ce20d3e
Repository: https://github.com/gavrylenkoivan/go-data.git
Documentation: pkg.go.dev

# README

Go Data

This project was inspired by Spring Data

Quick start

You can use data.SQLRepository instead of your custom repository:

import (
    _ "github.com/lib/pq"
    "github.com/gavrylenkoIvan/go-data"
)

const (
    driver = "postgres"
    url = "postgres_url"
)

// Define struct that will be used in SQLRepository
type Note struct {
	ID    int
	Text  string
}

// This method is required to use SQLRepository
func (n Note) GetTableName() string {
	return "test"
}

func main() {
    // Open db connection
    db, err := sql.Open(driver, url)
	if err != nil {
		log.Fatal(err)
	}

    // Define SQLRepository
    repo := data.NewSQLRepository[int, Note](db)
    
    // You are ready to go!
    res, err := r.GetById(1) 
}

Or you can use it as wrapper for your custom repository:

import (
    _ "github.com/lib/pq"
    "github.com/gavrylenkoIvan/go-data"
)

const (
    driver = "postgres"
    url = "postgres_url"
)

// Create struct that will be used in SQLRepository
type Note struct {
	ID    int
	Text  string
}

// This method is required to use SQLRepository
func (n Note) GetTableName() string {
	return "test"
}

// Define custom repo
type NoteRepo struct {
    db *sqlx.DB
}

func NewNoteRepo(db *sqlx.DB) *NoteRepo {
    return &NoteRepo{
        db: db,
    }
}

func (r *NoteRepo) GetByText() (Note, err) {
    // Implement method
}

func main() {
    // Open db connection
    db, err := sql.Open(driver, url)
	if err != nil {
		log.Fatal(err)
	}

    // Init custom repository
    customRepo := NewNoteRepo(db)

    // Init SQLRepository with NoteRepo as child
    repo := data.NewSQLRepositoryWithChild[int, Note](customRepo, db)
}

# Packages

No description provided by the author

# Functions

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

# Structs

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

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author