We are currently working on a TypeScript project using Mongoose and have a question regarding best practices for handling Enums in models.
Our perspective is to view the enum as a KEY -> Value pair, where the "KEY" represents the item stored in the database and the "VALUE" signifies its real meaning or translation.
For example:
Here is our TypeScript enum:
enum ECurrency {
USD = 'United States dollar',
EUR = 'Euro'
}
And here is our Mongoose Schema:
...
currency: {
type: String,
enum: Object.keys(ECurrency),
required: true
}
So far, this setup appears to be functioning correctly.
However, when creating a new document with this schema, we typically use:
currency: ECurrency.USD
This returns 'United States dollar' and may result in an Error.
Therefore, our question is:
Should we utilize KEYS rather than values in the Mongoose Schema?
Is our concept of using the KEY as a reference for database operations and VALUE as a form of translation valid?
The current code works, but the manual input of "USD" or ECurrency["United States dollar"] each time we create a document feels cumbersome and we are uncertain about how to streamline this process.