mercredi 31 juillet 2019

How to name a function/method when use table Data Gateway pattern and the SQL query contains multiple join clauses?

There is a DAL in my application code. And I am using Table Data Gateway pattern for DAL which means I create class for each table.

Here is a demo, there are three tables in the database: users, addresses, organizations. I create a UserGateWay class for users table.

class UserGateWay {
  public async findById(id: string) {
    const query = `
      select * from users where user_id = ?
    `;
    return this.knex.raw(query, [id]);
  }

  public async findByIdWithAddress(id: string) {
    const query = `
      select
        u.*,
        addr.*
      from users as u
      inner join addresses as addr using (user_id)
      where u.user_id = ?;
    `;
    return this.knex.raw(query, [id]);
  }

  public async findByIdWithOtherTableDatas(id) {
    const query = `
      select
        u.*,
        addr.*,
        orgs.org_name,
      from users as u
      inner join addresses as addr using (user_id)
      inner join organizations as orgs using (org_id)
      where u.user_id = ?;
    `;
    return this.knex.raw(query, [id]);
  }
}

findById method is simple, the issue is how should I name the findByIdWithAddress and findByIdWithOtherTableDatas methods.

If there are many join clauses within the method, I don't know how to name the method. Especially the very similar but different scenes.

I don't want to name it like findByIdWithAddressAndOrgsAndOthers. Maybe I use this pattern in the wrong way or this pattern is not suitable for table join case?

Aucun commentaire:

Enregistrer un commentaire