neuron

package module
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2020 License: Apache-2.0 Imports: 6 Imported by: 2

README

Neuron Logo

Neuron Core Go Report Card GoDoc Build Status GitHub

neuron is the golang cloud-native, distributed ORM implementation.

What is neuron?

neuron is a cloud-ready Golang ORM. It's design allows querying multiple related models located on different data stores or repositories using a single API.

Design

Package neuron provides golang ORM implementation designed to process microservice repositories. This concept enhanced the requirements for the models mapping and query processor.

The mapped models must have all the field's with defined type. The initial model mapping was based on the JSONAPI v1.0 model definition. The distributed environment required each relationship to be specified of it's kind and type, just as their foreign keys.

The query processor works as the orchestrator and choreographer. It allows to join the queries where each model's use a different database. It also allows distributed transactions transactions.

Install

go get github.com/neuronlabs/neuron

Docs

Quick Start

  • Define the models
package models

// User is the model that is stored on the 'main' repository.
// It is related to multiple 'Pet' models.
type User struct {
    ID      int
    Name    string
    Surname string
    Pets    []*Pet `neuron:"foreign=OwnerID"`
}

// Pet is the model related with the User.
// It is stored in the 'secondary' repository.
type Pet struct {
    ID          int
    Name        string
    OwnerID     int     `neuron:"type=foreign"`
}

// RepositoryName implements RepositoryNamer interface.
func (p *Pet) RepositoryName() string {
    return "secondary"
}
  • Import repositories and Create, Read or get Default *config.Controller
package main

import (
    "context"
    // blank imported repository registers it's factory
    // and the driver.
    _ "github.com/neuronlabs/neuron-postgres"

    "github.com/neuronlabs/neuron"
    "github.com/neuronlabs/neuron/config"
    "github.com/neuronlabs/neuron/log"

)

func main() {
    cfg := config.Default()
    // Provided create config 'cfg' to the Controller method.
    n, err := neuron.New(cfg)
    if err != nil {
        log.Fatal(err)
    } 

    // As the 'neuron' allows to use multiple repository for the models
    // we can declare the DefaultRepository within the config. The first 
    // registered repository would be set as the default as well. 
    mainDB := &config.Repository{
        // Currently registered repository 'neuron-pq' has it's driver name: 'pq'.
        DriverName: "postgres",        
        Host: "localhost",   
        Port: 5432,
        Username: "main_db_user",
        Password: "main_db_password",
        DBName: "main",
    }
    if err = n.RegisterRepository("main", mainDB); err != nil {
        log.Fatal(err)
    }

    // We can register and use different repository for other models.
    secondaryDB := &config.Repository{        
        DriverName: "postgres",        
        Host: "172.16.1.10",
        Port: 5432,
        Username: "secondary_user",
        Password: "secondary_password",
        DBName: "secondary",
    }

    // Register secondary repository.
    if err = n.RegisterRepository("secondary", secondaryDB); err != nil {
        log.Fatal(err)
    }

    if err = n.DialAll(context.TODO()); err != nil {
        log.Fatal(err)    
    }
    
    // Register models 
    if err = n.RegisterModels(models.User{}, models.Pet{}); err != nil {
        log.Fatal(err)
    }

    // If necessary migrate the models
    if err = n.MigrateModels(models.User{}, models.Pet{}); err != nil {
        log.Fatal(err)
    }

    // Start application and query models
    users := []*models.User{}
    err = n.Query(&users).        
        .Filter("Name IN","John", "Sam"). // Filter the results by the 'Name' which might be 'John' or 'Sam'.        
        .Sort("-ID"). // Sort the results by user's ids.
        .Include("Pets"). // Include the 'Pets' field for each user in the 'users'.
        .List() // List all the users with the name 'John' or 'Sam' with 'id' ordered in decrease manner.
    if  err != nil {
        log.Fatal(err)
    }
    log.Infof("Queried users: %v", users)
}

Packages

