Skip to content

Latest commit

 

History

History
80 lines (60 loc) · 3.2 KB

README.md

File metadata and controls

80 lines (60 loc) · 3.2 KB

Mantle

Go wrapper for SQL and NoSQL dbs. Separate interface for SQL and NoSQL Datastores. Use Mantle's pooling to monitor all interactions to external strores. Idea is to monitir all external datastore interactions thorugh a common pool and strictly moniter that.

  1. Currently using miniminal vitess pool as a common connection pool.
  2. If this pool is unable to serve some complex fuctionality, datastores' pool would be used extending methods like get(), return_to_pool() and get_active().

Get the package:

    go get github.com/goibibo/mantle

Code:

    package main

    import (
            "fmt"
            "github.com/goibibo/mantle"
            "time"
    )

    func main(){
            //sample data
            keyValue := map[string]interface{}{"key1":"val1", "key2":"val2", "key3":"val3"}
            //extra params to be passed to connection
            options := map[string]string{"db":"1"}
            hostNPort := []string{"localhost:6379"}

            orm := mantle.Orm{Driver: "redis", HostAndPorts: hostNPort}

            //selecting a particular db
            orm := mantle.Orm{Driver: "redis", HostAndPorts: hostNPort, Options: options}

            //default "localhost:6379 is used when hostAndPort is not passed"
            //orm := mantle.Orm{Driver: "redis"}

            //this connects to redis at localhost:6379 by default
            //orm := mantle.Orm{}

            connection := orm.NewNoSQL()

            fmt.Println(connection.Set("key", "value2")) //output: true
            fmt.Println(connection.Get("key"))           //value2
            fmt.Println(connection.Delete("key"))        //1
            fmt.Println(connection.Get("key"))           //""

            fmt.Println(connection.MSet(keyValue))       //true
            fmt.Println(connection.MGet("key3", "key2")) //[val3 val2]

            connection.Expire("key", 1)
            time.Sleep(1 * time.Second)
            fmt.Println(connection.Get("key"))           //""

            /*Execute any redis command*/
            connection.Execute("LPUSH", "test", "a")
            connection.Execute("LPUSH", "test", "b")
            connection.Execute("LPUSH", "test", "c")
            values, _ := connection.Execute("LRANGE", "test", 0, -1)
            fmt.Println(values)                          //[[99] [98] [97]]

            connection.Setex("key", 1, "value")
            fmt.Println(connection.Get("key"))           //value
            time.Sleep(1 * time.Second)
            fmt.Println(connection.Get("key"))           //""

            //For MySQL
            //DB query
            query := "select * from flight_controllerdata"

            connections := []string{"root:@tcp(127.0.0.1:3306)/bm"}
            //Create mantle Driver with settings
            orm := mantle.Orm{Driver: "mysql", HostAndPorts: connections}

            // Create a new connection
            conn := orm.NewMySQL()

            //Get query response and print
            response, _ := conn.Select(query)
            fmt.Println(response)
    }