From dd805eec50211c0431dd0f321db075bda32a69f0 Mon Sep 17 00:00:00 2001 From: Ondrej Brinkel Date: Sat, 27 Apr 2013 15:52:56 +0200 Subject: [PATCH 1/7] added unserialize method to "object" datatype and implemented usage of this in validateAndUpdateFromParams() --- lib/datatypes.js | 20 ++++++++++++++++++++ lib/index.js | 26 +++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/datatypes.js b/lib/datatypes.js index 18b21c7e..7e50f15b 100644 --- a/lib/datatypes.js +++ b/lib/datatypes.js @@ -230,6 +230,26 @@ datatypes = { // FIXME: Does escaping a JSONized object make sense? return _serialize(val, opts); } + , unserialize: function(input, options) { + // unserialize strings only + if (typeof input == 'string') { + // catch exceptions from JSON.parse and return a null object instead + try { + return JSON.parse(input); + } + catch (err) { + return null; + } + } + // if it's already an object we do not need to unserialize + else if (typeof input == 'object') { + return input; + } + // as a fallback return a null object + else { + return null; + } + } } , 'date': { diff --git a/lib/index.js b/lib/index.js index 7b6ae5fd..1051e508 100644 --- a/lib/index.js +++ b/lib/index.js @@ -46,6 +46,7 @@ var util = require('util') // Native Node util module , EventEmitter = require('events').EventEmitter , utils = require('utilities') , adapters = require('./adapters') + , datatypes = require('./datatypes') , query; // Lazy-load query; it depends on model/index utils.mixin(model, new (function () { @@ -755,11 +756,13 @@ utils.mixin(model, new (function () { , name = item.type , type = model.descriptionRegistry[name] , properties = type.properties + , property , validated = null , errs = null , camelizedKey , skip = opts.skip , skipKeys = {} + , datatype , val; item.emit('beforeValidate') @@ -774,7 +777,28 @@ utils.mixin(model, new (function () { for (var p in passedParams) { // Allow leading underscores in the keys for pseudo-privates camelizedKey = utils.string.camelize(p, {leadingUnderscore: true}); - params[camelizedKey] = passedParams[p]; + + // user defined properties might need unserialization + if (typeof properties[camelizedKey] != 'undefined') { + property = properties[camelizedKey]; + + if (typeof datatypes[property.datatype] != 'undefined') { + datatype = datatypes[property.datatype]; + + // unserialize the property if it's datatype has an unserialize method + if (typeof datatype['unserialize'] == 'function') { + val = datatype.unserialize(passedParams[p]); + } else { + val = passedParams[p]; + } + } else { + val = passedParams[p]; + } + } else { + val = passedParams[p]; + } + + params[camelizedKey] = val; } } else { From 77766d7139cd45ef59df68c8e0360863c016947f Mon Sep 17 00:00:00 2001 From: Ondrej Brinkel Date: Tue, 30 Apr 2013 21:42:45 +0200 Subject: [PATCH 2/7] fixed merge conflicts quickfix for missing useUTC flag in datetime.serialize() --- lib/datatypes.js | 5 +++++ lib/index.js | 27 +++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/datatypes.js b/lib/datatypes.js index 7e50f15b..f57d3358 100644 --- a/lib/datatypes.js +++ b/lib/datatypes.js @@ -301,6 +301,11 @@ datatypes = { , serialize: function (input, options) { var val , opts = options || {}; + + if (typeof model.useUTC == 'undefined') { + model.useUTC = true; + } + if (model.useUTC) { val = utils.date.toUTC(input); } diff --git a/lib/index.js b/lib/index.js index 7bbdf111..75259915 100644 --- a/lib/index.js +++ b/lib/index.js @@ -46,7 +46,7 @@ var util = require('util') // Native Node util module , EventEmitter = require('events').EventEmitter , utils = require('utilities') , adapters = require('./adapters') - , Query + , datatypes = require('./datatypes') , query; // Lazy-load query; it depends on model/index utils.mixin(model, new (function () { @@ -756,11 +756,13 @@ utils.mixin(model, new (function () { , name = item.type , type = model.descriptionRegistry[name] , properties = type.properties + , property , validated = null , errs = null , camelizedKey , skip = opts.skip , skipKeys = {} + , datatype , val; item.emit('beforeValidate') @@ -775,7 +777,28 @@ utils.mixin(model, new (function () { for (var p in passedParams) { // Allow leading underscores in the keys for pseudo-privates camelizedKey = utils.string.camelize(p, {leadingUnderscore: true}); - params[camelizedKey] = passedParams[p]; + + // user defined properties might need unserialization + if (typeof properties[camelizedKey] != 'undefined') { + property = properties[camelizedKey]; + + if (typeof datatypes[property.datatype] != 'undefined') { + datatype = datatypes[property.datatype]; + + // unserialize the property if it's datatype has an unserialize method + if (typeof datatype['unserialize'] == 'function') { + val = datatype.unserialize(passedParams[p]); + } else { + val = passedParams[p]; + } + } else { + val = passedParams[p]; + } + } else { + val = passedParams[p]; + } + + params[camelizedKey] = val; } } else { From 2e13872f0d4e7e5c0e98942a47cd479a1222be6f Mon Sep 17 00:00:00 2001 From: Ondrej Brinkel Date: Tue, 30 Apr 2013 21:45:30 +0200 Subject: [PATCH 3/7] fixed more merge conflicts --- lib/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/index.js b/lib/index.js index 75259915..0b2dca93 100644 --- a/lib/index.js +++ b/lib/index.js @@ -46,6 +46,7 @@ var util = require('util') // Native Node util module , EventEmitter = require('events').EventEmitter , utils = require('utilities') , adapters = require('./adapters') + , Query , datatypes = require('./datatypes') , query; // Lazy-load query; it depends on model/index From 65adce26767a8a491050956a2e3339dafb72b2ba Mon Sep 17 00:00:00 2001 From: Ondrej Brinkel Date: Thu, 2 May 2013 08:59:41 +0200 Subject: [PATCH 4/7] fixed circular dependency of datatypes in index.js --- lib/datatypes.js | 5 ----- lib/index.js | 7 ++++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/datatypes.js b/lib/datatypes.js index f57d3358..7e50f15b 100644 --- a/lib/datatypes.js +++ b/lib/datatypes.js @@ -301,11 +301,6 @@ datatypes = { , serialize: function (input, options) { var val , opts = options || {}; - - if (typeof model.useUTC == 'undefined') { - model.useUTC = true; - } - if (model.useUTC) { val = utils.date.toUTC(input); } diff --git a/lib/index.js b/lib/index.js index 0b2dca93..5fe62074 100644 --- a/lib/index.js +++ b/lib/index.js @@ -47,7 +47,6 @@ var util = require('util') // Native Node util module , utils = require('utilities') , adapters = require('./adapters') , Query - , datatypes = require('./datatypes') , query; // Lazy-load query; it depends on model/index utils.mixin(model, new (function () { @@ -766,6 +765,8 @@ utils.mixin(model, new (function () { , datatype , val; + this.datatypes = this.datatypes || require('./datatypes'); + item.emit('beforeValidate') model[name].emit('beforeValidate', item, passedParams); @@ -783,8 +784,8 @@ utils.mixin(model, new (function () { if (typeof properties[camelizedKey] != 'undefined') { property = properties[camelizedKey]; - if (typeof datatypes[property.datatype] != 'undefined') { - datatype = datatypes[property.datatype]; + if (typeof this.datatypes[property.datatype] != 'undefined') { + datatype = this.datatypes[property.datatype]; // unserialize the property if it's datatype has an unserialize method if (typeof datatype['unserialize'] == 'function') { From 16402e34b93231af978f2613335dcdc9cb227f53 Mon Sep 17 00:00:00 2001 From: Ondrej Brinkel Date: Wed, 26 Jun 2013 11:04:56 +0200 Subject: [PATCH 5/7] ugly quick hack to get foreignIds back working --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index e7888095..7c7a6de0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1075,7 +1075,7 @@ model.ModelDefinitionBase = function (name) { // Should probably listen for an event that signals // base models are set up if (!opts.through) { - setTimeout(createForeignKey(assnName), 0); + setTimeout(createForeignKey(assnName), 1000); // FIXME: this is just a quick hack to prevent missing model definitions } }; }); From 64784ddfefe24473ee73045c46bff15fc9b58da0 Mon Sep 17 00:00:00 2001 From: Ondrej Brinkel Date: Wed, 26 Jun 2013 11:36:43 +0200 Subject: [PATCH 6/7] ugly quick hack to get foreignIds back working --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 7c7a6de0..4a7923bf 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1075,7 +1075,7 @@ model.ModelDefinitionBase = function (name) { // Should probably listen for an event that signals // base models are set up if (!opts.through) { - setTimeout(createForeignKey(assnName), 1000); // FIXME: this is just a quick hack to prevent missing model definitions + setTimeout(createForeignKey(assnName), 2000); // FIXME: this is just a quick hack to prevent missing model definitions } }; }); From 899d9aa1b6ce75ce3e41d0374bde250baffe488b Mon Sep 17 00:00:00 2001 From: Ondrej Brinkel Date: Fri, 28 Jun 2013 20:34:07 +0200 Subject: [PATCH 7/7] removed ugly hack as last commit from mde solved the issue --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 01d998d6..f040716e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1076,7 +1076,7 @@ model.ModelDefinitionBase = function (name) { // Should probably listen for an event that signals // base models are set up if (!opts.through) { - setTimeout(createForeignKey(assnName), 2000); // FIXME: this is just a quick hack to prevent missing model definitions + setTimeout(createForeignKey(assnName), 0); } }; });