Skip to content

Creating tables

ArteMerlow edited this page May 28, 2025 · 1 revision

Creating tables

In ModularORM, tables are represented as classes with decorators. To create a new entity (table), create a class similar to the code below

// Here you can additionally specify any parameters. For example, a comment to the table, CHARSET or enable migrations
@Table() // Or specify { ... }
@NamedTable('ur_table_name') // Table name
class User extends Module {
    
    @AutoIncrementId() // Creates an auto-increment key
    public id!: number;
    
    @Column({ type: ColumnType.VARCHAR(64), notNull: true })
    public name!: string;
    
}

In this example, we created a simple table with two fields: id as an auto-incrementing index and name as a string. Then ModularORM itself will "pick it up" and create it physically if you imported it.

What can I specify in @Table?

@Table({
    // Table generation priority. 
    // The higher the number, the earlier the table will be created relative to others.
    priority: 1000,
    // Comment to the table
    comment: 'Users list',
    // Collation of the table
    collation: "utf8_general_ci",
    // Row format of the table
    rowFormat: "Dynamic",
    // You can enable migrations for one specific table.
    migrations: false
})

What can I specify in @Column?

@Column({
    // The type of the default, specifying the data type (e.g., TEXT, VARCHAR(255), INTEGER).
    // This value is used to define how the default's data will be stored in the database.
    type: ColumnType.FLOAT,
    
    // Whether the default should auto-increment (usually for primary key columns).
    // If set to true, the database will automatically generate a unique value for this default with each new record.
    autoIncrement: false,
    
    // The default value for the default, used when no value is provided during an insert.
    // This can be any valid data type, depending on the default type.
    defaultValue: 1,
    
    // Whether the default is non-nullable. If true, the default must contain a value for every record.
    // If false, the default can contain NULL values.
    notNull: true,
    
    // Specifies whether the default should have a unique constraint. If set to true, the database will enforce uniqueness for this default.
    // Only one record with each unique value will be allowed in the default.
    unique: false,
    
    // Indicates whether the default should be indexed. An index is created for faster searching and sorting operations.
    // If true, an index will be created on the default.
    index: false,
        
    // Defines the value to be set when the default is updated. Commonly used for timestamp fields (e.g., onUpdate: CURRENT_TIMESTAMP).
    // This value will be automatically applied when the default is updated.
    onUpdate: undefined,
    
    // Defines the foreign key relationship for this column.
    // If this property is specified, the column will be a foreign key referencing another table.
    // referencedTable: The name of the table that this column references.
    // referencedColumn: The name of the column in the referenced table that this column refers to.
    // Foreign keys help maintain data integrity by ensuring that a value in this column must exist in the referenced table.
    foreignKey: {
        referencedTable: 'some_table', // The name of the table that this column references
        referencedColumn: 'some_column', // The name of the column in the referenced table
    },
    
    // Specifies the behavior when a referenced row is deleted in the parent table.
    // This value will only be used if the column is a foreign key.
    // Possible values:
    // "CASCADE": Automatically delete rows that reference the deleted row.
    // "SET NULL": Set the foreign key column to NULL when the referenced row is deleted.
    // "RESTRICT": Prevent the deletion of the referenced row if there are dependent rows in this table.
    onDeleteForeign: "CASCADE",
    
    // Specifies the behavior when a referenced row is updated in the parent table.
    // This value will only be used if the column is a foreign key.
    // Possible values:
    // "CASCADE": Automatically update rows that reference the updated row.
    // "SET NULL": Set the foreign key column to NULL when the referenced row is updated.
    // "RESTRICT": Prevent the update of the referenced row if there are dependent rows in this table.
    onUpdateForeign: "CASCADE",

    // Comment
    comment: 'Some comment'
})

Clone this wiki locally