Node.js Core & Model : Where Sequelize Update Values made

Heechul Yoon·2020년 8월 9일
0

LOG

목록 보기
58/62

this posting is in line with the role of modules at Node.js project

when in comes to update data to database, we would use sequelize update method or SQL raw query. either way would need update values and conditions to conduct operation.

Sequelize Update

then where could we set update values and conditions?
let us see two different style of update

  • case_1
// model.js
   async putMemberSnsVisible(transaction, updateInfo, memberFilter) {
    try {
      const result = await MemberSns.update(
        {
[1]       is_visible: updateInfo.is_visible
        },
        {
          where: { users_id: memberFilter.memberId, type: updateInfo.code },
          transaction: transaction
        }
      )

      return result
    } catch (err) {
      throw new Error(`[EA501] MemberModel.putMemberSnsVisible with error: ${err}`)
    }
  }
  • case_2
// model.js
   async putMemberSnsVisible(transaction, updateInfo, memberFilter) {
    try {
      const result = await MemberSns.update(
[1]     updateInfo, {
          where: { users_id: memberFilter.memberId, type: updateInfo.code },
          transaction: transaction
        }
      )

      return result
    } catch (err) {
      throw new Error(`[EA501] MemberModel.putMemberSnsVisible with error: ${err}`)
    }
  }

case_1.[1] : set update value at the model.js level and update the assigned data from updateInfo(Object)
case_2.[1] : set update value at the core.js level and update any kind of data in updateInfo(Object)

in project as a intergration of modules in which each module has its distinguished role, case_2 would be more ideal to the modulized project

Why?

  • single operation
    - model could only focus on pure update operation not having any other side feature such as setting update values. It is not multi-operation even if it has update value setter at the code level but to do so, the code would be more direct and easier to be interpreted
  • reusable
    - the function declared at the models.js would be used any other feature at the core level.
profile
Quit talking, Begin doing

0개의 댓글