mysql-schema v1.0.0
mysql-schema
Generate a JSON representing the schema of your MySQL database.
Install
$ npm i -g mysql-schema
Why?
To generate schema definitions of MySQL databases automatically.
To abstract your code and repeat things the least possible.
When you care about the shape, content relies on you. Be water.
Usage
This tool enables you to generate a json file representing your mysql database.
CLI usage
This is a short explanation of how the CLI works.
$ mysql-schema
[--user] root # database user = process.env.DB_USER || 'root'
[--password] # database user password = process.env.DB_PASSWORD || ''
[--host] 127.0.0.1 # database host = process.env.DB_HOST || '127.0.0.1'
[--port] 3306 # database port = process.env.DB_PORT || 3306
[--database] test # database name = process.env.DB_NAME || 'test'
[--configurations] # database configurations file = process.env.DB_CONFIGURATIONS || false
[--extensions] # database schema extensions = process.env.DB_EXTENSIONS || false
[--as-json] # to generate a json & not a js = false
--output schema.{db}.js # output file = "schema.${database}.js"API usage
This is a short explanation of how the API works.
require("mysql-schema").getSchema({
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || '',
host: process.env.DB_HOST || '127.0.0.1',
port: process.env.DB_PORT || '3306',
database: process.env.DB_NAME || 'test',
configurations: process.env.DB_CONFIGURATIONS || false,
extensions: process.env.DB_EXTENSIONS || false,
asJson: false,
output: `./schema.${database}.js`
})API reference
const MySQLSchema = require("mysql-schema")
Master class of the mysql-schema API.
MySQLSchema.getSchema(options:Object):Promise<schema:Object>
Asynchronous.
Description: Generates a schema representing the MySQL database pointing.
Parameter: options - object with properties (all of them optional):
user:string- user of the database- default:
process.env.DB_USERor"root"
- default:
password:string- password of the database- default:
process.env.DB_PASSWORDor""
- default:
host:string- host of the database- default:
process.env.DB_HOSTor"127.0.0.1"
- default:
port:string- port of the database- default:
process.env.DB_PORTor3306
- default:
database:string- name of the database- default:
process.env.DB_NAMEor"test"
- default:
configurations:string- configurations file for the database (overrides the other parameters)- default:
process.env.DB_CONFIGURATIONSorfalse - usage: object that allows to set the same database configuration from an external file.
- default:
extensions:string- extensions file for the generation- default:
process.env.DB_EXTENSIONSorfalse - usage: object that allows to extend the schema:
perTable: filled with nested table, column, and extension properties.perColumn: filled with nested column, and extension properties.- in
general: filled with extension properties.
- default:
asJson:boolean- flag to output ajsoninstead of ajsfile- default:
false - usage: flag to output a JSON file. As JSON, functions, regex and dates are lost as genuine types in the exportation.
- default:
output:string- destination of the file with the schema- default:
process.env.DB_SCHEMAorfalse
- default:
MySQLSchema.stringifyFn(value:Object, spaces:Number):String
Description: Like JSON.stringify, but with a replacer that
converts to JavaScript instead, accepting Function, RegExp
and Date objects as native data.
Issues
Please, report issues and suggestions here.
License
This project is licensed under WTFPL or What The Fuck Public License, which means 'do what you want with it'.