@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.
@@ -0,0 +1,263 @@
1
+ import { AST } from 'node-sql-parser';
2
+
3
+ interface DatabaseSchema {
4
+ name: string;
5
+ dialect: 'mysql' | 'postgres' | 'sqlite' | 'sqlserver' | string;
6
+ charset?: string;
7
+ collation?: string;
8
+ comment?: string;
9
+ tables: TableSchema[];
10
+ metadata?: {
11
+ createdAt?: string;
12
+ updatedAt?: string;
13
+ version?: string;
14
+ engine?: string;
15
+ [key: string]: any;
16
+ };
17
+ tablesMap?: Record<string, TableSchema>;
18
+ }
19
+ interface TableSchema {
20
+ name: string;
21
+ comment?: string;
22
+ schema?: string;
23
+ primaryKeys?: string[];
24
+ autoIncrement?: boolean;
25
+ columns: ColumnSchema[];
26
+ foreignKeys?: TableForeignKey[];
27
+ indexes?: TableIndex[];
28
+ }
29
+ interface TableForeignKey {
30
+ name?: string;
31
+ columns: string[];
32
+ referencedTable: string;
33
+ referencedColumns: string[];
34
+ onDelete?: string;
35
+ onUpdate?: string;
36
+ }
37
+ interface ColumnSchema {
38
+ name: string;
39
+ type: SQLType;
40
+ nullable: boolean;
41
+ primaryKey: boolean;
42
+ unique: boolean;
43
+ default?: string | null;
44
+ comment?: string;
45
+ unsigned?: boolean;
46
+ generated?: boolean;
47
+ }
48
+ interface TableIndex {
49
+ name: string;
50
+ columns: string[];
51
+ unique?: boolean;
52
+ type?: string;
53
+ }
54
+ type SQLType = IntType | BigIntType | FloatType | DecimalType | VarcharType | TextType | BooleanType | DateType | DateTimeType | JsonType | EnumType;
55
+ interface IntType {
56
+ kind: 'int';
57
+ length?: number;
58
+ }
59
+ interface BigIntType {
60
+ kind: 'bigint';
61
+ length?: number;
62
+ }
63
+ interface FloatType {
64
+ kind: 'float';
65
+ precision?: number;
66
+ scale?: number;
67
+ }
68
+ interface DecimalType {
69
+ kind: 'decimal';
70
+ precision?: number;
71
+ scale?: number;
72
+ }
73
+ interface VarcharType {
74
+ kind: 'varchar';
75
+ length?: number;
76
+ }
77
+ interface TextType {
78
+ kind: 'text';
79
+ }
80
+ interface BooleanType {
81
+ kind: 'boolean';
82
+ }
83
+ interface DateType {
84
+ kind: 'date';
85
+ }
86
+ interface DateTimeType {
87
+ kind: 'datetime';
88
+ }
89
+ interface JsonType {
90
+ kind: 'json';
91
+ }
92
+ interface EnumType {
93
+ kind: 'enum';
94
+ values: string[];
95
+ }
96
+
97
+ interface TypeResolver {
98
+ resolve(def: ColumnDefinition['definition']): SQLType | null;
99
+ }
100
+ interface SQLParserOptions {
101
+ dialect: 'mysql' | 'postgres' | 'sqlite' | 'sqlserver' | string;
102
+ dbName?: string;
103
+ strictMode?: boolean;
104
+ ignoreComments?: boolean;
105
+ parseForeignKeys?: boolean;
106
+ parseIndexes?: boolean;
107
+ typeResolvers?: TypeResolver[];
108
+ }
109
+ interface ColumnDefinition {
110
+ resource: 'column';
111
+ column: {
112
+ column: string;
113
+ };
114
+ definition: {
115
+ dataType: string;
116
+ length?: number | number[];
117
+ scale?: number;
118
+ unsigned?: boolean;
119
+ generated?: boolean;
120
+ expr?: {
121
+ value: Array<{
122
+ value?: string;
123
+ raw?: string;
124
+ }>;
125
+ };
126
+ };
127
+ nullable?: {
128
+ type: string;
129
+ };
130
+ primary_key?: boolean;
131
+ unique?: boolean;
132
+ default_val?: {
133
+ value: any;
134
+ };
135
+ comment?: {
136
+ value: {
137
+ value: string;
138
+ };
139
+ };
140
+ }
141
+ declare function parseSQL(sql: string, options?: SQLParserOptions): DatabaseSchema;
142
+ declare class SQLParser {
143
+ options: SQLParserOptions & {
144
+ strictMode: boolean;
145
+ ignoreComments: boolean;
146
+ parseForeignKeys: boolean;
147
+ parseIndexes: boolean;
148
+ typeResolvers: TypeResolver[];
149
+ };
150
+ private parser;
151
+ constructor(options?: SQLParserOptions);
152
+ parseDatabase(ast: AST[], dbName?: string): DatabaseSchema;
153
+ private isCreateTable;
154
+ private parseTable;
155
+ private parseColumn;
156
+ private parseTableConstraint;
157
+ private parseIndex;
158
+ private defaultTypeResolver;
159
+ private mapSQLType;
160
+ private parseDefault;
161
+ }
162
+
163
+ declare class XormGenerator extends AGenerator {
164
+ private options;
165
+ constructor(options?: Options);
166
+ generateDatabase(database: DatabaseSchema): string;
167
+ generateTable(table: TableSchema): string;
168
+ generateColumn(column: ColumnSchema): string;
169
+ mapSQLType(type: SQLType): string;
170
+ private generateXormTag;
171
+ }
172
+
173
+ declare class GormGenerator extends AGenerator {
174
+ private options;
175
+ constructor(options?: Options);
176
+ generateDatabase(database: DatabaseSchema): string;
177
+ generateTable(table: TableSchema): string;
178
+ generateColumn(column: ColumnSchema): string;
179
+ mapSQLType(type: SQLType): string;
180
+ private generateGormTag;
181
+ }
182
+
183
+ declare class GolangGenerator extends AGenerator {
184
+ private options;
185
+ constructor(options?: Options);
186
+ generateDatabase(database: DatabaseSchema): string;
187
+ generateTable(table: TableSchema): string;
188
+ generateColumn(column: ColumnSchema): string;
189
+ mapSQLType(type: SQLType): string;
190
+ private generateGoTag;
191
+ }
192
+
193
+ declare class TypeScriptGenerator extends AGenerator {
194
+ private options;
195
+ constructor(options?: Options);
196
+ protected formatFieldName(name: string): string;
197
+ generateDatabase(database: DatabaseSchema): string;
198
+ generateTable(table: TableSchema): string;
199
+ generateColumn(column: ColumnSchema): string;
200
+ mapSQLType(type: SQLType, columnName?: string): string;
201
+ }
202
+
203
+ interface Options {
204
+ language: 'go' | 'typescript' | 'gorm' | 'xorm';
205
+ generateComments?: boolean;
206
+ namespace?: string;
207
+ }
208
+ declare abstract class AGenerator {
209
+ abstract generateDatabase(database: DatabaseSchema): string;
210
+ abstract generateTable(table: TableSchema): string;
211
+ abstract generateColumn(column: ColumnSchema): string;
212
+ abstract mapSQLType(type: SQLType): string;
213
+ protected formatTypeName(name: string): string;
214
+ protected formatFieldName(name: string): string;
215
+ protected generateDefaultValue(column: ColumnSchema): string;
216
+ protected needsTimeImport(database: DatabaseSchema): boolean;
217
+ }
218
+ declare class GeneratorFactory {
219
+ static createGenerator(language: 'go' | 'typescript' | 'gorm' | 'xorm', options?: Options): Promise<TypeScriptGenerator | GolangGenerator | GormGenerator | XormGenerator>;
220
+ }
221
+
222
+ interface GenerateOptions {
223
+ output: string;
224
+ language: string;
225
+ mode: string;
226
+ namespace?: string;
227
+ dialect: string;
228
+ dbName: string;
229
+ }
230
+
231
+ declare function generateCode(sql: string, options: {
232
+ language: 'go' | 'typescript' | 'gorm' | 'xorm';
233
+ namespace?: string;
234
+ dialect?: string;
235
+ dbName?: string;
236
+ }): Promise<string>;
237
+
238
+ interface Reader {
239
+ read(): Promise<string>;
240
+ }
241
+
242
+ interface ReaderOptions {
243
+ type: 'string' | 'file';
244
+ source: string;
245
+ [key: string]: any;
246
+ }
247
+ declare class ReaderFactory {
248
+ static createReader(options: ReaderOptions): Reader;
249
+ }
250
+
251
+ declare class StringReader implements Reader {
252
+ private sql;
253
+ constructor(sql: string);
254
+ read(): Promise<string>;
255
+ }
256
+
257
+ declare class FileReader implements Reader {
258
+ private filePath;
259
+ constructor(filePath: string);
260
+ read(): Promise<string>;
261
+ }
262
+
263
+ export { AGenerator, type BigIntType, type BooleanType, type ColumnSchema, type DatabaseSchema, type DateTimeType, type DateType, type DecimalType, type EnumType, FileReader, type FloatType, type GenerateOptions, GeneratorFactory, GolangGenerator, GormGenerator, type IntType, type JsonType, type Reader, ReaderFactory, type ReaderOptions, SQLParser, type SQLParserOptions, type SQLType, StringReader, type TableIndex, type TableSchema, type TextType, type TypeResolver, TypeScriptGenerator, type VarcharType, XormGenerator, generateCode, parseSQL };
@@ -0,0 +1,263 @@
1
+ import { AST } from 'node-sql-parser';
2
+
3
+ interface DatabaseSchema {
4
+ name: string;
5
+ dialect: 'mysql' | 'postgres' | 'sqlite' | 'sqlserver' | string;
6
+ charset?: string;
7
+ collation?: string;
8
+ comment?: string;
9
+ tables: TableSchema[];
10
+ metadata?: {
11
+ createdAt?: string;
12
+ updatedAt?: string;
13
+ version?: string;
14
+ engine?: string;
15
+ [key: string]: any;
16
+ };
17
+ tablesMap?: Record<string, TableSchema>;
18
+ }
19
+ interface TableSchema {
20
+ name: string;
21
+ comment?: string;
22
+ schema?: string;
23
+ primaryKeys?: string[];
24
+ autoIncrement?: boolean;
25
+ columns: ColumnSchema[];
26
+ foreignKeys?: TableForeignKey[];
27
+ indexes?: TableIndex[];
28
+ }
29
+ interface TableForeignKey {
30
+ name?: string;
31
+ columns: string[];
32
+ referencedTable: string;
33
+ referencedColumns: string[];
34
+ onDelete?: string;
35
+ onUpdate?: string;
36
+ }
37
+ interface ColumnSchema {
38
+ name: string;
39
+ type: SQLType;
40
+ nullable: boolean;
41
+ primaryKey: boolean;
42
+ unique: boolean;
43
+ default?: string | null;
44
+ comment?: string;
45
+ unsigned?: boolean;
46
+ generated?: boolean;
47
+ }
48
+ interface TableIndex {
49
+ name: string;
50
+ columns: string[];
51
+ unique?: boolean;
52
+ type?: string;
53
+ }
54
+ type SQLType = IntType | BigIntType | FloatType | DecimalType | VarcharType | TextType | BooleanType | DateType | DateTimeType | JsonType | EnumType;
55
+ interface IntType {
56
+ kind: 'int';
57
+ length?: number;
58
+ }
59
+ interface BigIntType {
60
+ kind: 'bigint';
61
+ length?: number;
62
+ }
63
+ interface FloatType {
64
+ kind: 'float';
65
+ precision?: number;
66
+ scale?: number;
67
+ }
68
+ interface DecimalType {
69
+ kind: 'decimal';
70
+ precision?: number;
71
+ scale?: number;
72
+ }
73
+ interface VarcharType {
74
+ kind: 'varchar';
75
+ length?: number;
76
+ }
77
+ interface TextType {
78
+ kind: 'text';
79
+ }
80
+ interface BooleanType {
81
+ kind: 'boolean';
82
+ }
83
+ interface DateType {
84
+ kind: 'date';
85
+ }
86
+ interface DateTimeType {
87
+ kind: 'datetime';
88
+ }
89
+ interface JsonType {
90
+ kind: 'json';
91
+ }
92
+ interface EnumType {
93
+ kind: 'enum';
94
+ values: string[];
95
+ }
96
+
97
+ interface TypeResolver {
98
+ resolve(def: ColumnDefinition['definition']): SQLType | null;
99
+ }
100
+ interface SQLParserOptions {
101
+ dialect: 'mysql' | 'postgres' | 'sqlite' | 'sqlserver' | string;
102
+ dbName?: string;
103
+ strictMode?: boolean;
104
+ ignoreComments?: boolean;
105
+ parseForeignKeys?: boolean;
106
+ parseIndexes?: boolean;
107
+ typeResolvers?: TypeResolver[];
108
+ }
109
+ interface ColumnDefinition {
110
+ resource: 'column';
111
+ column: {
112
+ column: string;
113
+ };
114
+ definition: {
115
+ dataType: string;
116
+ length?: number | number[];
117
+ scale?: number;
118
+ unsigned?: boolean;
119
+ generated?: boolean;
120
+ expr?: {
121
+ value: Array<{
122
+ value?: string;
123
+ raw?: string;
124
+ }>;
125
+ };
126
+ };
127
+ nullable?: {
128
+ type: string;
129
+ };
130
+ primary_key?: boolean;
131
+ unique?: boolean;
132
+ default_val?: {
133
+ value: any;
134
+ };
135
+ comment?: {
136
+ value: {
137
+ value: string;
138
+ };
139
+ };
140
+ }
141
+ declare function parseSQL(sql: string, options?: SQLParserOptions): DatabaseSchema;
142
+ declare class SQLParser {
143
+ options: SQLParserOptions & {
144
+ strictMode: boolean;
145
+ ignoreComments: boolean;
146
+ parseForeignKeys: boolean;
147
+ parseIndexes: boolean;
148
+ typeResolvers: TypeResolver[];
149
+ };
150
+ private parser;
151
+ constructor(options?: SQLParserOptions);
152
+ parseDatabase(ast: AST[], dbName?: string): DatabaseSchema;
153
+ private isCreateTable;
154
+ private parseTable;
155
+ private parseColumn;
156
+ private parseTableConstraint;
157
+ private parseIndex;
158
+ private defaultTypeResolver;
159
+ private mapSQLType;
160
+ private parseDefault;
161
+ }
162
+
163
+ declare class XormGenerator extends AGenerator {
164
+ private options;
165
+ constructor(options?: Options);
166
+ generateDatabase(database: DatabaseSchema): string;
167
+ generateTable(table: TableSchema): string;
168
+ generateColumn(column: ColumnSchema): string;
169
+ mapSQLType(type: SQLType): string;
170
+ private generateXormTag;
171
+ }
172
+
173
+ declare class GormGenerator extends AGenerator {
174
+ private options;
175
+ constructor(options?: Options);
176
+ generateDatabase(database: DatabaseSchema): string;
177
+ generateTable(table: TableSchema): string;
178
+ generateColumn(column: ColumnSchema): string;
179
+ mapSQLType(type: SQLType): string;
180
+ private generateGormTag;
181
+ }
182
+
183
+ declare class GolangGenerator extends AGenerator {
184
+ private options;
185
+ constructor(options?: Options);
186
+ generateDatabase(database: DatabaseSchema): string;
187
+ generateTable(table: TableSchema): string;
188
+ generateColumn(column: ColumnSchema): string;
189
+ mapSQLType(type: SQLType): string;
190
+ private generateGoTag;
191
+ }
192
+
193
+ declare class TypeScriptGenerator extends AGenerator {
194
+ private options;
195
+ constructor(options?: Options);
196
+ protected formatFieldName(name: string): string;
197
+ generateDatabase(database: DatabaseSchema): string;
198
+ generateTable(table: TableSchema): string;
199
+ generateColumn(column: ColumnSchema): string;
200
+ mapSQLType(type: SQLType, columnName?: string): string;
201
+ }
202
+
203
+ interface Options {
204
+ language: 'go' | 'typescript' | 'gorm' | 'xorm';
205
+ generateComments?: boolean;
206
+ namespace?: string;
207
+ }
208
+ declare abstract class AGenerator {
209
+ abstract generateDatabase(database: DatabaseSchema): string;
210
+ abstract generateTable(table: TableSchema): string;
211
+ abstract generateColumn(column: ColumnSchema): string;
212
+ abstract mapSQLType(type: SQLType): string;
213
+ protected formatTypeName(name: string): string;
214
+ protected formatFieldName(name: string): string;
215
+ protected generateDefaultValue(column: ColumnSchema): string;
216
+ protected needsTimeImport(database: DatabaseSchema): boolean;
217
+ }
218
+ declare class GeneratorFactory {
219
+ static createGenerator(language: 'go' | 'typescript' | 'gorm' | 'xorm', options?: Options): Promise<TypeScriptGenerator | GolangGenerator | GormGenerator | XormGenerator>;
220
+ }
221
+
222
+ interface GenerateOptions {
223
+ output: string;
224
+ language: string;
225
+ mode: string;
226
+ namespace?: string;
227
+ dialect: string;
228
+ dbName: string;
229
+ }
230
+
231
+ declare function generateCode(sql: string, options: {
232
+ language: 'go' | 'typescript' | 'gorm' | 'xorm';
233
+ namespace?: string;
234
+ dialect?: string;
235
+ dbName?: string;
236
+ }): Promise<string>;
237
+
238
+ interface Reader {
239
+ read(): Promise<string>;
240
+ }
241
+
242
+ interface ReaderOptions {
243
+ type: 'string' | 'file';
244
+ source: string;
245
+ [key: string]: any;
246
+ }
247
+ declare class ReaderFactory {
248
+ static createReader(options: ReaderOptions): Reader;
249
+ }
250
+
251
+ declare class StringReader implements Reader {
252
+ private sql;
253
+ constructor(sql: string);
254
+ read(): Promise<string>;
255
+ }
256
+
257
+ declare class FileReader implements Reader {
258
+ private filePath;
259
+ constructor(filePath: string);
260
+ read(): Promise<string>;
261
+ }
262
+
263
+ export { AGenerator, type BigIntType, type BooleanType, type ColumnSchema, type DatabaseSchema, type DateTimeType, type DateType, type DecimalType, type EnumType, FileReader, type FloatType, type GenerateOptions, GeneratorFactory, GolangGenerator, GormGenerator, type IntType, type JsonType, type Reader, ReaderFactory, type ReaderOptions, SQLParser, type SQLParserOptions, type SQLType, StringReader, type TableIndex, type TableSchema, type TextType, type TypeResolver, TypeScriptGenerator, type VarcharType, XormGenerator, generateCode, parseSQL };