Git Product home page Git Product logo

nero's Introduction

GoDoc Reference Github Actions Coverage Status Go Report Card

Nero

A library for generating the repository pattern.

Motivation

We heavily use the repository pattern in our codebases and we often write our queries manually. It becomes tedious and repetitive as we have more tables/models to maintain. So, we decided to experiment on creating this library to generate our repositories automatically.

Goals

  • Decouple implementation from the Repository interface
  • Easy integration with existing codebase
  • Minimal API

Installation

$ go get github.com/stevenferrer/nero

Example

See the official example and integration test for a more complete demo.

import (
    "database/sql"

    // import the generated package
    "github.com/stevenferrer/nero-example/productrepo"
)

func main() {
    dsn := "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable"
    db, err := sql.Open("postgres", dsn)
    ...

    ctx := context.Background()


    // initialize the repository (and optionally enable debug)
    productRepo := productrepo.NewPostgresRepository(db).Debug()

    // create
    creator := productrepo.NewCreator().Name("Product 1")
    productID, err := productRepo.Create(ctx, creator)
    ...

    // query
    queryer := productrepo.NewQueryer().Where(productrepo.IDEq(product1ID))
    product, err := productRepo.QueryOne(ctx, queryer)
    ...

    // update
    now := time.Now()
    updater := productrepo.NewUpdater().Name("Updated Product 1").
        UpdatedAt(&now).Where(productrepo.IDEq(product1ID))
    _, err = productRepo.Update(ctx, updater)
    ...

    // delete
    deleter := productrepo.NewDeleter().Where(productrepo.IDEq(product1ID))
    _, err = productRepo.Delete(ctx, deleter)
    ...
}

Supported back-ends

Below is the list of supported back-ends.

Back-end Library
PostgreSQL lib/pq
SQLite mattn/go-sqlite3
MySQL/MariaDB (soon) go-sql-driver/mysql

If your your back-end is not yet supported, you can implement your own custom back-end.

Custom back-ends

Implementing a custom back-end is very easy. In fact, you don't have to use the official back-ends. You can implement custom back-ends (Oracle, MSSQL, BoltDB, MongoDB, etc.) by implementing the Template interface.

See official postgres template for reference.

Limitations

Currently, we only support basic CRUD and aggregate operations (i.e. count, sum). If you have more complex requirements other than that, we suggest that you just write your repositories manually, at least for now.

We're still in the process of brain-storming how to elegantly support other operations such as joins. If you have any ideas, we'd love to hear from you!

Standing on the shoulders of giants

This project wouldn't be possible without the amazing open-source projects it was built upon:

Also, the following have a huge influence on this project and deserves most of the credits:

  • ent - An entity framework for Go. Simple, yet powerful ORM for modeling and querying data.
  • SQLBoiler - Generate a Go ORM tailored to your database schema.

What's in the name?

The project name is inspired by an anti-bird in an anime called Black Clover. The anti-bird, which the Black Bulls squad calls Nero is apparently a human named Secre Swallowtail. It's a really cool anime with lots of magic!

Contributing

Any suggestions and ideas are very much welcome, feel free to open an issue or make a pull request!

License

Apache-2.0

nero's People

Contributors

stevenferrer avatar

Stargazers

 avatar Alessandro Marotta avatar Roman avatar  avatar Andrew Svirin avatar  avatar  avatar Patryk Orwat avatar John Gosset avatar Juan Alvarez avatar  avatar Fabio Ribeiro avatar Charles avatar Nikita avatar Owen Ou avatar Agbeku Joshua Mawuli avatar Alexey Gromov avatar Artifex avatar ivan avatar Ivan Zakrevsky avatar King'ori Maina avatar 像树一样成长! avatar Alexfilus avatar Tihomir Jovičić avatar Ciaran Liedeman avatar Marco Beierer avatar Daniel Lohse avatar Eugene Oskin avatar Cezar avatar Wes McNamee avatar Radu Topala avatar Lucas Bremgartner avatar Can Evgin avatar yogy fresta rahmawan avatar Eden avatar

Watchers

 avatar Tihomir Jovičić avatar  avatar  avatar

Forkers

emacsway

nero's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.