Permalink
Browse files

Fix dates in memory adapter

  • Loading branch information...
1 parent 9fc4e5e commit 2fd2157d28bfaefa08e7f313285fbdefe8cca7dc @1602 committed Apr 6, 2013
Showing with 26 additions and 2 deletions.
  1. +26 −2 lib/adapters/memory.js
View
@@ -70,7 +70,7 @@ Memory.prototype.exists = function exists(model, id, callback) {
Memory.prototype.find = function find(model, id, callback) {
process.nextTick(function () {
- callback(null, id in this.cache[model] && JSON.parse(this.cache[model][id]));
+ callback(null, id in this.cache[model] && this.fromDb(model, this.cache[model][id]));
}.bind(this));
};
@@ -79,10 +79,34 @@ Memory.prototype.destroy = function destroy(model, id, callback) {
process.nextTick(callback);
};
+Memory.prototype.fromDb = function(model, data) {
+ if (!data) return null;
+ data = JSON.parse(data);
+ var props = this._models[model].properties;
+ 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) {
var self = this;
var nodes = Object.keys(this.cache[model]).map(function (key) {
- return JSON.parse(this.cache[model][key]);
+ return this.fromDb(model, this.cache[model][key]);
}.bind(this));
if (filter) {

0 comments on commit 2fd2157

Please sign in to comment.