nero

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

README

Nero Github Actions Coverage Status Go Report Card

A library for generating the repository layer code. Please see this blog post for more details.

Installation

$ go get github.com/sf9v/nero

Example

See the integration test for a more complete example.

import (
    "database/sql"

    _ "github.com/lib/pq"

    // import the generated package
    "github.com/sf9v/nero-example/repository"
)

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

    // initialize a new postgres repository
    repo := repository.NewPostgresRepository(db).Debug()

    // create a product
    productID, err := repo.Create(
        context.Background(), 
        repository.NewCreator().Name("Product 1"),
    )
    ...	

    // query the product
    product, err := repo.QueryOne(
        context.Background(), 
        repository.NewQueryer().
            Where(repository.IDEq(product1ID)),
    )
    ...

    // update the product
    now := time.Now()
    _, err = repo.Update(
        context.Background(), 
        repository.NewUpdater().
            Name("Updated Product 1").
            UpdatedAt(&now).
            Where(repository.IDEq(product1ID),
        ),
    )
    ...
    
    // delete the product
    _, err = repo.Delete(
        context.Background(), 
        repository.NewDeleter().
            Where(repository.IDEq(product1ID),
        ),
    )
    ...
}

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 project to generate our repository layer automatically.

Goals

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

Supported back-ends

Currently, we have official support for PostgreSQL, which is what we mainly use. Other back-ends shall be supported as time permits.

Back-end Library
PostgreSQL lib/pq
SQLite (soon)
MySQL/MariaDB (soon)
MongoDB ???

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 (ClickHouse, Cassandra, CouchDB , Badger, H2, etc.) by implementing the Templater interface. This interface is specifically created to support extensibility and customisability.

You can refer to the official postgres template and this example schema.

Standing on the shoulders of giants

This project wouldn't be possible with the amazing open-source projects it was built on.

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 is a tool to generate a Go ORM tailored to your database schema.

Contributing

Any suggestions and ideas on how to improve this library are very much welcome! Please feel free to open an issue or by making a pull request.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFuncMap added in v0.2.0

func NewFuncMap() template.FuncMap

NewFuncMap returns a tempalte func map

func ParseTemplater added in v0.2.0

func ParseTemplater(tmpl Templater) (*template.Template, error)

ParseTemplater parses the repository templater

Types

type Column

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

Column is a column

func (*Column) CanHavePreds added in v0.2.0

func (c *Column) CanHavePreds() bool

CanHavePreds returns true if column can have predicate functions

func (*Column) FieldName added in v0.2.0

func (c *Column) FieldName() string

FieldName returns the field name

func (*Column) Identifier added in v0.2.0

func (c *Column) Identifier() string

Identifier returns the lower-camelized field name

func (*Column) IdentifierPlural added in v0.2.0

func (c *Column) IdentifierPlural() string

IdentifierPlural returns the plural form of lower-camelized field name

func (*Column) IsArray added in v0.2.0

func (c *Column) IsArray() bool

IsArray returns true if column is an array or a slice

func (*Column) IsAuto added in v0.2.0

func (c *Column) IsAuto() bool

IsAuto returns the auto flag

func (*Column) IsComparable added in v0.2.0

func (c *Column) IsComparable() bool

IsComparable returns the column comparable flag

func (*Column) IsNillable added in v0.2.0

func (c *Column) IsNillable() bool

IsNillable returns true if the column is nillable

func (*Column) IsOptional added in v0.2.0

func (c *Column) IsOptional() bool

IsOptional returns the optional flag

func (*Column) IsValueScanner added in v0.2.0

func (c *Column) IsValueScanner() bool

IsValueScanner returns true if column implements value scanner

func (*Column) Name added in v0.2.0

func (c *Column) Name() string

Name returns the column name

func (*Column) TypeInfo added in v0.2.0

func (c *Column) TypeInfo() *mira.TypeInfo

TypeInfo returns the type info

type ColumnBuilder added in v0.2.0

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

ColumnBuilder is a column

func NewColumnBuilder added in v0.2.0

func NewColumnBuilder(name string, v interface{}) *ColumnBuilder

NewColumnBuilder returns a ColumnBuilder

func (*ColumnBuilder) Auto added in v0.2.0

func (cb *ColumnBuilder) Auto() *ColumnBuilder

Auto sets the column as auto-filled

func (*ColumnBuilder) Build added in v0.2.0

func (cb *ColumnBuilder) Build() *Column

Build builds the column

func (*ColumnBuilder) Comparable added in v0.2.0

func (cb *ColumnBuilder) Comparable() *ColumnBuilder

Comparable sets the column as comparable

func (*ColumnBuilder) Optional added in v0.2.0

func (cb *ColumnBuilder) Optional() *ColumnBuilder

Optional sets the column as optional

func (*ColumnBuilder) StructField added in v0.2.0

