Best practice for enums in mongoDB?

12,435 views
Skip to first unread message

Lynn

unread,
Aug 5, 2014, 2:22:57 PM8/5/14
to loopb...@googlegroups.com
I am trying to add a simple enum to my user model like so:

{
   
"name"       : "user",
   
"properties" : {
       
"identifier": {
           
"type": "string",
           
"id": true
       
},
       
"firstName": "string",
       
"lastName": "string",
       
"email": "string",
       
"phone": [{
           
"tag": "string",
           
"number": "string"
       
}],
        "userType": {
           
"type": "string",
           
"enum": ["customer", "partner"]
       
}

   
},
   
"base": "User"
}

But it doesn't seem to be enforced, so I'm guessing this is not supported?!

So my question is what's best practice here? Create a userTypes model with the two types and relate it back to user? Seems like such overkill, but it's all I'm coming up with.

Thanks.

Raymond Feng

unread,
Aug 5, 2014, 2:28:33 PM8/5/14
to Lynn, loopb...@googlegroups.com
I don’t think enum is supported out of box. I guess it will be used mostly for validation purposes. If so, you can define a hook or custom validator to the model to check on valid values based on the enum list (Model.definition.properties.userType.enum).

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lynn

unread,
Aug 5, 2014, 3:18:29 PM8/5/14
to loopb...@googlegroups.com
That worked. Thanks.
Reply all
Reply to author
Forward
0 new messages