@itrocks/mysql-to-schema 0.0.10 → 0.1.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/cjs/to-type.js CHANGED
@@ -80,7 +80,7 @@ class ToType {
80
80
  case 'text':
81
81
  case 'varbinary':
82
82
  case 'varchar':
83
- return 65_535;
83
+ return 65535;
84
84
  case 'date':
85
85
  return 10;
86
86
  case 'datetime':
@@ -93,16 +93,16 @@ class ToType {
93
93
  return 53;
94
94
  case 'enum':
95
95
  case 'set':
96
- return 1_048_575;
96
+ return 1048575;
97
97
  case 'int':
98
98
  case 'integer':
99
99
  return 10;
100
100
  case 'longblob':
101
101
  case 'longtext':
102
- return 4_294_967_295;
102
+ return 4294967295;
103
103
  case 'mediumblob':
104
104
  case 'mediumtext':
105
- return 16_777_215;
105
+ return 16777215;
106
106
  case 'mediumint':
107
107
  return 8;
108
108
  case 'smallint':
@@ -152,12 +152,12 @@ class ToType {
152
152
  case 'string':
153
153
  if (type.length === undefined)
154
154
  Object.assign(type, { length: 255, variableLength: true });
155
- else if (type.length > 16_777_215)
156
- type.length = 4_294_967_295;
157
- else if (type.length > 65_535)
158
- type.length = 16_777_215;
155
+ else if (type.length > 16777215)
156
+ type.length = 4294967295;
157
+ else if (type.length > 65535)
158
+ type.length = 16777215;
159
159
  else if (type.length > 255)
160
- type.length = 65_535;
160
+ type.length = 65535;
161
161
  else if (type.length > 3)
162
162
  Object.assign(type, { length: 255, variableLength: true });
163
163
  break;
@@ -190,7 +190,7 @@ class ToType {
190
190
  break;
191
191
  case 'enum':
192
192
  case 'set':
193
- type.length = 1_048_575;
193
+ type.length = 1048575;
194
194
  break;
195
195
  case 'time':
196
196
  type.length = 8; // missing fractional seconds
package/package.json CHANGED
@@ -9,20 +9,17 @@
9
9
  },
10
10
  "description": "Extracts a MySQL table structure and converts it into a table schema",
11
11
  "devDependencies": {
12
- "typescript": "~5.9"
12
+ "typescript": "^7.0"
13
13
  },
14
14
  "engines": {
15
- "node": ">=18"
16
- },
17
- "exports": {
18
- "import": "./esm/mysql-to-schema.js",
19
- "require": "./cjs/mysql-to-schema.js"
15
+ "node": ">=24"
20
16
  },
17
+ "exports": "./cjs/mysql-to-schema.js",
21
18
  "files": [
22
19
  "LICENSE",
23
20
  "README.md",
24
- "*/*.d.ts",
25
- "*/*.js"
21
+ "cjs",
22
+ "!*.map"
26
23
  ],
27
24
  "homepage": "https://it.rocks",
28
25
  "keywords": [
@@ -42,10 +39,7 @@
42
39
  "url": "git+https://github.com/itrocks-ts/mysql-to-schema.git"
43
40
  },
44
41
  "scripts": {
45
- "build": "npm run build:cjs && npm run build:esm",
46
- "build:cjs": "tsc -p tsconfig.cjs.json",
47
- "build:esm": "tsc -p tsconfig.esm.json"
42
+ "build": "tsc"
48
43
  },
49
- "types": "./esm/mysql-to-schema.d.ts",
50
- "version": "0.0.10"
44
+ "version": "0.1.1"
51
45
  }
@@ -1 +0,0 @@
1
- export { ToTable as MysqlToTable } from './to-table';
@@ -1,2 +0,0 @@
1
- export { ToTable as MysqlToTable } from './to-table';
2
- //# sourceMappingURL=mysql-to-schema.js.map
@@ -1,21 +0,0 @@
1
- import { Column } from '@itrocks/schema';
2
- import { Connection } from 'mariadb';
3
- import { ToType } from './to-type';
4
- export interface MysqlColumn {
5
- CHARACTER_SET_NAME: string;
6
- COLLATION_NAME: string;
7
- COLUMN_DEFAULT: string;
8
- COLUMN_KEY: '' | 'MUL' | 'PRI' | 'UNI';
9
- COLUMN_NAME: string;
10
- COLUMN_TYPE: string;
11
- EXTRA: string;
12
- IS_NULLABLE: 'NO' | 'YES';
13
- }
14
- export declare class ToColumn {
15
- connection: Connection;
16
- toType: ToType;
17
- constructor(connection: Connection, toType?: ToType);
18
- convert(columnName: string, tableName: string, databaseName?: string): Promise<Column>;
19
- convertMultiple(tableName: string, databaseName?: string): Promise<Column[]>;
20
- rowToColumn(row: MysqlColumn): Column;
21
- }
package/esm/to-column.js DELETED
@@ -1,47 +0,0 @@
1
- import { Column } from '@itrocks/schema';
2
- import { ToType } from './to-type';
3
- export class ToColumn {
4
- connection;
5
- toType;
6
- constructor(connection, toType = new ToType()) {
7
- this.connection = connection;
8
- this.toType = toType;
9
- }
10
- async convert(columnName, tableName, databaseName) {
11
- databaseName = databaseName ? `'${databaseName}'` : 'DATABASE()';
12
- const query = 'SELECT'
13
- + ' CHARACTER_SET_NAME, COLLATION_NAME, COLUMN_DEFAULT, COLUMN_KEY, COLUMN_NAME, COLUMN_TYPE, EXTRA, IS_NULLABLE'
14
- + ' FROM information_schema.COLUMNS'
15
- + ` WHERE COLUMN_NAME = '${columnName}' AND TABLE_NAME = '${tableName}' AND TABLE_SCHEMA = ${databaseName}`;
16
- return this.rowToColumn((await this.connection.query(query))[0]);
17
- }
18
- async convertMultiple(tableName, databaseName) {
19
- databaseName = databaseName ? `'${databaseName}'` : 'DATABASE()';
20
- const query = 'SELECT'
21
- + ' CHARACTER_SET_NAME, COLLATION_NAME, COLUMN_DEFAULT, COLUMN_KEY, COLUMN_NAME, COLUMN_TYPE, EXTRA, IS_NULLABLE'
22
- + ' FROM information_schema.COLUMNS'
23
- + ` WHERE TABLE_NAME = '${tableName}' AND TABLE_SCHEMA = ${databaseName}`
24
- + ' ORDER BY COLUMN_NAME';
25
- const columns = [];
26
- for (const row of await this.connection.query(query)) {
27
- columns.push(this.rowToColumn(row));
28
- }
29
- return columns;
30
- }
31
- rowToColumn(row) {
32
- const type = this.toType.convert(row.COLUMN_TYPE);
33
- if (row.COLLATION_NAME) {
34
- type.collate = row.COLLATION_NAME;
35
- }
36
- const column = new Column(row.COLUMN_NAME, type, {
37
- autoIncrement: row.EXTRA.includes('auto_increment'),
38
- canBeNull: row.IS_NULLABLE === 'YES',
39
- default: row.COLUMN_DEFAULT
40
- });
41
- if (((column.default === null) || (column.default === undefined)) && !column.canBeNull) {
42
- column.default = (type.name === 'string') ? '' : undefined;
43
- }
44
- return column;
45
- }
46
- }
47
- //# sourceMappingURL=to-column.js.map
package/esm/to-index.d.ts DELETED
@@ -1,24 +0,0 @@
1
- import { Index } from '@itrocks/schema';
2
- import { IndexKey } from '@itrocks/schema';
3
- import { IndexType } from '@itrocks/schema';
4
- import { Connection } from 'mariadb';
5
- export interface MysqlIndex {
6
- INDEX_NAME: string;
7
- INDEX_TYPE: IndexType;
8
- NON_UNIQUE: boolean;
9
- }
10
- export interface MysqlIndexKey {
11
- COLUMN_NAME: string;
12
- SEQ_IN_INDEX: number;
13
- SUB_PART: number | null;
14
- }
15
- export interface MysqlIndexRow extends MysqlIndex, MysqlIndexKey {
16
- }
17
- export declare class ToIndex {
18
- connection: Connection;
19
- constructor(connection: Connection);
20
- convert(indexName: string, tableName: string, databaseName?: string): Promise<Index>;
21
- convertMultiple(tableName: string, databaseName?: string): Promise<Index[]>;
22
- rowsToIndex(...rows: MysqlIndexRow[]): Index;
23
- rowToKey(row: MysqlIndexKey): IndexKey;
24
- }
package/esm/to-index.js DELETED
@@ -1,50 +0,0 @@
1
- import { Index } from '@itrocks/schema';
2
- import { IndexKey } from '@itrocks/schema';
3
- export class ToIndex {
4
- connection;
5
- constructor(connection) {
6
- this.connection = connection;
7
- }
8
- async convert(indexName, tableName, databaseName) {
9
- databaseName = databaseName ? `'${databaseName}'` : 'DATABASE()';
10
- const query = 'SELECT COLUMN_NAME, INDEX_NAME, INDEX_TYPE, NON_UNIQUE, SEQ_IN_INDEX, SUB_PART'
11
- + ' FROM information_schema.STATISTICS'
12
- + ` WHERE INDEX_NAME = '${indexName}' AND TABLE_SCHEMA = ${databaseName} AND TABLE_NAME = '${tableName}'`
13
- + ' ORDER BY SEQ_IN_INDEX';
14
- return this.rowsToIndex(...await this.connection.query(query));
15
- }
16
- async convertMultiple(tableName, databaseName) {
17
- databaseName = databaseName ? `'${databaseName}'` : 'DATABASE()';
18
- const query = 'SELECT COLUMN_NAME, INDEX_NAME, INDEX_TYPE, NON_UNIQUE, SEQ_IN_INDEX, SUB_PART'
19
- + ' FROM information_schema.STATISTICS'
20
- + ` WHERE TABLE_SCHEMA = ${databaseName} AND TABLE_NAME = '${tableName}'`
21
- + ' ORDER BY INDEX_NAME, SEQ_IN_INDEX';
22
- const indexes = [];
23
- let lastRow = undefined;
24
- const rows = [];
25
- for (const row of await this.connection.query(query)) {
26
- if (lastRow && (row.INDEX_NAME !== lastRow.INDEX_NAME)) {
27
- indexes.push(this.rowsToIndex(...rows));
28
- rows.length = 0;
29
- }
30
- rows.push(row);
31
- lastRow = row;
32
- }
33
- if (rows.length) {
34
- indexes.push(this.rowsToIndex(...rows));
35
- }
36
- return indexes;
37
- }
38
- rowsToIndex(...rows) {
39
- const keys = [];
40
- for (const row of rows) {
41
- keys.push(this.rowToKey(row));
42
- }
43
- const row = rows[0];
44
- return new Index(row.INDEX_NAME, keys, { type: row.NON_UNIQUE ? 'key' : ((row.INDEX_NAME === 'PRIMARY') ? 'primary' : 'unique'), unique: !row.NON_UNIQUE });
45
- }
46
- rowToKey(row) {
47
- return new IndexKey(row.COLUMN_NAME, row.SUB_PART ?? undefined);
48
- }
49
- }
50
- //# sourceMappingURL=to-index.js.map
package/esm/to-table.d.ts DELETED
@@ -1,19 +0,0 @@
1
- import { Table } from '@itrocks/schema';
2
- import { Connection } from 'mariadb';
3
- import { ToColumn } from './to-column';
4
- import { ToIndex } from './to-index';
5
- export interface MysqlTable {
6
- TABLE_COLLATION: string;
7
- ENGINE: 'ARCHIVE' | 'BDB' | 'CSV' | 'FEDERATED' | 'InnoDB' | 'MyISAM' | 'MEMORY' | 'MERGE' | 'NDBCluster';
8
- TABLE_NAME: string;
9
- }
10
- export declare class ToTable {
11
- connection: Connection;
12
- toColumn: ToColumn;
13
- toIndex: ToIndex;
14
- constructor(connection: Connection);
15
- convert(tableName: string, databaseName?: string): Promise<Table>;
16
- convertMultiple(databaseName?: string): Promise<Table[]>;
17
- rowToTable(row: MysqlTable, databaseName?: string): Promise<Table>;
18
- normalize(table: Table): void;
19
- }
package/esm/to-table.js DELETED
@@ -1,47 +0,0 @@
1
- import { Table } from '@itrocks/schema';
2
- import { ToColumn } from './to-column';
3
- import { ToIndex } from './to-index';
4
- export class ToTable {
5
- connection;
6
- toColumn;
7
- toIndex;
8
- constructor(connection) {
9
- this.connection = connection;
10
- this.toColumn = new ToColumn(this.connection);
11
- this.toIndex = new ToIndex(this.connection);
12
- }
13
- async convert(tableName, databaseName) {
14
- const databaseSql = databaseName ? `'${databaseName}'` : 'DATABASE()';
15
- const query = 'SELECT ENGINE, TABLE_COLLATION, TABLE_NAME'
16
- + ' FROM information_schema.TABLES'
17
- + ` WHERE TABLE_NAME = '${tableName}' AND TABLE_SCHEMA = ${databaseSql}`;
18
- return this.rowToTable((await this.connection.query(query))[0], databaseName);
19
- }
20
- async convertMultiple(databaseName) {
21
- const databaseSql = databaseName ? `'${databaseName}'` : 'DATABASE()';
22
- const query = 'SELECT ENGINE, TABLE_COLLATION, TABLE_NAME'
23
- + ' FROM information_schema.TABLES'
24
- + ` WHERE TABLE_SCHEMA = ${databaseSql}`
25
- + ' ORDER BY TABLE_NAME';
26
- const tables = [];
27
- for (const row of await this.connection.query(query)) {
28
- tables.push(this.rowToTable(row, databaseName));
29
- }
30
- return Promise.all(tables);
31
- }
32
- async rowToTable(row, databaseName) {
33
- return new Table(row.TABLE_NAME, {
34
- collation: row.TABLE_COLLATION,
35
- columns: await this.toColumn.convertMultiple(row.TABLE_NAME, databaseName),
36
- engine: row.ENGINE,
37
- indexes: await this.toIndex.convertMultiple(row.TABLE_NAME, databaseName)
38
- });
39
- }
40
- normalize(table) {
41
- const toType = this.toColumn.toType;
42
- for (const column of table.columns) {
43
- toType.normalize(column.type);
44
- }
45
- }
46
- }
47
- //# sourceMappingURL=to-table.js.map
package/esm/to-type.d.ts DELETED
@@ -1,11 +0,0 @@
1
- import { Type } from '@itrocks/schema';
2
- export declare class ToType {
3
- convert(mysqlType: string): Type;
4
- defaultLength(rawType: string): number | undefined;
5
- defaultPrecision(rawType: string): number | undefined;
6
- length(rawType: string, mysqlType: string): number | undefined;
7
- lengthMaxValue(mysqlType: string, signed: boolean): bigint | number | undefined;
8
- normalize(type: Type): void;
9
- precision(rawType: string, mysqlType: string): number | undefined;
10
- values(mysqlType: string): string[];
11
- }
package/esm/to-type.js DELETED
@@ -1,221 +0,0 @@
1
- import { Type } from '@itrocks/schema';
2
- export class ToType {
3
- convert(mysqlType) {
4
- const rawType = mysqlType.substring(0, Math.min((mysqlType + '(').indexOf('('), (mysqlType + ' ').indexOf(' ')));
5
- switch (rawType) {
6
- case 'binary':
7
- case 'blob':
8
- case 'longblob':
9
- case 'mediumblob':
10
- case 'tinyblob':
11
- case 'varbinary':
12
- return new Type('blob', {
13
- length: this.length(rawType, mysqlType),
14
- variableLength: rawType.startsWith('var')
15
- });
16
- case 'bit':
17
- return new Type('bit');
18
- case 'boolean':
19
- return new Type('boolean');
20
- case 'date':
21
- return new Type('date', { length: 10 });
22
- case 'datetime':
23
- return new Type('datetime', { length: 19 });
24
- case 'decimal':
25
- case 'double':
26
- case 'float':
27
- case 'numeric':
28
- case 'real':
29
- return new Type((rawType === 'decimal') ? 'decimal' : 'float', {
30
- length: this.length(rawType, mysqlType),
31
- precision: this.precision(rawType, mysqlType),
32
- signed: !mysqlType.includes('unsigned'),
33
- zeroFill: mysqlType.includes('zerofill')
34
- });
35
- case 'bigint':
36
- case 'int':
37
- case 'integer':
38
- case 'mediumint':
39
- case 'smallint':
40
- case 'tinyint':
41
- return new Type('integer', {
42
- length: this.length(rawType, mysqlType),
43
- maxValue: this.lengthMaxValue(mysqlType, !mysqlType.includes('unsigned')),
44
- signed: !mysqlType.includes('unsigned'),
45
- zeroFill: mysqlType.includes('zerofill')
46
- });
47
- case 'enum':
48
- case 'set':
49
- return new Type(rawType, {
50
- values: this.values(mysqlType)
51
- });
52
- case 'time':
53
- return new Type('time');
54
- case 'timestamp':
55
- return new Type('timestamp');
56
- case 'year':
57
- return new Type('year');
58
- }
59
- // char, longtext, mediumtext, text, tinytext, varchar
60
- return new Type('string', {
61
- length: this.length(rawType, mysqlType),
62
- variableLength: rawType.startsWith('var')
63
- });
64
- }
65
- defaultLength(rawType) {
66
- switch (rawType) {
67
- case 'bigint':
68
- return 20;
69
- case 'binary':
70
- case 'char':
71
- case 'tinyblob':
72
- case 'tinytext':
73
- return 255;
74
- case 'bit':
75
- return 64;
76
- case 'blob':
77
- case 'text':
78
- case 'varbinary':
79
- case 'varchar':
80
- return 65_535;
81
- case 'date':
82
- return 10;
83
- case 'datetime':
84
- return 19; // missing fractional seconds
85
- case 'decimal':
86
- case 'numeric':
87
- return 65;
88
- case 'double':
89
- case 'float':
90
- return 53;
91
- case 'enum':
92
- case 'set':
93
- return 1_048_575;
94
- case 'int':
95
- case 'integer':
96
- return 10;
97
- case 'longblob':
98
- case 'longtext':
99
- return 4_294_967_295;
100
- case 'mediumblob':
101
- case 'mediumtext':
102
- return 16_777_215;
103
- case 'mediumint':
104
- return 8;
105
- case 'smallint':
106
- return 5;
107
- case 'time':
108
- return 8; // missing fractional seconds
109
- case 'tinyint':
110
- return 3;
111
- case 'year':
112
- return 4;
113
- }
114
- }
115
- defaultPrecision(rawType) {
116
- switch (rawType) {
117
- case 'decimal':
118
- case 'numeric':
119
- return 30;
120
- case 'double':
121
- case 'float':
122
- case 'real':
123
- return 53;
124
- }
125
- }
126
- length(rawType, mysqlType) {
127
- const position = mysqlType.indexOf('(') + 1;
128
- return position
129
- ? +mysqlType.substring(position, Math.min(mysqlType.indexOf(')', position), (mysqlType + ',').indexOf(',', position)))
130
- : this.defaultLength(rawType);
131
- }
132
- lengthMaxValue(mysqlType, signed) {
133
- switch (mysqlType) {
134
- case 'tinyint': return signed ? 127 : 255;
135
- case 'smallint': return signed ? 32767 : 65535;
136
- case 'mediumint': return signed ? 8388607 : 16777215;
137
- case 'int':
138
- case 'integer': return signed ? 2147483647 : 4294967295;
139
- case 'bigint': return signed ? (2n ** 63n - 1n) : (2n ** 64n - 1n);
140
- }
141
- return undefined;
142
- }
143
- normalize(type) {
144
- switch (type.name) {
145
- case 'bit':
146
- type.length = 64;
147
- break;
148
- case 'blob':
149
- case 'string':
150
- if (type.length === undefined)
151
- Object.assign(type, { length: 255, variableLength: true });
152
- else if (type.length > 16_777_215)
153
- type.length = 4_294_967_295;
154
- else if (type.length > 65_535)
155
- type.length = 16_777_215;
156
- else if (type.length > 255)
157
- type.length = 65_535;
158
- else if (type.length > 3)
159
- Object.assign(type, { length: 255, variableLength: true });
160
- break;
161
- case 'boolean':
162
- type.length = 1;
163
- break;
164
- case 'date':
165
- type.length = 10;
166
- break;
167
- case 'datetime':
168
- type.length = 19; // missing fractional seconds
169
- break;
170
- case 'float':
171
- type.length = 53;
172
- break;
173
- case 'integer':
174
- if (type.length === undefined) {
175
- type.length = (type.maxValue === undefined) ? 20 : type.maxValue.toString().length;
176
- }
177
- if (type.length > 10)
178
- type.length = 20;
179
- else if (type.length > 8)
180
- type.length = 10;
181
- else if (type.length > 5)
182
- type.length = 8;
183
- else if (type.length > 3)
184
- type.length = 5;
185
- else
186
- type.length = 3;
187
- break;
188
- case 'enum':
189
- case 'set':
190
- type.length = 1_048_575;
191
- break;
192
- case 'time':
193
- type.length = 8; // missing fractional seconds
194
- break;
195
- case 'year':
196
- type.length = 4;
197
- break;
198
- }
199
- }
200
- precision(rawType, mysqlType) {
201
- const position = mysqlType.indexOf(',', mysqlType.indexOf('(')) + 1;
202
- return position
203
- ? +mysqlType.substring(position, mysqlType.indexOf(')', position))
204
- : this.defaultPrecision(rawType);
205
- }
206
- values(mysqlType) {
207
- const values = new Array();
208
- let start = mysqlType.indexOf("'", mysqlType.indexOf('(') + 1);
209
- while (start > -1) {
210
- start++;
211
- let end = mysqlType.indexOf("'", start);
212
- while (mysqlType[end + 1] === "'") {
213
- end = mysqlType.indexOf("'", end + 2);
214
- }
215
- values.push(mysqlType.slice(start, end).replaceAll("''", "'"));
216
- start = mysqlType.indexOf("'", end + 1);
217
- }
218
- return values;
219
- }
220
- }
221
- //# sourceMappingURL=to-type.js.map