Column Attributes in KtorAdmin
Learn how to define database column attributes in **KtorAdmin**, including metadata, constraints, and validation rules.
In this section, we will define various attributes for database columns using annotations in KtorAdmin. These attributes help specify metadata, constraints, and behavior of database columns in an admin panel.
Column Metadata
The @ColumnInfo
annotation allows defining essential properties of a column:
@ColumnInfo(
columnName = "description",
verboseName = "Description",
blank = false,
nullable = false,
defaultValue = "Default description",
readOnly = false,
unique = false
)
val desc = text("description")
columnName
(Default: Property name): Specifies the actual column name in the database. If not provided, the property name is used.verboseName
(Default: Column or Property name): A user-friendly name for display purposes.defaultValue
(Default: Empty String → null): Sets a default value if no data is provided.nullable
(Default: false): Determines if the column allows null values.unique
(Default: false): Ensures values are unique across the table.blank
(Default: true): Indicates whether the field can be left empty.readOnly
(Default: false): Prevents modification after initial creation.
Ignoring Columns in Admin Panel
Use @IgnoreColumn
for columns that should not appear in the admin panel, such as auto-generated IDs:
@IgnoreColumn
val id = integer("id").autoIncrement()
Applying Constraints with @Limits
@Limits
The @Limits
annotation allows defining validation constraints for columns. By default, no constraints are applied unless explicitly specified.
String Constraints:
@Limits(
maxLength = 255,
minLength = 5,
regexPattern = "[A-Za-z0-9 ]*"
)
val title = varchar("title", 255)
maxLength / minLength
(Default: No limit): Defines string length limits.regexPattern
(Default: No pattern): Ensures the value matches a specific pattern.
Email Validation:
@Limits(
regexPattern = """[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}"""
)
val email = varchar("email", 150)
Ensures the email format is valid.
Numeric Constraints:
@Limits(
maxCount = 100.0,
minCount = 1.0
)
val quantity = integer("quantity")
maxCount / minCount
(Default: No limit): Specifies numerical limits.
Date Constraints:
@Limits(
minDateRelativeToNow = 10 * 24 * 60 * 60 * 1000L, // At least 10 days before now
maxDateRelativeToNow = 7 * 24 * 60 * 60 * 1000L // Up to 7 days in the future
)
val createdAt = datetime("created_at")
minDateRelativeToNow / maxDateRelativeToNow
(Default: No restriction): Defines a valid time range relative to the current moment. The system subtractsminDateRelativeToNow
from the current time, ensuring the value is not earlier than the calculated result.maxDateRelativeToNow
ensures the date does not exceed the specified limit into the future. The system automatically applies these constraints based on the current timestamp.
File Constraints:
@LocalUpload
@Limits(
maxBytes = 1024 * 1024 * 20, // 20MB
allowedMimeTypes = ["video/mp4"]
)
val file = varchar("file", 1000).nullable()
maxBytes
(Default: No limit): Limits the file size.allowedMimeTypes
(Default: No restriction): Restricts the file type.
By applying these annotations, you can effectively manage database column properties and enforce necessary validations in KtorAdmin.
Last updated