nero

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFuncMap

func NewFuncMap() template.FuncMap

NewFuncMap returns a template func map

func ParseTemplate

func ParseTemplate(t Template) (*template.Template, error)

ParseTemplate parses the repository template

Types

type ErrRequiredField

type ErrRequiredField struct {
	// contains filtered or unexported fields
}

ErrRequiredField is a required field error

func NewErrRequiredField

func NewErrRequiredField(field string) *ErrRequiredField

NewErrRequiredField returns an ErrFieldRequired error

func (*ErrRequiredField) Error

func (e *ErrRequiredField) Error() string

type Field

type Field struct {
	// contains filtered or unexported fields
}

Field is a field

func (*Field) Identifier

func (f *Field) Identifier() string

Identifier returns the lower-camelized struct field

func (*Field) IdentifierPlural

func (f *Field) IdentifierPlural() string

IdentifierPlural returns the plural form of identifier

func (*Field) IsArray

func (f *Field) IsArray() bool

IsArray returns true if field is an array or a slice

func (*Field) IsAuto

func (f *Field) IsAuto() bool

IsAuto returns the auto flag

func (*Field) IsComparable

func (f *Field) IsComparable() bool

IsComparable returns true if field is comparable i.e. with comparisong operators

func (*Field) IsNillable

func (f *Field) IsNillable() bool

IsNillable returns true if the field is nillable

func (*Field) IsOptional

func (f *Field) IsOptional() bool

IsOptional returns the optional flag

func (*Field) IsValueScanner

func (f *Field) IsValueScanner() bool

IsValueScanner returns true if field implements value scanner

func (*Field) Name

func (f *Field) Name() string

Name returns the field name

func (*Field) StructField

func (f *Field) StructField() string

StructField returns the struct field

func (*Field) TypeInfo

func (f *Field) TypeInfo() *mira.TypeInfo

TypeInfo returns the type info

type FieldBuilder

type FieldBuilder struct {
	// contains filtered or unexported fields
}

FieldBuilder is a field builder

func NewFieldBuilder

func NewFieldBuilder(name string, v interface{}) *FieldBuilder

NewFieldBuilder takes a field name and a value and returns a FieldBuilder

func (*FieldBuilder) Auto

func (fb *FieldBuilder) Auto() *FieldBuilder

Auto sets the auto-populated flag

func (*FieldBuilder) Build

func (fb *FieldBuilder) Build() *Field

Build builds the field

func (*FieldBuilder) Optional

func (fb *FieldBuilder) Optional() *FieldBuilder

Optional sets the optional flag

func (*FieldBuilder) StructField

func (fb *FieldBuilder) StructField(structField string) *FieldBuilder

StructField sets the struct field

type Logger

type Logger interface {
	Printf(string, ...interface{})
}

Logger is an interface that wraps the Printf method

type PostgresTemplate

type PostgresTemplate struct {
	// contains filtered or unexported fields
}

PostgresTemplate is the template for generating a postgres repository

func NewPostgresTemplate

func NewPostgresTemplate() *PostgresTemplate

NewPostgresTemplate returns a new PostgresTemplate

func (*PostgresTemplate) Content

func (t *PostgresTemplate) Content() string

Content returns the template content

func (*PostgresTemplate) Filename

func (t *PostgresTemplate) Filename() string

Filename returns the filename

func (*PostgresTemplate) WithFilename

func (t *PostgresTemplate) WithFilename(filename string) *PostgresTemplate

WithFilename overrides the default filename

type SQLRunner

type SQLRunner interface {
	Query(string, ...interface{}) (*sql.Rows, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRow(string, ...interface{}) *sql.Row
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
	Exec(string, ...interface{}) (sql.Result, error)
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

SQLRunner is an interface that wraps the standard sql methods

type SQLiteTemplate

type SQLiteTemplate struct {
	// contains filtered or unexported fields
}

SQLiteTemplate is a template for generating an sqlite repository

func NewSQLiteTemplate

func NewSQLiteTemplate() *SQLiteTemplate

NewSQLiteTemplate returns a new SQLiteTemplate

func (*SQLiteTemplate) Content

func (t *SQLiteTemplate) Content() string

Content returns the template content

func (*SQLiteTemplate) Filename

func (t *SQLiteTemplate) Filename() string

Filename returns the filename

func (*SQLiteTemplate) WithFilename

func (t *SQLiteTemplate) WithFilename(filename string) *SQLiteTemplate

WithFilename overrides the default filename

type Schema

type Schema struct {
	// contains filtered or unexported fields
}

Schema is a schema used for generating the repository

func (*Schema) Fields

func (s *Schema) Fields() []*Field

Fields returns the fields

func (*Schema) Identity

func (s *Schema) Identity() *Field

Identity returns the identity field

func (*Schema) Imports

func (s *Schema) Imports() []string

Imports returns the pkg imports

func (*Schema) PkgName

func (s *Schema) PkgName() string

PkgName returns the pkg name

func (*Schema) Table

func (s *Schema) Table() string

Table returns the database table name

func (*Schema) Templates

func (s *Schema) Templates() []Template

Templates returns the templates

func (*Schema) TypeIdentifier

func (s *Schema) TypeIdentifier() string

TypeIdentifier returns the type identifier

func (*Schema) TypeIdentifierPlural

func (s *Schema) TypeIdentifierPlural() string

TypeIdentifierPlural returns the plural form of type identifier

func (*Schema) TypeInfo

func (s *Schema) TypeInfo() *mira.TypeInfo

TypeInfo returns the type info

func (*Schema) TypeName

func (s *Schema) TypeName() string

TypeName returns the type name

func (*Schema) TypeNamePlural

func (s *Schema) TypeNamePlural() string

TypeNamePlural returns the plural form of the type name

type SchemaBuilder

type SchemaBuilder struct {
	// contains filtered or unexported fields
}

SchemaBuilder is used for building a schema

func NewSchemaBuilder

func NewSchemaBuilder(v interface{}) *SchemaBuilder

NewSchemaBuilder takes a struct value and returns a SchemaBuilder

func (*SchemaBuilder) Build

func (sb *SchemaBuilder) Build() *Schema

Build builds the schema

func (*SchemaBuilder) Fields

func (sb *SchemaBuilder) Fields(fields ...*Field) *SchemaBuilder

Fields sets the fields

func (*SchemaBuilder) Identity

func (sb *SchemaBuilder) Identity(field *Field) *SchemaBuilder

Identity sets the identity field

func (*SchemaBuilder) PkgName

func (sb *SchemaBuilder) PkgName(pkgName string) *SchemaBuilder

PkgName sets the package name

func (*SchemaBuilder) Table

func (sb *SchemaBuilder) Table(table string) *SchemaBuilder

Table sets the database table/collection name

func (*SchemaBuilder) Templates

func (sb *SchemaBuilder) Templates(templates ...Template) *SchemaBuilder

Templates sets the templates

type Template

type Template interface {
	// Filename is the filename of the generated file
	Filename() string
	// Content is returns the template content
	Content() string
}

Template is an interface that wraps the Filename and Content method

type Tx

type Tx interface {
	Commit() error
	Rollback() error
}

Tx is an interface that wraps the Commit and Rollback method

type ValueScanner

type ValueScanner interface {
	driver.Valuer
	sql.Scanner
}

ValueScanner is an interface that wraps the driver.Valuer and sql.Scanner interface

Directories

Path Synopsis
gen
test
integration/playerrepo
Code generated by nero, DO NOT EDIT.
Code generated by nero, DO NOT EDIT.
x
etc

Jump to

Keyboard shortcuts

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