NodeJS封装mongodb

2021-01-12 10:56:09

参考地址 NodeJS操作 Mongodb 并且对 Mongodb 类库进行封装

我的目录如下:

在这里插入图片描述

第一步,安装 Node 依赖

在当前目录下输入:

npm install mongodb -S1
第二步,编写配置文件

在 config 文件夹内的 index.js 内编写如下代码:

module.exports = {
    // 地址
    address:'localhost',
    // 端口
    port:'27017',
    // 数据库
    database:'koa2',
    // 用户名
    username:'root',
    // 密码
    password:'123456'}123456789101112
第三步,编码 app.js 实现增删该查封装
// mongodb 操作包const mongodb = require('mongodb').MongoClient;//配置文件const config = require('./config/index');// 根据状态判断是否有用户名let state = null;if(config.username!=''&&config.password!=''){// 有用户名密码
    state = true;}else{// 没有用户名密码
    state = false;};// 定义基本类class app{
    // 多次连接共享实例对象
    static getInstance(){
        if(!app.instance){
            app.instance = new app();
        };
        // 简化性能提升
        return app.instance;
    }

    //默认初始化执行方法
    constructor(){
        // 存放mongodb连接后的对象
        this.dbClient = '';
        // 初始化连接数据库
        this.connect()
    };
    
    // 连接
    connect(){
        if(state){// 有用户名密码
            return new Promise((resolve,reject) => {
                if(!this.dbClient){
                    mongodb.connect('mongodb://'+config.username+':'+config.password+'@'+config.address+':'+config.port+'/',{
                        useNewUrlParser:true
                    },(err,client) => {
                        if(!err){
                            this.dbClient = client.db(config.database);
                            resolve(this.dbClient);
                        }else{
                            reject(err);
                        };
                    });
                }else{
                    resolve(this.dbClient);
                };
            });
        }else{// 没有用户名密码
            return new Promise((resolve,reject) => {
                if(!this.dbClient){
                    mongodb.connect('mongodb://'+config.address+':'+config.port+'/',{
                        useNewUrlParser:true
                    },(err,client) => {
                        if(!err){
                            this.dbClient = client.db(config.database);
                            resolve(this.dbClient);
                        }else{
                            reject(err);
                        };
                    });
                }else{
                    resolve(this.dbClient);
                };
            });
        };
    };

    // 添加
    add(tableName,json){
        return new Promise((resolve,reject) =>{
            this.connect().then(db => {
                db.collection(tableName).insertOne(json,(err,result) => {
                    if(!err){
                        resolve(result);
                        return;
                    };
                    reject(err);
                });
            });
        });
    };

    // 删除
    remove(tableName,json){
        return new Promise((resolve,reject) => {
            this.connect().then(db => {
                db.collection(tableName).removeOne(json,(err,result) => {
                    if(!err){
                        resolve(result);
                        return;
                    };
                    reject(err);
                });
            });
        });
    };

    // 更新
    update(tableName,condition,json){
        return new Promise((resolve,reject) => {
            this.connect().then(db => {
                db.collection(tableName).updateOne(condition,{
                    $set:json                },(err,result) => {
                    if(!err){
                        resolve(result);
                        return;
                    };
                    reject(err);
                });
            });
        });
    };

    // 查询
    find(tableName,json){
        return new Promise((resolve,reject) => {
            this.connect().then(db => {
                let result = db.collection(tableName).find(json);
                result.toArray((err,data) => {
                    if(!err){
                        resolve(data);
                        return;
                    }
                    reject(err);
                });
            });
        });
    };};// 导出模块module.exports = app.getInstance();123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139

完成封装


  • 2023-08-19 18:57:08

    强制杀死nginx进程简单粗暴

    nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory),最简单的办法,杀死nginx进程,重启