Comparing version 1.13.10 to 1.13.11
139
lib/model.js
@@ -34,4 +34,6 @@ var util = require(__dirname+'/util.js'); | ||
this._tableReady = false; | ||
this._indexesFetched = false; // Whether the indexes were fetched or not | ||
this._indexesToCreate = 0; | ||
this._foreignIndexesToCreate = 0; // many to many indexes left to create | ||
this._indexes = {}; // indexName -> true | ||
@@ -218,3 +220,7 @@ this._error = null; // If an error occured, we won't let people save things | ||
// We need to check tableCreated because _indexWasCreated can be called when a foreign index is built. | ||
if ((this._getModel()._tableCreated === true) && (this._getModel()._indexesToCreate === 0) && (this._getModel()._foreignIndexesToCreate === 0)) { | ||
if ((this._getModel()._tableCreated === true) | ||
&& (this._getModel()._indexesToCreate === 0) | ||
&& (this._getModel()._foreignIndexesToCreate === 0) | ||
&& (this._getModel()._indexesFetched === true)) { | ||
this._setReady(); | ||
@@ -231,6 +237,11 @@ } | ||
if ((self._getModel()._indexesToCreate === 0) && (self._getModel()._foreignIndexesToCreate === 0)) { | ||
// Will trigger the queries waiting | ||
this._setReady(); | ||
} | ||
self.fetchIndexes().then(function() { | ||
if ((self._getModel()._indexesToCreate === 0) | ||
&& (self._getModel()._foreignIndexesToCreate === 0)) { | ||
// Will trigger the queries waiting | ||
this._setReady(); | ||
} | ||
}).error(function(error) { | ||
self._error = error; | ||
}) | ||
}; | ||
@@ -272,3 +283,44 @@ | ||
Model.prototype.fetchIndexes = function() { | ||
var self = this; | ||
return new Promise(function(resolve, reject) { | ||
var fetch = function() { | ||
var r = self._thinky.r; | ||
var query = new Query(self); | ||
r.table(self.getTableName()).indexList().do(function(indexes) { | ||
return r.table(self.getTableName()).indexWait(r.args(indexes)) | ||
}).run().then(function(cursor) { | ||
cursor.toArray().then(function(indexes) { | ||
for(var i=0; i<indexes.length; i++) { | ||
self._getModel()._indexes[indexes[i].index] = true; | ||
} | ||
self._getModel()._indexesFetched = true; | ||
if ((self._getModel()._tableCreated === true) | ||
&& (self._getModel()._indexesToCreate === 0) | ||
&& (self._getModel()._foreignIndexesToCreate === 0)) { | ||
self._setReady(); | ||
} | ||
}); | ||
}).error(function(error) { | ||
self._getModel()._setError(error); | ||
}); | ||
} | ||
if (self._getModel()._tableCreated === true) { | ||
fetch(); | ||
} | ||
else { | ||
self.once('created', function() { | ||
fetch(); | ||
}); | ||
} | ||
}); | ||
} | ||
Model.prototype.ensureIndex = function(name, fn, opts) { | ||
@@ -281,38 +333,45 @@ var self = this; | ||
var ensureIndex = function() { | ||
if (fn === undefined) { | ||
fn = function(doc) { return doc(name) }; | ||
} | ||
var tableName = self.getTableName(); | ||
return r.branch( | ||
r.table(tableName).indexList().contains(name), | ||
r.table(tableName).indexWait(name), | ||
r.branch( | ||
r.table(tableName).info()("primary_key").eq(name), | ||
return new Promise(function(resolve, reject) { | ||
var ensureIndex = function() { | ||
if (fn === undefined) { | ||
fn = function(doc) { return doc(name) }; | ||
} | ||
var tableName = self.getTableName(); | ||
return r.branch( | ||
r.table(tableName).indexList().contains(name), | ||
r.table(tableName).indexWait(name), | ||
r.table(tableName).indexCreate(name, fn, opts || {}).do(function() { | ||
return r.table(tableName).indexWait(name) | ||
}) | ||
) | ||
).run().then(function(result) { | ||
self._indexWasCreated('local'); | ||
}).error(function(error) { | ||
if (error.message.match(/^Index `/)) { | ||
r.branch( | ||
r.table(tableName).info()("primary_key").eq(name), | ||
r.table(tableName).indexWait(name), | ||
r.table(tableName).indexCreate(name, fn, opts || {}).do(function() { | ||
return r.table(tableName).indexWait(name) | ||
}) | ||
) | ||
).run().then(function(result) { | ||
self._getModel()._indexes[name] = true; | ||
self._indexWasCreated('local'); | ||
} | ||
else { | ||
self._getModel()._setError(error); | ||
} | ||
}) | ||
} | ||
resolve(); | ||
}).error(function(error) { | ||
if (error.message.match(/^Index `/)) { | ||
self._indexWasCreated('local'); | ||
} | ||
else { | ||
self._getModel()._setError(error); | ||
} | ||
if (self._getModel()._tableCreated === true) { | ||
ensureIndex(); | ||
} | ||
else { | ||
self.once('created', function() { | ||
reject(error) | ||
}) | ||
} | ||
if (self._getModel()._tableCreated === true) { | ||
ensureIndex(); | ||
}); | ||
} | ||
} | ||
else { | ||
self.once('created', function() { | ||
ensureIndex(); | ||
}); | ||
} | ||
}) | ||
} | ||
@@ -382,2 +441,3 @@ | ||
joinedModel._indexWasCreated('local'); | ||
joinedModel._getModel()._indexes[rightKey] = true; | ||
self._indexWasCreated('foreign'); | ||
@@ -409,2 +469,3 @@ }).error(function(error) { | ||
joinedModel._indexWasCreated('local'); | ||
joinedModel._getModel()._indexes[rightKey] = true; | ||
self._indexWasCreated('foreign'); | ||
@@ -494,2 +555,3 @@ }).error(function(error) { | ||
self._indexWasCreated('local'); | ||
self._getModel()._indexes[leftKey] = true; | ||
joinedModel._indexWasCreated('foreign'); | ||
@@ -507,2 +569,3 @@ } | ||
self._indexWasCreated('local'); | ||
self._getModel()._indexes[leftKey] = true; | ||
joinedModel._indexWasCreated('foreign'); | ||
@@ -581,2 +644,3 @@ }).error(function(error) { | ||
joinedModel._indexWasCreated('local'); | ||
joinedModel._getModel()._indexes[rightKey] = true; | ||
self._indexWasCreated('foreign'); | ||
@@ -598,2 +662,3 @@ }).error(function(error) { | ||
joinedModel._indexWasCreated('local'); | ||
joinedModel._getModel()._indexes[rightKey] = true; | ||
self._indexWasCreated('foreign'); | ||
@@ -808,2 +873,4 @@ }).error(function(error) { | ||
joinedModel._indexWasCreated('foreign'); | ||
self._getModel()._indexes[leftKey] = true; | ||
joinedModel._getModel()._indexes[rightKey] = true; | ||
resolve(); | ||
@@ -810,0 +877,0 @@ } |
@@ -259,6 +259,43 @@ var Promise = require('bluebird'); | ||
(function(key) { | ||
Query.prototype[key] = function() { | ||
var query = new Query(this._model, this._query[key].apply(this._query, arguments)); | ||
return query; | ||
} | ||
switch (key) { | ||
case 'orderBy': | ||
Query.prototype[key] = function() { | ||
if ((arguments.length === 1) | ||
&& (typeof arguments[0] === 'string') | ||
&& (this._model._indexes[arguments[0]] === true)) { | ||
this._query = this._query[key]({index: arguments[0]}); | ||
return this; | ||
} | ||
else { | ||
return new Query(this._model, this._query[key].apply(this._query, arguments)); | ||
} | ||
} | ||
break; | ||
case 'filter': | ||
Query.prototype[key] = function() { | ||
if ((arguments.length === 1) | ||
&& (util.isPlainObject(arguments[0]))) { | ||
var keys = Object.keys(arguments[0]); | ||
if ((keys.length === 1) && (this._model._indexes[keys[0]] === true)) { | ||
this._query = this._query.getAll(arguments[0][keys[0]], {index: keys[0]}); | ||
return this; | ||
} | ||
else { | ||
return new Query(this._model, this._query[key].apply(this._query, arguments)); | ||
} | ||
} | ||
else { | ||
return new Query(this._model, this._query[key].apply(this._query, arguments)); | ||
} | ||
} | ||
break; | ||
default: | ||
Query.prototype[key] = function() { | ||
return new Query(this._model, this._query[key].apply(this._query, arguments)); | ||
} | ||
} | ||
})(key); | ||
@@ -268,3 +305,6 @@ } | ||
Query.prototype.toString = function() { | ||
return this._query.toString(); | ||
} | ||
module.exports = Query; |
@@ -132,2 +132,5 @@ var rethinkdbdash = require('rethinkdbdash'); | ||
model._getModel()._tableCreated = true; | ||
model.emit('created'); | ||
model._getModel()._tableReady = true; | ||
model.emit('ready'); | ||
} | ||
@@ -134,0 +137,0 @@ return model; |
{ | ||
"name": "thinky", | ||
"version": "1.13.10", | ||
"version": "1.13.11", | ||
"description": "RethinkDB ORM for Node.js", | ||
@@ -27,3 +27,3 @@ "main": "lib/thinky.js", | ||
"dependencies":{ | ||
"rethinkdbdash": "~1.13.0", | ||
"rethinkdbdash": "~1.13.11", | ||
"bluebird": "~ 2.1.3" | ||
@@ -30,0 +30,0 @@ }, |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
146435
3178
2
Updatedrethinkdbdash@~1.13.11