The neuron is composed of the following packages:

  • query - used to query the model's repositories.
  • controller - is the neuron's core, that registers and stores the models and contains configurations required by other packages.
  • mapping - contains the information about the mapped models their fields and settings
  • config - contains the configurations structures.
  • class - contains github.com/neuronlabs/errors class instances used by the neuron package.
  • repository - is a package used to store, get and register the repositories nad their factories.
  • log - is the logging interface for the neuron based applications.

Documentation

Overview

Package neuron is the cloud-native, distributed ORM implementation.

It's design allows to use the separate repository for each model, with a possibility to have different relationships types between them.

neuron consists of following packages:

  • config - contains the configurations for all packages.

  • controller - is the neuron's core, that registers and stores the models and contains configurations required by other packages.

  • errors - classified errors.

  • query - used to create queries, filters, sort, pagination on base of mapped models.

  • mapping - contains the information about the mapped models their fields and settings.

  • log - is the neuron service logging interface structure for the neuron based applications.

  • repository - is a package used to store and register the repositories.

    It is also used to get the repository/factory per model. A modular design allows to use and compile only required repositories.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Begin

func Begin() *query.Tx

Begin starts new transaction with the default isolation.

func BeginCtx

func BeginCtx(ctx context.Context, opts *query.TxOptions) *query.Tx

BeginCtx starts new transaction. Provided context 'ctx' is used until the transaction is committed or rolled back.

func RunInTransaction

func RunInTransaction(ctx context.Context, orm query.DB, txFn TxFn) (err error)

RunInTransaction creates a new transaction and handles rollback / commit based on the error return by the txFn.

Types

type Neuron

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

Neuron is the main structure that stores the controller, create queries, registers models and repositories.

func New

func New(cfg *config.Controller) (*Neuron, error)

New creates new Neuron controller.

func NewC

func NewC(c *controller.Controller) *Neuron

NewC creates new Neuron service with the provided 'c' controller.

func (*Neuron) CloseAll

func (n *Neuron) CloseAll(ctx context.Context) error

CloseAll gently closes repositories connections.

func (*Neuron) Controller

func (n *Neuron) Controller() *controller.Controller

Controller gets neuron controller.

func (*Neuron) DialAll

func (n *Neuron) DialAll(ctx context.Context) error

DialAll establish connections for all neuron repositories.

func (*Neuron) HealthCheck

func (n *Neuron) HealthCheck(ctx context.Context) (*repository.HealthResponse, error)

HealthCheck checks all repositories health for the default controller.

func (*Neuron) MigrateModels

func (n *Neuron) MigrateModels(ctx context.Context, models ...interface{}) error

MigrateModels updates or creates provided models representation in their related repositories. A representation of model might be a database table, collection etc. Model's repository must implement repository.Migrator.

func (*Neuron) RegisterModels

func (n *Neuron) RegisterModels(models ...interface{}) error

RegisterModels registers all models into default controller. This function requires repositories to be registered before. Returns error if the model was already registered.

func (*Neuron) RegisterRepository

func (n *Neuron) RegisterRepository(name string, repo *config.Repository) error

RegisterRepository registers repository into default controller. Returns error if the repository with given name was already registered. By default the first registered repository is set to the default repository for all models that doesn't define their repositories, unless default controller's config DisallowDefaultRepository is set to false.

type TxFn

type TxFn func(orm query.DB) error

TxFn is the function used to run WithTransaction.

Directories

Path Synopsis
Package annotation contains constants used as mapping annotations.
Package annotation contains constants used as mapping annotations.
Package config contains configuration structures used by all neuron packages.
Package config contains configuration structures used by all neuron packages.
Package controller contains root neuron structure.
Package controller contains root neuron structure.
errors module
Package log contains default neuron logger interface with it's subcomponents.
Package log contains default neuron logger interface with it's subcomponents.
Package mapping contains neuron models mapped structures.
Package mapping contains neuron models mapped structures.
Package query is the package used that defines the neuron query it's structure, processor, transactions.
Package query is the package used that defines the neuron query it's structure, processor, transactions.
Package repository is the package that defines neuron repositories and it's factories.
Package repository is the package that defines neuron repositories and it's factories.

Jump to

Keyboard shortcuts

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