Skip to content
Anshuman Chhapolia edited this page Mar 23, 2024 · 2 revisions

DAO ( Data Access Objects )

The DAO layer is responsible for the communication between the application and the database. It is the layer that will perform the CRUD operations on the database.

A dao class is created for each entity in the application. The dao class will have methods to perform the CRUD operations on the entity.

Here is an example of a dao class:

export class AddressDao extends Dao<Address> {
  constructor(database: Database) {
    super(database, Address, "address");
  }
}

The Dao class is a generic class that takes the entity type as a parameter. The Dao class has methods to perform CRUD operations on the entity namely:

  1. create: To create a new entry in the database.
  2. read: To read an entry from the database. It takes a primary key or a filter object.
  3. update: To update entries in the database. It takes a primary key or a filter object. and the update object.
  4. delete: To delete entries from the database. It takes a primary key or a filter object.
  5. readMany: To read multiple entries from the database. Read many comes with a lot of customization options. Read more about it here.

We suggest you perform all the database operations in the dao layer. The service layer should only call the dao layer to perform the database operations. This will help in keeping the code clean and maintainable.