Skip to content

Database

Enums

When there is a set of pre-populated values that there is a need to use in the code, enums -- or plain objects in javascript -- should be used instead of either arrays or just plain strings.

The enum name and keys should be in UPPER_SNAKE_CASE The values when applicable should be also in UPPER_SNAKE_CASE and the same as the keys.

This helps developers

  • to have autocompletion in the code editor when using the enum.
  • to identify that a value belongs to an enum from the payload of a request, down to the value in a database column. (when values of an enum are UPPER_SNAKE_CASE)

example javascript

javascript
const DATASOURCE_TARGET_MODULE = {
    PRODUCT: "PRODUCT",
    CORPORATE: "CORPORATE",
};
const DATASOURCE_TARGET_MODULE = {
    PRODUCT: "PRODUCT",
    CORPORATE: "CORPORATE",
};

example typescript

typescript
enum DATASOURCE_TARGET_MODULE {
    PRODUCT = "PRODUCT",
    CORPORATE = "CORPORATE",
}
enum DATASOURCE_TARGET_MODULE {
    PRODUCT = "PRODUCT",
    CORPORATE = "CORPORATE",
}

The values of an enum in an array form can be retrieved by using Object.values(ENUM_NAME)

Warning When using enums in typescript never leave the values empty. This would let typescript infer the values as auto increment numbers starting from 0. This is a potential for many bugs.