postgres

package module
v0.0.0-...-79003c6 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2020 License: Apache-2.0, Apache-2.0 Imports: 18 Imported by: 0

README

Neuron Logo

Neuron Postgres Go Report Card GoDoc Build Status License

neuron-postgres is the github.com/neuronlabs/neuron postgres repository driver.

What is Neuron-Postgres

Neuron-PQ is the PostgreSQL ORM Repository driver for the github.com/neuronlabs/neuron.

Installation

go get github.com/neuronlabs/neuron-postgres

Neuron-postgres is the extension for the neuron requires github.com/neuronlabs/neuron root package.

QuickStart

  1. Prepare or read the Config Repository - github.com/neuronlabs/neuron/config by creating structure or by using viper github.com/spf13/viper and create the neuron controller The DriverName variable (driver_name in config) should be set to postgres in order to match the neuron-postgres Repository and Factory

models.go

package main

type User struct {
    ID      int   
    Name    string
    Surname string
    Cars    []*Car `neuron:"foreign=OwnerID"`
}

type Car struct {
    ID              int    
    RegisterNumber  string  `db:"unique"`
    Owner           *User   `neuron:"type=relation;foreign=OwnerID"`
    OwnerID         int     `db:"notnull"`
}

main.go

package main

import (
    "context"

    "github.com/neuronlabs/neuron"
    "github.com/neuronlabs/neuron/config"
    "github.com/neuronlabs/neuron/log"
    // import postgres repository driver
    _ "github.com/neuronlabs/neuron-plugins/repository/postgres"
)


func main(){
    cfg := &config.Repository{
        DriverName: "postgres",
        Host: "localhost",
        Port: 5432,
        DBName:"dbname",
        Password: "my-password",
        Username: "username",
    }
    // Register the neuron-postgres repository for you application by initializing the repository package
    // Add more custom repository configurations for different models if you 
    // need different connection or credentials
    err := neuron.RegisterRepository("mypostgres", cfg)
    if err != nil { 
        log.Fatal(err)
    }    
    
    if err = neuron.DialAll(context.Background()); err != nil {
        log.Fatal(err)   
    }
    
    // Register the models 
    if err = neuron.RegisterModels(&User{}, &Car{}); err != nil {
        log.Fatal(err)
    }
    
    // We can create and update current tables for given model by auto migrating it.
    if err = neuron.MigrateModels(context.Background(), &User{}); err != nil {
        log.Fatal(err)    
    }

    // Use the models within the Gateway API or just as an ORM models.
    user := &User{
        Name: "Mathew",
        Surname: "Smith",
    }

    err = neuron.Query(user).Create()
    if err != nil {
        log.Fatal(err)
    }
    log.Infof("Inserted user: %v successfully", user)
}

Examples

config.yml

{
    "repositories": {
        "postgres": {
            "driver_name": "postgres",
            "host": "localhost",
            "port": "5432",
            "dbname": "testing",
            "username": "testing",
            "password": "testing"
        }
    },
    "default_repository_name": "postgres"
}

Docs

Documentation

Overview

Package postgres implements the github.com/neuronlabs/neuron/repository Factory and Repository interfaces. It allows to query PostgreSQL based databases using github.com/jackc/pgx driver. The repository implements:

  • query.FullRepository
  • repository.Repository
  • repository.Migrator

The repository allows to share single transaction per multiple models - if all are registered within single database.

Index

Constants

View Source
const FactoryName = "postgres"

FactoryName defines the name of the factory.

Variables

This section is empty.

Functions

This section is empty.

Types

type Factory

type Factory struct{}

Factory is the pq.Postgres factory.

func (*Factory) DriverName

func (f *Factory) DriverName() string

DriverName gets the Factory repository name. Implements repository.Postgres interface.

func (*Factory) New

func (f *Factory) New(cfg *config.Service) (service.Service, error)

New creates new PQ repository for the provided config 'cfg'.

type Postgres

type Postgres struct {
	ConnPool           *pgxpool.Pool
	Config             *pgxpool.Config
	AutoSelectNotNulls bool
	// PostgresVersion is the numerical version of the postgres server.
	PostgresVersion int

	Transactions map[uuid.UUID]pgx.Tx
	// contains filtered or unexported fields
}

Postgres is the neuron repository that allows to query postgres databases. It allows to query PostgreSQL based databases using github.com/jackc/pgx driver. The repository implements:

  • query.FullRepository
  • repository.Repository
  • repository.Migrator

The repository allows to share single transaction per multiple models - if all are registered within single database.

func New

func New() *Postgres

New creates new postgres instance.

func (*Postgres) Begin

func (p *Postgres) Begin(ctx context.Context, tx *query.Transaction) error

Begin starts a transaction for the given scope. Implements Begin method of the query.Transactioner interface.

func (*Postgres) Close

func (p *Postgres) Close(ctx context.Context) (err error)

Close closes given repository connections.

func (*Postgres) Commit

func (p *Postgres) Commit(ctx context.Context, tx *query.Transaction) error

Commit commits the scope's transaction Implements Commit method from the query.Commiter interface

func (*Postgres) Count

func (p *Postgres) Count(ctx context.Context, s *query.Scope) (int64, error)

Count implements query.Counter interface.

func (*Postgres) Delete

func (p *Postgres) Delete(ctx context.Context, s *query.Scope) (int64, error)

Delete deletes all the values that matches scope's filters. Implements repository.Repository interface.

func (*Postgres) Dial

func (p *Postgres) Dial(ctx context.Context) (err error)

Dial implements repository.Postgres interface. Creates a new Connection Pool for given repository.

func (*Postgres) FactoryName

func (p *Postgres) FactoryName() string

FactoryName returns the name of the factory for this Postgres. Implements repository.Repository interface.

func (*Postgres) Find

func (p *Postgres) Find(ctx context.Context, s *query.Scope) error

Find lists all the values that matches scope's filters, sorts and pagination. Implements repository.Repository interface.

func (*Postgres) HealthCheck

func (p *Postgres) HealthCheck(ctx context.Context) (*service.HealthResponse, error)

HealthCheck implements repository.Repository interface. It creates basic queries that checks if the connection is alive and returns given health response. The health response contains also notes with postgres version.

func (*Postgres) ID

func (p *Postgres) ID() string

ID returns unique repository id.

func (*Postgres) Insert

func (p *Postgres) Insert(ctx context.Context, s *query.Scope) error

Insert depending on the query efficiently inserts models with related fieldSets. Implements repository.Repository interface.

func (*Postgres) MigrateModels

func (p *Postgres) MigrateModels(ctx context.Context, models ...*mapping.ModelStruct) error

MigrateModels implements repository.Migrator interface. The method creates models tables if not exists and updates the columns per given model fields.

func (*Postgres) RegisterModels

func (p *Postgres) RegisterModels(models ...*mapping.ModelStruct) error

RegisterModels implements repository.Repository interface.

func (*Postgres) Rollback

func (p *Postgres) Rollback(ctx context.Context, tx *query.Transaction) error

Rollback rolls back the transaction for given scope

func (*Postgres) Update

func (p *Postgres) Update(ctx context.Context, s *query.Scope) (int64, error)

Update patches all the values that matches scope's filters, sorts and pagination Implements repository.Repository interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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