Creating a Model
Creating a User Model
To create the simplest User class, you should first define the Users
model. Here is an example code to create such a model:
Пояснення:
@Table({name: 'users'})
: Specifies that this model will be stored in a named tableusers
.@PrimaryGeneratedColumn({type: 'BIGINT'})
: Declares a fielduser_id
as the primary key of the typeBIGINT
, which is automatically generated.@String({type: "VARCHAR", length: 255})
: Defines a fieldusername
as a string up to 255 characters long, with unique values and mandatory.@Column({options: {nullable: false}})
: Indicates that fieldsemail
andpassword
are requeired.@Column({options: {dataType: 'BOOLEAN', defaultValue: false}})
: Declares a fieldis_active
as a boolean with a default valuefalse
.
Додавання Моделі до Системи
After creating the model, you need to add it to the database configuration in your ORM system. This is done by including the model in the models
array when initializing the database manager:
Creating a Table in the Database
To create the users
table in the database, follow these steps:
Initialize the database connection using
createOrmConnection()
.Run
npm run start
to synchronize theUsers
class and map it incurrent_database_ingot
in themigrations
table for subsequent migration creation.
Last updated