Registry / database / mysql-json-schema

mysql-json-schema

JSON →
library1.2.0jsnpmunverified

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.

npm install mysql-json-schema
INSTALL
IMPORT
SIG · MYSQL-JSON-SCHEMA
M
mysql-json-schema
databasejavascriptv1.2.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default import
✓ import mysqlSchema from 'mysql-json-schema';
✗ const mysqlSchema = require('mysql-json-schema');
Package uses CommonJS default export; can be imported using ESM syntax as shown, but require also works. TypeScript definitions are not included.
CreateConnection
✓ import { CreateConnection } from 'mysql-json-schema';
✗ const { CreateConnection } = require('mysql-json-schema');
Named export for synchronous connection; also available as a named export.
GetSchema
✓ import { GetSchema } from 'mysql-json-schema';
✗ import GetSchema from 'mysql-json-schema';
GetSchema is a named export, not a default export. Must be imported with curly braces.

Demonstrates exporting a full schema to a single JSON file and using individual methods to retrieve a schema object.

const mysqlSchema = require('mysql-json-schema'); const args = { host: process.env.DB_HOST ?? 'localhost', user: process.env.DB_USER ?? 'root', password: process.env.DB_PASSWORD ?? '', database: process.env.DB_NAME ?? 'test', outputFolder: './schema' }; mysqlSchema.ExportSchemaToFile(args) .then((result) => { console.log('Schema exported to file:', result); }) .catch((err) => { console.error('Error:', err); }); // Or using individual methods: const connection = mysqlSchema.CreateConnection({ host: 'localhost', user: 'root', password: '', database: 'test' }); const schema = mysqlSchema.GetSchema(connection); console.log(JSON.stringify(schema, null, 2)); connection.end();
Debug
Known issues
breakingVersion 1.0.0 changed the export format from an array of tables to an object with tables and relations.
fix
Update code to expect { tables: [...], relations: [...] } instead of just an array.
affects: >=1.0.0
deprecatedCreateConnection is deprecated in favor of CreateConnectionAsync for promise-based usage.
fix
Use CreateConnectionAsync and await or .then() to get the connection.
affects: >=1.1.0
gotchaThe 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.
fix
Always call connection.end() after using synchronous methods, or use async methods with proper error handling.
affects: >=0.0.0
breakingOutput file contains only the schema JSON without directory creation.
fix
Ensure the outputFolder exists before calling ExportSchemaToFile or ExportSchemaToFiles.
affects: >=1.2.0
Errors
Common errors & fixes
TypeError: mysqlSchema.CreateConnection is not a function
Importing the module incorrectly, e.g., as a default import when using ESM.
fix
Use require('mysql-json-schema') or import { CreateConnection } from 'mysql-json-schema'.
Error: Cannot find module 'mysql'
Missing peer dependency 'mysql'.
fix
Run 'npm install mysql' alongside mysql-json-schema.
ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'host'
Invalid database credentials.
fix
Double-check the user, password, host, and database name in the connection arguments.
Cannot read property 'length' of undefined
Schema export methods expect a specific internal structure; if the connection is not established properly, the result may be undefined.
fix
Ensure CreateConnection or CreateConnectionAsync returns a valid connection object before calling schema methods.
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies
mysqlrequiredNode MySQL driver to connect to MySQL databases
Agent activity
7 hits · last 30 days
node
6
Resources
mysql-json-schema — npm install mysql-json-schema · libregistry