Module: WingedCouch::Models::Bulk

Included in:
WingedCouch::Model
Defined in:
lib/winged_couch/models/bulk.rb

Instance Method Summary collapse

Instance Method Details

#bulk(group_size = 300) ⇒ Object

Performs batch insert to the database

Examples:

User.bulk(400) do
  User.create(name: 'user1')
  User.create(name: 'user2')
  User.create(name: 'user3')
end
# this example makes only single request to the database

Parameters:

  • group_size (Fixnum) (defaults to: 300)

    size of one batch


38
39
40
41
42
43
44
45
46
47
# File 'lib/winged_couch/models/bulk.rb', line 38

def bulk(group_size = 300)
  @bulk = true
  yield

  bulk_records.in_groups_of(group_size, false) do |group|
    docs = group.map(&:native_document).map(&:data)
    database.bulk(docs)
  end
  @bulk = false
end

#bulk?true, false

Returns true if execution is in MyModel.bulk {} block

This method is internal, but you still can use it

Returns:

  • (true, false)

22
23
24
# File 'lib/winged_couch/models/bulk.rb', line 22

def bulk?
  @bulk
end

#bulk_recordsObject

Returns records, created during

MyModel.bulk {}

This records are still in memory


12
13
14
# File 'lib/winged_couch/models/bulk.rb', line 12

def bulk_records
  @bulk_records ||= []
end