In Loopback 4, you can create models which are then connected to a data source to write them out and read them back in.
So let's say I have this enum:
enum Priority {
LOW = "LOW",
MEDIUM = "MEDIUM",
HIGH = "HIGH"
};
Using the Typescript playground this compiles to:
var Priority;
(function (Priority) {
Priority["LOW"] = "LOW";
Priority["MEDIUM"] = "MEDIUM";
Priority["HIGH"] = "HIGH";
})(Priority || (Priority = {}));
So the values are strings, therefore a database (e.g. MySQL) would be able to store this value without problem.
But let's say this is being read from a database. What is going to be the resulting type? Will I get a runtime error?
Typescript types are erased at runtime - values of your enum type will simply be strings at runtime. Extracting a string with one of the expected values for your enum from the database, and then treat it as a value of type
Priority
will behave as you want.You only need to ensure that no other code accessing the database sets the priority to value different from the three given ones.