zoukankan      html  css  js  c++  java
  • koa 基础(二十四)封装 DB 库 --- 新增数据、更新数据、删除数据

    1.根目录/module/db.js

    /**
     * DB库
     */
    var MongoClient = require('mongodb').MongoClient;
    var Config = require('./config.js');
    
    class Db {
      /**
       * 单例
       * 解决多次实例化,实例不共享的问题
       */
      static getInstance() {
        if (!Db.instance) {
          Db.instance = new Db();
        }
        return Db.instance;
      }
    
      constructor() {
        this.dbClient = ''; /*属性 存放db对象*/
        this.connect(); /*实例化的时候就连接数据库*/
      }
    
      connect() { /*连接数据库*/
        let _that = this;
        return new Promise((resolve, reject) => {
          if (!_that.dbClient) { /*解决数据库多次连接的问题*/
            MongoClient.connect(Config.dbUrl, (err, client) => {
              if (err) {
                reject(err);
              } else {
                _that.dbClient = client.db(Config.dbName);
                resolve(_that.dbClient);
              }
            })
          } else {
            resolve(_that.dbClient);
          }
        })
      }
    
      find(collectionName, json) { /*查询数据*/
        return new Promise((resolve, reject) => {
          this.connect().then((db) => {
            var result = db.collection(collectionName).find(json);
            result.toArray((err, docs) => {
              if (err) {
                reject(err);
                return;
              }
              resolve(docs);
            })
          })
        })
      }
    
      update(collectionName, json1, json2) { /*更新数据*/
        return new Promise((resolve, reject) => {
          this.connect().then((db) => {
            db.collection(collectionName).updateOne(json1, {
              $set: json2
            }, (err, result) => {
              if (err) {
                reject(err);
              } else {
                resolve(result);
              }
            })
          })
        })
      }
    
      insert(collectionName, json) { /*新增数据*/
        return new Promise((resolve, reject) => {
          this.connect().then((db) => {
            db.collection(collectionName).insertOne(json, function (err, result) {
              if (err) {
                reject(err);
              } else {
                resolve(result);
              }
            })
          })
        })
      }
    
      remove(collectionName, json) { /*删除数据*/
        return new Promise((resolve, reject) => {
          this.connect().then((db) => {
            db.collection(collectionName).removeOne(json, function (err, result) {
              if (err) {
                reject(err);
              } else {
                resolve(result);
              }
            })
          })
        })
      }
    }
    
    module.exports = Db.getInstance();

    .

  • 相关阅读:
    Linux 系统中 sudo 命令的 10 个技巧
    如何在 Linux 中配置基于密钥认证的 SSH
    选择 NoSQL 数据库需要考虑的 10 个问题
    10 个 Linux 中方便的 Bash 别名
    扒一扒 EventServiceProvider 源代码
    [Binary Hacking] ABI and EABI
    瀑布流 ajax 预载入 json
    PHP5+标准函数库观察者之实现
    使用汇编分析c代码的内存分布
    but no declaration can be found for element 'aop:aspectj-autoproxy'.
  • 原文地址:https://www.cnblogs.com/crazycode2/p/10965003.html
Copyright © 2011-2022 走看看