md2sql

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2022 License: AGPL-3.0 Imports: 7 Imported by: 0

README

md2sql

Generate SQL/ERD from Markdown list.

$ go install github.com/shibukawa/md2sql...

You can try on your browser.

Example

Simple Example
  • All table should start "table: " prefix.
  • Each column is formatted in "name: type".
* table: BasicTable
  * id: integer
  * name: string
$ md2sql -f sql simple.md

You can get the following SQL:

CREATE TABLE BasicTable(
    id INTEGER NOT NULL,
    name TEXT NOT NULL
);

Also you can get the PlantUML and Mermaid.js ERD by using -f plantuml or -f mermaid:

@startuml

entity BasicTable {
  --
  *id:INTEGER
  *name:TEXT
}

@enduml
erDiagram

BasicTable {
  INTEGER id
  TEXT name
}
Basic Example

You can specify primary key, index and foreign key.

  • @ prefix of name means primary key. If you omit type, it becomes auto incremental field.
  • $ prefix of name means index.
  • * prefix of type field means foreign key.
* table: User
    * @id
    * name: string
    * $email: string
    * age: integer
    * job: *Job.id

* table: Job
    * @id
    * name: string

You can get the following SQL:

CREATE TABLE User(
    id SERIAL,
    name TEXT NOT NULL,
    email TEXT NOT NULL,
    age INTEGER NOT NULL,
    job INTEGER NOT NULL,
    PRIMARY KEY(id),
    FOREIGN KEY(job) REFERENCES Job(id)
);

CREATE UNIQUE INDEX INDEX_User_email ON User(email);

CREATE TABLE Job(
    id SERIAL,
    name TEXT NOT NULL,
    PRIMARY KEY(id)
);
Associative Entity

You can specify Associative Entity by using [] suffix.

* table: User
    * @id
    * name: string
    * age: integer
    * jobs: *Job.id[]

* table: Job
    * @id
    * name: string

You can get the following SQL:

CREATE TABLE User(
    id SERIAL,
    name TEXT NOT NULL,
    age INTEGER NOT NULL,
    PRIMARY KEY(id)
);

CREATE TABLE Job(
    id SERIAL,
    name TEXT NOT NULL,
    PRIMARY KEY(id)
);

CREATE TABLE User_jobs(
    id SERIAL PRIMARY KEY,
    User_id INTEGER,
    Job_id INTEGER,
    FOREIGN KEY(User_id) REFERENCES User(id),
    FOREIGN KEY(Job_id) REFERENCES Job(id)
);
Stereotype

PlantUML generator can represent stereotypes of tables. There are 7 types you can use:

label meaning Mark
table: Generic table. E
master: Its represents system. M
tran: or transaction: It represents activity. T
summary: It keeps cache of query result. E
work: Temporary table W
view: View V
associativeentity: Associative Entity A

And tables that has -(minus) or _(underscore) prefixed means dependent tables.

* master: Users
    * @id
    * name: string
    * age: integer
    * jobs: *Job.id[]

* -tran: Tests
    * @user: Users.id
    * @date: Date
    * score: integer

Web Interface

This tool also provides a web interface.

$ cd cmd/frontend
$ npm run dev

It uses plantuml.com to generate image from PlantUML source. You can run plantuml server locally by using docker:

$ docker compose up -d
$ cd cmd/frontend
$ NEXT_PUBLIC_PLANTUML_SERVER=http://localhost:18080 npm run dev

License

AGPL

Documentation

Index

Constants

View Source
const (
	OutputFields Filter = 1 << iota
	OutputKeys
	OutputForeignKeyDef
	OutputMaster
	OutputTransaction
	OutputWork
	OutputSummary
	OutputView
	OutputAE

	OutputAllTable = OutputMaster | OutputTransaction | OutputWork | OutputSummary | OutputView | OutputAE

	OutputSkeltonModel  = OutputMaster | OutputTransaction | OutputAE
	OutputBirdViewModel = OutputMaster | OutputTransaction | OutputAE | OutputKeys

	OutputAll              = OutputExceptForeignKey | OutputForeignKeyDef
	OutputExceptForeignKey = OutputFields | OutputKeys | OutputAllTable
)

Variables

This section is empty.

Functions

func DialectStrings

func DialectStrings() []string

DialectStrings returns a slice of all String values of the enum

func DumpGraphviz added in v1.2.0

func DumpGraphviz(w io.Writer, tables []*Table, m ModelType, d Dialect) error

func DumpMermaid

func DumpMermaid(w io.Writer, tables []*Table, d Dialect) error

func DumpPlantUML

func DumpPlantUML(w io.Writer, tables []*Table, d Dialect) error

func DumpSQL

func DumpSQL(w io.Writer, tables []*Table, d Dialect) error

Types

type Cardinality

type Cardinality int
const (
	ZeroOrOne Cardinality = iota
	ExactlyOne
	ZeroOrMore
)

type Column

type Column struct {
	Name                 string
	Type                 string
	LinkTable            string
	LinkColumn           string
	PrimaryKey           bool
	AutoIncrement        bool
	Index                bool
	Nullable             bool
	AssociativeEntity    bool
	ForeignKeyConstraint bool
}

func ParseColumn

func ParseColumn(src string) (*Column, error)

type Dialect

type Dialect int
const (
	PostgreSQL Dialect = iota
	MySQL
	SQLite
)

func DialectString

func DialectString(s string) (Dialect, error)

DialectString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func DialectValues

func DialectValues() []Dialect

DialectValues returns all values of the enum

func ToDialect

func ToDialect(src string) Dialect

func (Dialect) EnableForeignKey added in v1.2.0

func (d Dialect) EnableForeignKey(hasForeignKey bool) string

func (Dialect) IsADialect

func (i Dialect) IsADialect() bool

IsADialect returns "true" if the value is listed in the enum definition. "false" otherwise

func (Dialect) PrimaryKeyBaseType

func (d Dialect) PrimaryKeyBaseType(t string) string

func (Dialect) PrimaryKeySQLType

func (d Dialect) PrimaryKeySQLType(t string, autoIncrement bool) string

func (Dialect) String

func (i Dialect) String() string

func (Dialect) TypeConversion

func (d Dialect) TypeConversion(t string) string

type Filter added in v1.3.0

type Filter int

type ModelType added in v1.2.0

type ModelType int
const (
	PhysicalModel ModelType = iota
	LogicalModel
	ConceptualModel
)

type Relation

type Relation struct {
	FromTable       string
	FromCardinality Cardinality
	ToTable         string
	ToCardinality   Cardinality
	Label           string
}

type Table

type Table struct {
	Type        TableType
	Independent bool
	Name        string
	Columns     []*Column
}

func Parse

func Parse(r io.Reader) ([]*Table, error)

type TableType added in v1.3.0

type TableType int
const (
	EntityTable TableType = iota
	MasterTable
	TransactionTable
	WorkTable
	SummaryTable
	View
	AssociativeEntity
)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL