Comparing version 0.0.4 to 0.0.5
@@ -21,14 +21,14 @@ 'use strict'; | ||
*/ | ||
constructor(schema, options) { | ||
constructor(schema, options = {}) { | ||
if (arguments.length < 1) throw new Error('Schema is missing'); | ||
this._isValidFormatType(schema); | ||
this._checkFormatType(schema); | ||
this._checkOptions(options); | ||
this._options = options; | ||
this._schema = schema; | ||
this._options = options || {}; | ||
this._preHooks = []; | ||
this._postHooks = []; | ||
this.methods = {}; | ||
@@ -38,3 +38,3 @@ } | ||
/** | ||
* _isValidFormatType(): Check this._schema for a properly | ||
* _checkFormatType(): Check this._schema for a properly | ||
* formatting | ||
@@ -44,3 +44,3 @@ * | ||
*/ | ||
_isValidFormatType(schema) { | ||
_checkFormatType(schema) { | ||
Object.keys(schema).forEach(key => { | ||
@@ -53,5 +53,4 @@ if (schema[key].type && !isSupportedType(schema[key].type)) { | ||
if (schema[key].type && Object.keys(schema[key]).length > 1) { | ||
const fieldOptions = ['type', 'required', 'unique', 'default', 'match', 'enum', 'min', 'max', 'maxlength', 'validate']; | ||
Object.keys(schema[key]).forEach(propertyKey => { | ||
if (fieldOptions.indexOf(propertyKey) < 0) { | ||
if (!this._isValidProperty(propertyKey)) { | ||
throw new Error('Unsupported propertie variable: ' + propertyKey); | ||
@@ -64,3 +63,3 @@ } | ||
if (!schema[key].type && isObject(schema[key])) { | ||
return this._isValidFormatType(schema[key]); | ||
return this._checkFormatType(schema[key]); | ||
} | ||
@@ -71,2 +70,21 @@ }); | ||
/** | ||
* _checkOptionss(): Check this._options for a properly | ||
* formatting | ||
* | ||
* @param {Object} options | ||
*/ | ||
_checkOptions(options) { | ||
Object.keys(options).forEach(key => { | ||
if (!this._isValidOption(key)) throw new Error('Unsupported schema option: ' + key); | ||
// Objects inheritOptions | ||
if (key == 'inheritOptions' && isObject(options[key])) { | ||
Object.keys(options[key]).forEach(inheritOptionsKey => { | ||
if (!this._isValidInheritOption(inheritOptionsKey, options[key])) throw new Error('Unsupported schema inherit option: ' + inheritOptionsKey); | ||
}); | ||
} | ||
}); | ||
} | ||
/** | ||
* pre(): Bind pre hook functions to the Schema | ||
@@ -108,4 +126,74 @@ * | ||
} | ||
/** | ||
* _isValidProperty(): Check is the property field is allowed or not | ||
* | ||
* @param {String} property Property name | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
_isValidProperty(property) { | ||
const fieldProperties = ['type', 'required', 'unique', 'default', 'match', 'enum', 'min', 'max', 'maxlength', 'validate']; | ||
return fieldProperties.indexOf(property) > -1; | ||
} | ||
/** | ||
* _isValidOption(): Check is the option field is | ||
* allowed or not | ||
* | ||
* @param {String} option Option name | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
_isValidOption(option) { | ||
const fieldOptions = ['timestamps', 'inheritOptions']; | ||
return fieldOptions.indexOf(option) > -1; | ||
} | ||
/** | ||
* _isValidInheritOption(): Check is the inherit | ||
* options field is allowed or not | ||
* | ||
* @param {String} inheritOption Inherit Option name | ||
* @param {String} value Inherit Option value | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
_isValidInheritOption(inheritOption, value) { | ||
const fieldInheritOptions = ['discriminatorKey', 'merge']; | ||
const mergeOptions = ['methods', 'preHooks', 'postHooks']; | ||
if (fieldInheritOptions.indexOf(inheritOption) < 0) return false; | ||
if (inheritOption === 'merge') { | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = value.merge[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
let m = _step.value; | ||
if (mergeOptions.indexOf(m) < 0) return false; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
module.exports = Schema; |
{ | ||
"name": "moltyjs", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "A tiny ODM for MongoDB with multy tenancy support.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -5,3 +5,3 @@ # What is moltyjs? | ||
# Install | ||
## Install | ||
@@ -12,3 +12,3 @@ ```shell | ||
# Usage | ||
## Usage | ||
@@ -22,3 +22,3 @@ ```javascript | ||
# Connect to a DB | ||
## Connect to a DB | ||
@@ -44,3 +44,3 @@ To connect to a database use the "connect()" function passing trough 'options' payload all the settings required: | ||
# Create a new Schema | ||
## Create a new Schema | ||
@@ -47,0 +47,0 @@ Molty Schema are based on Mongoose Schema structure with some changes on the declaration of the inherit schema options. I even keep some field options name to make the Molty integration as easier as posible in those project are currently running Mongoose. |
@@ -13,14 +13,14 @@ const utils = require('./utils'); | ||
*/ | ||
constructor(schema, options) { | ||
constructor(schema, options = {}) { | ||
if (arguments.length < 1) throw new Error('Schema is missing'); | ||
this._isValidFormatType(schema); | ||
this._checkFormatType(schema); | ||
this._checkOptions(options); | ||
this._options = options; | ||
this._schema = schema; | ||
this._options = options || {}; | ||
this._preHooks = []; | ||
this._postHooks = []; | ||
this.methods = {}; | ||
@@ -30,3 +30,3 @@ } | ||
/** | ||
* _isValidFormatType(): Check this._schema for a properly | ||
* _checkFormatType(): Check this._schema for a properly | ||
* formatting | ||
@@ -36,3 +36,3 @@ * | ||
*/ | ||
_isValidFormatType(schema) { | ||
_checkFormatType(schema) { | ||
Object.keys(schema).forEach(key => { | ||
@@ -45,16 +45,4 @@ if (schema[key].type && !isSupportedType(schema[key].type)) { | ||
if (schema[key].type && Object.keys(schema[key]).length > 1) { | ||
const fieldOptions = [ | ||
'type', | ||
'required', | ||
'unique', | ||
'default', | ||
'match', | ||
'enum', | ||
'min', | ||
'max', | ||
'maxlength', | ||
'validate', | ||
]; | ||
Object.keys(schema[key]).forEach(propertyKey => { | ||
if (fieldOptions.indexOf(propertyKey) < 0) { | ||
if (!this._isValidProperty(propertyKey)) { | ||
throw new Error('Unsupported propertie variable: ' + propertyKey); | ||
@@ -67,3 +55,3 @@ } | ||
if (!schema[key].type && isObject(schema[key])) { | ||
return this._isValidFormatType(schema[key]); | ||
return this._checkFormatType(schema[key]); | ||
} | ||
@@ -74,2 +62,25 @@ }); | ||
/** | ||
* _checkOptionss(): Check this._options for a properly | ||
* formatting | ||
* | ||
* @param {Object} options | ||
*/ | ||
_checkOptions(options) { | ||
Object.keys(options).forEach(key => { | ||
if (!this._isValidOption(key)) | ||
throw new Error('Unsupported schema option: ' + key); | ||
// Objects inheritOptions | ||
if (key == 'inheritOptions' && isObject(options[key])) { | ||
Object.keys(options[key]).forEach(inheritOptionsKey => { | ||
if (!this._isValidInheritOption(inheritOptionsKey, options[key])) | ||
throw new Error( | ||
'Unsupported schema inherit option: ' + inheritOptionsKey, | ||
); | ||
}); | ||
} | ||
}); | ||
} | ||
/** | ||
* pre(): Bind pre hook functions to the Schema | ||
@@ -113,4 +124,64 @@ * | ||
} | ||
/** | ||
* _isValidProperty(): Check is the property field is allowed or not | ||
* | ||
* @param {String} property Property name | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
_isValidProperty(property) { | ||
const fieldProperties = [ | ||
'type', | ||
'required', | ||
'unique', | ||
'default', | ||
'match', | ||
'enum', | ||
'min', | ||
'max', | ||
'maxlength', | ||
'validate', | ||
]; | ||
return fieldProperties.indexOf(property) > -1; | ||
} | ||
/** | ||
* _isValidOption(): Check is the option field is | ||
* allowed or not | ||
* | ||
* @param {String} option Option name | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
_isValidOption(option) { | ||
const fieldOptions = ['timestamps', 'inheritOptions']; | ||
return fieldOptions.indexOf(option) > -1; | ||
} | ||
/** | ||
* _isValidInheritOption(): Check is the inherit | ||
* options field is allowed or not | ||
* | ||
* @param {String} inheritOption Inherit Option name | ||
* @param {String} value Inherit Option value | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
_isValidInheritOption(inheritOption, value) { | ||
const fieldInheritOptions = ['discriminatorKey', 'merge']; | ||
const mergeOptions = ['methods', 'preHooks', 'postHooks']; | ||
if (fieldInheritOptions.indexOf(inheritOption) < 0) return false; | ||
if (inheritOption === 'merge') { | ||
for (let m of value.merge) { | ||
if (mergeOptions.indexOf(m) < 0) return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
module.exports = Schema; |
80227
2239