These are chat archives for Automattic/mongoose

Feb 2016
Feb 22 2016 16:10
Hey, just curious. Does anyone here keep the middleware in separate files? (and load them as plugins); or do you guys just define them right there in the Models schema?
Currently, I keep all the Mongoose middleware in a src/lib/plugins/mongoose/ folder, and load them in the schema, im just wondering if I can justify creating a src/models/middleware/ folder, if theres a "best practice" as to where to keep the middleware
Feb 22 2016 17:19
@jhyland87 I generally just inline the middleware in each schema file at the bottom. Having your models in their own module is already abstracting it away. I find that it's easy for new hires to see right away what happens via middleware on each model if it's in the schema file. Just my 2c.
Feb 22 2016 17:22
@James1x0 Yeah, I see what you mean. I want to be able to quickly and easily enable/disable middleware functionality, and there may be a lot (depending on the model), so keeping them in separate files, and loading them as plugins seems to work best for me
$ tree -d src/
├── dev
├── lib
│   ├── helpers
│   ├── plugins
│   └── utils
└── models
    └── middleware
Thats the current setup
works ok, was just wondering if theres a real standard place to put them, lol. Im doing some guessing. This is my first "real" NodeJS application
I followed articles like this as close as I can.
Feb 22 2016 18:29
@jhyland87 afaik, there's no "standard" place to put modulized middleware. I put my model helpers and utilities in the model folder as a folder as well, so putting them in a middleware folder in models is probably the best way to go.
Alexander Grasley
Feb 22 2016 20:11
Is there a way to make a field that can be either one type or another type? For instance, a field that validates for String or Number but not Array or Object?
Diego Aguilar Aguilar
Feb 22 2016 20:30
Hello, I've set an enum for a field
how can I get those values importing a Schema?
my own schema
Feb 22 2016 23:27
@vkarpov15 @kba guys I have a doubt about mongodb text index: I have to index ALL the string fields, but I use mongoose-schema-extend, so some fields are in the root schema and some others in leaf schemas (obiouvsly different fields for each leaf schemas), any idea?? :(