@chihqiang/sql-quicktype 0.0.1 → 0.0.3

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/README.md CHANGED
@@ -6,13 +6,14 @@
6
6
 
7
7
  - **SQL 解析器**:支持解析 MySQL、PostgreSQL、SQLite、SQL Server 等多种数据库方言的 DDL 语句
8
8
  - **多语言代码生成**:支持生成 TypeScript、Go、GORM、XORM 等多种语言的类型定义
9
- - **灵活的数据源**:支持从 SQL 字符串或数据库连接生成代码
9
+ - **灵活的数据源**:支持从 SQL 字符串、文件或数据库连接生成代码
10
10
  - **类型安全**:生成的代码包含完整的类型信息,提高代码安全性
11
11
  - **注释保留**:自动保留表和列的注释信息
12
12
  - **CLI 工具**:提供命令行工具,方便快速生成代码
13
13
  - **自定义类型解析器**:支持注入自定义的类型解析器,灵活处理特殊的 SQL 类型
14
14
  - **类型属性支持**:支持整数类型的显示宽度、浮点数类型的精度和小数位数
15
- - **现代化 API**:使用异步 API,支持动态模块导入
15
+ - **同步 API**:生成器采用同步设计,无异步开销,调用简单直接
16
+ - **并发查询**:数据库模式支持 `Promise.allSettled` 并发查询,大幅提升多表生成性能
16
17
  - **自动命名空间**:Go/GORM/XORM 语言自动使用默认命名空间 `models`
17
18
 
18
19
  ## 安装
@@ -84,7 +85,7 @@ const sql = `
84
85
  const dbSchema = parseSQL(sql, { dialect: 'mysql', dbName: 'my_database' });
85
86
 
86
87
  // 生成 TypeScript 代码
87
- const generator = await GeneratorFactory.createGenerator('typescript');
88
+ const generator = GeneratorFactory.createGenerator('typescript');
88
89
  const code = generator.generateDatabase(dbSchema);
89
90
  console.log(code);
90
91
  ```
@@ -93,26 +94,26 @@ console.log(code);
93
94
 
94
95
  ```typescript
95
96
  // TypeScript
96
- const tsGenerator = await GeneratorFactory.createGenerator('typescript');
97
+ const tsGenerator = GeneratorFactory.createGenerator('typescript');
97
98
  const tsCode = tsGenerator.generateDatabase(dbSchema);
98
99
 
99
100
  // Go
100
- const goGenerator = await GeneratorFactory.createGenerator('go');
101
+ const goGenerator = GeneratorFactory.createGenerator('go');
101
102
  const goCode = goGenerator.generateDatabase(dbSchema);
102
103
 
103
104
  // GORM
104
- const gormGenerator = await GeneratorFactory.createGenerator('gorm');
105
+ const gormGenerator = GeneratorFactory.createGenerator('gorm');
105
106
  const gormCode = gormGenerator.generateDatabase(dbSchema);
106
107
 
107
108
  // XORM
108
- const xormGenerator = await GeneratorFactory.createGenerator('xorm');
109
+ const xormGenerator = GeneratorFactory.createGenerator('xorm');
109
110
  const xormCode = xormGenerator.generateDatabase(dbSchema);
110
111
  ```
111
112
 
112
113
  #### 生成单个表的代码
113
114
 
114
115
  ```typescript
115
- const generator = await GeneratorFactory.createGenerator('typescript');
116
+ const generator = GeneratorFactory.createGenerator('typescript');
116
117
  const tableCode = generator.generateTable(dbSchema.tables[0]);
117
118
  console.log(tableCode);
118
119
  ```
@@ -120,7 +121,7 @@ console.log(tableCode);
120
121
  #### 使用自定义选项
121
122
 
122
123
  ```typescript
123
- const generator = await GeneratorFactory.createGenerator('gorm', {
124
+ const generator = GeneratorFactory.createGenerator('gorm', {
124
125
  language: 'gorm',
125
126
  namespace: 'models',
126
127
  generateComments: true
@@ -164,21 +165,13 @@ const dbSchema = parseSQL(sql, {
164
165
  #### 从文件读取 SQL
165
166
 
166
167
  ```typescript
167
- import { ReaderFactory } from '@chihqiang/sql-quicktype';
168
+ import { readSQLFromFile, readSQLFromString } from '@chihqiang/sql-quicktype';
168
169
 
169
170
  // 从文件读取
170
- const fileReader = ReaderFactory.createReader({
171
- type: 'file',
172
- source: './schema.sql'
173
- });
174
- const sql = await fileReader.read();
171
+ const sqlFromFile = await readSQLFromFile('./schema.sql');
175
172
 
176
173
  // 从字符串读取
177
- const stringReader = ReaderFactory.createReader({
178
- type: 'string',
179
- source: 'CREATE TABLE users (id INT PRIMARY KEY);'
180
- });
181
- const sql = await stringReader.read();
174
+ const sqlFromString = readSQLFromString('CREATE TABLE users (id INT PRIMARY KEY);');
182
175
  ```
183
176
 
184
177
  #### 使用 generateCode 函数
@@ -197,7 +190,7 @@ const sql = `
197
190
  `;
198
191
 
199
192
  // 生成 TypeScript 代码
200
- const tsCode = await generateCode(sql, {
193
+ const tsCode = generateCode(sql, {
201
194
  language: 'typescript',
202
195
  namespace: 'models',
203
196
  dialect: 'mysql',
@@ -206,7 +199,7 @@ const tsCode = await generateCode(sql, {
206
199
  console.log(tsCode);
207
200
 
208
201
  // 生成 GORM 代码
209
- const gormCode = await generateCode(sql, {
202
+ const gormCode = generateCode(sql, {
210
203
  language: 'gorm',
211
204
  namespace: 'models',
212
205
  dialect: 'mysql',
@@ -330,7 +323,7 @@ console.log(gormCode);
330
323
 
331
324
  **返回值:**
332
325
 
333
- - `Promise<string>`: 生成的代码字符串
326
+ - `string`: 生成的代码字符串(同步,无需 await)
334
327
 
335
328
  ### GeneratorFactory.createGenerator(language, options)
336
329
 
@@ -346,21 +339,31 @@ console.log(gormCode);
346
339
 
347
340
  **返回值:**
348
341
 
349
- - `Promise<AGenerator>`: 生成器实例的 Promise
342
+ - `BaseGenerator`: 生成器基类
350
343
 
351
- ### ReaderFactory.createReader(options)
344
+ ### readSQLFromFile(path)
352
345
 
353
- 创建读取器实例。
346
+ 从文件读取 SQL 内容。
354
347
 
355
348
  **参数:**
356
349
 
357
- - `options` (ReaderOptions): 读取器选项
358
- - `type` (string): 读取器类型(string、file)
359
- - `source` (string): 数据源
350
+ - `path` (string): SQL 文件路径
351
+
352
+ **返回值:**
353
+
354
+ - `Promise<string>`: SQL 文件内容
355
+
356
+ ### readSQLFromString(sql)
357
+
358
+ 从字符串读取 SQL 内容。
359
+
360
+ **参数:**
361
+
362
+ - `sql` (string): SQL 字符串
360
363
 
361
364
  **返回值:**
362
365
 
363
- - `Reader`: 读取器实例
366
+ - `string`: 返回原 SQL 字符串(同步,仅做空值校验)
364
367
 
365
368
  ## 示例
366
369