From 730a6f9f27d62d92530b8807c73ecba1892182a9 Mon Sep 17 00:00:00 2001 From: lizheming Date: Sun, 6 Sep 2020 13:15:49 +0800 Subject: [PATCH 1/4] feat: stringify data when column type is json --- lib/schema.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/schema.js b/lib/schema.js index 55e1036..206732e 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -88,6 +88,9 @@ module.exports = class MysqlSchema extends Schema { if (tinyType.indexOf('int') > -1) return parseInt(value, 10) || 0; if (['double', 'float', 'decimal'].indexOf(tinyType) > -1) return parseFloat(value, 10) || 0; if (tinyType === 'bool') return value ? 1 : 0; + if (this.config.jsonFormat && tinyType === 'json') { + return JSON.stringify(value); + } return value; } }; From e5fb089f3cb0237f5ddbb776aee034cb852f06a4 Mon Sep 17 00:00:00 2001 From: lizheming Date: Mon, 7 Sep 2020 20:43:42 +0800 Subject: [PATCH 2/4] feat: add schema json parse logic --- lib/query.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/query.js b/lib/query.js index f359cce..4f653d3 100644 --- a/lib/query.js +++ b/lib/query.js @@ -5,6 +5,25 @@ const Mysql = require('think-mysql'); * mysql query */ module.exports = class MysqlQuery extends Query { + select(options, cache) { + if(!this.config.jsonFormat) { + return super.select(options, cache); + } + + return Promise.all([ + super.select(options, cache), + this.schema.getSchema() + ]).then(([data, schema]) => { + const keys = Object.keys(schema).filter(key => schema[key].tinyType === 'json'); + (Array.isArray(data) ? data : [data]).forEach(row => { + keys.filter(key => row[key] !== undefined).forEach(key => { + row[key] = JSON.parse(row[key]); + }); + }); + return data; + }); + } + /** * get socket * @param {String|Object} sql From 1fb33e0399f69082768ecc0af38da4d5f8e655bc Mon Sep 17 00:00:00 2001 From: lizheming Date: Mon, 7 Sep 2020 20:45:14 +0800 Subject: [PATCH 3/4] fix: eslint error --- lib/query.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/query.js b/lib/query.js index 4f653d3..a516be6 100644 --- a/lib/query.js +++ b/lib/query.js @@ -6,7 +6,7 @@ const Mysql = require('think-mysql'); */ module.exports = class MysqlQuery extends Query { select(options, cache) { - if(!this.config.jsonFormat) { + if (!this.config.jsonFormat) { return super.select(options, cache); } @@ -23,7 +23,7 @@ module.exports = class MysqlQuery extends Query { return data; }); } - + /** * get socket * @param {String|Object} sql From 4e39cf98adedcef6f46a55ee807f25157277d3c5 Mon Sep 17 00:00:00 2001 From: lizheming Date: Mon, 7 Sep 2020 21:14:01 +0800 Subject: [PATCH 4/4] feat: update test --- test/lib/query.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ test/lib/schema.js | 5 ++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/test/lib/query.js b/test/lib/query.js index f59765f..955ad3d 100644 --- a/test/lib/query.js +++ b/test/lib/query.js @@ -3,6 +3,50 @@ const helper = require('think-helper'); const Base = require('../../lib/query'); const Parser = require('../../lib/parser'); +ava.test('select jsonFormat false', t => { + t.plan(2); + + const instance = new Base(); + instance.__proto__.__proto__.select = function(options, cache) { + t.is(options, 1); + t.is(cache, 2); + } + instance.select(1, 2); +}) + +ava.test('select jsonFormat true', async t => { + t.plan(2); + + const instance = new Base({jsonFormat: true}); + instance.schema = { + getSchema() { + return { + title: { + tinyType: 'varchar' + }, + content: { + tinyType: 'varchar' + }, + json: { + tinyType: 'json' + } + }; + } + } + instance.__proto__.__proto__.select = function() { + return {title: 'hello', content: 'world', json: JSON.stringify([1,2,3,4])}; + } + const data = await instance.select(); + t.deepEqual(data, {title: 'hello', content: 'world', json: [1,2,3,4]}); + + + instance.__proto__.__proto__.select = function() { + return [{title: 'hello', content: 'world', json: JSON.stringify([1,2,3,4])}]; + } + const data2 = await instance.select(); + t.deepEqual(data2, [{title: 'hello', content: 'world', json: [1,2,3,4]}]); +}); + ava.test('socket is function', t => { const instance = new Base(); t.true(helper.isFunction(instance.socket)); diff --git a/test/lib/schema.js b/test/lib/schema.js index e39db27..7d6f038 100644 --- a/test/lib/schema.js +++ b/test/lib/schema.js @@ -379,7 +379,9 @@ test('schema get normal schema 4', async t => { }); test('schema parse type', t => { - const schema = new Schema({}); + t.plan(13); + + const schema = new Schema({jsonFormat: true}); t.is(schema.parseType('enum', '1'), '1'); t.is(schema.parseType('set', 'True'), 'True'); t.is(schema.parseType('bigint', 'False'), 'False'); @@ -392,4 +394,5 @@ test('schema parse type', t => { t.is(schema.parseType('bool', '0'), 1); t.is(schema.parseType('bool', ''), 0); t.is(schema.parseType('xxx', 'aaa'), 'aaa'); + t.is(schema.parseType('json', [1,2,3,4]), '[1,2,3,4]'); });