Giter Site home page Giter Site logo

gorm-bulk's Introduction

gorm-bulk

Build Status Coverage Status Documentation

Perform regular Gorm tasks - in bulk!

This project aims to support the missing feature from the famous ORM Gorm. The feature I'm talking about is bulk support which has been on the wish list since 2014; see here.

This is inspired by t-tiger/gorm-bulk-insert which in turn is inspired by this comment but with the focus on flexibility and letting the end user handle final SQL, final values, how many to bulk at once etcetera.

Project status

This project is in it's early phase and since I want to ensure that the end user interface ends up as smoot, simple and flexible as possible I won't create a v1.0 release tag until I feel the most important things are in to place.

This doesn't mean that things aren't workign as indented, just that the API might change without notice.

Installation

go get -u github.com/bombsimon/gorm-bulk/...

Usage

Generate slice conversion

To be able to iterate over any type you have to pass an interface slice ([]interface{}). To make this easier this package is bundled with a code generator that will generate functions to convert []*<T> and []<T> to []interface{}.

See exmaples for details about how to use go generate and what the result will look like.

Bulk actions

This package ships with a few standard bulk action methods. A bulk action uses an ExecFunc which takes a *gorm.Scope (holding the table name, all the values and where you may set the SQL), a slice of all column names and a slice of all placeholder groups (a set of prepared statements for each slice element).

  • InsertFunc - Regular INSERT INTO with all passed values.
  • InsertIgnoreFunc - Run INSERT IGNORE INTO with all passed values which will just discard duplicates (and any other error).
  • InsertOnDuplicateKeyUpdateFunc - Run INSERT INTO ... VALUES(...) ON DUPLICATE KEY UPDATE x = VALUES(x).

Notice that InsertFunc and InsertIgnoreFunc will look at gorm:insert_option to fetch any user defined additions.

These three ExecFuncs are wrapped in BulkInsert, BulkInsertIgnore and BulkInsertOnDuplicateKeyUpdate so you only have to pass your *gorm.DB and interface slice.

func Example(db *gorm.DB, myTypes []MyType) error {
    myTypesAsInterface := MyTypeSliceToInterfaceSlice(myTypes)

    if err := gormbulk.BulkInsert(db, myTypesAsInterface); err != nil {
        return err
    }

    return nil
}

Creating your own action

To create your own action where you may return whatever SQL and values you want just implement an ExecFunc. This is how a simple INSERT INTO would be defined.

func MyCustomBulkFunc(scope *gorm.Scope, columnNames, placeholders []string) {
    scope.Raw(fmt.Sprintf(
        "INSERT INTO %s (%s) VALUES %s",
        scope.QuotedTableName(),
        strings.Join(columnNames, ", "), 
        strings.Join(placeholders, ", "), 
    ))
}

Using the bulk

If you just want to perform a simple bulk insert, use one of the pre implemented wrapper bulk functions and pass your *gorm.DB and data set, see the example.

gorm-bulk's People

Contributors

bombsimon avatar fnxpt avatar greenpart avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.