0.3.0 • Published 6 years ago

graphql-mysql-schema v0.3.0

Weekly downloads
16
License
MIT
Repository
github
Last release
6 years ago

graphql-mysql-schema

Build Status Coverage Status NPM version Dependency Status

Generate mysql table schema to graphql defination

Require

  • Node 7.6 or greater for async/await support

Usage

npm install graphql-mysql-schema --save

const mysqlGQL = require('graphql-mysql-schema');

let r = await mysqlGQL({
    host: '127.0.0.1',
    port: 3306,
    user: "myuser",
    password: 'mypwd',
    database: 'db1'
}, {
    tableName: 't1'
});

console.log(r);

output:

type t2 {
    # comment of c1
    c1: String,
    # comment of c2
    c2: Int,
    c3: String,
    # comment of c4
    c4: Float,
    # comment of c5
    c5: Int,
}

default generate rules:

mysql typegraphql type
varcharString
charString
tinytextString
mediumtextString
longtextString
intInt
tinyintInt
smallintInt
mediumintInt
bigintInt
floatFloat
doubleFloat
realFloat
decimalFloat
dateString
timeString
datetimeString
timestampString
yearString
OthersUNKNOWN

Options

mysqlGQL(mysqlOpt, options)

mysqlOpt: reference

options:

  • tableName: string, name of mysql table. required
  • genRule: function or object, rule of generating columns, user-defined rule. optional
  • unknown: unknown type, default "UNKNOWN", optional

Example: override default type rules

// "varchar" will be generated as "my-type" type, instead of "String"
const overrideRules = {
    varchar: 'my-type',
};
let r = await mysqlGQL({
    host: '127.0.0.1',
    port: 3306,
    user: "root",
    password: 'pwd',
    database: 'db'
}, {
    tableName: 'tn',
    genRule: overrideRules
});

console.log(r);

Example: user define generate rule

const TypeMap = {
    varchar: 'String',
    char: 'String',
    tinytext: 'String',
    text: 'String',
    // ...
};

// input: string, column type in mysql
function genType(input) {
    let gType = TypeMap.hasOwnProperty(input) ? TypeMap[input] : 'UNKNOWN';
    return gType;
}

let r = await mysqlGQL({
    host: '127.0.0.1',
    port: 3306,
    user: "myuser",
    password: 'mypwd',
    database: 'db1'
}, {
    tableName: 't2',
    genFn: genType
});

console.log(r);

CLI

npm install graphql-mysql-schema -g

mygql -H 127.0.0.1 -P 3306 -U myuser -p mypwd -D mydb t2

output:

type t2 {
    # comment of c1
    c1: String,
    # comment of c2
    c2: Int,
    c3: String,
    # comment of c4
    c4: Float,
    # comment of c5
    c5: Int,
}

options:

Usage: mygql [options] <tablename ...>

Options:
  -V, --version           output the version number
  -H, --host [value]      Mysql host
  -P, --port [value]      Mysql port
  -U, --user [value]      Mysql user
  -p, --pwd [value]       Mysql password
  -D, --database [value]  Mysql database
  -h, --help              output usage information

Test

npm test

0.3.0

6 years ago

0.2.0

6 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago