ncore

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 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

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.

Install

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

Docs

  • Neuron-Core: https://neuronlabs.github.io/neuron-core
  • GoDoc: https://godoc.org/github.com/neuronlabs/neuron-core
  • Addons and Repositories: https://neuronlabs.github.io/

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/config"
    "github.com/neuronlabs/neuron-core"
)

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.AddStringFilter("filter[users][name][$in]","John", "Sam")
    // it might also be sorted
    s.SortBy("-id")
    
    // list all the users with the name 'John' or 'Sam' with 'id' ordered 
    // descending.
    if err = s.List(); err != nil {
        panic(err)
    }

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 is composed of multiple packages where the most important are:

  • 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 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.

Jump to

Keyboard shortcuts

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