Converts a TypeScript class and columns structure into a RDB schema
npm install @itrocks/reflect-to-schema




Converts a TypeScript class and columns structure into a RDB schema.
*This documentation was written by an artificial intelligence and may contain errors or approximations.
It has not yet been fully reviewed by a human. If anything seems unclear or incomplete,
please feel free to contact the author of this package.*
``bash`
npm i @itrocks/reflect-to-schema
@itrocks/reflect-to-schema exposes a single public classReflectToTable, which converts a reflected TypeScript class into aTable instance from @itrocks/schema.
The package relies on the metadata provided by other @itrocks/*@itrocks/reflect
packages (, @itrocks/property-type,@itrocks/store, @itrocks/length, @itrocks/precision,@itrocks/range, @itrocks/value, etc.). You typically do not use it
in isolation, but as part of a model layer where classes are already
decorated and registered.
`ts
import { ReflectToTable } from '@itrocks/reflect-to-schema'
class User {
id!: number
email!: string
}
const reflectToTable = new ReflectToTable()
const table = reflectToTable.convert(User)
console.log(table.name) // "user"
console.log(table.columns) // array of Column definitions
console.log(table.indexes) // array of Index definitions
`
In this simple example, ReflectToTable analyses the User class
using the reflection metadata and generates:
- a default primary key column id with an auto-incrementing integeremail
type
- one column for each reflected property ( in the example),@itrocks/class-view
with type and nullability inferred from the property metadata
- primary and secondary indexes, including representative indexes if
defined by
ReflectToTable is most often used together with:
- @itrocks/mysql-to-schema – to read existing MySQL/MariaDB schemas@itrocks/schema-diff
- and @itrocks/schema-diff-mysql – to compute@itrocks/schema-to-mysql
and translate schema differences
- – to generate SQL statements@itrocks/mysql-maintainer
- – a higher-level helper that orchestrates
all these steps
The following example is adapted from the documentation of
@itrocks/mysql-to-schema and shows the role ofReflectToTable in a typical workflow:
`ts
import mariadb from 'mariadb'
import type { Connection } from 'mariadb'
import { MysqlToTable } from '@itrocks/mysql-to-schema'
import { ReflectToTable } from '@itrocks/reflect-to-schema'
import { TableDiff } from '@itrocks/schema-diff'
import { SchemaDiffMysql } from '@itrocks/schema-diff-mysql'
import { SchemaToMysql } from '@itrocks/schema-to-mysql'
class User {
id!: number
email!: string
}
async function synchronizeUserTable(connection: Connection) {
const tableName = 'user'
// Schema from the database
const mysqlToTable = new MysqlToTable(connection)
const existingSchema = await mysqlToTable.convert(tableName)
mysqlToTable.normalize(existingSchema)
// Schema from the TypeScript model
const reflectToTable = new ReflectToTable()
const targetSchema = reflectToTable.convert(User)
// Compute the diff and translate it to SQL
const diff = new TableDiff(existingSchema, targetSchema)
const diffToMysql = new SchemaDiffMysql()
const sql = diffToMysql.sql(diff, / allowDeletions / false)
if (sql.trim()) {
await connection.query(sql)
}
}
async function main() {
const pool = mariadb.createPool({
host: 'localhost',
user: 'root',
password: 'secret',
database: 'my_app',
})
const connection = await pool.getConnection()
try {
await synchronizeUserTable(connection)
}
finally {
connection.release()
await pool.end()
}
}
main().catch(console.error)
`
In real applications, you will usually rely directly on
@itrocks/mysql-maintainer, which wraps this pattern and usesReflectToTable internally.
Main entry point of the package. ReflectToTable is an alias of theToTable
internal class.
It converts a reflected TypeScript class (or constructor function) into
an @itrocks/schema Table definition, based on the metadata provided@itrocks/*
by the surrounding ecosystem.
#### Constructor
`ts`
new ReflectToTable()
Creates a new converter instance. The instance is stateless and can be
reused to convert multiple classes.
#### Methods
##### convert(type: Type): Table
Convert a given class (or constructor function) into a Table
instance.
`ts
import type { ObjectOrType } from '@itrocks/class-type'
import { ReflectToTable } from '@itrocks/reflect-to-schema'
function tableOf
return new ReflectToTable().convert(type)
}
`
###### Parameters
- type: Type – a class constructor or reflected type as defined by@itrocks/class-type
. The type must be registered in a store via@itrocks/store
(storeOf(type) must return a non-empty name),
otherwise an error is thrown.
###### Returns
- Table – a Table instance from @itrocks/schema containing:name
- – the table name, typically derived from the store namecollation
- – default collation (utf8mb4_0900_ai_ci)engine
- – default storage engine (InnoDB)columns
- – columns generated from the class properties, includingid
an auto-increment column and additional columns inferred fromindexes
metadata (type, optionality, former names, value ranges, lengths,
etc.)
- – indexes for primary key, representative properties, andid
-like relations
###### Errors
- Throws a string error "Type not stored " + type if the given typestoreOf(type)
is not registered in a store ( is falsy).
- Generating table definitions from your model layer – convert each
of your domain classes into a Table and feed them into other schema@itrocks/schema-diff
tools (, @itrocks/schema-to-mysql, etc.).ReflectToTable
- Synchronizing a database schema with TypeScript models – combine
with @itrocks/mysql-to-schema,@itrocks/schema-diff
, and @itrocks/schema-diff-mysql to computeTable
and apply schema migrations.
- Inspecting or documenting your data model – generate a
programmatic representation for tooling, documentation@itrocks/mysql-maintainer
generators, or introspection utilities.
- Higher-level maintenance workflows – use libraries such as
, which build on top ofReflectToTable` to provide opinionated automated migrations.