immudb

package module
v0.0.0-...-5d136ef Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

README

IMMUGORM

This repository is a driver for immudb. to act as a database for GORM.

Quick Start

Clone immudb repository, compile immudb and launch it:

git clone https://github.com/codenotary/immudb
make immudb
./immudb

Below see an example on how to get GORM to work with immudb:

package main

import (
    immugorm "github.com/codenotary/immugorm"
    "gorm.io/gorm"
    "gorm.io/gorm/logger"
)

type Product struct {
    ID     int `gorm:"primarykey"`
    Code   string
    Price  uint
    Amount uint
}

func main() {
    db, err := gorm.Open(immugorm.Open("immudb://immudb:immudb@127.0.0.1:3322/defaultdb?sslmode=disable", &immugorm.ImmuGormConfig{Verify: false}), &gorm.Config{
        Logger: logger.Default.LogMode(logger.Info),
    })
    if err != nil {
        panic(err)
    }

    // Migrate the schema
    err = db.AutoMigrate(&Product{})
    if err != nil {
        panic(err)
    }
    // Create
    err = db.Create(&Product{Code: "D43", Price: 100, Amount: 500}).Error
    if err != nil {
        panic(err)
    }
    // Read
    var product Product
    // find just created one
    err = db.First(&product).Error
    if err != nil {
        panic(err)
    }
    // find product with code D42
    err = db.First(&product, "code = ?", "D43").Error
    if err != nil {
        panic(err)
    }
    // Update - update product's price to 200
    err = db.Model(&product).Update("Price", 888).Error
    if err != nil {
        panic(err)
    }

    // Update - update multiple fields
    err = db.Model(&product).Updates(Product{Price: 200, Code: "F42"}).Error
    if err != nil {
        panic(err)
    }

    err = db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"}).Error
    if err != nil {
        panic(err)
    }

    // Delete - delete product
    err = db.Delete(&product, product.ID).Error
    if err != nil {
        panic(err)
    }
}
Open with Immudb Options

It's possible open a connection with immudb options to provide more control over the connection or enable some features that are not available by standard DSN.

import (
    "github.com/codenotary/immudb/pkg/client"
    immugorm "github.com/codenotary/immugorm"
    "gorm.io/gorm"
    "gorm.io/gorm/logger"
)

...

opts := client.DefaultOptions()

opts.Username = "immudb"
opts.Password = "immudb"
opts.Database = "defaultdb"
opts.HealthCheckRetries = 10

db, err := gorm.Open(immugorm.OpenWithOptions(opts, &immugorm.ImmuGormConfig{Verify: false}), &gorm.Config{
    Logger: logger.Default.LogMode(logger.Info),
})

IMMUDB SPECIAL FEATURES

TamperProof read

Immugorm is able to take benefits from immudb tamperproof capabilities. It's possible to activate tamperproof read by setting Verify: true when opening db. The only difference is that verifications returns proofs needed to mathematically verify that the data was not tampered.

Note that generating that proof has a slight performance impact.

    db, err := gorm.Open(immugorm.Open(opts, &immugorm.ImmuGormConfig{Verify: true}), &gorm.Config{})
Timetravel

Time travel allows reading data from SQL as if it was in some previous state.

The state is indicated by transaction id, that is a monotonically increasing number that is assigned to each transaction by immudb Each operation is assigned a transaction id.

db.Clauses(immugorm.BeforeTx(9)).Last(&entity, 1)

Warnings

This is an experimental software. The API is not stable yet and may change without notice. There are limitations:

  • missing support related to altering or deleting already existent elements on schema. No drop table, index, alter table or column
  • missing float type
  • missing left join
  • no support for composite primary key
  • no support for polymorphism
  • no support for foreign constraints
  • is mandatory to have a primary key on tables
  • no default values
  • order is limit to one indexed column
  • group by not supported
  • no support for prepared statements
  • not condition missing
  • no transaction with savepoint
  • no nested transactions
  • having nor yet supported

Documentation

Index

Constants

View Source
const DriverName = "immudb"

Variables

View Source
var (
	ErrConstraintsNotImplemented = errors.New("constraints not implemented")
	ErrNotImplemented            = errors.New("not implemented")
	ErrCorruptedData             = errors.New("corrupted data")
	ErrTimeTravelNotAvailable    = errors.New("time travel is not available if verify flag is provided. This will change soon")
)

