From 5542839c3e8a744e4500f43b811bfcbfe3bf1aad Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 6 Jun 2014 09:38:36 -0700 Subject: [PATCH 1/4] Map object/json to TEXT See https://github.com/strongloop/loopback-connector-mysql/issues/36 --- lib/mysql.js | 7 +++++-- test/mysql.test.js | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index c298b4f..6d3d219 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -980,10 +980,12 @@ function datatype(p) { switch (p.type.name) { default: case 'String': - case 'JSON': dt = columnType(p, 'VARCHAR'); dt = stringOptionsByType(p, dt); break; + case 'JSON': + case 'Object': + case 'Any': case 'Text': dt = columnType(p, 'TEXT'); dt = stringOptionsByType(p, dt); @@ -1023,7 +1025,8 @@ function stringOptionsByType(p, dt) { default: case 'varchar': // The maximum length for an ID column is 1000 bytes - var len = p.limit || ((p.type !== String) ? 32768 : p.id ? 255: 1024); + // The maximum row size is 64K + var len = p.limit || ((p.type !== String) ? 8192 : p.id ? 255: 1024); dt += '(' + len + ')'; break; case 'char': diff --git a/test/mysql.test.js b/test/mysql.test.js index e81f97b..b5c4e03 100644 --- a/test/mysql.test.js +++ b/test/mysql.test.js @@ -10,6 +10,8 @@ describe('mysql', function () { Post = db.define('PostWithDefaultId', { title: { type: String, length: 255, index: true }, content: { type: String }, + comments: [String], + history: Object, stars: Number }); @@ -33,6 +35,26 @@ describe('mysql', function () { }); }); + it('should allow array or object', function (done) { + Post.create({title: 'a', content: 'AAA', comments: ['1', '2'], + history: {a: 1, b: 'b'}}, function (err, post) { + + should.not.exist(err); + + Post.findById(post.id, function (err, p) { + p.id.should.be.equal(post.id); + + p.content.should.be.equal(post.content); + p.title.should.be.equal('a'); + p.comments.should.eql(['1', '2']); + p.history.should.eql({a: 1, b: 'b'}); + + done(); + }); + }); + + }); + it('updateOrCreate should update the instance', function (done) { Post.create({title: 'a', content: 'AAA'}, function (err, post) { post.title = 'b'; From 99b2950dab2752c6c860ef917b956aa13c6ea934 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 10 Jun 2014 16:06:16 -0700 Subject: [PATCH 2/4] Fix comparison for null and boolean values --- lib/mysql.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/mysql.js b/lib/mysql.js index 6d3d219..2a48299 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -306,6 +306,7 @@ MySQL.prototype.toDatabase = function (prop, val) { return this.toDatabase(prop, val); } } + return this.toDatabase(prop, val); } if (!prop) { return this.client.escape(val); From e52ae4b262fdcf0dee67ce12057df95285792d9b Mon Sep 17 00:00:00 2001 From: Johnny Bill Date: Mon, 16 Jun 2014 12:32:29 +0200 Subject: [PATCH 3/4] cannot read property of undefined fixed fields seems to be undefined sometimes. --- lib/mysql.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mysql.js b/lib/mysql.js index 2a48299..9debd5e 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -635,7 +635,7 @@ MySQL.prototype.autoupdate = function (models, cb) { wait++; self.query('SHOW FIELDS FROM ' + self.tableEscaped(model), function (err, fields) { self.query('SHOW INDEXES FROM ' + self.tableEscaped(model), function (err, indexes) { - if (!err && fields.length) { + if (!err && fields && fields.length) { self.alterTable(model, fields, indexes, done); } else { self.createTable(model, done); From 7c16481f9e4f93bdd5161e9f7ec7d544c4c4491a Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 23 Jun 2014 10:24:06 -0700 Subject: [PATCH 4/4] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b4cc7d8..b17917f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-connector-mysql", - "version": "1.3.0", + "version": "1.4.0", "description": "MySQL connector for loopback-datasource-juggler", "main": "index.js", "scripts": {