Created
August 11, 2015 03:54
-
-
Save okunishinishi/eab9912be967ce5e5a85 to your computer and use it in GitHub Desktop.
mysqlのテーブル定義からJSON Schemaを生成する ref: http://qiita.com/okunishinishi@github/items/3baecbb67b43b16bf308
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Mysql connect config. | |
var config = { | |
user: 'root', | |
password: 'my_password', | |
host: 'localhost', | |
database: 'my_db' | |
}; | |
// Get spec for connected database | |
mysqlspec(config, function (err, schema) { | |
console.log("schema=" + JSON.stringify(schema, null, 4)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
schema = { // Schemas for in "my_db" database. | |
'TEST_SHOP': { // Schema for in "my_db.TEST_SHOP" table. | |
name: 'my_db.TEST_SHOP', | |
properties: { | |
id: {type: 'integer', maxLength: 5}, | |
article: {type: 'integer', maxLength: 4}, | |
dealer: {type: 'string', maxLength: 20}, | |
price: {type: 'number'} | |
}, | |
required: ['id', 'article', 'dealer', 'price'], | |
additionalProperties: false | |
}, | |
'TEST_PERSON':{ | |
/**...*/ | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SHOW DATABASES; | |
SHOW TABLES IN <database_name>; | |
DESC <table_name>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var mysql = require('mysql'), | |
argx = require('argx'); | |
/** @constructor */ | |
function MysqlDescriber(config) { | |
var s = this; | |
s._config = config; | |
} | |
MysqlDescriber.prototype = { | |
_connection: undefined, | |
/** | |
* Connect to database. | |
* @returns {MysqlDescriber} - Returns self. | |
*/ | |
connect: function () { | |
var s = this; | |
s._connection = mysql.createConnection(s._config); | |
return s; | |
}, | |
/** | |
* Disconnect from data base. | |
* @returns {MysqlDescriber} - Returns self. | |
*/ | |
disconnect: function () { | |
var s = this; | |
if (s._connection) { | |
s._connection.end(); | |
} | |
return s; | |
}, | |
/** | |
* Execute a sql. | |
* @param {string} sql - SQL String to execute. | |
* @param {string[]} [values] - Values to apply | |
* @param {function} callback - Callback when done. | |
* @returns {MysqlDescriber} - Returns self. | |
*/ | |
execute: function (sql, values, callback) { | |
var args = argx(arguments); | |
callback = args.pop('function'); | |
sql = args.shift(); | |
values = args.remain() || []; | |
var s = this; | |
s._connection.query(sql, values, callback); | |
return s; | |
}, | |
/** | |
* Show databases. | |
* @param {function} callback - Callback when done. | |
* @returns {MysqlDescriber} - Returns self. | |
*/ | |
showDatabases: function (callback) { | |
var s = this; | |
s.execute("SHOW DATABASES;", callback); | |
return s; | |
}, | |
/** | |
* Show tables. | |
* @param {string} database - Name of database. | |
* @param {function} callback - Callback when done. | |
* @returns {MysqlDescriber} - Returns self. | |
*/ | |
showTables: function (database, callback) { | |
var s = this; | |
var sql = ["SHOW TABLES IN", database].join(' '); | |
s.execute(sql, callback); | |
return s; | |
}, | |
/** | |
* Desc table. | |
* @param {string} database - Name of database. | |
* @param {string} table - Name of table. | |
* @param {function} callback - Callback when done. | |
* @returns {MysqlDescriber} - Returns self. | |
*/ | |
descTable: function (database, table, callback) { | |
var s = this, | |
sql = ["DESC", [database, table].join('.')].join(' '); | |
s.execute(sql, callback); | |
return s; | |
} | |
}; | |
module.exports = MysqlDescriber; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ Field: 'last_name', | |
Type: 'varchar(255)', | |
Null: 'YES', | |
Key: '', | |
Default: null, | |
Extra: '' }, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _parseRequired(col) { | |
return col.Null === 'NO'; | |
} | |
function _parseType(col) { | |
var s = this; | |
var type = col.Type; | |
if (!type) { | |
return undefined; | |
} | |
var mysqlType = type.replace(/\([\d,]+\)/, '').toUpperCase().split(' ').shift(); | |
switch (mysqlType) { | |
case 'LONGTEXT': | |
case 'MEDIUMTEXT': | |
case 'TEXT': | |
case 'TINYTEXT': | |
case 'VARCHAR': | |
case 'CHAR': | |
return 'string'; | |
case 'TINYINT': | |
case 'SMALLINT': | |
case 'MEDIUMINT': | |
case 'INT': | |
case 'BIGINT': | |
return 'integer'; | |
case 'FLOAT': | |
case 'DOUBLE': | |
case 'DECIMAL': | |
case 'NUMERIC': | |
return 'number'; | |
case 'DATE': | |
case 'DATETIME': | |
return 'date'; | |
case 'LONGBLOB': | |
case 'MEDIUMBLOB': | |
case 'TINYBLOB': | |
return 'string'; | |
default: | |
return mysqlType; | |
} | |
} | |
function _parseMaxlength(col) { | |
var type = col.Type; | |
if (!type) { | |
return undefined; | |
} | |
var matched = type.match(/\((\d+)\)/); | |
return matched ? Number(matched.pop()) : undefined; | |
} | |
function _cleanEmptyProperty(data) { | |
Object.keys(data).forEach(function (key) { | |
var isEmpty = (key === undefined) || (key === null) || (key === ''); | |
if (isEmpty) { | |
delete data[key]; | |
} | |
}); | |
return data; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install mysqlspec --save-dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mysqlspec = require('mysqlspec'); | |
// Mysql connect config. | |
var config = { | |
user: 'root', | |
password: 'my_password', | |
host: 'localhost', | |
database: 'my_db' | |
}; | |
// Get spec for connected database | |
mysqlspec(config, function (err, schema) { | |
console.log("schema=" + JSON.stringify(schema, null, 4)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
schema = { // Schemas for in "my_db" database. | |
'TEST_SHOP': { // Schema for in "my_db.TEST_SHOP" table. | |
name: 'my_db.TEST_SHOP', | |
properties: { | |
id: {type: 'integer', maxLength: 5}, | |
article: {type: 'integer', maxLength: 4}, | |
dealer: {type: 'string', maxLength: 20}, | |
price: {type: 'number'} | |
}, | |
required: ['id', 'article', 'dealer', 'price'], | |
additionalProperties: false | |
}, | |
'TEST_PERSON':{ | |
/**...*/ | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment