All Projects → sunary → gorm-bulk-insert

sunary / gorm-bulk-insert

Licence: other
implement BulkInsert using gorm

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to gorm-bulk-insert

gin-gorm-api-example
[Article] Minimal code for Golang based API
Stars: ✭ 98 (+476.47%)
Mutual labels:  gorm
Go-bjut
A bbs system.
Stars: ✭ 60 (+252.94%)
Mutual labels:  gorm
echo-gorm-realworld-app
realworld application built with Golang + Echo + Gorm
Stars: ✭ 18 (+5.88%)
Mutual labels:  gorm
jsonuri
🌳 阿里剑鱼、iceluna、vanex 数据操作底层库,使用O(n) 复杂度回溯祖先节点
Stars: ✭ 131 (+670.59%)
Mutual labels:  insert
go-12factor-example
Example the 12factor app using golang
Stars: ✭ 20 (+17.65%)
Mutual labels:  gorm
newrelic-context
Contains different helpers to make life easier with NewRelic and Context.
Stars: ✭ 21 (+23.53%)
Mutual labels:  gorm
ecto conditionals
EctoConditionals implements a flexibly functional find_or_create and upsert behavior for Ecto models.
Stars: ✭ 17 (+0%)
Mutual labels:  upsert
gen
Gen: Friendly & Safer GORM powered by Code Generation
Stars: ✭ 677 (+3882.35%)
Mutual labels:  gorm
GoGonicEcommerceApi
Ecommerce Rest API application built in Go with Gin Gonic + Gorm
Stars: ✭ 81 (+376.47%)
Mutual labels:  gorm
RepositoryHelpers
📦 Extensions for HttpClient and Custom Repository based on dapper
Stars: ✭ 22 (+29.41%)
Mutual labels:  insert
public
util toolkit for go.golang 通用函数包
Stars: ✭ 135 (+694.12%)
Mutual labels:  gorm
gt-crud
gin+gorm+mysql+api[两步自动crud]
Stars: ✭ 15 (-11.76%)
Mutual labels:  gorm
Go-Gin-Api
基于golang开源框架 gin封装的api框架
Stars: ✭ 42 (+147.06%)
Mutual labels:  gorm
iris-admin
Web admin for iris-go framwork
Stars: ✭ 602 (+3441.18%)
Mutual labels:  gorm
gorm-hibernate5
GORM for Hibernate 5
Stars: ✭ 51 (+200%)
Mutual labels:  gorm
iam
企业级的 Go 语言实战项目:认证和授权系统
Stars: ✭ 1,900 (+11076.47%)
Mutual labels:  gorm
go-cms
基于beego框架的cms系统
Stars: ✭ 127 (+647.06%)
Mutual labels:  gorm
goft-gin
基于gin的开发脚手架
Stars: ✭ 108 (+535.29%)
Mutual labels:  gorm
gorm-cursor-paginator
A paginator doing cursor-based pagination based on GORM
Stars: ✭ 92 (+441.18%)
Mutual labels:  gorm
linkbucket-go
This project has been moved permanently to: https://fossil.avalos.me/linkbucket-go
Stars: ✭ 16 (-5.88%)
Mutual labels:  gorm

Gorm Bulk Insert/Upsert

Gorm Bulk Insert is a library to implement bulk insert/upsert using gorm.

Purpose

Save bulk records

Installation

$ go get github.com/sunary/gorm-bulk-insert

This library depends on gorm, following command is also necessary unless you've installed gorm.

$ go get github.com/jinzhu/gorm

Usage

BulkInsert

bulk.BulkInsert(db, bulkData)
// or
bulk.BulkInsertWithTableName(db, tableName, bulkData)

BulkUpsert

bulk.BulkUpsert(db, bulkData, uniqueKeys)
// or
bulk.BulkUpsertWithTableName(db, tableName, bulkData, uniqueKeys)

Example

package main

import (
	"log"
	"time"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	bulk "github.com/sunary/gorm-bulk-insert"
)

type User struct {
	ID        int
	UserName  string `gorm:"column:name"`
	Age       int
	Hobby     string `gorm:"-"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (u User) TableName() string {
	return "user"
}

func main() {
	db, err := gorm.Open("mysql", "root:password@tcp(localhost:3306)/db_name")
	if err != nil {
		log.Fatal(err)
	}

	var bulkData []interface{}
	for i := 0; i < 10000; i++ {
		bulkData = append(bulkData,
			User{
				UserName: "sunary",
				Age:      22,
				Hobby:    "dance",
			},
		)
	}

	err = bulk.BulkInsert(db, bulkData)
	// or err = bulk.BulkInsertWithTableName(db, User{}.TableName(), bulkData)
	if err != nil {
		log.Fatal(err)
	}

	var bulkUpsertData []interface{}
	for i := 0; i < 100; i++ {
		bulkUpsertData = append(bulkUpsertData,
			User{
				UserName: "sunary",
				Age:      22,
				Hobby:    "soccer",
			},
		)
	}

	err = bulk.BulkUpsert(db, bulkUpsertData, []string{"name"})
	// or err = bulk.BulkUpsertWithTableName(db, User{}.TableName(), bulkUpsertData, []string{"name"})
	if err != nil {
		log.Fatal(err)
	}
}

License

This project is under Apache 2.0 License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].