func (cb *ColumnBuilder) StructField(structField string) *ColumnBuilder

StructField sets the struct field name override. This is useful when the inferred struct field is different from the actual field e.g. The struct field of the model is "ID" but being referred to as "Id" in the generated code

type ErrRequiredField added in v0.2.0

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

ErrRequiredField is a required field error

func NewErrRequiredField added in v0.2.0

func NewErrRequiredField(field string) *ErrRequiredField

NewErrRequiredField returns an ErrFieldRequired error

func (*ErrRequiredField) Error added in v0.2.0

func (e *ErrRequiredField) Error() string

type Logger

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

Logger is a logger interface

type PostgresTemplate added in v0.2.0

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

PostgresTemplate is the template for generating a postgres repository

func NewPostgresTemplate added in v0.2.0

func NewPostgresTemplate() *PostgresTemplate

NewPostgresTemplate returns a new PostgresTemplate

func (*PostgresTemplate) Filename added in v0.2.0

func (t *PostgresTemplate) Filename() string

Filename returns the filename

func (*PostgresTemplate) Template added in v0.2.0

func (t *PostgresTemplate) Template() string

Content returns the template content

func (*PostgresTemplate) WithFilename added in v0.2.0

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 the standard sql interface runner

type Schema

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

Schema is a schema used for generating the repository

func (*Schema) Collection

func (s *Schema) Collection() string

Collection returns the collection

func (*Schema) Columns

func (s *Schema) Columns() []*Column

Columns returns the columns

func (*Schema) Identity added in v0.2.0

func (s *Schema) Identity() *Column

Identity returns the identity column

func (*Schema) Imports added in v0.2.0

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

Imports returns the pkg imports

func (*Schema) PkgName added in v0.2.0

func (s *Schema) PkgName() string

PkgName returns the pkg name

func (*Schema) Templaters added in v0.2.0

func (s *Schema) Templaters() []Templater

Templaters returns the templaters

func (*Schema) TypeIdentifier added in v0.2.0

func (s *Schema) TypeIdentifier() string

TypeIdentifier returns the type identifier

func (*Schema) TypeIdentifierPlural added in v0.2.0

func (s *Schema) TypeIdentifierPlural() string

TypeIdentifierPlural returns the plural form of type identifier

func (*Schema) TypeInfo added in v0.2.0

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

TypeInfo returns the type info

func (*Schema) TypeName added in v0.2.0

func (s *Schema) TypeName() string

TypeName returns the type name

func (*Schema) TypeNamePlural added in v0.2.0

func (s *Schema) TypeNamePlural() string

type SchemaBuilder added in v0.2.0

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

SchemaBuilder is schema builder

func NewSchemaBuilder added in v0.2.0

func NewSchemaBuilder(v interface{}) *SchemaBuilder

NewSchemaBuilder takes a model struct value and returns a SchemaBuilder

func (*SchemaBuilder) Build added in v0.2.0

func (sb *SchemaBuilder) Build() *Schema

Build builds the schema

func (*SchemaBuilder) Collection added in v0.2.0

func (sb *SchemaBuilder) Collection(collection string) *SchemaBuilder

Collection sets the collection

func (*SchemaBuilder) Columns added in v0.2.0

func (sb *SchemaBuilder) Columns(columns ...*Column) *SchemaBuilder

Columns sets the columns

func (*SchemaBuilder) Identity added in v0.2.0

func (sb *SchemaBuilder) Identity(column *Column) *SchemaBuilder

Identity sets the identity column

func (*SchemaBuilder) PkgName added in v0.2.0

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

PkgName sets the pkg name

func (*SchemaBuilder) Templates added in v0.2.0

func (sb *SchemaBuilder) Templates(templaters ...Templater) *SchemaBuilder

Templates sets the templates

type Templater

type Templater interface {
	// Filename is the filename of the generated file
	Filename() string
	// Template is template for generating the repository implementation
	Template() string
}

Templater is an interface that wraps the Filename and Template 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 the composition of driver.Valuer and sql.Scanner interfaces

Directories

Path Synopsis
Package aggregate contains types for implementing aggregate functions
Package aggregate contains types for implementing aggregate functions
Package comparison contains types for comparison operations
Package comparison contains types for comparison operations
Package example contains example types with schema definitions for demo and testing purposes.
Package example contains example types with schema definitions for demo and testing purposes.
Package gen contains types and functions for code generation
Package gen contains types and functions for code generation
Package sort contains types for implement sort/order functions
Package sort contains types for implement sort/order functions
Package test contains tests
Package test contains tests
gen
Package gen contains code generation tests
Package gen contains code generation tests
integration command
integration/playerrepo
Code generated by nero, DO NOT EDIT.
Code generated by nero, DO NOT EDIT.
x

Jump to

Keyboard shortcuts

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