Skip to content

A small plugin to add bulk insertion functionality to Bookshelfjs

License

Notifications You must be signed in to change notification settings

1mike12/bookshelf-bulk-save

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bookshelf Bulk Save

This plugin adds a bulkSave() method to Bookshelf.js Collection objects.

Bookshelf natively does not come with a bulk insert option. Instead, the only option is to use invokeThen("save") on collections. The underlying knex library does however have bulk inserts.

note: This does not run updates, as bulk updates are not supported natively by knex.

Requirements

requires es6 syntax to work.

Installation

npm install bookshelf-bulk-save

Then in your bookshelf configuration:

const bookshelf = require('bookshelf')(knex);
bookshelf.plugin(require('bookshelf-bulk-save');

Usage

If your db dialect supports it, it will also update the new ids and return the collection, similar to how save() on a single model returns the updated model. As of writing this is (pg, mssql, oracledb). Otherwise it will simply return true on successful insert.

//migration
module.exports = {
    up: (knex, Promise) =>{
        return knex.schema.createTableIfNotExists("users", function(table){
            table.increments();
            table.string("name");
        });
    },
    down: (knex, Promise) =>{
        return knex.schema.dropTableIfExists("users")
    }
};

//to insert
const Users = bookshelf.Collection.extend({model: User});
const UserCollection = Users.forge([
    {
        name: "bob",
        foo: "bar"
    },
    {
        name: "joe",
        foo: "bar"
    }
]);

let users = await UserCollection.bulkSave();

//if using one of the supported dialects that returns inserted rows
//users = [{id: 1, name: "bob}, {id:2, name: "joe"}]

About

A small plugin to add bulk insertion functionality to Bookshelfjs

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published