Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,21 @@ Model.prototype.getTableName = function() {
return this._getModel()._name;
}

/*
* Return the schema for this Model
*/
Model.prototype.getSchema = function() {
return this._schema;
}

/*
* Return the exported representation of the schema for this model
*/
Model.prototype.exportSchema = function() {
return this.getSchema().export();
}



Model.prototype.fetchIndexes = function() {
var self = this;
Expand All @@ -263,7 +278,7 @@ Model.prototype.fetchIndexes = 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))
return r.table(self.getTableName()).indexWait(r.args(indexes))
}).run().then(function(indexes) {
for(var i=0; i<indexes.length; i++) {
self._getModel()._indexes[indexes[i].index] = true;
Expand Down Expand Up @@ -322,7 +337,7 @@ Model.prototype.ensureIndex = function(name, fn, opts) {
})
)
).run().then(function(result) {
self._getModel()._indexes[name] = true;
self._getModel()._indexes[name] = true;
self._indexWasCreated('local');

resolve();
Expand Down Expand Up @@ -516,7 +531,7 @@ Model.prototype.belongsTo = function(joinedModel, fieldDoc, leftKey, rightKey, o
})
)
)

if (self._getModel()._tableCreated === true) {
query.run().then(function(result) {
self._indexWasCreated('local');
Expand Down Expand Up @@ -585,7 +600,7 @@ Model.prototype.hasMany = function(joinedModel, fieldDoc, leftKey, rightKey, opt
type: 'hasMany'
};
joinedModel._getModel()._localKeys[rightKey] = true;

options = options || {};
if (options.init !== false) {
var tableName = joinedModel.getTableName();
Expand Down Expand Up @@ -1120,7 +1135,7 @@ Model.prototype._parse = function(data) {
}
}, function(error) {
var newError = new Error("The results could not be converted to instances of `"+self.getTableName()+"`\nDetailed error: "+error.message);

return reject(newError);
});

Expand Down
1 change: 1 addition & 0 deletions lib/type/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ TypeAny.prototype.schema = function() {
TypeAny.prototype.validate = function() {
return this;
}
TypeAny.prototype.export = 'any';

module.exports = TypeAny;
11 changes: 8 additions & 3 deletions lib/type/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ TypeArray.prototype.validate = function(array, prefix, options) {
}
else {
if ((this._min !== -1) && (this._min > array.length)){
throw new Errors.ValidationError("Value for "+prefix+" must have at least "+this._min+" elements.")
throw new Errors.ValidationError("Value for "+prefix+" must have at least "+this._min+" elements.")
}
if ((this._max !== -1) && (this._max < array.length)){
throw new Errors.ValidationError("Value for "+prefix+" must have at most "+this._max+" elements.")
throw new Errors.ValidationError("Value for "+prefix+" must have at most "+this._max+" elements.")
}
if ((this._length !== -1) && (this._length !== array.length)){
throw new Errors.ValidationError("Value for "+prefix+" must be an array with "+this._length+" elements.")
throw new Errors.ValidationError("Value for "+prefix+" must be an array with "+this._length+" elements.")
}

for(var i=0; i<array.length; i++) {
Expand Down Expand Up @@ -165,4 +165,9 @@ TypeArray.prototype._getDefaultFields = function(prefix, defaultFields, virtualF
}


TypeArray.prototype.export = function() {
return '[' + util.result(this._schema.export, this._schema) + ']';
}


module.exports = TypeArray;
3 changes: 3 additions & 0 deletions lib/type/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,7 @@ TypeBoolean.prototype._getDefaultFields = function(prefix, defaultFields, virtua
}


TypeBoolean.prototype.export = 'boolean';


module.exports = TypeBoolean;
3 changes: 3 additions & 0 deletions lib/type/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,7 @@ TypeBuffer.prototype._getDefaultFields = function(prefix, defaultFields, virtual
}


TypeBuffer.prototype.export = 'buffer';


module.exports = TypeBuffer;
7 changes: 5 additions & 2 deletions lib/type/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ TypeDate.prototype.validate = function(date, prefix, options) {
// We check for min/max only if we could create a javascript date from the value
if (jsDate !== undefined) {
if ((this._min instanceof Date) && (this._min > jsDate)){
throw new Errors.ValidationError("Value for "+prefix+" must be after "+this._min+".")
throw new Errors.ValidationError("Value for "+prefix+" must be after "+this._min+".")
}
if ((this._max instanceof Date) && (this._max < jsDate)){
throw new Errors.ValidationError("Value for "+prefix+" must be before "+this._max+".")
throw new Errors.ValidationError("Value for "+prefix+" must be before "+this._max+".")
}
}
}
Expand All @@ -155,4 +155,7 @@ TypeDate.prototype._getDefaultFields = function(prefix, defaultFields, virtualFi
}


TypeDate.prototype.export = 'date';


module.exports = TypeDate;
11 changes: 7 additions & 4 deletions lib/type/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ TypeNumber.prototype.validate = function(number, prefix, options) {
options = util.mergeOptions(this._options, options);

if (util.validateIfUndefined(number, prefix, "number", options)) return;

if ((typeof this._validator === "function") && (this._validator(number) === false)) {
throw new Errors.ValidationError("Validator for the field "+prefix+" returned `false`.");
}
Expand All @@ -122,13 +122,13 @@ TypeNumber.prototype.validate = function(number, prefix, options) {
}
else {
if ((this._min !== -1) && (this._min > number)){
throw new Errors.ValidationError("Value for "+prefix+" must be greater than "+this._min+".")
throw new Errors.ValidationError("Value for "+prefix+" must be greater than "+this._min+".")
}
if ((this._max !== -1) && (this._max < number)){
throw new Errors.ValidationError("Value for "+prefix+" must be less than "+this._max+".")
throw new Errors.ValidationError("Value for "+prefix+" must be less than "+this._max+".")
}
if ((this._integer === true) && (number%1 !== 0)){
throw new Errors.ValidationError("Value for "+prefix+" must be an integer.")
throw new Errors.ValidationError("Value for "+prefix+" must be an integer.")
}
}
}
Expand All @@ -144,4 +144,7 @@ TypeNumber.prototype._getDefaultFields = function(prefix, defaultFields, virtual
}


TypeNumber.prototype.export = 'number';


module.exports = TypeNumber;
15 changes: 15 additions & 0 deletions lib/type/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,20 @@ TypeObject.prototype._getDefaultFields = function(prefix, defaultFields, virtual
}
}

TypeObject.prototype.export = function() {
if (Object.keys(this._schema).length > 0) {
var exported = {};
util.loopKeys(this._schema, function(_schema, key) {
var type = _schema[key];
if (type.export != null) {
exported[key] = util.result(type.export, type);
}
});
return exported;
} else {
return 'object';
}
}


module.exports = TypeObject;
3 changes: 3 additions & 0 deletions lib/type/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,7 @@ TypePoint.prototype._getDefaultFields = function(prefix, defaultFields, virtualF
}
}


TypePoint.prototype.export = 'point';

module.exports = TypePoint;
22 changes: 12 additions & 10 deletions lib/type/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ TypeString.prototype.default = function(fnOrValue) {
/**
* Set a custom validator that will be called with the string. The validator
* should return a boolean whether the field is valid or not.
* @param {function} fn
* @param {function} fn
* @return {TypeString}
*/
TypeString.prototype.validator = function(fn) {
Expand All @@ -269,7 +269,7 @@ TypeString.prototype.validator = function(fn) {
/**
* Set the valid values for this field. The arguments must be strings
* or an array of strings.
* @param {...string|Array.<string>} fn
* @param {...string|Array.<string>} fn
* @return {TypeString}
*/
TypeString.prototype.enum = function() {
Expand Down Expand Up @@ -320,28 +320,28 @@ TypeString.prototype.validate = function(str, prefix, options) {
}
else {
if ((this._min !== -1) && (this._min > str.length)){
throw new Errors.ValidationError("Value for "+prefix+" must be longer than "+this._min+".")
throw new Errors.ValidationError("Value for "+prefix+" must be longer than "+this._min+".")
}
if ((this._max !== -1) && (this._max < str.length)){
throw new Errors.ValidationError("Value for "+prefix+" must be shorter than "+this._max+".")
throw new Errors.ValidationError("Value for "+prefix+" must be shorter than "+this._max+".")
}
if ((this._length !== -1) && (this._length !== str.length)){
throw new Errors.ValidationError("Value for "+prefix+" must be a string with "+this._length+" characters.")
throw new Errors.ValidationError("Value for "+prefix+" must be a string with "+this._length+" characters.")
}
if ((this._regex instanceof RegExp) && (this._regex.test(str) === false)) {
throw new Errors.ValidationError("Value for "+prefix+" must match the regex.")
throw new Errors.ValidationError("Value for "+prefix+" must match the regex.")
}
if ((this._alphanum === true) && (validator.isAlphanumeric(str) === false)) {
throw new Errors.ValidationError("Value for "+prefix+" must be an alphanumeric string.")
throw new Errors.ValidationError("Value for "+prefix+" must be an alphanumeric string.")
}
if ((this._email === true) && (validator.isEmail(str) === false)) {
throw new Errors.ValidationError("Value for "+prefix+" must be a valid email.")
throw new Errors.ValidationError("Value for "+prefix+" must be a valid email.")
}
if ((this._lowercase === true) && (validator.isLowercase(str) === false)) {
throw new Errors.ValidationError("Value for "+prefix+" must be a lowercase string.")
throw new Errors.ValidationError("Value for "+prefix+" must be a lowercase string.")
}
if ((this._uppercase === true) && (validator.isUppercase(str) === false)) {
throw new Errors.ValidationError("Value for "+prefix+" must be a uppercase string.")
throw new Errors.ValidationError("Value for "+prefix+" must be a uppercase string.")
}
if ((this._enum !== undefined) && (this._enum[str] !== true)) {
var validValues = Object.keys(this._enum);
Expand Down Expand Up @@ -386,5 +386,7 @@ TypeString.prototype._getDefaultFields = function(prefix, defaultFields, virtual
return this;
}

TypeString.prototype.export = 'string';


module.exports = TypeString;
11 changes: 10 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ util.tryCatch = tryCatch;
* - doc {Document} the document that triggered the hooks
* - fn {Function} the main function
* - fnArgs {Array} arguments for `fn`
* @return {Promise=}
* @return {Promise=}
*/
function hook(options) {
var preHooks = options.preHooks;
Expand Down Expand Up @@ -331,4 +331,13 @@ function toArray(args) {
}
util.toArray = toArray;

function result(obj, receiver) {
if (typeof obj === 'function') {
return obj.call(receiver);
} else {
return obj;
}
}
util.result = result;

module.exports = util;
Loading