Decorator's
@Column
The @Column
decorator processes the property and makes it ready for saving to the table as a column. It is mandatory* to use it when defining a database field in a model.
@Comment
(
comment:
string
)
Types:
any
The @Comment
decorator adds a description or comment to the specified column in the database. It helps document the purpose of the column and improves database readability.
@Default
(
value:
any
)
Types:
any
The @Default
decorator sets a default value for the column when no data is provided. If a value is not specified during insertion, the column will automatically be assigned the defined default value.
@Required
/ @AllowNull
Types:
any
The @Required
decorator makes the column mandatory, ensuring that a value must be provided when inserting or updating a record.
The @AllowNull
decorator allows the column to accept NULL values, making the field optional.
Where @Required
enforces non-null constraints, while @AllowNull
permits null values in the database.
@Unique
Types:
any
The @Unique
decorator ensures that the column contains only unique values. Each record must have a distinct value for this field, preventing duplicate entries in the database.
@Index
Types:
any
The @Index
decorator adds an index to the column, improving query performance by allowing faster lookups and searches in the database.
@AutoIncrement
Types:
Number | BigInt
The @AutoIncrement
decorator automatically increments the column value for each new record. It is typically used for primary keys to ensure unique, sequential values without manual assignment.
@PrimaryKey
Types:
any
The @PrimaryKey
decorator designates the column as the primary key, ensuring each record has a unique identifier.
Note
*
:
If @AutoIncrement
is used, @PrimaryKey
should be applied only to the column where @AutoIncrement
is defined.
@Enum
(
values:
any[]
)
Types:
any
The @Enum
decorator defines a column with a restricted set of possible values, enforcing that only one of the predefined options can be stored in the database. It is typically used for columns that have a fixed set of values, such as status or type fields
@Encrypt
(
method:
string
,
cipherKey:
string
,
iv:
string
)
Types:
String
The @Encrypt
decorator encrypts the value of a column using a specified encryption algorithm and keys.
For example, using:
The decorator applies the aes-256-cbc
encryption method, with the encryption key '12345678912345678901234567890123'
and an initialization vector (IV) of '1234567891234567'
. This ensures that the column’s data is encrypted before being saved to the database and can only be decrypted using the correct algorithm, key, and IV.
@Decrypt
(
method:
string
,
cipherKey:
string
,
iv:
string
)
Types:
String
The @Decrypt
decorator is used to automatically decrypt the value of a column when it is retrieved from the database.
For example, using:
The decorator decrypts the encrypted data using the aes-256-cbc
algorithm, the specified encryption key '12345678912345678901234567890123'
, and the initialization vector (IV) '1234567891234567'
. This ensures that the stored data is decrypted and returned in its original, readable form when queried from the database.
@Hash
(
method:
string
,
digest
?
:
crypto
.
BinaryToTextEncoding
)
Types:
String
The @Hash
decorator applies a hashing function to the column’s value before storing it in the database.
For example, using:
Uses the SHA256
hashing algorithm and specifies 'hex'
as the digest format. This means the column’s value is hashed using the SHA256
method and stored as a hexadecimal string in the database, providing a secure, irreversible transformation of the original data.
@UUID
(
v
?
:
1
|
4
|
6
|
7
|
object
)
Types:
String
The @UUID
function generates a Universally Unique Identifier (UUID) for a column based on the specified version. It supports multiple versions, including 1
, 4
, 6
, and 7
, or a configuration object
for finer control.
• v1
: Generates a time-based UUID.
• v4
: Generates a random UUID.
• v6
: Generates a UUID similar to v1 but optimized for database indexing.
• v7
: Generates a time-ordered UUID based on Unix timestamps.
• v3
and v5
: Generate namespace-based UUIDs using a given namespace and name.
If no version is specified, a default UUID
version may be used based on the implementation.
Last updated