From dbd5efed5068ece9765ffb25f2511fc5c44de45a Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Mon, 1 Jul 2013 13:16:51 -0700 Subject: [PATCH] Revert adapter serialization. Remove geo point distance indicator. --- lib/adapters/memory.js | 20 ++++++++++++++++++-- lib/dao.js | 3 ++- lib/geo.js | 1 - lib/list.js | 8 ++++++++ test/datatype.test.js | 1 + test/relations.test.js | 1 + 6 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/adapters/memory.js b/lib/adapters/memory.js index 361bd1c9..1ed68c8f 100644 --- a/lib/adapters/memory.js +++ b/lib/adapters/memory.js @@ -86,9 +86,25 @@ Memory.prototype.destroy = function destroy(model, id, callback) { Memory.prototype.fromDb = function(model, data) { if (!data) return null; data = JSON.parse(data); - var ctor = this._models[model].model; var props = this._models[model].properties; - return ctor(data); + Object.keys(data).forEach(function (key) { + var val = data[key]; + if (typeof val === 'undefined' || val === null) { + return; + } + if (props[key]) { + switch(props[key].type.name) { + case 'Date': + val = new Date(val.toString().replace(/GMT.*$/, 'GMT')); + break; + case 'Boolean': + val = new Boolean(val); + break; + } + } + data[key] = val; + }); + return data; }; Memory.prototype.all = function all(model, filter, callback) { diff --git a/lib/dao.js b/lib/dao.js index a3941c0c..eb779979 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -364,7 +364,8 @@ DataAccessObject.find = function find(params, cb) { this.schema.adapter.all(this.modelName, params, function (err, data) { if (data && data.forEach) { data.forEach(function (d, i) { - var obj = new constr(d); + var obj = new constr; + obj._initProperties(d, false); if (params && params.include && params.collect) { data[i] = obj.__cachedRelations[params.collect]; } else { diff --git a/lib/geo.js b/lib/geo.js index a9f405c8..1972662d 100644 --- a/lib/geo.js +++ b/lib/geo.js @@ -60,7 +60,6 @@ exports.filter = function (arr, filter) { // dont add } else { distances[obj.id] = d; - loc.distance = d; result.push(obj); } }); diff --git a/lib/list.js b/lib/list.js index 5e481ce9..4eebd96a 100644 --- a/lib/list.js +++ b/lib/list.js @@ -14,6 +14,14 @@ function List(data, type, parent) { if (!(list instanceof List)) { return new List(data); } + + if(typeof data === 'string') { + try { + data = JSON.parse(data); + } catch(e) { + throw new Error('could not create List from JSON string: ', data); + } + } if (data && data instanceof List) data = data.items; diff --git a/test/datatype.test.js b/test/datatype.test.js index d0397f44..6fd61e79 100644 --- a/test/datatype.test.js +++ b/test/datatype.test.js @@ -35,6 +35,7 @@ describe('datatypes', function() { }); function testFind(next) { + debugger; Model.findById(id, function(err, m) { should.not.exist(err); should.exist(m); diff --git a/test/relations.test.js b/test/relations.test.js index 3b916dd1..9c35898e 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -196,6 +196,7 @@ describe('relations', function() { Article.create(function(e, article) { article.tags.create({name: 'popular'}, function(e, t) { t.should.be.an.instanceOf(Tag); + console.log(t); ArticleTag.findOne(function(e, at) { should.exist(at); at.tagId.toString().should.equal(t.id.toString());