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.
Install with npm:
$ npm i --save generate-schema
Optionally, add -g
to the above if you want the generate-schema
command line executable.
Usage: generate-schema [options ...] [file]
Common Options:
-h, --help output usage information
-V, --version output the version number
-q, --quiet Skip help message in program output
Mode Options:
-g, --generic Generic JSON Primitives schema output
-j, --json-schema JSON Schema output
-s, --mysql MySQL Table Schema output
-m, --mongoose Mongoose Schema output
-b, --big-query Google BigQuery Schema output
-c, --clickhouse Clickhouse Table Schema output
When no file is specified, generate-schema
enters a REPL mode.
$ generate-schema -b
generate-schema v2.5.1 (bigquery)
Type "exit" to quit.
Type {a:"b"} to see an example.
> {a:"b"}
[
{
"name": "a",
"type": "STRING",
"mode": "NULLABLE"
}
]
var GenerateSchema = require('generate-schema')
// Capture Schema Output
var schema = GenerateSchema.json('Product', [
{
"id": 2,
"name": "An ice sculpture",
"price": 12.50,
"tags": ["cold", "ice"],
"dimensions": {
"length": 7.0,
"width": 12.0,
"height": 9.5
},
"warehouseLocation": {
"latitude": -78.75,
"longitude": 20.4
}
},
{
"id": 3,
"name": "A blue mouse",
"price": 25.50,
"dimensions": {
"length": 3.1,
"width": 1.0,
"height": 1.0
},
"warehouseLocation": {
"latitude": 54.4,
"longitude": -32.7
}
}
])
Outputs:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product Set",
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "number"
},
"name": {
"type": "string"
},
"price": {
"type": "number"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"dimensions": {
"type": "object",
"properties": {
"length": {
"type": "number"
},
"width": {
"type": "number"
},
"height": {
"type": "number"
}
}
},
"warehouseLocation": {
"type": "object",
"properties": {
"latitude": {
"type": "number"
},
"longitude": {
"type": "number"
}
}
}
},
"required": [
"id",
"name",
"price",
"dimensions",
"warehouseLocation"
],
"title": "Product"
}
}
Generates a generic schema from object
. Property types are described using primitives.
Generates MySQL Table Schema from object
.
tableName
is optional, defaults togeneric
object
must be of typeObject
orArray
Generates JSON Schema from object
.
title
is optionalobject
must be of typeObject
orArray
Generates a Mongoose Schema from object
.
Generates a Google BigQuery schema from object
.
Generates ClickHouse Table Schema from object
.
tableName
is optional, defaults togeneric
object
must be of typeObject
orArray
dateField
Date field for ENGINE, must be of typeDate