@itrocks/mysql-to-schema 0.0.9 → 0.1.0
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 +211 -0
- package/package.json +9 -13
- package/esm/mysql-to-schema.d.ts +0 -1
- package/esm/mysql-to-schema.js +0 -2
- package/esm/to-column.d.ts +0 -21
- package/esm/to-column.js +0 -47
- package/esm/to-index.d.ts +0 -24
- package/esm/to-index.js +0 -50
- package/esm/to-table.d.ts +0 -19
- package/esm/to-table.js +0 -47
- package/esm/to-type.d.ts +0 -11
- package/esm/to-type.js +0 -221
package/README.md
CHANGED
|
@@ -7,3 +7,214 @@
|
|
|
7
7
|
# mysql-to-schema
|
|
8
8
|
|
|
9
9
|
Extracts a MySQL table structure and converts it into a table schema.
|
|
10
|
+
|
|
11
|
+
*This documentation was written by an artificial intelligence and may contain errors or approximations.
|
|
12
|
+
It has not yet been fully reviewed by a human. If anything seems unclear or incomplete,
|
|
13
|
+
please feel free to contact the author of this package.*
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm i @itrocks/mysql-to-schema mariadb
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
You must also have access to a running MySQL or MariaDB server and the
|
|
22
|
+
necessary credentials to connect to it.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
`@itrocks/mysql-to-schema` exposes a single entry point,
|
|
27
|
+
`MysqlToTable`, which reads the structure of one or more tables from a
|
|
28
|
+
MySQL/MariaDB database and converts them to `Table` objects from the
|
|
29
|
+
`@itrocks/schema` package.
|
|
30
|
+
|
|
31
|
+
The resulting schema objects can then be used by other
|
|
32
|
+
`@itrocks/*` utilities (such as `@itrocks/schema-diff` or
|
|
33
|
+
`@itrocks/schema-to-mysql`) to compare, migrate, or generate database
|
|
34
|
+
structures.
|
|
35
|
+
|
|
36
|
+
### Minimal example: read one table
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import mariadb from 'mariadb'
|
|
40
|
+
import { MysqlToTable } from '@itrocks/mysql-to-schema'
|
|
41
|
+
import type { Connection } from 'mariadb'
|
|
42
|
+
|
|
43
|
+
async function main() {
|
|
44
|
+
const pool = mariadb.createPool({
|
|
45
|
+
host: 'localhost',
|
|
46
|
+
user: 'root',
|
|
47
|
+
password: 'secret',
|
|
48
|
+
database: 'my_app',
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const connection: Connection = await pool.getConnection()
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const mysqlToTable = new MysqlToTable(connection)
|
|
55
|
+
|
|
56
|
+
// Read the schema of the `user` table from the current database
|
|
57
|
+
const table = await mysqlToTable.convert('user')
|
|
58
|
+
|
|
59
|
+
console.log(table.name) // 'user'
|
|
60
|
+
console.log(table.columns) // array of Column definitions
|
|
61
|
+
console.log(table.indexes) // array of Index definitions
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
connection.release()
|
|
65
|
+
await pool.end()
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
main().catch(console.error)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Complete example: synchronize table structure with application model
|
|
73
|
+
|
|
74
|
+
The package is typically combined with
|
|
75
|
+
`@itrocks/reflect-to-schema`, `@itrocks/schema-diff`, and
|
|
76
|
+
`@itrocks/schema-to-mysql` (for instance via
|
|
77
|
+
`@itrocks/mysql-maintainer`) to keep an existing database in sync with
|
|
78
|
+
your application model.
|
|
79
|
+
|
|
80
|
+
Below is a simplified, standalone example showing the core idea:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import mariadb from 'mariadb'
|
|
84
|
+
import type { Connection } from 'mariadb'
|
|
85
|
+
import { MysqlToTable } from '@itrocks/mysql-to-schema'
|
|
86
|
+
import { ReflectToTable } from '@itrocks/reflect-to-schema'
|
|
87
|
+
import { TableDiff } from '@itrocks/schema-diff'
|
|
88
|
+
import { SchemaDiffMysql } from '@itrocks/schema-diff-mysql'
|
|
89
|
+
import { SchemaToMysql } from '@itrocks/schema-to-mysql'
|
|
90
|
+
|
|
91
|
+
class User {
|
|
92
|
+
id!: number
|
|
93
|
+
email!: string
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function synchronizeUserTable(connection: Connection) {
|
|
97
|
+
const tableName = 'user'
|
|
98
|
+
|
|
99
|
+
// Schema from the database
|
|
100
|
+
const mysqlToTable = new MysqlToTable(connection)
|
|
101
|
+
const existingSchema = await mysqlToTable.convert(tableName)
|
|
102
|
+
mysqlToTable.normalize(existingSchema)
|
|
103
|
+
|
|
104
|
+
// Schema from the TypeScript model
|
|
105
|
+
const reflectToTable = new ReflectToTable()
|
|
106
|
+
const targetSchema = reflectToTable.convert(User)
|
|
107
|
+
|
|
108
|
+
// Compute the diff and translate it to SQL
|
|
109
|
+
const diff = new TableDiff(existingSchema, targetSchema)
|
|
110
|
+
const diffToMysql = new SchemaDiffMysql()
|
|
111
|
+
const sql = diffToMysql.sql(diff, /* allowDeletions */ false)
|
|
112
|
+
|
|
113
|
+
if (sql.trim()) {
|
|
114
|
+
await connection.query(sql)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async function main() {
|
|
119
|
+
const pool = mariadb.createPool({
|
|
120
|
+
host: 'localhost',
|
|
121
|
+
user: 'root',
|
|
122
|
+
password: 'secret',
|
|
123
|
+
database: 'my_app',
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
const connection = await pool.getConnection()
|
|
127
|
+
try {
|
|
128
|
+
await synchronizeUserTable(connection)
|
|
129
|
+
}
|
|
130
|
+
finally {
|
|
131
|
+
connection.release()
|
|
132
|
+
await pool.end()
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
main().catch(console.error)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
In real-world applications you will often use
|
|
140
|
+
`@itrocks/mysql-maintainer`, which wraps this pattern for you and
|
|
141
|
+
relies on `MysqlToTable` under the hood.
|
|
142
|
+
|
|
143
|
+
## API
|
|
144
|
+
|
|
145
|
+
### `class MysqlToTable`
|
|
146
|
+
|
|
147
|
+
Main entry point of the package. Wraps a MariaDB/MySQL connection and
|
|
148
|
+
provides methods to read table structures and convert them into
|
|
149
|
+
`@itrocks/schema` `Table` instances.
|
|
150
|
+
|
|
151
|
+
`MysqlToTable` is an alias of the internal `ToTable` class exported by
|
|
152
|
+
this package.
|
|
153
|
+
|
|
154
|
+
#### Constructor
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
new MysqlToTable(connection: Connection)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Creates a converter bound to a given MariaDB connection.
|
|
161
|
+
|
|
162
|
+
##### Parameters
|
|
163
|
+
|
|
164
|
+
- `connection: Connection` – an open `mariadb` connection. The
|
|
165
|
+
instance remains the caller's responsibility: you must handle
|
|
166
|
+
creation, error management, and closing of the connection.
|
|
167
|
+
|
|
168
|
+
#### Methods
|
|
169
|
+
|
|
170
|
+
##### `convert(tableName: string, databaseName?: string): Promise<Table>`
|
|
171
|
+
|
|
172
|
+
Reads the structure of a single table from the database and converts it
|
|
173
|
+
to a `Table` instance from `@itrocks/schema`.
|
|
174
|
+
|
|
175
|
+
- `tableName` – the name of the table to inspect.
|
|
176
|
+
- `databaseName` *(optional)* – explicit database name. If omitted, the
|
|
177
|
+
current database of the connection (`DATABASE()`) is used.
|
|
178
|
+
|
|
179
|
+
The returned `Table` contains the table name, collation, engine,
|
|
180
|
+
columns, and indexes.
|
|
181
|
+
|
|
182
|
+
##### `convertMultiple(databaseName?: string): Promise<Table[]>`
|
|
183
|
+
|
|
184
|
+
Reads all tables from a database and returns an array of `Table`
|
|
185
|
+
instances.
|
|
186
|
+
|
|
187
|
+
- `databaseName` *(optional)* – explicit database name. If omitted, the
|
|
188
|
+
current database of the connection is used.
|
|
189
|
+
|
|
190
|
+
Tables are ordered by name, matching the `ORDER BY TABLE_NAME` clause
|
|
191
|
+
used internally.
|
|
192
|
+
|
|
193
|
+
##### `rowToTable(row: MysqlTable, databaseName?: string): Promise<Table>`
|
|
194
|
+
|
|
195
|
+
Low-level helper that converts a single row from
|
|
196
|
+
`information_schema.TABLES` into a `Table` instance. You typically do
|
|
197
|
+
not call this directly; it is used internally by `convert` and
|
|
198
|
+
`convertMultiple`.
|
|
199
|
+
|
|
200
|
+
##### `normalize(table: Table): void`
|
|
201
|
+
|
|
202
|
+
Normalizes the column types of a `Table` instance so that they can be
|
|
203
|
+
compared more easily with other schema representations (for instance
|
|
204
|
+
those generated by `@itrocks/reflect-to-schema`).
|
|
205
|
+
|
|
206
|
+
This mutates the given `table` object in place.
|
|
207
|
+
|
|
208
|
+
## Typical use cases
|
|
209
|
+
|
|
210
|
+
- Inspect the structure of an existing MySQL/MariaDB table from a
|
|
211
|
+
running database and expose it as a `Table` object.
|
|
212
|
+
- Generate comparison diffs between a live database and a schema
|
|
213
|
+
defined in code (for example, to migrate an existing database to a
|
|
214
|
+
new model).
|
|
215
|
+
- Build tooling that automatically synchronizes database schemas based
|
|
216
|
+
on differences between the live database and your application's
|
|
217
|
+
desired structure.
|
|
218
|
+
- List all tables in a database along with their columns, collations,
|
|
219
|
+
engines, and indexes, either for introspection tools or for
|
|
220
|
+
documentation purposes.
|
package/package.json
CHANGED
|
@@ -9,25 +9,24 @@
|
|
|
9
9
|
},
|
|
10
10
|
"description": "Extracts a MySQL table structure and converts it into a table schema",
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"typescript": "
|
|
12
|
+
"typescript": "^6.0"
|
|
13
13
|
},
|
|
14
14
|
"engines": {
|
|
15
|
-
"node": ">=
|
|
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
|
-
"
|
|
25
|
-
"
|
|
21
|
+
"cjs",
|
|
22
|
+
"!*.map"
|
|
26
23
|
],
|
|
27
24
|
"homepage": "https://it.rocks",
|
|
28
25
|
"keywords": [
|
|
29
26
|
"backend",
|
|
30
27
|
"it.rocks",
|
|
28
|
+
"mapping",
|
|
29
|
+
"mariadb",
|
|
31
30
|
"mysql",
|
|
32
31
|
"schema",
|
|
33
32
|
"structure",
|
|
@@ -40,10 +39,7 @@
|
|
|
40
39
|
"url": "git+https://github.com/itrocks-ts/mysql-to-schema.git"
|
|
41
40
|
},
|
|
42
41
|
"scripts": {
|
|
43
|
-
"build": "
|
|
44
|
-
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
45
|
-
"build:esm": "tsc -p tsconfig.esm.json"
|
|
42
|
+
"build": "tsc"
|
|
46
43
|
},
|
|
47
|
-
"
|
|
48
|
-
"version": "0.0.9"
|
|
44
|
+
"version": "0.1.0"
|
|
49
45
|
}
|
package/esm/mysql-to-schema.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { ToTable as MysqlToTable } from './to-table';
|
package/esm/mysql-to-schema.js
DELETED
package/esm/to-column.d.ts
DELETED
|
@@ -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
|