{"id":43287,"library":"mysql-json-schema","title":"mysql-json-schema","description":"Export MySQL database schemas to JSON format, including table relationships. Version 1.2.0. This library extracts table structure, fields, and foreign key relations from a MySQL database and outputs them as JSON. It also supports heuristic discovery of relationships by field naming conventions (prefix/suffix aliases). It provides both synchronous and asynchronous connection methods, as well as granular functions for fetching schemas, table lists, fields, and relations. Suitable for documentation generation or migration tasks.","status":"active","version":"1.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/llorentegerman/mysql-json-schema","tags":["javascript","mysql","mysql json","mysql json schema","mysql schema","json schema"],"install":[{"cmd":"npm install mysql-json-schema","lang":"bash","label":"npm"},{"cmd":"yarn add mysql-json-schema","lang":"bash","label":"yarn"},{"cmd":"pnpm add mysql-json-schema","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Node MySQL driver to connect to MySQL databases","package":"mysql","optional":false}],"imports":[{"note":"Package uses CommonJS default export; can be imported using ESM syntax as shown, but require also works. TypeScript definitions are not included.","wrong":"const mysqlSchema = require('mysql-json-schema');","symbol":"default import","correct":"import mysqlSchema from 'mysql-json-schema';"},{"note":"Named export for synchronous connection; also available as a named export.","wrong":"const { CreateConnection } = require('mysql-json-schema');","symbol":"CreateConnection","correct":"import { CreateConnection } from 'mysql-json-schema';"},{"note":"GetSchema is a named export, not a default export. Must be imported with curly braces.","wrong":"import GetSchema from 'mysql-json-schema';","symbol":"GetSchema","correct":"import { GetSchema } from 'mysql-json-schema';"}],"quickstart":{"code":"const mysqlSchema = require('mysql-json-schema');\n\nconst args = {\n  host: process.env.DB_HOST ?? 'localhost',\n  user: process.env.DB_USER ?? 'root',\n  password: process.env.DB_PASSWORD ?? '',\n  database: process.env.DB_NAME ?? 'test',\n  outputFolder: './schema'\n};\n\nmysqlSchema.ExportSchemaToFile(args)\n  .then((result) => {\n    console.log('Schema exported to file:', result);\n  })\n  .catch((err) => {\n    console.error('Error:', err);\n  });\n\n// Or using individual methods:\nconst connection = mysqlSchema.CreateConnection({\n  host: 'localhost',\n  user: 'root',\n  password: '',\n  database: 'test'\n});\n\nconst schema = mysqlSchema.GetSchema(connection);\nconsole.log(JSON.stringify(schema, null, 2));\nconnection.end();","lang":"javascript","description":"Demonstrates exporting a full schema to a single JSON file and using individual methods to retrieve a schema object."},"warnings":[{"fix":"Update code to expect { tables: [...], relations: [...] } instead of just an array.","message":"Version 1.0.0 changed the export format from an array of tables to an object with tables and relations.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Use CreateConnectionAsync and await or .then() to get the connection.","message":"CreateConnection is deprecated in favor of CreateConnectionAsync for promise-based usage.","severity":"deprecated","affected_versions":">=1.1.0"},{"fix":"Always call connection.end() after using synchronous methods, or use async methods with proper error handling.","message":"The library uses the mysql package, which does not support prepared statements by default. Callback-based APIs may cause connection pool issues if not properly closed.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Ensure the outputFolder exists before calling ExportSchemaToFile or ExportSchemaToFiles.","message":"Output file contains only the schema JSON without directory creation.","severity":"breaking","affected_versions":">=1.2.0"}],"env_vars":null,"search_vec":"'1.2.0':16 'alias':48 'also':37 'asynchron':54 'connect':55 'convent':46 'databas':7,30 'discoveri':40 'document':72 'export':5 'extract':19 'fetch':63 'field':22,44,67 'foreign':24 'format':11 'function':61 'generat':73 'granular':60 'heurist':39 'includ':12 'javascript':77 'json':3,10,35,80,82,86 'key':25 'librari':18 'list':66 'method':56 'migrat':75 'mysql':2,6,29,78,79,81,84 'mysql-json-schema':1 'name':45 'output':32 'prefix/suffix':47 'provid':50 'relat':26,69 'relationship':14,42 'schema':4,8,64,83,85,87 'structur':21 'suitabl':70 'support':38 'synchron':52 'tabl':13,20,65 'task':76 'version':15 'well':58","created_at":"2026-06-05T16:59:04.566309+00:00","updated_at":"2026-06-05T16:59:04.566309+00:00","problems":[{"fix":"Use require('mysql-json-schema') or import { CreateConnection } from 'mysql-json-schema'.","cause":"Importing the module incorrectly, e.g., as a default import when using ESM.","error":"TypeError: mysqlSchema.CreateConnection is not a function"},{"fix":"Run 'npm install mysql' alongside mysql-json-schema.","cause":"Missing peer dependency 'mysql'.","error":"Error: Cannot find module 'mysql'"},{"fix":"Double-check the user, password, host, and database name in the connection arguments.","cause":"Invalid database credentials.","error":"ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'host'"},{"fix":"Ensure CreateConnection or CreateConnectionAsync returns a valid connection object before calling schema methods.","cause":"Schema export methods expect a specific internal structure; if the connection is not established properly, the result may be undefined.","error":"Cannot read property 'length' of undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/llorentegerman/mysql-json-schema#readme","github":"https://github.com/llorentegerman/mysql-json-schema","docs":null,"changelog":null,"pypi":null,"npm":"mysql-json-schema","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}