Socket
Socket
Sign inDemoInstall

graphql-mysql-schema

Package Overview
Dependencies
12
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    graphql-mysql-schema

Generate mysql table schema to graphql defination


Version published
Weekly downloads
204
decreased by-18.4%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

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

Keywords

FAQs

Last updated on 03 Apr 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc