Models & Database Layer
Model and Database Layer
In the world of modern web development, a robust and efficient database interaction layer is essential. Etherial.TS recognizes this need and places a strong emphasis on using a comprehensive and well-maintained Object-Relational Mapping (ORM) tool. This choice ensures that your database operations are not only smooth but also scalable and maintainable over time.
For our model and database layer, we have chosen to leverage the power of Sequelize along with TypeScript. Sequelize is a battle-tested ORM that has been maintained and refined for many years. Its TypeScript version provides type safety and ensures that your database interactions are seamless and free from runtime errors.
To define a model using Sequelize in Etherial.TS, you can use the following example:
import { Table, Model } from "etherial/components/database/provider";
@Table({
timestamps: true,
tableName: "users",
freezeTableName: true,
})
export class User extends Model<User> {
@AutoIncrement
@PrimaryKey
@AllowNull(false)
@Column
id: number;
@AllowNull(false)
@Column
firstname: string;
}Here, we've created a User model that represents the "users" table in the database. Note the use of TypeScript for strong typing and Sequelize decorators to define table properties.
Best Practices
As a best practice, Etherial.TS recommends naming your models in the singular form, such as "User" for an individual user entity. However, when it comes to the actual database tables, they should follow the plural convention, like "users" for the "users" table. This consistent naming convention ensures clarity and consistency in your database structure.
With Sequelize and TypeScript as integral components of Etherial.TS, you can confidently manage your database interactions, perform queries, and maintain your database schema with ease and precision.
Last updated
Was this helpful?