@chihqiang/sql-quicktype 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +451 -0
- package/dist/cli.js +1302 -0
- package/dist/index.d.mts +263 -0
- package/dist/index.d.ts +263 -0
- package/dist/index.global.js +117 -0
- package/dist/index.js +1216 -0
- package/dist/index.mjs +1175 -0
- package/package.json +82 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2025 chihqiang
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
# sql-quicktype
|
|
2
|
+
|
|
3
|
+
将原始 SQL DDL 转换为结构化、类型化的数据库 Schema 中间表示(IR),可直接在代码中使用和分析。
|
|
4
|
+
|
|
5
|
+
## 功能特性
|
|
6
|
+
|
|
7
|
+
- **SQL 解析器**:支持解析 MySQL、PostgreSQL、SQLite、SQL Server 等多种数据库方言的 DDL 语句
|
|
8
|
+
- **多语言代码生成**:支持生成 TypeScript、Go、GORM、XORM 等多种语言的类型定义
|
|
9
|
+
- **灵活的数据源**:支持从 SQL 字符串或数据库连接生成代码
|
|
10
|
+
- **类型安全**:生成的代码包含完整的类型信息,提高代码安全性
|
|
11
|
+
- **注释保留**:自动保留表和列的注释信息
|
|
12
|
+
- **CLI 工具**:提供命令行工具,方便快速生成代码
|
|
13
|
+
- **自定义类型解析器**:支持注入自定义的类型解析器,灵活处理特殊的 SQL 类型
|
|
14
|
+
- **类型属性支持**:支持整数类型的显示宽度、浮点数类型的精度和小数位数
|
|
15
|
+
- **现代化 API**:使用异步 API,支持动态模块导入
|
|
16
|
+
- **自动命名空间**:Go/GORM/XORM 语言自动使用默认命名空间 `models`
|
|
17
|
+
|
|
18
|
+
## 安装
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pnpm add @chihqiang/sql-quicktype
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## 使用方法
|
|
25
|
+
|
|
26
|
+
### CLI 使用
|
|
27
|
+
|
|
28
|
+
#### 从 SQL 字符串生成代码
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
# 生成 GORM 代码(默认)
|
|
32
|
+
sql-quicktype sql -s "CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(255));"
|
|
33
|
+
|
|
34
|
+
# 生成 TypeScript 代码
|
|
35
|
+
sql-quicktype sql -s "CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(255));" -l typescript -o ./output
|
|
36
|
+
|
|
37
|
+
# 生成 XORM 代码
|
|
38
|
+
sql-quicktype sql -s "CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(255));" -l xorm -o ./output
|
|
39
|
+
|
|
40
|
+
# 生成 Go 代码
|
|
41
|
+
sql-quicktype sql -s "CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(255));" -l go -o ./output
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
#### 从数据库连接生成代码
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# 从MySQL数据库生成代码(每个表生成单独的文件)
|
|
48
|
+
sql-quicktype db -h localhost -P 3306 -u user -p password -d dbname -l typescript -o ./output
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
#### 高级选项
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
# 指定数据库方言
|
|
55
|
+
sql-quicktype sql -s "CREATE TABLE users (id INT PRIMARY KEY);" -x postgres
|
|
56
|
+
|
|
57
|
+
# 指定命名空间/包名
|
|
58
|
+
sql-quicktype sql -s "CREATE TABLE users (id INT PRIMARY KEY);" -n models
|
|
59
|
+
|
|
60
|
+
# 为每个表生成单独的文件(仅 sql 命令支持)
|
|
61
|
+
sql-quicktype sql -s "CREATE TABLE users (id INT PRIMARY KEY);" -m multi -o ./output
|
|
62
|
+
|
|
63
|
+
# 指定数据库名称
|
|
64
|
+
sql-quicktype sql -s "CREATE TABLE users (id INT PRIMARY KEY);" --db-name my_database
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 编程 API 使用
|
|
68
|
+
|
|
69
|
+
#### 基本用法
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { parseSQL, GeneratorFactory } from '@chihqiang/sql-quicktype';
|
|
73
|
+
|
|
74
|
+
// 解析 SQL
|
|
75
|
+
const sql = `
|
|
76
|
+
CREATE TABLE users (
|
|
77
|
+
id INT PRIMARY KEY AUTO_INCREMENT,
|
|
78
|
+
name VARCHAR(255) NOT NULL,
|
|
79
|
+
email VARCHAR(255) UNIQUE,
|
|
80
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
81
|
+
) COMMENT '用户表';
|
|
82
|
+
`;
|
|
83
|
+
|
|
84
|
+
const dbSchema = parseSQL(sql, { dialect: 'mysql', dbName: 'my_database' });
|
|
85
|
+
|
|
86
|
+
// 生成 TypeScript 代码
|
|
87
|
+
const generator = await GeneratorFactory.createGenerator('typescript');
|
|
88
|
+
const code = generator.generateDatabase(dbSchema);
|
|
89
|
+
console.log(code);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
#### 生成不同语言的代码
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
// TypeScript
|
|
96
|
+
const tsGenerator = await GeneratorFactory.createGenerator('typescript');
|
|
97
|
+
const tsCode = tsGenerator.generateDatabase(dbSchema);
|
|
98
|
+
|
|
99
|
+
// Go
|
|
100
|
+
const goGenerator = await GeneratorFactory.createGenerator('go');
|
|
101
|
+
const goCode = goGenerator.generateDatabase(dbSchema);
|
|
102
|
+
|
|
103
|
+
// GORM
|
|
104
|
+
const gormGenerator = await GeneratorFactory.createGenerator('gorm');
|
|
105
|
+
const gormCode = gormGenerator.generateDatabase(dbSchema);
|
|
106
|
+
|
|
107
|
+
// XORM
|
|
108
|
+
const xormGenerator = await GeneratorFactory.createGenerator('xorm');
|
|
109
|
+
const xormCode = xormGenerator.generateDatabase(dbSchema);
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
#### 生成单个表的代码
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
const generator = await GeneratorFactory.createGenerator('typescript');
|
|
116
|
+
const tableCode = generator.generateTable(dbSchema.tables[0]);
|
|
117
|
+
console.log(tableCode);
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
#### 使用自定义选项
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
const generator = await GeneratorFactory.createGenerator('gorm', {
|
|
124
|
+
language: 'gorm',
|
|
125
|
+
namespace: 'models',
|
|
126
|
+
generateComments: true
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const code = generator.generateDatabase(dbSchema);
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
#### 使用自定义类型解析器
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { parseSQL, TypeResolver } from '@chihqiang/sql-quicktype';
|
|
136
|
+
|
|
137
|
+
// 定义自定义类型解析器
|
|
138
|
+
class CustomTypeResolver implements TypeResolver {
|
|
139
|
+
resolve(def: { dataType: string; length?: number | number[]; scale?: number; }) {
|
|
140
|
+
const dt = def.dataType.toLowerCase();
|
|
141
|
+
|
|
142
|
+
// 自定义处理 'decimal' 类型
|
|
143
|
+
if (dt === 'decimal') {
|
|
144
|
+
return {
|
|
145
|
+
kind: 'decimal' as const,
|
|
146
|
+
precision: 19,
|
|
147
|
+
scale: 4
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 对于其他类型,返回 null,让默认解析器处理
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// 解析 SQL,传递自定义类型解析器
|
|
157
|
+
const dbSchema = parseSQL(sql, {
|
|
158
|
+
dialect: 'mysql',
|
|
159
|
+
dbName: 'test_db',
|
|
160
|
+
typeResolvers: [new CustomTypeResolver()]
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
#### 从文件读取 SQL
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
import { ReaderFactory } from '@chihqiang/sql-quicktype';
|
|
168
|
+
|
|
169
|
+
// 从文件读取
|
|
170
|
+
const fileReader = ReaderFactory.createReader({
|
|
171
|
+
type: 'file',
|
|
172
|
+
source: './schema.sql'
|
|
173
|
+
});
|
|
174
|
+
const sql = await fileReader.read();
|
|
175
|
+
|
|
176
|
+
// 从字符串读取
|
|
177
|
+
const stringReader = ReaderFactory.createReader({
|
|
178
|
+
type: 'string',
|
|
179
|
+
source: 'CREATE TABLE users (id INT PRIMARY KEY);'
|
|
180
|
+
});
|
|
181
|
+
const sql = await stringReader.read();
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### 使用 generateCode 函数
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
import { generateCode } from '@chihqiang/sql-quicktype';
|
|
188
|
+
|
|
189
|
+
// 定义 SQL
|
|
190
|
+
const sql = `
|
|
191
|
+
CREATE TABLE users (
|
|
192
|
+
id INT PRIMARY KEY AUTO_INCREMENT,
|
|
193
|
+
name VARCHAR(255) NOT NULL,
|
|
194
|
+
email VARCHAR(255) UNIQUE,
|
|
195
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
196
|
+
) COMMENT '用户表';
|
|
197
|
+
`;
|
|
198
|
+
|
|
199
|
+
// 生成 TypeScript 代码
|
|
200
|
+
const tsCode = await generateCode(sql, {
|
|
201
|
+
language: 'typescript',
|
|
202
|
+
namespace: 'models',
|
|
203
|
+
dialect: 'mysql',
|
|
204
|
+
dbName: 'my_database'
|
|
205
|
+
});
|
|
206
|
+
console.log(tsCode);
|
|
207
|
+
|
|
208
|
+
// 生成 GORM 代码
|
|
209
|
+
const gormCode = await generateCode(sql, {
|
|
210
|
+
language: 'gorm',
|
|
211
|
+
namespace: 'models',
|
|
212
|
+
dialect: 'mysql',
|
|
213
|
+
dbName: 'my_database'
|
|
214
|
+
});
|
|
215
|
+
console.log(gormCode);
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## 支持的 SQL 类型
|
|
219
|
+
|
|
220
|
+
### 基本类型
|
|
221
|
+
|
|
222
|
+
| SQL 类型 | TypeScript | Go | 说明 |
|
|
223
|
+
|---------|-----------|-----|------|
|
|
224
|
+
| INT | number | int | 整数 |
|
|
225
|
+
| BIGINT | number | int64 | 大整数 |
|
|
226
|
+
| FLOAT | number | float32 | 浮点数 |
|
|
227
|
+
| DOUBLE | number | float64 | 双精度浮点数 |
|
|
228
|
+
| DECIMAL | number | float64 | 精确小数 |
|
|
229
|
+
| VARCHAR | string | string | 可变长度字符串 |
|
|
230
|
+
| TEXT | string | string | 文本 |
|
|
231
|
+
| LONGTEXT | string | string | 长文本 |
|
|
232
|
+
| BOOLEAN | boolean | bool | 布尔值 |
|
|
233
|
+
| DATE | Date | string | 日期 |
|
|
234
|
+
| DATETIME | Date | string | 日期时间 |
|
|
235
|
+
| TIMESTAMP | Date | string | 时间戳 |
|
|
236
|
+
| JSON | unknown | string | JSON 数据 |
|
|
237
|
+
| ENUM | string literal union | string | 枚举 |
|
|
238
|
+
|
|
239
|
+
### 特殊类型
|
|
240
|
+
|
|
241
|
+
- **BLOB**:映射为 text/string
|
|
242
|
+
- **TIME**:映射为 text/string
|
|
243
|
+
- **YEAR**:映射为 int
|
|
244
|
+
- **TINYINT**:长度为 1 时映射为 boolean,否则为 int
|
|
245
|
+
- **SMALLINT**:映射为 int
|
|
246
|
+
- **MEDIUMINT**:映射为 int
|
|
247
|
+
|
|
248
|
+
## CLI 选项
|
|
249
|
+
|
|
250
|
+
### sql 命令选项
|
|
251
|
+
|
|
252
|
+
从 SQL 字符串生成代码。
|
|
253
|
+
|
|
254
|
+
| 选项 | 简写 | 说明 | 默认值 |
|
|
255
|
+
|------|------|------|--------|
|
|
256
|
+
| --sql | -s | SQL 字符串(必需) | - |
|
|
257
|
+
| --output | -o | 输出目录 | ./output |
|
|
258
|
+
| --language | -l | 目标语言(go/gorm/typescript/xorm) | gorm |
|
|
259
|
+
| --mode | -m | 输出模式(single/multi) | single |
|
|
260
|
+
| --namespace | -n | 命名空间/包名(go/gorm/xorm 默认:models) | - |
|
|
261
|
+
| --dialect | -x | SQL 方言(mysql/postgres/sqlite/sqlserver) | mysql |
|
|
262
|
+
| --db-name | - | 数据库名称 | my_database |
|
|
263
|
+
|
|
264
|
+
### db 命令选项
|
|
265
|
+
|
|
266
|
+
从数据库连接生成代码(每个表生成单独的文件)。
|
|
267
|
+
|
|
268
|
+
| 选项 | 简写 | 说明 | 默认值 |
|
|
269
|
+
|------|------|------|--------|
|
|
270
|
+
| --host | -h | MySQL数据库主机 | 127.0.0.1 |
|
|
271
|
+
| --port | -P | MySQL数据库端口 | 3306 |
|
|
272
|
+
| --user | -u | MySQL数据库用户名 | root |
|
|
273
|
+
| --password | -p | MySQL数据库密码 | 空字符串 |
|
|
274
|
+
| --database | -d | MySQL数据库名称 | test |
|
|
275
|
+
| --output | -o | 输出目录 | ./output |
|
|
276
|
+
| --language | -l | 目标语言(go/gorm/typescript/xorm) | gorm |
|
|
277
|
+
| --namespace | -n | 命名空间/包名(go/gorm/xorm 默认:models) | - |
|
|
278
|
+
| --dialect | -x | SQL 方言(mysql/postgres/sqlite/sqlserver) | mysql |
|
|
279
|
+
| --db-name | - | 数据库名称 | my_database |
|
|
280
|
+
|
|
281
|
+
## API 文档
|
|
282
|
+
|
|
283
|
+
### parseSQL(sql, options)
|
|
284
|
+
|
|
285
|
+
将 SQL 字符串解析为数据库 Schema。
|
|
286
|
+
|
|
287
|
+
**参数:**
|
|
288
|
+
|
|
289
|
+
- `sql` (string): SQL 字符串
|
|
290
|
+
- `options` (SQLParserOptions): 解析器选项
|
|
291
|
+
- `dialect` (string): 数据库方言(mysql、postgres、sqlite、sqlserver)
|
|
292
|
+
- `dbName` (string): 数据库名称
|
|
293
|
+
- `strictMode` (boolean): 是否启用严格模式(默认:false)
|
|
294
|
+
- `ignoreComments` (boolean): 是否忽略注释(默认:false)
|
|
295
|
+
- `parseForeignKeys` (boolean): 是否解析外键约束(默认:true)
|
|
296
|
+
- `parseIndexes` (boolean): 是否解析索引(默认:true)
|
|
297
|
+
- `typeResolvers` (TypeResolver[]): 自定义类型解析器数组
|
|
298
|
+
|
|
299
|
+
**返回值:**
|
|
300
|
+
|
|
301
|
+
- `DatabaseSchema`: 数据库 Schema 对象
|
|
302
|
+
|
|
303
|
+
### TypeResolver 接口
|
|
304
|
+
|
|
305
|
+
自定义类型解析器接口,用于扩展默认的类型解析逻辑。
|
|
306
|
+
|
|
307
|
+
**方法:**
|
|
308
|
+
|
|
309
|
+
- `resolve(def)`: 解析 SQL 类型定义为 SQLType
|
|
310
|
+
- `def` (object): 列定义对象
|
|
311
|
+
- `dataType` (string): SQL 数据类型
|
|
312
|
+
- `length` (number | number[]): 长度信息
|
|
313
|
+
- `scale` (number): 小数位数
|
|
314
|
+
- `unsigned` (boolean): 是否无符号
|
|
315
|
+
- `generated` (boolean): 是否为生成列
|
|
316
|
+
- **返回值** (SQLType | null): 解析后的 SQLType 对象,返回 null 则使用默认解析器
|
|
317
|
+
|
|
318
|
+
### generateCode(sql, options)
|
|
319
|
+
|
|
320
|
+
从 SQL 模式生成指定语言的代码。
|
|
321
|
+
|
|
322
|
+
**参数:**
|
|
323
|
+
|
|
324
|
+
- `sql` (string): SQL 模式字符串
|
|
325
|
+
- `options` (object): 生成选项
|
|
326
|
+
- `language` (string): 目标语言(typescript、go、gorm、xorm)
|
|
327
|
+
- `namespace` (string): 命名空间/包名(可选)
|
|
328
|
+
- `dialect` (string): SQL 方言(可选,默认:mysql)
|
|
329
|
+
- `dbName` (string): 数据库名称(可选,默认:my_database)
|
|
330
|
+
|
|
331
|
+
**返回值:**
|
|
332
|
+
|
|
333
|
+
- `Promise<string>`: 生成的代码字符串
|
|
334
|
+
|
|
335
|
+
### GeneratorFactory.createGenerator(language, options)
|
|
336
|
+
|
|
337
|
+
创建语言生成器实例。
|
|
338
|
+
|
|
339
|
+
**参数:**
|
|
340
|
+
|
|
341
|
+
- `language` (string): 目标语言(typescript、go、gorm、xorm)
|
|
342
|
+
- `options` (Options): 生成器选项
|
|
343
|
+
- `language` (string): 目标语言
|
|
344
|
+
- `generateComments` (boolean): 是否生成注释
|
|
345
|
+
- `namespace` (string): 命名空间/包名
|
|
346
|
+
|
|
347
|
+
**返回值:**
|
|
348
|
+
|
|
349
|
+
- `Promise<AGenerator>`: 生成器实例的 Promise
|
|
350
|
+
|
|
351
|
+
### ReaderFactory.createReader(options)
|
|
352
|
+
|
|
353
|
+
创建读取器实例。
|
|
354
|
+
|
|
355
|
+
**参数:**
|
|
356
|
+
|
|
357
|
+
- `options` (ReaderOptions): 读取器选项
|
|
358
|
+
- `type` (string): 读取器类型(string、file)
|
|
359
|
+
- `source` (string): 数据源
|
|
360
|
+
|
|
361
|
+
**返回值:**
|
|
362
|
+
|
|
363
|
+
- `Reader`: 读取器实例
|
|
364
|
+
|
|
365
|
+
## 示例
|
|
366
|
+
|
|
367
|
+
### 示例 1:生成 TypeScript 接口
|
|
368
|
+
|
|
369
|
+
输入 SQL:
|
|
370
|
+
|
|
371
|
+
```sql
|
|
372
|
+
CREATE TABLE users (
|
|
373
|
+
id INT PRIMARY KEY AUTO_INCREMENT,
|
|
374
|
+
name VARCHAR(255) NOT NULL COMMENT '用户名',
|
|
375
|
+
email VARCHAR(255) UNIQUE COMMENT '用户邮箱',
|
|
376
|
+
age INT COMMENT '用户年龄',
|
|
377
|
+
is_active BOOLEAN DEFAULT TRUE COMMENT '是否激活',
|
|
378
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间'
|
|
379
|
+
) COMMENT '用户表';
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
输出 TypeScript:
|
|
383
|
+
|
|
384
|
+
```typescript
|
|
385
|
+
// Database: my_database
|
|
386
|
+
// Dialect: mysql
|
|
387
|
+
|
|
388
|
+
// users 表结构
|
|
389
|
+
export interface Users {
|
|
390
|
+
id?: number
|
|
391
|
+
name: string; // 用户名
|
|
392
|
+
email: string; // 用户邮箱
|
|
393
|
+
age?: number; // 用户年龄
|
|
394
|
+
isActive?: boolean; // 是否激活
|
|
395
|
+
createdAt?: Date; // 创建时间
|
|
396
|
+
}
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
### 示例 2:生成 GORM 模型
|
|
400
|
+
|
|
401
|
+
输入 SQL:
|
|
402
|
+
|
|
403
|
+
```sql
|
|
404
|
+
CREATE TABLE users (
|
|
405
|
+
id INT PRIMARY KEY AUTO_INCREMENT,
|
|
406
|
+
name VARCHAR(255) NOT NULL,
|
|
407
|
+
email VARCHAR(255) UNIQUE
|
|
408
|
+
);
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
输出 GORM:
|
|
412
|
+
|
|
413
|
+
```go
|
|
414
|
+
package models
|
|
415
|
+
|
|
416
|
+
type Users struct {
|
|
417
|
+
ID int `gorm:"column:id;primaryKey;autoIncrement"`
|
|
418
|
+
Name string `gorm:"column:name;not null"`
|
|
419
|
+
Email string `gorm:"column:email;unique"`
|
|
420
|
+
}
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
### 示例 3:生成 XORM 模型
|
|
424
|
+
|
|
425
|
+
输入 SQL:
|
|
426
|
+
|
|
427
|
+
```sql
|
|
428
|
+
CREATE TABLE users (
|
|
429
|
+
id INT PRIMARY KEY AUTO_INCREMENT,
|
|
430
|
+
name VARCHAR(255) NOT NULL
|
|
431
|
+
);
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
输出 XORM:
|
|
435
|
+
|
|
436
|
+
```go
|
|
437
|
+
package models
|
|
438
|
+
|
|
439
|
+
type Users struct {
|
|
440
|
+
ID int `xorm:"'id' pk autoincr"`
|
|
441
|
+
Name string `xorm:"'name' notnull"`
|
|
442
|
+
}
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
## 贡献
|
|
446
|
+
|
|
447
|
+
欢迎提交 [Issue](https://github.com/chihqiang/sql-quicktype/issues) 和 [Pull Request](https://github.com/chihqiang/sql-quicktype/pulls)!
|
|
448
|
+
|
|
449
|
+
## 许可证
|
|
450
|
+
|
|
451
|
+
[Apache-2.0](https://github.com/chihqiang/sql-quicktype/blob/main/LICENSE)
|