Below is a JavaScript script (written for Node.js) that converts a PostgreSQL database schema provided as an SQL string (containing CREATE TABLE statements) into a Graphviz dot file. The output Graphviz model represents tables as nodes with a record shape, where each column is shown as a field. Edges are drawn between tables based on foreign key relationships, pointing from the foreign key column to the referenced column.
Key Features:
Input: An SQL string containing CREATE TABLE statements. It supports basic column definitions (e.g., id SERIAL) and foreign key constraints (e.g., FOREIGN KEY (user_id) REFERENCES users(id)).
Output: A Graphviz dot string that can be used to generate a diagram (e.g., with the dot command-line tool).
Graphviz Node Representation: Each table is a node with a label that includes the table name and its columns (e.g., <id> id: SERIAL). This allows edges to point to specific columns.
Edge Representation: Foreign key relationships are shown as directed edges from the foreign key column to the referenced column.
Limitations: This script uses a simple parser and assumes straightforward SQL syntax. It may not handle complex cases like composite foreign keys, constraints with commas, or advanced data types perfectly. For more robust parsing, consider using a library like sql-parser.
Graphviz Attributes: The script includes basic styling from your example (e.g., font names, rank direction).
Example Usage:
Save the script to a file, e.g., sqlToGraphviz.js.
Run it with Node.js, providing the SQL as input. For example:
bash
echo "CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(100)); CREATE TABLE orders (id SERIAL PRIMARY KEY, user_id INTEGER, FOREIGN KEY (user_id) REFERENCES users(id));" | node sqlToGraphviz.js
The output will be a Graphviz dot string. You can pipe it to the dot tool to generate an image:
An edge from orders:user_id to users:id, representing the foreign key relationship.
You can visualize this with Graphviz tools (e.g., dot -Tpng -o output.png input.dot).
Notes:
Customization: If you want to add more features (e.g., handling primary keys by bolding them in the label), you can modify the label generation in sqlToDot. For example, check for primary key constraints (not currently parsed) and add styling.
Error Handling: The script assumes valid SQL input. You might want to add error handling for malformed SQL or missing referenced tables.
Comparison to Your Example: Your provided Graphviz example shows a generic data structure with numeric node names and arbitrary field values. This script adapts that style to a database schema context, using table and column names for clarity. If you need a more generic or data-oriented graph (e.g., based on INSERT statements), let me know for adjustments.
Improvements: For production use, consider using a more advanced SQL parser library (e.g., node-sql-parser) to handle complex SQL syntax.
If this doesn't fully match your needs or you have additional requirements, provide more details!