@itrocks/mysql-to-schema 0.0.9 → 0.0.10

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.
Files changed (2) hide show
  1. package/README.md +211 -0
  2. package/package.json +3 -1
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
@@ -28,6 +28,8 @@
28
28
  "keywords": [
29
29
  "backend",
30
30
  "it.rocks",
31
+ "mapping",
32
+ "mariadb",
31
33
  "mysql",
32
34
  "schema",
33
35
  "structure",
@@ -45,5 +47,5 @@
45
47
  "build:esm": "tsc -p tsconfig.esm.json"
46
48
  },
47
49
  "types": "./esm/mysql-to-schema.d.ts",
48
- "version": "0.0.9"
50
+ "version": "0.0.10"
49
51
  }