dbulker

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2021 License: MIT Imports: 14 Imported by: 0

README

DBulker GoDoc Go Report Card

Dbulker is a data transporter between MongoDB to Mysql (for now)

Getting Package

go get github.com/kratiwitz/dbulker

Prerequisites

  • Go
  • Mysql
  • MongoDB

Simple Usage

Create struct for your MongoDB object. Id field has a lot of tags. bson tag for MongoDB, bulker for matches field in dbulker, bulker_rdb for MySQL column.

type Movie struct { 
	Id            string     `bson:"_id" bulker:"_id" bulker_rdb:"id"`
	Name          string     `bulker:"name"`
	Author        string     `bulker:"author"`
	Description   string     `bulker:"description"`
}

Create MongoDB client

mongodb, err := dbulker.NewMongoDBClient("mongodb://localhost:27017", "movies")

Create Mysql client

mysqldb, err := dbulker.NewMysqlClient("root:root@tcp(127.0.0.1:3306)", "movie")

Get Movies from Mongo

data, err := mongodb.GetAll("movies", Movie{})

And write first data to Mysql

mysqldb.FillAutoPlainSingle("movie", data[0])

Or write all data

mysqldb.FillAutoPlainMultiple("movie", data)

Relational

Let's assume that your data in MongoDB is as follows and you want to pass this data to Mysql in a related way.

{
    "name": "test",
    "author": "test test",
    "categories": ["test", "test"],
    "sectionList": [
        "https://test.com/test/section-1",
        "https://test.com/test/section-2"
    ],
    "image": "test/test.png",
    "description": "test",
}

You must separate sections List and categories into different tables. You can use that.

type Movie struct {
	Name          string     `bulker:"name"`
	Author        string     `bulker:"author"`
	Description   string     `bulker:"description"`
	Categories    []Category `bulker:"categories" relation_name:"movie_category" table:"category"`
	SectionList   []Section  `bulker:"sectionList" relation_name:"movie_section" table:"section"`
}

type Category struct {
	Id   string `bulker:"id" bulker_type:"primary" bulker_column:"INT NOT NULL AUTO_INCREMENT"`
	Name string `bulker:"name" bulker_type:"main" bulker_column:"TEXT NULL"`
}

type Section struct {
	Id  string `bulker:"id" bulker_type:"primary" bulker_column:"INT NOT NULL AUTO_INCREMENT"`
	Url string `bulker:"url" bulker_type:"main" bulker_column:"TEXT NULL"`
}

func main() {
	mongodb, err := NewMongoDBClient("mongodb://localhost:27017", "movie_db")
	CheckError(err)

	mysqldb, err := NewMysqlClient("root:root@tcp(127.0.0.1:3306)", "movie_db")
	CheckError(err)

	data, err := mongodb.GetAll("movies", Movie{})
	CheckError(err)

	err = mysqldb.FillAutoNestedSingle("movie", data[0])
	CheckError(err)
}

func CheckError(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

image

Your database result will look like this;

Movie Table

id name author description
0 test test test

Category Table

id name
0 test
1 test

Section Table

id url
0 test
1 test

Junction Table movie_category

movie_id category_id
1 0
1 1

Junction Table movie_section

movie_id section_id
1 0
1 1

Tags

bulker

bulker tag is a must for dbulker understand what is name of your field on MongoDB

Usage

Name string `bulker:"name"`
bulker_rbd

bulker_rbd represents your field in your Table of Mysql

Usage

SecondaryName string `bulker_rdb:"secondary_name"`
relation_name

relation_name declare a junction table name for your relation of data

Usage

Categories []Category `relation_name:"movie_category"`
table

table declare a different table name for your nested data

Usage

Categories []Category `table:"category"`
bulker_type

bulker_type declare selected column is PRIMARY or main. If select PRIMARY the column will id in Mysql, if select main column is main field of data.

Usage

Name string `bulker:"name" bulker_type:"main"`
bulker_column

bulker_column directly set of your Mysql field.

Usage

Id  string `bulker:"id" bulker_column:"INT NOT NULL AUTO_INCREMENT"`
bulker_unique

bulker_unique reduce for repeated values.

Usage

Name string `bulker:"name" bulker_type:"main" bulker_unique:"true" bulker_column:"TEXT NULL"`

License

The MIT License (MIT). See License File for more information.

Documentation

Index

Constants

View Source
const (
	TagRelationName       = "relation_name"
	TagBulker             = "bulker"
	TagUnique             = "bulker_unique"
	TagBulkerType         = "bulker_type"
	TagBulkerRDB          = "bulker_rdb"
	TagBulkerColumn       = "bulker_column"
	TagTable              = "table"
	CommaToken            = ","
	RightParanthesisToken = ")"
	LeftParanthesisToken  = "("
	ValuesToken           = "VALUES"
)
View Source
const (
	InsertTemplate      = "INSERT INTO %s (%s) VALUES %s"
	CreateTableTemplate = "CREATE TABLE IF NOT EXISTS %s (%s)"
	PrimaryKeyTemplate  = "PRIMARY KEY (`%s`)"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database interface {
	Connect() error
	FindAll(string, interface{}) ([]interface{}, error)
	FillAutoPlainMultiple(string, []interface{}) error
	FillAutoPlainSingle(string, interface{}) (int64, error)
}

type MongoDB

type MongoDB struct {
	ConnectionString string
	Database         string
	// contains filtered or unexported fields
}

func NewMongoDBClient

func NewMongoDBClient(connectionString, database string) (*MongoDB, error)

NewMongoDBClient create MongoDB client, connect to db and return MongoDB

func (*MongoDB) Connect

func (mongodb *MongoDB) Connect() error

Connect open connection to mongo

func (*MongoDB) GetAll

func (mongodb *MongoDB) GetAll(collection string, custom interface{}) ([]interface{}, error)

GetAll returns given collection data

type MysqlDB

type MysqlDB struct {
	ConnectionString string
	Database         string
	// contains filtered or unexported fields
}

func NewMysqlClient

func NewMysqlClient(connectionString, database string) (*MysqlDB, error)

NewMysqlClient create MysqlDB client, connect to db and return MysqlDB

func (*MysqlDB) Connect

func (mysqldb *MysqlDB) Connect() error

Connect open connection to mysql

func (*MysqlDB) FillAutoNestedSingle

func (mysqldb *MysqlDB) FillAutoNestedSingle(targetTable string, data interface{}) error

FillAutoNestedSingle, writes given data to the given table create table for nested objects if any and returns the last inserted id and error if any. Create automatically relations.

func (*MysqlDB) FillAutoPlainMultiple

func (mysqldb *MysqlDB) FillAutoPlainMultiple(targetTable string, data []interface{}) error

FillAutoPlainMultiple writes multiple given data to given table and returns an error if any Ignore any nested child.

func (*MysqlDB) FillAutoPlainSingle

func (mysqldb *MysqlDB) FillAutoPlainSingle(targetTable string, data interface{}) (int64, error)

FillAutoPlainSingle, writes given data to the given table and returns the last inserted id and error if any. Ignore any nested child.

Jump to

Keyboard shortcuts

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