Dbulker is a data transporter between MongoDB to Mysql (for now)
go get github.com/kratiwitz/dbulker
- Go
- Mysql
- MongoDB
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)
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)
}
}
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 |
bulker
tag is a must for dbulker understand what is name of your field on MongoDB
Usage
Name string `bulker:"name"`
bulker_rbd
represents your field in your Table of Mysql
Usage
SecondaryName string `bulker_rdb:"secondary_name"`
relation_name
declare a junction table name for your relation of data
Usage
Categories []Category `relation_name:"movie_category"`
table
declare a different table name for your nested data
Usage
Categories []Category `table:"category"`
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
directly set of your Mysql field.
Usage
Id string `bulker:"id" bulker_column:"INT NOT NULL AUTO_INCREMENT"`
bulker_unique
reduce for repeated values.
Usage
Name string `bulker:"name" bulker_type:"main" bulker_unique:"true" bulker_column:"TEXT NULL"`
The MIT License (MIT). See License File for more information.