Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mvom

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mvom - npm Package Compare versions

Comparing version 0.2.0 to 0.2.1

schemaType/BaseDateType/index.js

5

CHANGELOG.md
# CHANGELOG.md
## 0.2.1
###### _2018-10-22_
- The constant values for queries will now be converted from the external schema format to the internal u2 format.
This affects the date/time types as well as Booleans. @shawnmcknight
## 0.2.0

@@ -3,0 +8,0 @@ ###### _2018-09-18_

2

package.json
{
"name": "mvom",
"author": "STORIS",
"version": "0.2.0",
"version": "0.2.1",
"description": "Multivalue Object Mapper",

@@ -6,0 +6,0 @@ "main": "./index.js",

@@ -31,16 +31,2 @@ 'use strict';

class Query {
/**
* Format a list of conditional expressions
* @function formatConditionList
* @memberof Query
* @static
* @private
* @param {string} dictionaryId - Multivalue dictionary to use in expression
* @param {string} operator - Relational operator to use in expression
* @param {string[]} valueList - Array of constant values to use in expressions
* @param {string} joinString - String to join conditional expressions with
* @returns {string} Formatted group of joined conditional expressions
* @throws {InvalidParameterError} An invalid parameter was passed to the function
*/
constructor(Model, selectionCriteria = {}, options = {}) {

@@ -109,29 +95,2 @@ _initialiseProps.call(this);

/**
* Format a constant for use in queries
* @function formatConstant
* @memberof Query
* @static
* @private
* @param {string} constant - Constant value to format
* @returns {string} Constant value enclosed in appropriate quotation marks
* @throws {Error} Passed constant parameter contains both single and double quotes
*/
/* static methods */
/**
* Format a conditional expression
* @function formatCondition
* @memberof Query
* @static
* @private
* @param {string} dictionaryId - Multivalue dictionary to use in expression
* @param {string} operator - Relational operator to use in expression
* @param {string} value - Constant value to use in expression
* @returns {string} Formatted conditional expression
* @throws {Error} (indirect) Passed constant parameter contains both single and double quotes
*/
/**
* Set the limit value for query

@@ -185,2 +144,44 @@ * @function limit

/**
* Format a conditional expression
* @function _formatCondition
* @memberof Query
* @instance
* @private
* @param {string} property - String keypath of property
* @param {string} operator - Relational operator to use in expression
* @param {string} value - Constant value to use in expression
* @returns {string} Formatted conditional expression
* @throws {Error} (indirect) Passed constant parameter contains both single and double quotes
*/
/**
* Format a list of conditional expressions
* @function _formatConditionList
* @memberof Query
* @instance
* @private
* @param {string} property - String keypath of property
* @param {string} operator - Relational operator to use in expression
* @param {string[]} valueList - Array of constant values to use in expressions
* @param {string} joinString - String to join conditional expressions with
* @returns {string} Formatted group of joined conditional expressions
* @throws {InvalidParameterError} An invalid parameter was passed to the function
*/
/**
* Format a constant for use in queries
* @function _formatConstant
* @memberof Query
* @instance
* @private
* @param {string} property - String keypath of property
* @param {string} constant - Constant value to format
* @returns {string} Constant value enclosed in appropriate quotation marks
* @throws {Error} Passed constant parameter contains both single and double quotes
*/
/**
* Format the selection criteria object into a string to use in multivalue query

@@ -225,2 +226,13 @@ * @function _formatSelectionCriteria

/**
* Get the function to convert query constant to internal u2 format (if applicable)
* @function _getQueryTransformer
* @memberof Query
* @instance
* @private
* @param {string} property - String keypath of property
* @returns {Function|undefined} Function to execute to convert query constant to internal u2 format
*/
/**
* Set options passed via constructor

@@ -239,30 +251,2 @@ * @function _setOptions

Query.formatCondition = (dictionaryId, operator, value) => `${dictionaryId} ${operator} ${Query.formatConstant(value)}`;
Query.formatConditionList = (dictionaryId, operator, valueList, joinString) => {
if (!Array.isArray(valueList)) {
throw new _InvalidParameter2.default({ parameterName: 'valueList' });
}
const conditionList = valueList.map(value => Query.formatCondition(dictionaryId, operator, value));
return conditionList.length === 1 ? conditionList[0] : `(${conditionList.join(` ${joinString} `)})`;
};
Query.formatConstant = constant => {
if (constant.includes(`'`) && constant.includes(`"`)) {
// cannot query if string has both single and double quotes in it
throw new Error('Query constants cannot contain both single and double quotes');
}
let quoteCharacter;
if (constant.includes(`"`)) {
quoteCharacter = `'`;
} else {
quoteCharacter = `"`;
}
return `${quoteCharacter}${constant}${quoteCharacter}`;
};
var _initialiseProps = function () {

@@ -319,2 +303,39 @@ this.exec = async () => {

this._formatCondition = (property, operator, value) => {
const dictionaryId = this._getDictionaryId(property);
return `${dictionaryId} ${operator} ${this._formatConstant(property, value)}`;
};
this._formatConditionList = (property, operator, valueList, joinString) => {
if (!Array.isArray(valueList)) {
throw new _InvalidParameter2.default({ parameterName: 'valueList' });
}
const conditionList = valueList.map(value => this._formatCondition(property, operator, value));
return conditionList.length === 1 ? conditionList[0] : `(${conditionList.join(` ${joinString} `)})`;
};
this._formatConstant = (property, constant) => {
let constantToFormat = constant;
const queryTransformer = this._getQueryTransformer(property);
if (typeof queryTransformer === 'function') {
constantToFormat = queryTransformer(constantToFormat);
}
if (constantToFormat.includes(`'`) && constantToFormat.includes(`"`)) {
// cannot query if string has both single and double quotes in it
throw new Error('Query constants cannot contain both single and double quotes');
}
let quoteCharacter;
if (constantToFormat.includes(`"`)) {
quoteCharacter = `'`;
} else {
quoteCharacter = `"`;
}
return `${quoteCharacter}${constantToFormat}${quoteCharacter}`;
};
this._formatSelectionCriteria = (criteria = {}) => {

@@ -340,7 +361,5 @@ const criteriaProperties = Object.keys(criteria);

const dictionaryId = this._getDictionaryId(queryProperty);
if (Array.isArray(queryValue)) {
// assume $in operator if queryValue is an array
return Query.formatConditionList(dictionaryId, '=', queryValue, 'or');
return this._formatConditionList(queryProperty, '=', queryValue, 'or');
}

@@ -350,3 +369,3 @@

// assume equality if queryValue is not an object
return Query.formatCondition(dictionaryId, '=', queryValue);
return this._formatCondition(queryProperty, '=', queryValue);
}

@@ -358,23 +377,23 @@

case '$eq':
return Query.formatCondition(dictionaryId, '=', mvValue);
return this._formatCondition(queryProperty, '=', mvValue);
case '$gt':
return Query.formatCondition(dictionaryId, '>', mvValue);
return this._formatCondition(queryProperty, '>', mvValue);
case '$gte':
return Query.formatCondition(dictionaryId, '>=', mvValue);
return this._formatCondition(queryProperty, '>=', mvValue);
case '$lt':
return Query.formatCondition(dictionaryId, '<', mvValue);
return this._formatCondition(queryProperty, '<', mvValue);
case '$lte':
return Query.formatCondition(dictionaryId, '<=', mvValue);
return this._formatCondition(queryProperty, '<=', mvValue);
case '$ne':
return Query.formatCondition(dictionaryId, '#', mvValue);
return this._formatCondition(queryProperty, '#', mvValue);
case '$contains':
return Query.formatCondition(dictionaryId, 'like', `...${mvValue}...`);
return this._formatCondition(queryProperty, 'like', `...${mvValue}...`);
case '$startsWith':
return Query.formatCondition(dictionaryId, 'like', `${mvValue}...`);
return this._formatCondition(queryProperty, 'like', `${mvValue}...`);
case '$endsWith':
return Query.formatCondition(dictionaryId, 'like', `...${mvValue}`);
return this._formatCondition(queryProperty, 'like', `...${mvValue}`);
case '$in':
return Query.formatConditionList(dictionaryId, '=', mvValue, 'or');
return this._formatConditionList(queryProperty, '=', mvValue, 'or');
case '$nin':
return Query.formatConditionList(dictionaryId, '#', mvValue, 'and');
return this._formatConditionList(queryProperty, '#', mvValue, 'and');
default:

@@ -423,2 +442,4 @@ // unknown operator

this._getQueryTransformer = property => this._Model.schema.paths[property] && this._Model.schema.paths[property].transformToQuery;
this._setOptions = ({ limit, skip, sort } = {}) => {

@@ -425,0 +446,0 @@ this.limit(limit);

@@ -19,14 +19,6 @@ 'use strict';

var _ComplexType = require('../ComplexType');
var _BasePrimitiveArrayType = require('../BasePrimitiveArrayType');
var _ComplexType2 = _interopRequireDefault(_ComplexType);
var _BasePrimitiveArrayType2 = _interopRequireDefault(_BasePrimitiveArrayType);
var _SimpleType = require('../SimpleType');
var _SimpleType2 = _interopRequireDefault(_SimpleType);
var _InvalidParameter = require('../../Errors/InvalidParameter');
var _InvalidParameter2 = _interopRequireDefault(_InvalidParameter);
var _handleRequiredValidation = require('../../shared/handleRequiredValidation');

@@ -40,22 +32,12 @@

* An Array Schema Type
* @extends ComplexType
* @param {SimpleType} valueSchemaType - A schemaType representing the type of the array's contents
* @throws {InvalidParameterError} An invalid parameter was passed to the function
* @extends BasePrimitiveArrayType
*/
class ArrayType extends _ComplexType2.default {
constructor(valueSchemaType) {
if (!(valueSchemaType instanceof _SimpleType2.default)) {
// array values must be a child of SimpleType class
throw new _InvalidParameter2.default({ parameterName: 'valueSchemaType' });
}
super();
class ArrayType extends _BasePrimitiveArrayType2.default {
constructor(...args) {
var _temp;
this.get = record => {
return _temp = super(...args), this.get = record => {
const value = this._valueSchemaType.getFromMvData(record);
return typeof value === 'undefined' ? [] : (0, _castArray2.default)(value).map(itemValue => this._valueSchemaType.transformFromDb(itemValue));
};
this.set = (originalRecord, setValue) => this._valueSchemaType.setIntoMvData(originalRecord, (0, _castArray2.default)(setValue).map(value => this._valueSchemaType.transformToDb(value)));
this.validate = async (value, document) => {
}, this.set = (originalRecord, setValue) => this._valueSchemaType.setIntoMvData(originalRecord, (0, _castArray2.default)(setValue).map(value => this._valueSchemaType.transformToDb(value))), this.validate = async (value, document) => {
const castValue = (0, _castArray2.default)(value);

@@ -69,26 +51,4 @@

return (0, _compact2.default)((0, _flatten2.default)((await Promise.all(this._validators.concat((0, _handleRequiredValidation2.default)(this._required, this._validateRequired)).map(async ({ validator, message }) => !(await validator(castValue, document)) && message).concat(castValue.map(async arrayItem => this._valueSchemaType.validate(arrayItem, document)))))));
};
this._validateRequired = async value => value.length > 0;
const { required = false } = valueSchemaType.definition;
/**
* A schemaType representing the type of the array's contents
* @member {SimpleType} _valueSchemaType
* @memberof ArrayType
* @instance
* @private
*/
this._valueSchemaType = valueSchemaType;
/**
* Required validation value for the array
* @member {Boolean|Function} _required
* @memberof ArrayType
* @instance
* @private
*/
this._required = required;
}, _temp;
}
/* public instance methods */

@@ -101,2 +61,3 @@

* @instance
* @override
* @param {*[]} record - Data to get values from

@@ -113,2 +74,3 @@ * @returns {*[]} Array of formatted data values

* @instance
* @override
* @param {*[]} originalRecord - Record structure to use as basis for applied changes

@@ -125,2 +87,3 @@ * @param {*[]} setValue - Array to set into record

* @instance
* @override
* @async

@@ -133,16 +96,4 @@ * @param {*[]} value - Array to validate

/* private instance methods */
/**
* Array required validator
* @function _validateRequired
* @memberof ArrayType
* @instance
* @private
* @async
* @param {*[]} value - Array to validate
* @returns {Promise.<Boolean>} True if valid / false if invalid
*/
}
exports.default = ArrayType;

@@ -36,2 +36,12 @@ 'use strict';

this.transformToDb = value => value ? '1' : '0';
this.transformToQuery = value => {
if ([true, 'true', 'TRUE'].includes(value)) {
return '1';
}
if ([false, 'false', 'FALSE'].includes(value)) {
return '0';
}
return value;
};
}

@@ -63,4 +73,16 @@

*/
/**
* Transform query constants to u2 formatted Boolean
* @function transformToQuery
* @memberof SimpleType
* @instance
* @public
* @override
* @param {Boolean|string|*} value - Value to convert
* @returns {'1'|'0'|*} Returns "1" if boolean or string true, "0" if boolean or string false, and original value if anything else
*/
}
exports.default = BooleanType;

@@ -11,2 +11,6 @@ 'use strict';

var _BaseDateType = require('../BaseDateType');
var _BaseDateType2 = _interopRequireDefault(_BaseDateType);
var _ISOCalendarDateType = require('../ISOCalendarDateType');

@@ -20,6 +24,2 @@

var _SimpleType = require('../SimpleType');
var _SimpleType2 = _interopRequireDefault(_SimpleType);
var _InvalidParameter = require('../../Errors/InvalidParameter');

@@ -37,3 +37,3 @@

* An ISOCalendarDateTime Schema Type
* @extends SimpleType
* @extends BaseDateType
* @param {Object} definition - Data definition

@@ -44,3 +44,3 @@ * @param {string} definition.path - 1-indexed String path

*/
class ISOCalendarDateTimeType extends _SimpleType2.default {
class ISOCalendarDateTimeType extends _BaseDateType2.default {
constructor(definition) {

@@ -47,0 +47,0 @@ if (definition.path == null) {

@@ -11,5 +11,5 @@ 'use strict';

var _SimpleType = require('../SimpleType');
var _BaseDateType = require('../BaseDateType');
var _SimpleType2 = _interopRequireDefault(_SimpleType);
var _BaseDateType2 = _interopRequireDefault(_BaseDateType);

@@ -30,7 +30,7 @@ var _TransformData = require('../../Errors/TransformData');

* An ISOCalendarDate Schema Type
* @extends SimpleType
* @extends BaseDateType
* @param {Object} definition - Data definition
* @param {string} [definition.path = null] - 1-indexed String path
*/
class ISOCalendarDateType extends _SimpleType2.default {
class ISOCalendarDateType extends _BaseDateType2.default {
/* static properties */

@@ -37,0 +37,0 @@

@@ -29,3 +29,3 @@ 'use strict';

* An ISOTime Schema Type
* @extends SimpleType
* @extends BaseDateType
* @param {Object} definition - Data definition

@@ -32,0 +32,0 @@ * @param {string} [definition.path = null] - 1-indexed String path

@@ -19,14 +19,6 @@ 'use strict';

var _ComplexType = require('../ComplexType');
var _BasePrimitiveArrayType = require('../BasePrimitiveArrayType');
var _ComplexType2 = _interopRequireDefault(_ComplexType);
var _BasePrimitiveArrayType2 = _interopRequireDefault(_BasePrimitiveArrayType);
var _SimpleType = require('../SimpleType');
var _SimpleType2 = _interopRequireDefault(_SimpleType);
var _InvalidParameter = require('../../Errors/InvalidParameter');
var _InvalidParameter2 = _interopRequireDefault(_InvalidParameter);
var _handleRequiredValidation = require('../../shared/handleRequiredValidation');

@@ -40,22 +32,12 @@

* A Nested Array Schema Type
* @extends ComplexType
* @param {SimpleType} valueSchemaType - A schemaType representing the type of the child array's contents
* @throws {InvalidParameterError} An invalid parameter was passed to the function
* @extends BasePrimitiveArrayType
*/
class NestedArrayType extends _ComplexType2.default {
constructor(valueSchemaType) {
if (!(valueSchemaType instanceof _SimpleType2.default)) {
// array values must be a child of SimpleType class
throw new _InvalidParameter2.default({ parameterName: 'valueSchemaType' });
}
super();
class NestedArrayType extends _BasePrimitiveArrayType2.default {
constructor(...args) {
var _temp;
this.get = record => {
return _temp = super(...args), this.get = record => {
const value = this._valueSchemaType.getFromMvData(record);
return typeof value === 'undefined' ? [] : (0, _castArray2.default)(value).map(itemValue => (0, _castArray2.default)(itemValue).map(nestedValue => this._valueSchemaType.transformFromDb(nestedValue)));
};
this.set = (originalRecord, setValue) => this._valueSchemaType.setIntoMvData(originalRecord, (0, _castArray2.default)(setValue).map(value => (0, _castArray2.default)(value).map(nestedValue => this._valueSchemaType.transformToDb(nestedValue))));
this.validate = async (value, document) => {
}, this.set = (originalRecord, setValue) => this._valueSchemaType.setIntoMvData(originalRecord, (0, _castArray2.default)(setValue).map(value => (0, _castArray2.default)(value).map(nestedValue => this._valueSchemaType.transformToDb(nestedValue)))), this.validate = async (value, document) => {
const castValue = (0, _castArray2.default)(value);

@@ -70,26 +52,4 @@

return (0, _compact2.default)((0, _flatten2.default)((await Promise.all(this._validators.concat((0, _handleRequiredValidation2.default)(this._required, this._validateRequired)).map(async ({ validator, message }) => !(await validator(castValue, document)) && message).concat((0, _flatten2.default)(castValue).map(async arrayItem => this._valueSchemaType.validate(arrayItem, document)))))));
};
this._validateRequired = async value => value.length > 0;
const { required = false } = valueSchemaType.definition;
/**
* A schemaType representing the type of the child array's contents
* @member {SimpleType} _valueSchemaType
* @memberof NestedArrayType
* @instance
* @private
*/
this._valueSchemaType = valueSchemaType;
/**
* Required validation value for the array
* @member {Boolean|Function} _required
* @memberof NestedArrayType
* @instance
* @private
*/
this._required = required;
}, _temp;
}
/**

@@ -100,2 +60,3 @@ * Get value from mv data

* @instance
* @override
* @param {*[]} record - Data to get values from

@@ -112,2 +73,3 @@ * @returns {Array.<Array.<*>>} Nested array of formatted data values

* @instance
* @override
* @param {*[]} originalRecord - Record structure to use as basis for applied changes

@@ -124,2 +86,3 @@ * @param {Array.<Array.<*>>} setValue - Nested array to set into record

* @instance
* @override
* @async

@@ -132,16 +95,4 @@ * @param {Array.<Array.<*>>} value - Nested array to validate

/* private instance methods */
/**
* Nested array required validator
* @function _validateRequired
* @memberof NestedArrayType
* @instance
* @private
* @async
* @param {Array.<Array.<*>>} value - Nested array to validate
* @returns {Promise.<Boolean>} True if valid / false if invalid
*/
}
exports.default = NestedArrayType;

@@ -173,2 +173,13 @@ 'use strict';

/**
* Transform query constants to the format schema
* @function transformToQuery
* @memberof SimpleType
* @instance
* @public
* @param {*} value - Value to convert
* @returns {*} No transformation - returns original input value
*/
/**
* Validate the simple type

@@ -242,2 +253,4 @@ * @function validate

this.transformToQuery = value => value;
this.validate = async (value, document) =>

@@ -244,0 +257,0 @@ // combining all the validation into one array of promise.all

@@ -15,2 +15,6 @@ 'use strict';

var _handleEnumValidation = require('./handleEnumValidation');
var _handleEnumValidation2 = _interopRequireDefault(_handleEnumValidation);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -23,2 +27,3 @@

* @param {string} definition.path - 1-indexed String path
* @param {string[]} [definition.enum] - Array of allowed values
* @throws {InvalidParameterError} An invalid parameter was passed to the function

@@ -31,4 +36,16 @@ */

}
if (definition.enum != null && !Array.isArray(definition.enum)) {
throw new _InvalidParameter2.default({ parameterName: 'definition.enum' });
}
super(definition);
/**
* Array of allowed enumerations
* @member {string[]|null} _enum
* @memberof StringType
* @instance
* @private
*/
this.transformFromDb = value => value == null ? null : String(value);

@@ -38,3 +55,10 @@

this._validateEnum = async value => this._enum == null || this._enum.includes(value);
this._validateRequired = async value => value != null && value !== '';
this._enum = definition.enum || null;
// add validators for this type
this._validators.unshift((0, _handleEnumValidation2.default)(this._validateEnum));
}

@@ -67,2 +91,14 @@

/**
* Enum validator
* @function _validateEnum
* @memberof StringType
* @instance
* @private
* @async
* @param {string} value - String to validate
* @returns {Promise.<Boolean>} True if valid / false if invalid
*/
/**
* String required validator

@@ -69,0 +105,0 @@ * @function _validateRequired

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc