Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types and statuses as enums #226

Closed
alkhachatryan opened this issue Oct 18, 2022 · 1 comment
Closed

Types and statuses as enums #226

alkhachatryan opened this issue Oct 18, 2022 · 1 comment

Comments

@alkhachatryan
Copy link

Hello. I'm new to Nodejs and found this briliant repo to work with. It helps me to understand how nodejs works, what expressjs is and so on. This is not a bug report, but a question related with nodejs, mongoDB and this boilerplate implementation.

I came to NodeJS from PHP and relational DBs stack and I got confused: don't you use enums in backend and integers in mongo?
Let's say we have here column which can be enumed and replaced with numeric column type to make DB faster: user.role.
As much as I read I see nobody uses integers and such type of columns, but strings: status, role and so on.

In PHP and relational DB stack we had enums and numeric values and in DB we stored the value of that enum so DB could query with that columns faster. Does not it work here?

@ghost
Copy link

ghost commented Nov 19, 2022

Hi @alkhachatryan

Here is How Enums Work in Mongoose

Mongoose String and Number types have an enum validator. The enum validator is an array that will check if the value given is an item in the array. If the value is not in the array, Mongoose will throw a ValidationError when you try to save().

const testSchema = new mongoose.Schema({
 status: {
   type: String,
   enum: ['valid', 'invalid']
 }
})

const Test = mongoose.model('Test', testSchema);

await Test.create({ name: 'not a status' }); // throws validation error
await Test.create({ name: 'valid' }); // works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant