Generate Schemas

Convert JSON Objects to MySQL, JSON Schema, Mongoose, Google BigQuery, Swag...

README

Generate Schemas


Convert JSON Objects to MySQL Table Schema, JSON Schema, Mongoose Schema, ClickHouse Schema, Google BigQuery, or a Generic template for documentation, code generation, and more.

Table of Contents


Installation
CLI
Options
REPL Mode
Example

Usage
Example
Methods
g.generic(Object object)
g.mysql([String tableName,] Mixed object)
g.json([String title,] Mixed object)
g.mongoose(Object object)
g.bigquery(Object object)
g.clickhouse([String tableName,] Mixed object, String dateField)

License

Installation


Install with npm :

  1. ``` shell
  2. $ npm i --save generate-schema
  3. ```

Optionally, add -gto the above if you want the generate-schemacommand line executable.

CLI


  1. ``` null
  2.   Usage: generate-schema [options ...] [file]

  3.   Common Options:

  4.     -h, --help         output usage information
  5.     -V, --version      output the version number
  6.     -q, --quiet        Skip help message in program output

  7.   Mode Options:
  8.     -g, --generic      Generic JSON Primitives schema output
  9.     -j, --json-schema  JSON Schema output
  10.     -s, --mysql        MySQL Table Schema output
  11.     -m, --mongoose     Mongoose Schema output
  12.     -b, --big-query    Google BigQuery Schema output
  13.     -c, --clickhouse   Clickhouse Table Schema output

  14. ```

REPL Mode


When no file is specified, generate-schemaenters a REPL mode.

Example


  1. ``` null
  2. $ generate-schema -b
  3. generate-schema v2.5.1 (bigquery)
  4. Type "exit" to quit.
  5. Type {a:"b"} to see an example.
  6. > {a:"b"}
  7. [
  8.   {
  9.     "name": "a",
  10.     "type": "STRING",
  11.     "mode": "NULLABLE"
  12.   }
  13. ]

  14. ```

Usage


  1. ``` js
  2. var GenerateSchema = require('generate-schema')
  3. ```

Example


  1. ``` null
  2. // Capture Schema Output
  3. var schema = GenerateSchema.json('Product', [
  4.     {
  5.         "id": 2,
  6.         "name": "An ice sculpture",
  7.         "price": 12.50,
  8.         "tags": ["cold", "ice"],
  9.         "dimensions": {
  10.             "length": 7.0,
  11.             "width": 12.0,
  12.             "height": 9.5
  13.         },
  14.         "warehouseLocation": {
  15.             "latitude": -78.75,
  16.             "longitude": 20.4
  17.         }
  18.     },
  19.     {
  20.         "id": 3,
  21.         "name": "A blue mouse",
  22.         "price": 25.50,
  23.         "dimensions": {
  24.             "length": 3.1,
  25.             "width": 1.0,
  26.             "height": 1.0
  27.         },
  28.         "warehouseLocation": {
  29.             "latitude": 54.4,
  30.             "longitude": -32.7
  31.         }
  32.     }
  33. ])

  34. ```

Outputs:

  1. ``` json
  2. {
  3.   "$schema": "http://json-schema.org/draft-04/schema#",
  4.   "title": "Product Set",
  5.   "type": "array",
  6.   "items": {
  7.     "type": "object",
  8.     "properties": {
  9.       "id": {
  10.         "type": "number"
  11.       },
  12.       "name": {
  13.         "type": "string"
  14.       },
  15.       "price": {
  16.         "type": "number"
  17.       },
  18.       "tags": {
  19.         "type": "array",
  20.         "items": {
  21.           "type": "string"
  22.         }
  23.       },
  24.       "dimensions": {
  25.         "type": "object",
  26.         "properties": {
  27.           "length": {
  28.             "type": "number"
  29.           },
  30.           "width": {
  31.             "type": "number"
  32.           },
  33.           "height": {
  34.             "type": "number"
  35.           }
  36.         }
  37.       },
  38.       "warehouseLocation": {
  39.         "type": "object",
  40.         "properties": {
  41.           "latitude": {
  42.             "type": "number"
  43.           },
  44.           "longitude": {
  45.             "type": "number"
  46.           }
  47.         }
  48.       }
  49.     },
  50.     "required": [
  51.       "id",
  52.       "name",
  53.       "price",
  54.       "dimensions",
  55.       "warehouseLocation"
  56.     ],
  57.     "title": "Product"
  58.   }
  59. }
  60. ```

Methods


g.generic(Object object)


Generates a generic schema from object. Property types are described using primitives.

g.mysql([String tableName,] Mixed object)


Generates MySQL Table Schema from object.

tableNameis optional, defaults to generic
objectmust be of type Objector Array

g.json([String title,] Mixed object)


Generates JSON Schema from object.

titleis optional
objectmust be of type Objector Array

g.mongoose(Object object)


Generates a Mongoose Schema from object.

g.bigquery(Object object)


Generates a Google BigQuery schema from  object.

g.clickhouse([String tableName,] Mixed object, String dateField)


Generates ClickHouse Table Schema from object.

tableNameis optional, defaults to generic
objectmust be of type Objector Array
dateFieldDate field for ENGINE, must be of type Date

License


MIT