From 6da6521157b3372b8e0a522a63c90df8b48222ba Mon Sep 17 00:00:00 2001 From: Pit Date: Thu, 29 Oct 2015 16:53:11 +0300 Subject: [PATCH 1/2] Boost Processor.castValue() --- lib/processor.js | 52 ++++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/lib/processor.js b/lib/processor.js index 846fe72..b173b37 100644 --- a/lib/processor.js +++ b/lib/processor.js @@ -43,41 +43,33 @@ Processor.prototype.cast = function(table, values) { */ Processor.prototype.castValue = function(table, key, value, attributes) { - - var self = this; - var identity = table; - var attr; - - // Check for a columnName, serialize so we can do any casting - Object.keys(this.schema[identity].attributes).forEach(function(attribute) { - if(self.schema[identity].attributes[attribute].columnName === key) { - attr = attribute; - return; - } - }); - - if(!attr) attr = key; - - // Lookup Schema "Type" - if(!this.schema[identity] || !this.schema[identity].attributes[attr]) return; - var type; - - if(!_.isPlainObject(this.schema[identity].attributes[attr])) { - type = this.schema[identity].attributes[attr]; - } else { - type = this.schema[identity].attributes[attr].type; + var _schema = this.schema[identity]; + if (!_schema) return; + + // Cache mappings of column names to attribute names to boost further castings + if (!_schema._columns) { + _schema._columns = {}; + _.each(_schema.attributes, function(attr, attrName) { + _schema._columns[attr.columnName || attrName] = attrName; + }); } + var attr = _schema._columns[key]; + if (!_schema.attributes[attr]) return; + var type = _.isPlainObject(_schema.attributes[attr]) + ? _schema.attributes[attr].type + : _schema.attributes[attr]; if(!type) return; // Attempt to parse Array - if(type === 'array') { - try { - attributes[key] = JSON.parse(value); - } catch(e) { - return; - } + switch(type) { + case 'array': + try { + attributes[key] = JSON.parse(value); + } catch(e) { + return; + } + break; } - }; From 1aa4bedd731be2a40e589fcba95293a9c9321074 Mon Sep 17 00:00:00 2001 From: anotherpit Date: Thu, 29 Oct 2015 17:59:27 +0300 Subject: [PATCH 2/2] Fix parameter names --- lib/processor.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/processor.js b/lib/processor.js index b173b37..4129b39 100644 --- a/lib/processor.js +++ b/lib/processor.js @@ -36,14 +36,15 @@ Processor.prototype.cast = function(table, values) { /** * Cast a value * + * @param {String} table * @param {String} key - * @param {Object|String|Integer|Array} value - * @param {Object} schema + * @param {Object|String|Number|Array} value + * @param {Object} attributes * @api private */ Processor.prototype.castValue = function(table, key, value, attributes) { - var _schema = this.schema[identity]; + var _schema = this.schema[table]; if (!_schema) return; // Cache mappings of column names to attribute names to boost further castings