Skip to content

WriteableTable.bulkAdd()

David Fahlander edited this page Apr 7, 2016 · 22 revisions

Since 1.3.0 (Bugfixed in 1.3.4 and 1.3.6-beta.2)

Syntax

table.bulkAdd(items, keys?)

Parameters

items Array of objects to add
keys (optional) Array of primary keys that corresponds to given items array

When to use the keys argument

  • If your primary key is inbound, you MUST NOT provide the keys argument.
  • If primary key is non-inbound but auto-incremented, keys argument is optional.
  • If primary key is non-inbound and non-auto-incremented, keys argument is compulsory.

Return Value

Promise

Returns with the resulting primary key of the object that was last in given array.

Errors

If some operations fail, bulkAdd will ignore those failures but return a rejected Promise with a Dexie.BulkError referencing failures. If caller does not catch that error, transaction will abort. If caller wants to ignore the failes, the bulkAdd() operations must be catched. NOTE: If you call bulkAdd() outside a transaction scope and an error occur on one of the operations, the successful operations will still be persisted to DB! If this is not desired, surround your call to bulkAdd() in a transaction and do not catch the bulkAdd() operation but just the transaction promise!

Remarks

Add all given objects to the store.

If you have a large number of objects to add to the object store, bulkAdd() is a little faster than doing add() in a loop. The object store needs to have inbound keys, or have auto-incemented keys in order for bulkAdd to work - i.e. you're table schema must not start with an empty string.

var db = new Dexie("test");
db.version(1).stores({
    tableWithInboundKeys: "id,x,y,z", // ok
    tableWithAutoIncNonInbound: "++,x,y,x", // ok 
    tableWithoutInboundKeys: ",x,y,z" // not ok
});

Sample

var db = new Dexie("test");
db.version(1).stores({
    raindrops: 'id,position'
});
var drops = [];
for (var i=0;i<100000;++i) {
    drops.push({id: i, position: [Math.random(),Math.random(),Math.random()]}),
}
db.raindrops.bulkAdd(drops).then(function(lastKey) {
    console.log("Done adding 100,000 raindrops all over the place");
    console.log("Last raindrop's id was: " + lastKey); // Will be 100000.
}).catch(Dexie.BulkError, function (e) {
    // Explicitely catching the bulkAdd() operation makes those successful
    // additions commit despite that there were errors.
    console.error ("Some raindrops did not succeed. However, " +
       100000-e.failures.length + " raindrops was added successfully");
});

See Also

WriteableTable.add()

Clone this wiki locally