All Projects → t-tiger → Gorm Bulk Insert

t-tiger / Gorm Bulk Insert

Licence: apache-2.0
implement BulkInsert using gorm, just pass a Slice of Struct. Simple and compatible.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Gorm Bulk Insert

Sql Fundamentals
👨‍🏫 Mike's SQL Fundamentals and Professional SQL Courses
Stars: ✭ 140 (-41.91%)
Mutual labels:  database, mysql, postgresql
Sqlcheck
Automatically identify anti-patterns in SQL queries
Stars: ✭ 2,062 (+755.6%)
Mutual labels:  database, mysql, postgresql
Dapper.fsharp
Lightweight F# extension for StackOverflow Dapper with support for MSSQL, MySQL and PostgreSQL
Stars: ✭ 145 (-39.83%)
Mutual labels:  database, mysql, postgresql
Db
Data access layer for PostgreSQL, CockroachDB, MySQL, SQLite and MongoDB with ORM-like features.
Stars: ✭ 2,832 (+1075.1%)
Mutual labels:  database, mysql, postgresql
Condenser
Condenser is a database subsetting tool
Stars: ✭ 189 (-21.58%)
Mutual labels:  database, mysql, postgresql
Kangaroo
SQL client and admin tool for popular databases
Stars: ✭ 127 (-47.3%)
Mutual labels:  database, mysql, postgresql
Database To Plantuml
Compile PostgreSQL and MySQL table information into a PlantUML description.
Stars: ✭ 157 (-34.85%)
Mutual labels:  database, mysql, postgresql
Csv2db
The CSV to database command line loader
Stars: ✭ 102 (-57.68%)
Mutual labels:  database, mysql, postgresql
Nut
Advanced, Powerful and easy to use ORM for Qt
Stars: ✭ 181 (-24.9%)
Mutual labels:  database, mysql, postgresql
Scalardb
Universal transaction manager
Stars: ✭ 178 (-26.14%)
Mutual labels:  database, mysql, postgresql
Directus
Open-Source Data Platform 🐰 — Directus wraps any SQL database with a real-time GraphQL+REST API and an intuitive app for non-technical users.
Stars: ✭ 13,190 (+5373.03%)
Mutual labels:  database, mysql, postgresql
Endb
Key-value storage for multiple databases. Supports MongoDB, MySQL, Postgres, Redis, and SQLite.
Stars: ✭ 208 (-13.69%)
Mutual labels:  database, mysql, postgresql
Backup Manager
Database backup manager for dumping to and restoring databases from S3, Dropbox, FTP, SFTP, and Rackspace Cloud
Stars: ✭ 1,589 (+559.34%)
Mutual labels:  database, mysql, postgresql
Scany
Library for scanning data from a database into Go structs and more
Stars: ✭ 228 (-5.39%)
Mutual labels:  database, mysql, postgresql
Next
Directus is a real-time API and App dashboard for managing SQL database content. 🐰
Stars: ✭ 111 (-53.94%)
Mutual labels:  database, mysql, postgresql
Querybuilder
SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
Stars: ✭ 2,111 (+775.93%)
Mutual labels:  database, mysql, postgresql
Prisma
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite & MongoDB (Preview)
Stars: ✭ 18,168 (+7438.59%)
Mutual labels:  database, mysql, postgresql
Qtl
A friendly and lightweight C++ database library for MySQL, PostgreSQL, SQLite and ODBC.
Stars: ✭ 92 (-61.83%)
Mutual labels:  database, mysql, postgresql
Linq2db
Linq to database provider.
Stars: ✭ 2,211 (+817.43%)
Mutual labels:  database, mysql, postgresql
Shardingsphere
Build criterion and ecosystem above multi-model databases
Stars: ✭ 14,989 (+6119.5%)
Mutual labels:  database, mysql, postgresql

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/v2

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"
	"log"
	"time"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	gormbulk "github.com/t-tiger/gorm-bulk-insert/v2"
)

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%[email protected]", 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.

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].