Git Product home page Git Product logo

dbspec's Introduction

DB Spec

Database Schema Design and Modeling Tool

Design Database Schema Without Writing SQL

1. Introduction

This project provides a mapping between specific symbols and corresponding data definition language (DDL) concepts. This mapping is intended to be used for parsing and converting data in various scenarios, such as databases, data analysis, and programming language parsing.

Features:

  1. Schema
  2. Table
  3. Column
  4. Types
  5. Default
  6. Auto Increment
  7. Primary Key
  8. Plus
  9. Comment
  10. Template

2. Specification

The mapping is defined as follows:

$    ; schema
#    ; table
%    ; template
...  ; slot
+    ; AUTO_INCREMENT, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
!    ; PRIMARY KEY
++   ; AUTO_INCREMENT PRIMARY KEY
=    ; DEFAULT

3. Hello World

user.dbs

# user  / TABLE OF USER   ; define a `user` table with comment 'TABLE OF USER'

id       n++   / id of user

name           / name of user
password s100  / password of user
avatar   S     / avatar of user

balance m      / balance of user
version N =0   / version of record
status  1 =0   / status of record

delete_at t    / delete time of record
create_at t=   / create time of record
update_at t+   / update time of record

user.sql

CREATE TABLE `user` (
  `id`        int AUTO_INCREMENT PRIMARY KEY COMMENT 'id of user',

  `name`      varchar(255) COMMENT 'name of user',
  `password`  varchar(100) COMMENT 'password of user',
  `avatar`    text COMMENT 'avatar of user',

  `balance`   decimal(16, 2) COMMENT 'balance of user',
  `version`   bigint DEFAULT 0 COMMENT 'version of record',
  `status`    int(1) DEFAULT 0 COMMENT 'status of record',

  `delete_at` datetime COMMENT 'delete time of record',
  `create_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'create time of record',
  `update_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'update time of record',

  COMMENT = 'TABLE OF USER'
)

4. Schema

Mark MySQL
$ schema name

schema.zz

(?<=\n|^)
(?:\$\s*)
(?:\w+)                    ; schema name
(?:\s*)
(?:--\s*(?:.*)(?=\n|$))?   ; sql comment
(?:\n)

demo.dbs

$ demo

demo.sql

create DATABASE `demo`

5. Table

Mark MySQL
# table name

table.zz

(?<=\n|^)
(?:\#)
\s*
(?:\w+)                    ; table name
\s*
(?://\s*(?:.*?))?          ; table comment
(?:--\s*(?:.*)(?=\n|$))?   ; sql comment
(?:\n)

user.dbs

# user

user.sql

CREATE TABLE `user` (
)

6. Column

column.zz

(?<=\n)
(?:\w+)                              ; name
\s*
(?:[nNmMtS]|\d+(?:,\d+)?|s(?:\d+)?)  ; types
\s*
(?:=\s*\w*)?                         ; default
\s*
(?:[+|++])?                          ; auto increment, auto increment primary key
\s*
(?:!)?                               ; primary key
\s*
(?://.*?)                            ; table comment
(?:--.*)                             ; sql comment
(?:;.*)                              ; spec comment
\n

7. Types

Referenced the Type Spec project.

7.1. Number

Mark Type
n int
N bigint
m decimal(16, 2)
M decimal(20, 6)
\d+ int(\d+)
\d*\,\d+ decimal(\d+, \d+)

user.dbs

# user

id      n
version N
status  1

balance  m
balance6 M
balancex 16,6

user.sql

CREATE TABLE `user` (
  `id`       int,
  `version`  bigint,
  `status`   int(1),

  `balance`  decimal(16, 2),
  `balance6` decimal(20, 6),
  `balancex` decimal(16, 6)
)

7.2. String

Mark Type
varchar
s\d+ varchar(\d+)
S text

user.dbs

# user

name
password s100
avatar S

user.sql

CREATE TABLE `user` (
  `name`     varchar(255),
  `password` varchar(100),
  `avatar`   text
)

7.3. Datetime

Mark Type
t datetime
d date

user.dbs

# user

vip_at       d
register_at  d =
login_at     d +

delete_at t
create_at t=
update_at t+

user.sql

CREATE TABLE `user` (
  `delete_at` datetime,
  `create_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
)

8. Plus

  1. Auto Increment
    • Auto Increment and Primary Key
  2. Default ... On Update
Mark MySQL
n +, N +, \d+ + AUTO_INCREMENT
++ AUTO_INCREMENT PRIMARY KEY
t +, d + DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

user.dbs

# user
id n ++
update_at t+

user.sql

CREATE TABLE `user` (
  `id` int AUTO_INCREMENT PRIMARY KEY
  `update_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
)

9. Key

Mark MySQL
! PRIMARY KEY

user.dbs

# user
id n !

user.sql

CREATE TABLE `user` (
  `id` int PRIMARY KEY
)

Default

Mark MySQL
= DEFAULT
t =, d =, DEFAULT CURRENT_TIMESTAMP

user.dbs

# user
status 1 =1

user.sql

CREATE TABLE `user` (
  `status` int(1) DEFAULT 1
)

10. Comment

Mark MySQL
/ table or column COMMENT
; spec comment
-- sql comment

user.dbs

# user  // TABLE OF USER  -- define a `user` table 
status 1 =1 // [0,1]      -- define a `status` column, type is int(1), comment is '[0,1]'

user.sql

CREATE TABLE `user` ( -- define a `user` table 
  `status` int(1) DEFAULT 1 COMMENT '[0,1]', -- define a `status` column, type is int(1), comment is '[0,1]'
  COMMENT = 'TABLE OF USER'
)

11. Template

Mark MySQL
% template name. default name is empty.
... slot. default slot is at the end of the template.

Default Template

template.dbs

%

id n++
...           -- Here is a slot
version   N
status    1

delete_at t
create_at t =
update_at t +

user.dbs

# user // TABLE OF USER

name
password s100
avatar   S

balance  m

user.sql

CREATE TABLE `user` (
  `id`        int AUTO_INCREMENT PRIMARY KEY,

  `name`      varchar(255),
  `password`  varchar(100),
  `avatar`    text,

  `balance`   decimal(16, 2),
  `version`   bigint DEFAULT 0,
  `status`    int(1) DEFAULT 0 COMMENT '[0,1]',

  `delete_at` datetime,
  `create_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  COMMENT = 'TABLE OF USER'
)

11.2 Named Template

all.spec

%1

id n++

#1 user  -- define a `user` table using template named `1`

name

all.sql

CREATE TABLE `user` (
  `id`   int AUTO_INCREMENT PRIMARY KEY,
  `name` varchar(255)
)

12. Ecosystem

  1. Type Spec
  2. DB Create
  3. ZZ

13. License

MIT

Copyright (c) 2023-present, Ronghai Ma

dbspec's People

Contributors

maronghai avatar

Watchers

 avatar

dbspec's Issues

spec-++

PLUS PLUS

mark type
++ AUTO_INCREMENT

spec-string

STRING

mark type
varchar(255)
s\d+ varchar(\d+)
S text

spec-number

NUMBER

mark type
n int
N bigint
\d+ int(\d+)
\d*,\d+ decimal(\d+, \d+)
m decimal(16, 2)
M decimal(20, 6)

spec-datetime

DATETIME

mark type
t datetime
t= datetime DEFAULT CURRENT_TIMESTAMP
t+ datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.