Fix object/json type mapping

This commit is contained in:
Raymond Feng 2014-05-25 09:46:55 -07:00
parent 649b2f125d
commit 33b86b51b2
3 changed files with 36 additions and 6 deletions

View File

@ -319,7 +319,7 @@ function mixinDiscovery(MySQL) {
} else {
return 'String';
}
break;
case 'VARCHAR':
case 'TINYTEXT':
case 'MEDIUMTEXT':
@ -343,6 +343,7 @@ function mixinDiscovery(MySQL) {
case 'YEAR':
case 'FLOAT':
case 'DOUBLE':
case 'BIGINT':
return 'Number';
case 'DATE':
case 'TIMESTAMP':

View File

@ -304,7 +304,12 @@ MySQL.prototype.toDatabase = function (prop, val) {
if (prop.type.name === 'GeoPoint') {
return val ? 'Point(' + val.lat + ',' + val.lng + ')' : 'NULL';
}
if (typeof prop.type === 'function') return this.client.escape(prop.type(val));
if (prop.type === Object) {
return this.client.escape(val);
}
if (typeof prop.type === 'function') {
return this.client.escape(prop.type(val));
}
return this.client.escape(val.toString());
};
@ -962,8 +967,12 @@ function stringOptionsByType(p, dt) {
switch (dt.toLowerCase()) {
default:
case 'varchar':
var len = p.limit || ((p.type !== String) ? 32768 : 1024);
dt += '(' + len + ')';
break;
case 'char':
dt += '(' + (p.limit || 255) + ')';
len = p.limit || 255;
dt += '(' + len + ')';
break;
case 'text':

View File

@ -49,6 +49,24 @@ describe('MySQL specific datatypes', function () {
});
});
it('should create a model instance with object/json types', function (done) {
var note = {a: 1, b: '2'};
var extras = {c: 3, d: '4'};
var em = EnumModel.create({animal: ANIMAL_ENUM.DOG, condition: 'sleepy',
mood: 'happy', note: note, extras: extras}, function (err, obj) {
assert.ok(!err);
assert.equal(obj.condition, 'sleepy');
EnumModel.findOne({where: {animal: ANIMAL_ENUM.DOG}}, function (err, found) {
assert.ok(!err);
assert.equal(found.mood, 'happy');
assert.equal(found.animal, ANIMAL_ENUM.DOG);
assert.deepEqual(found.note, note);
assert.deepEqual(found.extras, extras);
done();
});
});
});
it('should disconnect when done', function (done) {
db.disconnect();
done()
@ -67,7 +85,9 @@ function setup(done) {
EnumModel = db.define('EnumModel', {
animal: { type: ANIMAL_ENUM, null: false },
condition: { type: db.EnumFactory('hungry', 'sleepy', 'thirsty') },
mood: { type: db.EnumFactory('angry', 'happy', 'sad') }
mood: { type: db.EnumFactory('angry', 'happy', 'sad') },
note: Object,
extras: 'JSON'
});
blankDatabase(db, done);