Comparing version 1.13.12 to 1.14.0
@@ -1,4 +0,4 @@ | ||
function DocumentNotFound() { | ||
function DocumentNotFound(message) { | ||
Error.captureStackTrace(this, DocumentNotFound); | ||
this.message = "The query did not find a document and returned null."; | ||
this.message = message || "The query did not find a document and returned null."; | ||
}; | ||
@@ -5,0 +5,0 @@ DocumentNotFound.prototype = new Error(); |
116
lib/model.js
@@ -50,2 +50,3 @@ var util = require(__dirname+'/util.js'); | ||
this._methods = {}; | ||
this._staticMethods = {}; | ||
this._async = { | ||
@@ -290,16 +291,14 @@ init: false, | ||
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; | ||
}).run().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)) { | ||
if ((self._getModel()._tableCreated === true) | ||
&& (self._getModel()._indexesToCreate === 0) | ||
&& (self._getModel()._foreignIndexesToCreate === 0)) { | ||
self._setReady(); | ||
} | ||
}); | ||
self._setReady(); | ||
} | ||
@@ -1043,5 +1042,12 @@ }).error(function(error) { | ||
} | ||
Model.prototype.defineStatic = function(key, fn) { | ||
this._staticMethods[key] = fn; | ||
this[key] = function() { | ||
return fn.apply(this, arguments); | ||
}; | ||
} | ||
Model.prototype._parse = function(data) { | ||
@@ -1053,50 +1059,48 @@ var self = this; | ||
var p = new Promise(function(resolve, reject) { | ||
if ((util.isPlainObject(data)) && (data.__proto__._next !== undefined)) { // Checking if a cursor | ||
data.toArray().then(function(result) { | ||
util.tryCatch(function() { | ||
for(var i=0; i<result.length; i++) { | ||
result[i] = new self(result[i]) | ||
result[i].setSaved(true); | ||
if (Array.isArray(data)) { | ||
util.tryCatch(function() { | ||
for(var i=0; i<data.length; i++) { | ||
data[i] = new self(data[i]) | ||
data[i].setSaved(true); | ||
self.emit('retrieved', result[i]); | ||
self.emit('retrieved', data[i]); | ||
// Order matters here, we want the hooks to be executed *before* calling validate | ||
promise = util.hook({ | ||
postHooks: result[i]._getModel()._post.retrieve, | ||
doc: result[i], | ||
async: result[i]._getModel()._async.retrieve, | ||
fn: function() {} | ||
}) | ||
if (promise instanceof Promise) { | ||
promise.then(function() { | ||
var promise = result[i].validate(); | ||
if (promise instanceof Promise) { | ||
promise.then(resolve).error(reject); | ||
} | ||
else { | ||
resolve(); | ||
} | ||
}).error(reject); | ||
promises.push(promise); | ||
} | ||
else { | ||
promise = result[i].validate(); | ||
if (promise instanceof Promise) promises.push(promise); | ||
} | ||
// Order matters here, we want the hooks to be executed *before* calling validate | ||
promise = util.hook({ | ||
postHooks: data[i]._getModel()._post.retrieve, | ||
doc: data[i], | ||
async: data[i]._getModel()._async.retrieve, | ||
fn: function() {} | ||
}) | ||
if (promise instanceof Promise) { | ||
promise.then(function() { | ||
var promise = data[i].validate(); | ||
if (promise instanceof Promise) { | ||
promise.then(resolve).error(reject); | ||
} | ||
else { | ||
resolve(); | ||
} | ||
}).error(reject); | ||
promises.push(promise); | ||
} | ||
}, function(error) { | ||
var newError = new Error("The results could not be converted to instances of `"+self.getTableName()+"`\nDetailed error: "+error.message); | ||
return reject(newError); | ||
}); | ||
else { | ||
promise = data[i].validate(); | ||
if (promise instanceof Promise) promises.push(promise); | ||
} | ||
} | ||
}, function(error) { | ||
var newError = new Error("The results could not be converted to instances of `"+self.getTableName()+"`\nDetailed error: "+error.message); | ||
return reject(newError); | ||
}); | ||
if (promises.length > 0) { | ||
Promise.all(promises).then(function() { | ||
resolve(result); | ||
}).error(reject); | ||
} | ||
else { | ||
resolve(result); | ||
} | ||
}).error(reject) | ||
if (promises.length > 0) { | ||
Promise.all(promises).then(function() { | ||
resolve(data); | ||
}).error(reject); | ||
} | ||
else { | ||
resolve(data); | ||
} | ||
} | ||
@@ -1103,0 +1107,0 @@ else { |
@@ -6,2 +6,3 @@ var Promise = require('bluebird'); | ||
function Query(model, query) { | ||
var self = this; | ||
this._model = model; // constructor | ||
@@ -11,2 +12,10 @@ | ||
this._r = model._getModel()._thinky.r; | ||
for (var key in model._staticMethods) { | ||
(function(key) { | ||
self[key] = function() { | ||
return model._staticMethods[key].apply(self, arguments); | ||
}; | ||
})(key); | ||
} | ||
} | ||
@@ -48,2 +57,5 @@ | ||
} | ||
if (parse !== true) { | ||
fullOptions.cursor = true; | ||
} | ||
@@ -76,3 +88,11 @@ var self = this; | ||
} | ||
}).error(reject) | ||
}).error(function(err) { | ||
if (err.message.match(/^The query did not find a document and returned null./)) { | ||
//Reject with an instance of Errors.DocumentNotFound | ||
reject(new Errors.DocumentNotFound(err.message)) | ||
} | ||
else { | ||
reject(err) | ||
} | ||
}) | ||
}); | ||
@@ -100,3 +120,11 @@ } | ||
} | ||
}).error(reject); | ||
}).error(function(err) { | ||
if (err.message.match(/^The query did not find a document and returned null./)) { | ||
//Reject with an instance of Errors.DocumentNotFound | ||
reject(new Errors.DocumentNotFound(err.message)) | ||
} | ||
else { | ||
reject(err) | ||
} | ||
}) | ||
}); | ||
@@ -157,5 +185,11 @@ }); | ||
r.table(joins[key].model.getTableName()).getAll(doc(joins[key].leftKey), {index: joins[key].rightKey}).coerceTo("ARRAY").do(function(result) { | ||
innerQuery = new Query(joins[key].model, result.nth(0)); | ||
if ((modelToGet[key] != null) && (typeof modelToGet[key]._apply === 'function')) { | ||
innerQuery = modelToGet[key]._apply(innerQuery); | ||
} | ||
innerQuery = innerQuery.getJoin(modelToGet[key], getAll, gotModel)._query; | ||
return r.branch( | ||
result.count().eq(1), | ||
r.object(key, new Query(joins[key].model, result.nth(0)).getJoin(modelToGet[key], getAll, gotModel)._query), | ||
r.object(key, innerQuery), | ||
r.branch( | ||
@@ -177,5 +211,11 @@ result.count().eq(0), | ||
r.table(joins[key].model.getTableName()).getAll(doc(joins[key].leftKey), {index: joins[key].rightKey}).coerceTo("ARRAY").do(function(result) { | ||
innerQuery = new Query(joins[key].model, result.nth(0)); | ||
if ((modelToGet[key] != null) && (typeof modelToGet[key]._apply === 'function')) { | ||
innerQuery = modelToGet[key]._apply(innerQuery); | ||
} | ||
innerQuery = innerQuery.getJoin(modelToGet[key], getAll, gotModel)._query; | ||
return r.branch( | ||
result.count().eq(1), | ||
r.object(key, new Query(joins[key].model, result.nth(0)).getJoin(modelToGet[key], getAll, gotModel)._query), | ||
r.object(key, innerQuery), | ||
r.branch( | ||
@@ -265,7 +305,16 @@ result.count().eq(0), | ||
if ((Term.hasOwnProperty(key)) && (key !== 'run') && (key[0] !== '_')) { | ||
(function(key) { | ||
Query.prototype[key] = function() { | ||
return new Query(this._model, this._query[key].apply(this._query, arguments)); | ||
} | ||
})(key); | ||
if (key === 'get') { | ||
(function(key) { | ||
Query.prototype[key] = function() { | ||
return new Query(this._model, this._query[key].apply(this._query, arguments)).default(this._r.error("The query did not find a document and returned null.")); | ||
} | ||
})(key); | ||
} | ||
else { | ||
(function(key) { | ||
Query.prototype[key] = function() { | ||
return new Query(this._model, this._query[key].apply(this._query, arguments)); | ||
} | ||
})(key); | ||
} | ||
} | ||
@@ -272,0 +321,0 @@ } |
@@ -10,2 +10,3 @@ var util = require('util'); | ||
function validateSchema(schema, prefix) { | ||
// Validate a schema and add the field _enum if needed | ||
prefix = prefix || ''; | ||
@@ -23,6 +24,15 @@ if (isPlainObject(schema)) { | ||
&& (schema[key]._type !== Date) | ||
&& (schema[key]._type !== Buffer) | ||
&& (schema[key]._type !== Object) | ||
&& (schema[key]._type !== Array)) { | ||
throw new Error("The field `_type` must be `String`/`Number`/`Boolean`/`Date`/`Object`/`Array` for "+prefix+"["+key+"]"); | ||
throw new Error("The field `_type` must be `String`/`Number`/`Boolean`/`Date`/`Buffer`/`Object`/`Array` for "+prefix+"["+key+"]"); | ||
} | ||
// Add the set with the valid values for an enum | ||
if (Array.isArray(schema[key].enum)) { | ||
schema[key]._enum = {} | ||
for(var i=0; i<schema[key].enum.length; i++) { | ||
schema[key]._enum[schema[key].enum[i]] = true; | ||
} | ||
} | ||
} | ||
@@ -37,6 +47,7 @@ else { | ||
&& (schema[key] !== Date) | ||
&& (schema[key] !== Buffer) | ||
&& (schema[key] !== Object) | ||
&& (schema[key] !== Array)) { | ||
throw new Error("The value must be `String`/`Number`/`Boolean`/`Date`/`Object`/`Array` for "+prefix+"["+key+"]"); | ||
throw new Error("The value must be `String`/`Number`/`Boolean`/`Date`/`Buffer`/`Object`/`Array` for "+prefix+"["+key+"]"); | ||
} | ||
@@ -61,6 +72,7 @@ } | ||
&& (schema[0] !== Date) | ||
&& (schema[0] !== Buffer) | ||
&& (schema[0] !== Object) | ||
&& (schema[0] !== Array)) { | ||
throw new Error("The field `_type` must be `String`/`Number`/`Boolean`/`Date`/`Object`/`Array` in "+prefix); | ||
throw new Error("The field `_type` must be `String`/`Number`/`Boolean`/`Date`/`Buffer`/`Object`/`Array` in "+prefix); | ||
} | ||
@@ -79,3 +91,8 @@ } | ||
var result; | ||
if (isPlainObject(value) === true) { | ||
if (value instanceof Buffer) { | ||
// We do a shallow copy here because buffer could in theory | ||
// be pretty big | ||
return new Buffer(value); | ||
} | ||
else if (isPlainObject(value) === true) { | ||
result = {}; | ||
@@ -126,6 +143,6 @@ for(var key in value) { | ||
util.looseType = looseType; | ||
function pseudoTimeError(missingField, prefix) { | ||
throw new Error("The raw date object for "+prefix+" is missing the required field "+missingField+".") | ||
function pseudoTypeError(type, missingField, prefix) { | ||
throw new Error("The raw "+type+" object for "+prefix+" is missing the required field "+missingField+".") | ||
} | ||
util.pseudoTimeError = pseudoTimeError; | ||
util.pseudoTypeError = pseudoTypeError; | ||
@@ -132,0 +149,0 @@ function tryCatch(toTry, handleError) { |
{ | ||
"name": "thinky", | ||
"version": "1.13.12", | ||
"version": "1.14.0", | ||
"description": "RethinkDB ORM for Node.js", | ||
@@ -10,3 +10,3 @@ "main": "lib/thinky.js", | ||
"scripts": { | ||
"test": "mocha --check-leaks -t 20000" | ||
"test": "mocha --check-leaks -t 30000" | ||
}, | ||
@@ -28,3 +28,3 @@ "repository": { | ||
"dependencies":{ | ||
"rethinkdbdash": "~1.13.11", | ||
"rethinkdbdash": "~1.14.0", | ||
"bluebird": "~ 2.1.3" | ||
@@ -31,0 +31,0 @@ }, |
55
test.js
@@ -1,19 +0,40 @@ | ||
var Promise = require('bluebird'); | ||
var config = require(__dirname+'/config.js'); | ||
var thinky = require(__dirname+'/lib/thinky.js')(config); | ||
var r = thinky.r; | ||
var Errors = thinky.Errors; | ||
var p = new Promise(function(resolve, reject) { | ||
(function() { | ||
var p = new Promise(function(resolve1, reject) { | ||
reject(new Error("Catch me")); | ||
}); | ||
p.then(resolve).error(function(err) { | ||
console.log(err); | ||
reject(err); | ||
}); | ||
})(); | ||
var util = require(__dirname+'/test/util.js'); | ||
var assert = require('assert'); | ||
}).then(function() { | ||
console.log("Was expecting an error") | ||
}).error(function(err) { | ||
console.log(err); | ||
console.log("Done") | ||
}); | ||
var name = util.s8(); | ||
var Model = thinky.createModel(name, { | ||
id: String, | ||
str: String, | ||
num: Number, | ||
nested: { | ||
foo: Number, | ||
bar: Number | ||
} | ||
}) | ||
var str = util.s8(); | ||
var num = util.random(); | ||
doc = new Model({ | ||
str: str, | ||
num: num, | ||
nested: { foo: 1, bar: 2} | ||
}) | ||
doc.save().then(function(result) { | ||
console.log('saved'); | ||
assert.equal(typeof doc.id, 'string'); | ||
assert.equal(doc.isSaved(), true); | ||
doc.str = "lalala"; | ||
doc.nested.bar = 3; | ||
console.log(doc.getOldValue()); | ||
console.log(doc); | ||
}).error(console.log); |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
152580
3322
13
+ Addedrethinkdbdash@1.14.4(transitive)
- Removedrethinkdbdash@1.13.13(transitive)
Updatedrethinkdbdash@~1.14.0