blub
blub

Reputation: 9207

How to structure a Node MongoDB project?

In my Node.js project with mongodb.js (note, not mongoose) I have the following structure:

Roughly like this:

// database.js
let db;

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

Now I'd like to restructure each db function to it's own folder like database/user.js, but I'm not sure how to handle the global db var with separate files?

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

I'd like to avoid having to pass the db as parameter to all functions. What's the common way to structure this?

Upvotes: 0

Views: 2289

Answers (2)

kaviya bala
kaviya bala

Reputation: 1

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.

similarly for other crud operations you can use this as a reference.if you want to pass parameter in controller you can call it like @Post('getDeaprtmentDetails/:departmentId') async getDepartmentDetails( @Param('departmentId')departmentId: string) { return await this.service.retrieveDepartmentInfo(departmentId).then(res => { })

Upvotes: 0

Shreyas Ponkshe
Shreyas Ponkshe

Reputation: 116

// database.js
let db;

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

};

and

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

Upvotes: 1

Related Questions