1 Star 0 Fork 1

ecity/db-modeling-markdown

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

db-modeling-markdown README

Now, you may define your database tables in markdown files, and this utility will use them to generate .sql files.

Release Notes

2.0.2

Change Log

Samples

Define a markdown file(.db.md) like:

# Table: departments

## `Table`

| `Name`      | `Comment`             | `Character Set` | `Collation`        | `Engine` |
| ----------- | --------------------- | --------------- | ------------------ | -------- |
| departments | The department table. | utf8mb4         | utf8mb4_general_ci | InnoDB   |

## `Primary Key`

| `Columns`    |
| ------------ |
| DepartmentID |

## `Indexes`

## `Foreign Keys`

| `Columns` | `Ref Table` | `Ref Columns` | `Options` |
| --------- | ----------- | ------------- | --------- |
| ManagerID | employees   | EmployeeID    |           |
| ParentID  | departments | DepartmentID  |           |

## `Columns`

| `Label`         | `Name`         | `Type`                                 | `Nullable` | `Default`           | `Comment`            |
| --------------- | -------------- | -------------------------------------- | ---------- | ------------------- | -------------------- |
| Department ID   | DepartmentID   | int auto_increment                     | `No`       |                     | Department ID        |
| Department Name | DepartmentName | varchar(50)                            | `No`       |                     | Department Name      |
| Parent ID       | ParentID       | int                                    | `Yes`      |                     | Parent Department    |
| Manager ID      | ManagerID      | int                                    | `Yes`      |                     | Manager              |
| Created By      | CreatedBy      | varchar(255)                           | `No`       | ''                  | Created By User Name |
| Create Time     | CreateTime     | datetime                               | `No`       | current_timestamp() | Created Time         |
| Update By       | UpdateBy       | varchar(255)                           | `No`       | ''                  | Updated By User Name |
| Update Time     | UpdateTime     | datetime on update current_timestamp() | `No`       | current_timestamp() | Updated Time         |

You will get SQL scripts:

-- Create table 'departments'
CREATE TABLE `departments` (
    `DepartmentID` int auto_increment NOT NULL COMMENT 'Department ID'
  , `DepartmentName` varchar(50) NOT NULL COMMENT 'Department Name'
  , `ParentID` int COMMENT 'Parent Department'
  , `ManagerID` int COMMENT 'Manager'
  , `CreatedBy` varchar(255) NOT NULL DEFAULT '' COMMENT 'Created By User Name'
  , `CreateTime` datetime NOT NULL DEFAULT current_timestamp() COMMENT 'Created Time'
  , `UpdateBy` varchar(255) NOT NULL DEFAULT '' COMMENT 'Updated By User Name'
  , `UpdateTime` datetime on update current_timestamp() NOT NULL DEFAULT current_timestamp() COMMENT 'Updated Time'
  , PRIMARY KEY (DepartmentID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='The department table.'
;

Sample Code

import * as path from 'path'

import { DatabaseTypes, DbmOptions, DbmService } from '../src'

const options: DbmOptions = new DbmOptions()
options.database = DatabaseTypes.mariadb

// Generate database modeling markdown files (.db.md) from a database
options.sourceFolder = path.join(__dirname, `../../temp/tables/v1`)
await DbmService.fromDatabase(
	{
		databaseType: options.database,
		host: '127.0.0.1',
		port: 3306,
		user: 'root',
		password: 'password',
		database: 'my-db',
	},
	options.sourceFolder,
	true,
)

// Read table definition markdow files(.db.md), and produce a sql file with create-table-if-not-exists scripts
options.sourceFolder = path.join(__dirname, '../tests/data/mariadb/tables')
options.targetPath = path.join(__dirname, '../temp/createTableIf.sql')
DbmService.toCreateTableIfNotExistsSql(options)

// Read table definition markdow files(.db.md), and produce a sql file with create-table scripts
options.targetPath = path.join(__dirname, '../temp/createTable.sql')
DbmService.toCreateTableSql(options)

// Read table definition markdow files(.db.md), and produce a sql file with drop-table scripts
options.targetPath = path.join(__dirname, '../temp/dropTable.sql')
DbmService.toDropTableSql(options)

// Read table definition markdow files(.db.md), and produce a sql file with drop-then-create-table scripts
options.targetPath = path.join(__dirname, '../temp/dropThenCreateTable.sql')
DbmService.toDropThenCreateTableSql(options)

// Read table definition markdow files(.db.md), and produce a sql file with upgrade scripts
options.previousVersionFolder = path.join(__dirname, `../tests/data/mariadb/tables/v1`)
options.targetPath = path.join(__dirname, '../temp/upgrade.sql')
DbmService.toUpgradeSql(options)

// Read table definition markdow files(.db.md), and produce a json file
options.targetPath = path.join(__dirname, '../temp/model.json')
DbmService.toJson(options)

// Read table definition markdow files(.db.md), and produce a model object
options.targetPath = ''
const model = DbmService.toModel(options)
// eslint-disable-next-line no-console
console.log(JSON.stringify(model, null, 2))

// Generate database modeling markdown files (.db.md) from a model
DbmService.toMarkdown(model, path.join(__dirname, '../temp/tables'), true)

// Generate index.md from a model
DbmService.genIndexFile(model, options.sourceFolder)

Features

  • Support Databases

    • DB2
    • MariaDB
    • MS SQL Server
    • MySQL
    • Oracle Database
    • PostgreSQL
    • SQLite
  • Support generate SQL scripts of create tables.

  • Support generate SQL scripts of create tables if not exists.

  • Support generate SQL scripts of drop then create tables.

  • Support generate SQL scripts of drop tables.

  • Support generate SQL scripts of upgrade a database (mariadb/mysql only).

  • Support generate database definition markdown files from a database (mariadb/mysql only).

  • Support generate a json file of the database model.

  • Support generate an model object of the database model.

  • Support generate database definition markdown files from a database model (json).

  • Support generate index.md for a database model.

Contributing

Please see Contributing

Enjoy!

MIT License Copyright (c) 2020 Steven N. Yang(steven_nyang@outlook.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

使用markdown数据库建模 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sn-yang/db-modeling-markdown.git
git@gitee.com:sn-yang/db-modeling-markdown.git
sn-yang
db-modeling-markdown
db-modeling-markdown
master

搜索帮助