@picadeck/sql-parser
Parse raw SQL DDL statements into a UniversalSchema for visualization.
Installation
npm install @picadeck/sql-parserImport
import { parseSQLToSchema, parseSQLFile } from "@picadeck/sql-parser";API
parseSQLToSchema(sql: string, dialect: "postgresql" | "mysql"): Result
Parses a SQL DDL string containing CREATE TABLE statements. Returns a result object:
// On success:
{ success: true, schema: UniversalSchema }
// On failure:
{ success: false, error: string }parseSQLFile(filePath: string, dialect: "postgresql" | "mysql"): Result
Reads SQL from the given file path and passes it to parseSQLToSchema. Returns the same result shape.
Full Example
server.ts
import express from "express";
import { schemaVisualizer } from "@picadeck/core";
import { parseSQLFile } from "@picadeck/sql-parser";
const app = express();
const result = parseSQLFile("./migrations/schema.sql", "postgresql");
if (result.success) {
app.use("/schema", schemaVisualizer(result.schema));
} else {
console.error("Failed to parse SQL:", result.error);
}
app.listen(3000);You can also parse SQL strings directly:
import { parseSQLToSchema } from "@picadeck/sql-parser";
const sql = `
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
body TEXT,
author_id INTEGER REFERENCES users(id),
created_at TIMESTAMP DEFAULT NOW()
);
`;
const result = parseSQLToSchema(sql, "postgresql");Supported Types
| SQL Type | NormalizedType |
|---|---|
| VARCHAR / CHAR / TEXT | STRING / TEXT |
| INT / INTEGER / SERIAL | INTEGER |
| FLOAT / REAL / DOUBLE | FLOAT |
| DECIMAL / NUMERIC | DECIMAL |
| BOOLEAN / BOOL | BOOLEAN |
| TIMESTAMP / DATETIME | DATETIME |
| DATE | DATE |
| UUID | UUID |
| JSON / JSONB | JSON |
Notes
- Supports PostgreSQL and MySQL dialects. The dialect parameter affects type normalization and syntax parsing.
- FOREIGN KEY constraints are detected as relationships.
- Returns a result object instead of throwing, so you can handle parse errors gracefully.