【问题标题】:How to structure a Node MongoDB project?如何构建 Node MongoDB 项目?
【发布时间】:2018-02-16 14:17:40
【问题描述】:

在我的带有 mongodb.js 的 Node.js 项目中(注意,不是 mongoose)我有以下结构:

  • index.js
    • 设置数据库连接 (db.js)
    • 设置快递服务器(api.js)
  • api.js
    • 启动服务器
    • 定义路由并调用 db (db.js)
  • database.js
    • 启动db连接,db在文件中定义为全局
    • 每个 db 函数都使用全局 db 变量

大概是这样的:

// database.js
let db;

module.exports = {
    connect() {
       db = set value here;
    },
    findUser() {
      db.find(query);
    }
};

现在我想将每个 db 函数重组为它自己的文件夹,如 database/user.js,但我不确定如何使用单独的文件处理全局 db var?

// database/users.js
module.exports = {
    findUser() {
        db.find(query); <-- how to pass the db connection
    }
};

我想避免将 db 作为参数传递给所有函数。构建这个的常用方法是什么?

【问题讨论】:

  • 您可以从this project 获得一些想法。查看路由、控制器和模型的结构。
  • 那是mongoose项目,所以db连接隐藏在mongoose模型后面。我只使用 mongodb.js
  • 数据库作为参数传递给所有函数有什么问题?导入时只需传递一次:const findUser = require('users.js')(db)
  • 按照 RahphMex 的建议使用依赖注入,或者为您的数据库连接创建单例并使用它。

标签: node.js mongodb


【解决方案1】:
// database.js
let db;

module.exports = {
    connect() {
       db = set value here;
    },
    findUser() {
      db.find(query);
    },
    getDb(){
      return db;
    }

};

// database/users.js
var db = require('database.js).getDb();
module.exports = {
    findUser() {
        db.find(query); <-- how to pass the db connection
    }
};

【讨论】:

    【解决方案2】:
    for creating a project using mongodb with nodejs you need to learn the code structure as described below..it should contains modules,controller,services etc.inside the controller you need to write the functions through which you can call certain services to perform an action.
    
    i will tell you in detailed how to start with controller and services
    
    CONTROLLER INCLUDES 
    import { Controller, Post, Body, Param } from '@nestjs/common';
    import { DepartmentService } from './department.service';
    import { ApiTags } from '@nestjs/swagger';
    import { DepartmenDto } from './dto/addDepartment.dto';
    
    @Controller('department')
    @ApiTags('department')
    export class DepartmentController {
    
        constructor(private service: epartmentService) { }
    
        @Post('addDepartment')
        async addDepartment(
            @Body()registerDepartmentDto: addDepartmentDto) {
            return await this.service.insertDepartment(registerKidsDto).then(res => {
                return res; 
            });
        }
    }
    
    import { Injectable, Inject } from '@nestjs/common';
    import { Department} from './interface/department.interface';
    import { Model } from 'mongoose';
    import { DepartmenDto } from './dto/addDepartment.dto';
    
    @Injectable()
    export class DepartmentService {
        constructor(
            @Inject('DEPARTMENT_MODEL') private readonly DepartmentModel: Model <Kids>,
          ) {}
    
          async addDepartment(Departmentdto: DepartmentDto) {
            const createdCat = new this.DepartmentModel(Departmentdto);
            return await createdCat.save();
          }
    }
     I HAVE CREATED A DEPARTMENT MODULE AND WROTE SERVICE FOR ACCESSIUNG THIS.
    

    类似地,对于其他 crud 操作,您可以将其用作参考。如果您想在控制器中传递参数,您可以像这样调用它 @Post('getDeaprtmentDetails/:departmentId') 异步获取部门详细信息( @Param('departmentId')departmentId: 字符串) { 返回等待 this.service.retrieveDepartmentInfo(departmentId).then(res => { })

    【讨论】:

      猜你喜欢
      • 2013-07-03
      • 2018-05-21
      • 1970-01-01
      • 2020-01-25
      • 2020-04-05
      • 2014-10-09
      • 1970-01-01
      • 2018-08-17
      • 2010-10-10
      相关资源
      最近更新 更多