Skip to content

Commit

Permalink
Merge pull request #3 from thinkjs/feature/jsonFormat
Browse files Browse the repository at this point in the history
[feat] stringify data when column type is json
  • Loading branch information
welefen committed Sep 8, 2020
2 parents 42dea07 + 4e39cf9 commit 7e8a6d4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
44 changes: 44 additions & 0 deletions test/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
5 changes: 4 additions & 1 deletion test/lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,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');
Expand All @@ -398,4 +400,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]');
});

0 comments on commit 7e8a6d4

Please sign in to comment.