Skip to main content
Define MySQL tables using the mysqlTable function. Tables are the foundation of your database schema.

Table Definition

mysqlTable()

Defines a MySQL table with columns and constraints.
string
required
The name of the table in the database.
object | function
required
Column definitions. Can be an object of column builders or a function that receives column type helpers.
function
Function to define indexes, foreign keys, and other constraints. Should return an array of constraint builders.

Examples


mysqlTableCreator()

Creates a custom table creator with a name transformation function. Useful for adding prefixes or custom naming conventions.
function
required
Function that transforms table names. Receives the provided name and returns the actual database table name.

Examples


Schema Organization

mysqlTableWithSchema()

Defines a table within a specific MySQL schema/database.
string
required
The table name.
object
required
Column definitions.
function
required
Constraints and indexes configuration.
string
required
The schema/database name.
string
Base name for internal reference. Defaults to name.

Examples


Table Constraints

Constraints are defined in the third parameter (extraConfig) of mysqlTable.

Indexes


Primary Keys


Foreign Keys


Unique Constraints


Check Constraints


Type Helpers

MySqlTable

The base table class. All tables defined with mysqlTable() are instances of this class.

InferModel

Infer TypeScript types from table definitions.

Table Configuration

MySqlTableExtraConfigValue

Union type of all possible table configuration values.

Best Practices

Column-level constraints: Define simple constraints (like .notNull(), .unique(), .primaryKey()) directly on columns for better readability.
Table-level constraints: Use the extraConfig parameter for composite indexes, composite primary keys, and foreign keys.
Reserved words: Avoid using MySQL reserved keywords as table or column names. If necessary, they will be automatically quoted.
Naming conventions: Choose consistent naming for tables (e.g., plural form like users, posts) and stick with it throughout your schema.