Comparing version 1.13.5 to 1.13.6
@@ -234,3 +234,2 @@ var util = require(__dirname+'/util.js'); | ||
Model.prototype.ensureIndex = function(name, fn, opts) { | ||
@@ -826,2 +825,51 @@ var self = this; | ||
} | ||
Model.prototype.insert = function(docs) { | ||
var self = this; | ||
var r = self._getModel()._thinky.r; | ||
if (Array.isArray(docs) === false) { | ||
docs = [docs]; | ||
} | ||
var p = new Promise(function(resolve, reject) { | ||
var insert = function() { | ||
r.table(self.getTableName()).insert(docs).run().then(function(results) { | ||
if ((results.errors === 0) && (results.inserted === docs.length)) { | ||
try { | ||
var index = 0; | ||
// Set the primary keys | ||
for(var i=0; i<docs.length; i++) { | ||
if (docs[i].hasOwnProperty(self._getModel()._pk) === false) { | ||
docs[i][self._getModel()._pk] = results.generated_keys[index]; | ||
index++ | ||
} | ||
} | ||
// Create instances of the model | ||
for(i=0; i<docs.length; i++) { | ||
docs[i] = new self(docs[i]); | ||
} | ||
resolve(docs); | ||
} | ||
catch(err) { | ||
reject(err); | ||
} | ||
} | ||
else { | ||
reject(new Error("An error occurred during the batch insert.")) | ||
} | ||
}).error(reject); | ||
} | ||
if (self._getModel()._tableReady === true) { | ||
insert(); | ||
} | ||
else { | ||
self.once('ready', function() { | ||
insert(); | ||
}) | ||
} | ||
}) | ||
return p; | ||
} | ||
Model.prototype.then = function() { | ||
@@ -828,0 +876,0 @@ var query = new Query(this); |
@@ -47,2 +47,8 @@ var rethinkdbdash = require('rethinkdbdash'); | ||
var r = self.r; | ||
var init = function() { | ||
self._dbReady = true; | ||
for(var i=0; i<self._onDbReady.length; i++) { | ||
self._onDbReady[i](); | ||
} | ||
} | ||
r.dbList().contains(config.db).do(function(result) { | ||
@@ -54,9 +60,10 @@ return r.branch( | ||
) | ||
}).run().then(function(result) { | ||
self._dbReady = true; | ||
for(var i=0; i<self._onDbReady.length; i++) { | ||
self._onDbReady[i](); | ||
}).run().then(init).error(function(error) { | ||
if (error.message.match(/^Database `.*` already exists in/)) { | ||
// Someone created a database between the dbList and the dbCreate | ||
init(); | ||
} | ||
}).error(function(error) { | ||
throw error; | ||
else { | ||
throw error; | ||
} | ||
}); | ||
@@ -63,0 +70,0 @@ } |
{ | ||
"name": "thinky", | ||
"version": "1.13.5", | ||
"version": "1.13.6", | ||
"description": "RethinkDB ORM for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/thinky.js", |
121412
2689