Recursively cast props on fromDb for memory conn.
In the fromDb step, nested properties weren't being hydrated properly. This caused an issue with the ability to search on such properties as Dates. The properties would be hydrated as a String type, and as such, it was impossible to properly query on them.
This commit is contained in:
parent
8ebccb65d1
commit
3df1826730
|
@ -351,28 +351,46 @@ Memory.prototype.fromDb = function(model, data) {
|
||||||
data = deserialize(data);
|
data = deserialize(data);
|
||||||
var props = this._models[model].properties;
|
var props = this._models[model].properties;
|
||||||
for (var key in data) {
|
for (var key in data) {
|
||||||
var val = data[key];
|
data[key] = this._castPropertyValue(key, data[key], props);
|
||||||
if (val === undefined || val === null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (props[key]) {
|
|
||||||
switch (props[key].type.name) {
|
|
||||||
case 'Date':
|
|
||||||
val = new Date(val.toString().replace(/GMT.*$/, 'GMT'));
|
|
||||||
break;
|
|
||||||
case 'Boolean':
|
|
||||||
val = Boolean(val);
|
|
||||||
break;
|
|
||||||
case 'Number':
|
|
||||||
val = Number(val);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
data[key] = val;
|
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Memory.prototype._castPropertyValue = function(prop, val, props) {
|
||||||
|
var self = this;
|
||||||
|
if (val === undefined || val === null || !props[prop]) {
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(val)) {
|
||||||
|
return val.map(function(val) {
|
||||||
|
return self._castPropertyValue(prop, val, props);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var isArray = Array.isArray(props[prop].type);
|
||||||
|
var propType = isArray ? props[prop].type[0] : props[prop].type;
|
||||||
|
|
||||||
|
switch (propType.name) {
|
||||||
|
case 'Date':
|
||||||
|
val = new Date(val.toString().replace(/GMT.*$/, 'GMT'));
|
||||||
|
break;
|
||||||
|
case 'Boolean':
|
||||||
|
val = Boolean(val);
|
||||||
|
break;
|
||||||
|
case 'Number':
|
||||||
|
val = Number(val);
|
||||||
|
break;
|
||||||
|
case 'ModelConstructor':
|
||||||
|
for (var subProp in val) {
|
||||||
|
val[subProp] = this._castPropertyValue(subProp, val[subProp], propType.definition.properties);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return val;
|
||||||
|
};
|
||||||
|
|
||||||
function getValue(obj, path) {
|
function getValue(obj, path) {
|
||||||
if (obj == null) {
|
if (obj == null) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|
|
@ -157,6 +157,7 @@ describe('Memory connector', function() {
|
||||||
city: String,
|
city: String,
|
||||||
state: String,
|
state: String,
|
||||||
zipCode: String,
|
zipCode: String,
|
||||||
|
since: Date,
|
||||||
tags: [
|
tags: [
|
||||||
{
|
{
|
||||||
tag: String,
|
tag: String,
|
||||||
|
@ -166,6 +167,7 @@ describe('Memory connector', function() {
|
||||||
friends: [
|
friends: [
|
||||||
{
|
{
|
||||||
name: String,
|
name: String,
|
||||||
|
since: Date,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
@ -294,8 +296,7 @@ describe('Memory connector', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should successfully extract 2 users using implied and & and',
|
it('should successfully extract 2 users using implied and & and', function(done) {
|
||||||
function(done) {
|
|
||||||
User.find({
|
User.find({
|
||||||
where: {
|
where: {
|
||||||
name: 'John Lennon',
|
name: 'John Lennon',
|
||||||
|
@ -435,15 +436,14 @@ describe('Memory connector', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work when a regex is provided without the regexp operator',
|
it('should work when a regex is provided without the regexp operator', function(done) {
|
||||||
function(done) {
|
User.find({where: {name: /John.*/i}}, function(err, users) {
|
||||||
User.find({where: {name: /John.*/i}}, function(err, users) {
|
should.not.exist(err);
|
||||||
should.not.exist(err);
|
users.length.should.equal(1);
|
||||||
users.length.should.equal(1);
|
users[0].name.should.equal('John Lennon');
|
||||||
users[0].name.should.equal('John Lennon');
|
done();
|
||||||
done();
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
it('should support the regexp operator with regex strings', function(done) {
|
it('should support the regexp operator with regex strings', function(done) {
|
||||||
User.find({where: {name: {regexp: '^J'}}}, function(err, users) {
|
User.find({where: {name: {regexp: '^J'}}}, function(err, users) {
|
||||||
|
@ -516,6 +516,28 @@ describe('Memory connector', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should support date as nested property in query', function(done) {
|
||||||
|
var d = new Date('2017-01-01');
|
||||||
|
User.find({where: {'address.since': d}},
|
||||||
|
function(err, users) {
|
||||||
|
should.not.exist(err);
|
||||||
|
users.length.should.be.equal(1);
|
||||||
|
users[0].address.since.should.be.eql(d);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support date as array property in query', function(done) {
|
||||||
|
var d = new Date('1960-01-01');
|
||||||
|
User.find({where: {'friends.since': d}},
|
||||||
|
function(err, users) {
|
||||||
|
should.not.exist(err);
|
||||||
|
users.length.should.be.equal(2);
|
||||||
|
users[0].friends[0].since.should.be.eql(d);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should deserialize values after saving in upsert', function(done) {
|
it('should deserialize values after saving in upsert', function(done) {
|
||||||
User.findOne({where: {seq: 1}}, function(err, paul) {
|
User.findOne({where: {seq: 1}}, function(err, paul) {
|
||||||
User.updateOrCreate({id: paul.id, name: 'Sir Paul McCartney'},
|
User.updateOrCreate({id: paul.id, name: 'Sir Paul McCartney'},
|
||||||
|
@ -553,15 +575,16 @@ describe('Memory connector', function() {
|
||||||
city: 'San Jose',
|
city: 'San Jose',
|
||||||
state: 'CA',
|
state: 'CA',
|
||||||
zipCode: '95131',
|
zipCode: '95131',
|
||||||
|
since: new Date('2017-01-01'),
|
||||||
tags: [
|
tags: [
|
||||||
{tag: 'business'},
|
{tag: 'business'},
|
||||||
{tag: 'rent'},
|
{tag: 'rent'},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
friends: [
|
friends: [
|
||||||
{name: 'Paul McCartney'},
|
{name: 'Paul McCartney', since: new Date('1960-01-01')},
|
||||||
{name: 'George Harrison'},
|
{name: 'George Harrison', since: new Date('1960-01-01')},
|
||||||
{name: 'Ringo Starr'},
|
{name: 'Ringo Starr', since: new Date('1960-01-01')},
|
||||||
],
|
],
|
||||||
children: ['Sean', 'Julian'],
|
children: ['Sean', 'Julian'],
|
||||||
},
|
},
|
||||||
|
@ -578,11 +601,12 @@ describe('Memory connector', function() {
|
||||||
city: 'San Mateo',
|
city: 'San Mateo',
|
||||||
state: 'CA',
|
state: 'CA',
|
||||||
zipCode: '94065',
|
zipCode: '94065',
|
||||||
|
since: new Date('2017-02-01'),
|
||||||
},
|
},
|
||||||
friends: [
|
friends: [
|
||||||
{name: 'John Lennon'},
|
{name: 'John Lennon', since: new Date('1960-01-01')},
|
||||||
{name: 'George Harrison'},
|
{name: 'George Harrison', since: new Date('1960-01-01')},
|
||||||
{name: 'Ringo Starr'},
|
{name: 'Ringo Starr', since: new Date('1960-01-01')},
|
||||||
],
|
],
|
||||||
children: ['Stella', 'Mary', 'Heather', 'Beatrice', 'James'],
|
children: ['Stella', 'Mary', 'Heather', 'Beatrice', 'James'],
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue