본문 바로가기
Essay/Programming

Understanding the Plain Option in Sequelize

by RIEM 2023. 12. 5.

2023-07-26

What is Plain option in Sequelize

When working with Sequelize, there's a convenient method called get({ plain: true }) that allows you to retrieve a simple object instead of a complex entity. This can make your code cleaner and more straightforward.

How to Use It

To implement this feature, just add get({ plain: true }) immediately after using the 'find' method that would return the 'dataValues' property. Here's an example:

Original Code:

const appInfo = await this.appInfoRepository.findOneById(appId);

Updated Code with Plain Option:

const appInfo = await (await this.appInfoRepository.findOneById(appId)).get({ plain: true });

Why It Matters

Without the plain option, you would need to access object properties using app.dataValues . This approach adds complexity to your code. On the other hand, by employing the plain option, you can directly access data from the app variable, resulting in cleaner and readable code.

Conclusion

In conclusion, the plain option in Sequelize simplifies your code by providing a direct way to work with plain objects, enhancing both readability and maintainability.

'Essay > Programming' 카테고리의 다른 글

find() vs. filter() considering semantics  (0) 2023.12.05

댓글