db

package
v0.1.25 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2022 License: MIT Imports: 4 Imported by: 0

README

Ssi Core / Db

It is a package developed to establish database connections in microservice applications. Currently, only functions are written for mongodb, but you can also write for sql if you wish. It does not have any mandatory dependencies.

Mongo

Here are some functions related to mongodb. This package uses the mongo-driver package. Click to access the driver.

CalcMongoUri

This function is used to calculate the mongodb connection uri. It takes the host, port, username, password, database and options parameters. It returns the uri string.

Password and username not required

type:

type UriParams struct {
	Host string
	Port string
	User string
	Pass string
	Db   string
}

example:

package main

import(
    "github.com/ssibrahimbas/ssi-core/pkg/db"
    "fmt"
)

func main() {
    uri := db.CalcMongoUri(db.UriParams{
        Host: "localhost",
        Port: "27017",
        User: "", // not required
        Pass: "", // not required
        Db:   "test-app",
	})
	fmt.Println(uri) // mongodb://localhost:27017/test-app
}
Connect

Connects to the database with the given parameters. Returns Mongodb struct.

type:

type MongoDB struct {
	c   *mongo.Client
	db  *mongo.Database
	ctx context.Context
}

functions:

func (m *MongoDB) GetCollection(n string) *mongo.Collection
func (m *MongoDB) TransformId(id string) primitive.ObjectID

example:

package main

import(
    "github.com/ssibrahimbas/ssi-core/pkg/db"
    "fmt"
)

func main() {
    uri := db.CalcMongoUri(db.UriParams{
        Host: "localhost",
        Port: "27017",
        User: "", // not required
        Pass: "", // not required
        Db:   "test-app",
	})
	d, err := db.NewMongo(uri, a.Cnf.Db.Name)
    if err != nil {
        panic(err)
    }
    fmt.Println("Connected to database")
}

GetCollection

Returns the collection with the given name.

example:

package main

import(
    "github.com/ssibrahimbas/ssi-core/pkg/db"
    "fmt"
)

func main() {
    uri := db.CalcMongoUri(db.UriParams{
        Host: "localhost",
        Port: "27017",
        User: "", // not required
        Pass: "", // not required
        Db:   "test-app",
	})
	d, err := db.NewMongo(uri, a.Cnf.Db.Name)
    if err != nil {
        panic(err)
    }
    c := d.GetCollection("users") // returns mongo.*Collection
    fmt.Println(c.Name())
}

TransformId

Transforms the given string to primitive.ObjectID.

example:

package main

import(
    "github.com/ssibrahimbas/ssi-core/pkg/db"
    "fmt"
)

func main() {
    uri := db.CalcMongoUri(db.UriParams{
        Host: "localhost",
        Port: "27017",
        User: "", // not required
        Pass: "", // not required
        Db:   "test-app",
    })
    d, err := db.NewMongo(uri, a.Cnf.Db.Name)
    if err != nil {
        panic(err)
    }
    c := d.GetCollection("users")
    id := d.TransformId("5f9f1b9b9b9b9b9b9b9b9b9b")
    fmt.Println(id) // 5f9f1b9b9b9b9b9b9b9b9b9b
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalcMongoUri

func CalcMongoUri(params UriParams) string

Types

type MongoDB

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

func NewMongo

func NewMongo(uri string, dbName string) (*MongoDB, error)

func (*MongoDB) GetCollection

func (m *MongoDB) GetCollection(n string) *mongo.Collection

func (*MongoDB) TransformId

func (m *MongoDB) TransformId(id string) primitive.ObjectID

type UriParams

type UriParams struct {
	Host string
	Port string
	User string
	Pass string
	Db   string
}

Jump to

Keyboard shortcuts

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