Mongoose-based app architecture

后端 未结 2 1540
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-12 16:26

This is not a concrete app/code question, it\'s just about common app architecture.

I\'m trying to understand proper way to organize my mongoose application

2条回答
  •  无人及你
    2020-12-12 17:06

    That's pretty much how I go about it, with a few differences:

    • I don't think you can have the open listener inside your function in the db-layer. What I generally do when using a persistent connection like yours is start the application itself in the db open handler. If you don't want to use persistent connections, use createConnection in the db layer function, and make sure you close it before calling the callback. I am not sure if I am making myself clear. Let me know if you want a code example.
    • This is more of a general node.js tip, but I keep my database connection string and other configuration in a json file and require it wherever it is needed. You probably won't need another settings.js file after that.
    • You can also use schema functions (http://mongoosejs.com/docs/api.html#schema_Schema-method) to code some app functionality into your models itself.

提交回复
热议问题