gorm-bulk-insert

implement BulkInsert using gorm, just pass a Slice of Struct. Simple and compatible.

Github星跟蹤圖

Gorm Bulk Insert

Gorm Bulk Insert is a library to implement bulk insert using gorm. Execute bulk insert just by passing a slice of struct, as if you were using a gorm regularly.

Purpose

When saving a large number of records in database, inserting at once - instead of inserting one by one - leads to significant performance improvement. This is widely known as bulk insert.

Gorm is one of the most popular ORM and contains very developer-friendly features, but bulk insert is not provided.

This library is aimed to solve the bulk insert problem.

Installation

$ go get github.com/t-tiger/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

gormbulk.BulkInsert(db, sliceValue, 3000)

Third argument specifies the maximum number of records to bulk insert at once. This is because inserting a large number of records and embedding variable at once will exceed the limit of prepared statement.

Depending on the number of variables included, 2000 to 3000 is recommended.

gormbulk.BulkInsert(db, sliceValue, 3000, "Name", "Email")

Basically, inserting struct values are automatically chosen. However if you want to exclude some columns explicitly, you can specify as argument.

In the above pattern Name and Email fields are excluded.

Feature

  • Just pass a slice of struct as using gorm normally, records will be created.
    • NOTE: passing value must be a slice of struct. Map or other values are not compatible.
  • CreatedAt and UpdatedAt are automatically set to the current time.
  • Fields of relation such as belongsTo and hasMany are automatically excluded, but foreignKey is subject to Insert.

Example

package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
	"github.com/t-tiger/gorm-bulk-insert"
	"log"
	"time"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

type fakeTable struct {
	ID        int `gorm:"AUTO_INCREMENT"` 
	Name      string
	Email     string
	CreatedAt time.Time
	UpdatedAt time.Time
}

func main() {
	db, err := gorm.Open("mysql", "mydb")
	if err != nil {
		log.Fatal(err)
	}

	var insertRecords []interface{}
	for i := 0; i < 10; i++ {
		insertRecords = append(insertRecords,
			fakeTable{
				Name:  fmt.Sprintf("name%d", i),
				Email: fmt.Sprintf("test%d@test.com", i),
				// you don't need to set CreatedAt, UpdatedAt
			},
		)
	}

	err := gormbulk.BulkInsert(db, insertRecords, 3000)
	if err != nil {
		// do something
	}

	// columns you want to exclude from Insert, specify as an argument
	err = gormbulk.BulkInsert(db, insertRecords, 3000, "Email")
        if err != nil {
            // do something
        }
}

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

主要指標

概覽
名稱與所有者t-tiger/gorm-bulk-insert
主編程語言Go
編程語言Go (語言數: 1)
平台
許可證Apache License 2.0
所有者活动
創建於2018-12-28 14:25:21
推送於2023-03-07 02:00:29
最后一次提交2021-11-01 23:23:26
發布數8
最新版本名稱v2.1.0 (發布於 )
第一版名稱v1.0.0 (發布於 )
用户参与
星數279
關注者數5
派生數62
提交數64
已啟用問題?
問題數35
打開的問題數2
拉請求數13
打開的拉請求數2
關閉的拉請求數8
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?