ncore

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2019 License: Apache-2.0 Imports: 3 Imported by: 0

README

Neuron Core

Neuron-core is the golang cloud-native, distributed ORM implementation.

Go Report Card GoDoc Build Status

What is Neuron-Core?

Neuron-core is a cloud-ready Golang ORM. It's design allows to query multiple related models located on different datastores/repositories.

Design

The neuron-core is 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 needs to work as the orchestrator, choreographer. As each model's repository might use different database, it needs to implement distributed transactions.

Install

go get -u github.com/neuronlabs/neuron-core

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:"type=relation;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(
    // blank imported repository registers it's factory
    // and the driver.
    _ "github.com/neuronlabs/neuron-pq"
)

import (
    "github.com/neuronlabs/neuron-core"
    "github.com/neuronlabs/neuron-core/config"
)

func main() {
    cfg := config.ReadDefaultConfig()
    // By setting the LogLevel the default logger would be used.
    cfg.LogLevel = "debug"    
...    
  • Create the *controller.Controller and register repositories.
    // Provided create config 'cfg' to the Controller method.
    c := ncore.Controller(cfg)

    // As the 'neuron-core' 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: "pq",        
        Host: "localhost",   
        Port: "5432",
        Username: "main_db_user",
        Password: "main_db_password",
        DBName: "main",
    }
    if err := c.RegisterRepository("main", mainDB); err != nil {
        panic(err)
    }

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

    // Register secondary repository.
    if err := c.RegisterRepository("secondary", secondaryDB); err != nil {
        panic(err)
    }
  • Register models
    if err := c.RegisterModels(models.User{}, models.Pet{}); err != nil {
        panic(err)
    }
  • Query registered models
    users := []*User{}
    
    s := ncore.MustQueryC(c, &users)
    // the query scope may be filtered
    s.Filter("filter[users][name][$in]","John", "Sam")
    // it might also be sorted
    s.Sort("-id")
    
    // list all the users with the name 'John' or 'Sam' with 'id' ordered 
    // descending.
    if err = s.List(); err != nil {
        // resolve the error
    }

Packages

The neuron-core 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.
  • encoding/jsonapi - allows to marshal and unmarshal the model's, and queries by the https://jsonapi.org/ specification
  • errors - used as a default error package for the neuron packages.
  • errors/class - contains errors classification system for the neuron packages
  • 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.
  • i18n - is the neuron based application supported internationalization
  • common - common neuron variables, functions and definitions.

Documentation

Overview

Package ncore is the cloud-native, distributed ORM implementation. It's design allows to use the separate repository for each model, with a possiblity to have different relationships types between them. It consits the following packages: - ncore - (Neuron Core) the root package that gives easy access to all subpackages. - common - contains common variables and constants for neuron derivates. - controller - is the neuron's core, that registers and stores the models and contains configurations required by other packages.

  • config - contains the configurations for all packages.
  • query - used to query the model's repositories.
  • mapping - contains the information about the mapped models their fields and settings.
  • errors - used as a default error package for the neuron packages.
  • errors/class - contains errors classification system for the neuron packages.
  • encoding/jsonapi - allows to marshal and unmarshal the model's, and queries by the 'https://jsonapi.org/' specification
  • log - is the logging interface for the neuron based applications.
  • i18n - is the neuron based application supported internationalization.
  • 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 Controller

func Controller(cfg *config.Controller) (*controller.Controller, error)

Controller creates new controller for provided 'cfg' config.

func MustQuery added in v0.2.3

func MustQuery(model interface{}) *query.Scope

MustQuery creates the new query scope for the provided 'model' for the default controller. Panics on error.

func MustQueryC added in v0.2.3

func MustQueryC(c *controller.Controller, model interface{}) *query.Scope

MustQueryC creates the new query scope for the 'model' and 'c' controller. Panics on error.

func Query

func Query(model interface{}) (*query.Scope, error)

Query creates new query scope for the provided 'model' using the default controller.

func QueryC

func QueryC(c *controller.Controller, model interface{}) (*query.Scope, error)

QueryC creates new query scope for the provided 'model' and controller 'c'.

Types

This section is empty.

Directories

Path Synopsis
Package common contains common neuron variables, constants and functions.
Package common contains common neuron variables, constants and functions.
Package config contains configuration structures used by all neuron-core packages.
Package config contains configuration structures used by all neuron-core packages.
Package controller contains root neuron structure.
Package controller contains root neuron structure.
encoding
jsonapi
Package jsonapi implements encoding and decoding of JSON with JSONAPI v1.0 format.
Package jsonapi implements encoding and decoding of JSON with JSONAPI v1.0 format.
Package errors contains neuron error structure definitions.
Package errors contains neuron error structure definitions.
class
Package class contains neuron error classification system.
Package class contains neuron error classification system.
Package i18n defines model internationalization support.
Package i18n defines model internationalization support.
Package internal contains neuron internal variables and interfaces, used by multiple packages.
Package internal contains neuron internal variables and interfaces, used by multiple packages.
controller
Package controller defines root neuron structure - controller.
Package controller defines root neuron structure - controller.
models
Package models encapsulates models structure as well as their mapping process.
Package models encapsulates models structure as well as their mapping process.
query/filters
Package filters contains query filters and operator structure definitions.
Package filters contains query filters and operator structure definitions.
query/paginations
Package paginations contains the query pagianation structure definitions.
Package paginations contains the query pagianation structure definitions.
query/scope
Package scope defines and encapsulates query scope structure with the methods that should be used internally within the neuron-core module.
Package scope defines and encapsulates query scope structure with the methods that should be used internally within the neuron-core module.
query/sorts
Package sorts contains query sort order structures.
Package sorts contains query sort order structures.
safemap
Package safemap defines concurrently safe hash map.
Package safemap defines concurrently safe hash map.
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 namer defines the naming convention functions used to define registered model collection name and it's fields.
Package namer defines the naming convention functions used to define registered model collection name and it's fields.
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.
filters
Package filters defines the filter field structure, it's methods and create functions.
Package filters defines the filter field structure, it's methods and create functions.
mocks
Package mocks contains mocked neuron factory and repository.
Package mocks contains mocked neuron factory and repository.
tests
Package tests contains query unit tests.
Package tests contains query unit tests.
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.
mocks
Package mocks contains mocked repository and factory.
Package mocks contains mocked repository and factory.

Jump to

Keyboard shortcuts

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