Functions

func Open

func Open(dsn string, cfg *ImmuGormConfig) gorm.Dialector

func OpenWithOptions

func OpenWithOptions(opts *client.Options, cfg *ImmuGormConfig) gorm.Dialector

Types

type Column

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

func (Column) DatabaseTypeName

func (c Column) DatabaseTypeName() string

func (Column) DecimalSize

func (c Column) DecimalSize() (precision int64, scale int64, ok bool)

func (Column) Length

func (c Column) Length() (length int64, ok bool)

func (Column) Name

func (c Column) Name() string

func (Column) Nullable

func (c Column) Nullable() (nullable bool, ok bool)

type Dialector

type Dialector struct {
	DriverName string

	Conn gorm.ConnPool
	DSN  string
	// contains filtered or unexported fields
}

func (*Dialector) BindVarTo

func (dialector *Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{})

func (*Dialector) ClauseBuilders

func (dialector *Dialector) ClauseBuilders() map[string]clause.ClauseBuilder

func (*Dialector) DataTypeOf

func (dialector *Dialector) DataTypeOf(field *schema.Field) string

func (*Dialector) DefaultValueOf

func (dialector *Dialector) DefaultValueOf(field *schema.Field) clause.Expression

func (*Dialector) Explain

func (dialector *Dialector) Explain(sql string, vars ...interface{}) string

func (*Dialector) Initialize

func (dialector *Dialector) Initialize(db *gorm.DB) (err error)

func (*Dialector) Migrator

func (dialector *Dialector) Migrator(db *gorm.DB) gorm.Migrator

func (*Dialector) Name

func (dialector *Dialector) Name() string

func (*Dialector) QuoteTo

func (dialector *Dialector) QuoteTo(writer clause.Writer, str string)

func (*Dialector) RollbackTo

func (dialector *Dialector) RollbackTo(tx *gorm.DB, name string) error

func (*Dialector) SavePoint

func (dialector *Dialector) SavePoint(tx *gorm.DB, name string) error

type Exprs

type Exprs []clause.Expression

func (Exprs) Build

func (exprs Exprs) Build(builder clause.Builder)

type ImmuGormConfig

type ImmuGormConfig struct {
	Verify bool
}

type Migrator

type Migrator struct {
	migrator.Migrator
}

func (Migrator) AlterColumn

func (m Migrator) AlterColumn(value interface{}, name string) error

func (Migrator) ColumnTypes

func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error)

func (Migrator) CreateConstraint

func (m Migrator) CreateConstraint(interface{}, string) error

func (Migrator) CreateIndex

func (m Migrator) CreateIndex(value interface{}, name string) error

func (Migrator) CreateTable

func (m Migrator) CreateTable(values ...interface{}) error

func (Migrator) CurrentDatabase

func (m Migrator) CurrentDatabase() (name string)

func (Migrator) DropColumn

func (m Migrator) DropColumn(value interface{}, name string) error

func (Migrator) DropConstraint

func (m Migrator) DropConstraint(interface{}, string) error

func (Migrator) DropIndex

func (m Migrator) DropIndex(value interface{}, name string) error

func (Migrator) DropTable

func (m Migrator) DropTable(values ...interface{}) error

func (Migrator) HasColumn

func (m Migrator) HasColumn(value interface{}, name string) bool

func (Migrator) HasConstraint

func (m Migrator) HasConstraint(value interface{}, name string) bool

func (Migrator) HasIndex

func (m Migrator) HasIndex(value interface{}, name string) bool

func (Migrator) HasTable

func (m Migrator) HasTable(value interface{}) bool

func (Migrator) RenameIndex

func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error

func (*Migrator) RunWithoutForeignKey

func (m *Migrator) RunWithoutForeignKey(fc func() error) error

type TimeTravel

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

func BeforeTx

func BeforeTx(tx uint64) TimeTravel

func (TimeTravel) Build

func (tt TimeTravel) Build(builder clause.Builder)

func (TimeTravel) ModifyStatement

func (tt TimeTravel) ModifyStatement(stmt *gorm.Statement)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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