Koa初体验(2)

Koa2

MongoDB Compass Community 可视化工具

自行百度下载mongodb安装对应版本,进行登录

koa操作Mongodb数据库的DB类库

安装

npm i mongodb -S

引入

var MongoClict = require('mongodb').MongoClick

定义数据库的连接地址,以及配置数据库

koa 数据库的名称
var dbUrl = "mongodb://localhost:27017/"	// 定义地址
var dbName = 'koa'	// 定义数据库名称

// 连接数据库
MongoClient.connect(dbUrl,function(err,client){
	if(err){
		console.log(err)
		return
	}
	const db = client.db(dbName) // 需要连接到的数据库名称
	
})

操作数据库

// 增加数据

db.collection('user').insertOne({
	'username':"zhangsan",
	'age':22,
	'sex':"男"
},function(err,result){
	if(!err){
		console.log('增加成功')
		client.close()
		// 操作成功之后关闭数据库 
	}
})

// 查询数据
const result = db.collection('user').find({})

result.toArray((err,docs)=>{
	console.log(docs)
})

封装db类库
config.js

// 配置文件

var app = {
    
    dbUrl = 'mongodb://localhost:27017/',

    dbName = 'koa'
    
}


module.exports = app

db.js

var MongoClicent = require("mongodb").MongoClicent;
var Config = require("./config.js");

class Db {
  constructor() {
    this.dbClicent = "";

    this.connect(); // 实例化的时候连接数据库
  }
  //  解决多次实例化
  static getInstance() {
    if (!Db.instance) {
      Db.instance = new Db();
    } else {
      return Db.instance;
    }
  }
  // 连接数据库
  connect() {
    return new Promise((resolve, reject) => {
      // 解决多次连接数据库
      if (!this.dbClicent) {
        MongoClicent.connect(Config.dbUrl, (err, client) => {
          if (err) {
            reject(err);
          } else {
            const db = client.db(Config.dbName);
            this.dbClicent = db;
            resolve(this.dbClicent);
          }
        });
      } else {
        resolve(this.dbClicent);
      }
    });
  }
  // 查找
  find(collectionName, json) {
    return new Promise((resolve, reject) => {
      this.connect().then((db) => {
        const result = db.collection(collectionName).find(json);
        result.toArray((err, docs) => {
          if (err) {
            reject(err);
            return;
          }
          resolve(docs);
        });
      });
    });
  }
  // 添加
  insert(collectionName, json) {
    return new Promise((resolve, reject) => {
      this.connect().then((db) => {
        const result = db
          .collection(collectionName)
          .insertOne(json, (err, result) => {
            if (err) {
              reject(err);
              return;
            } else {
              resolve(result);
            }
          });
      });
    });
  }
  // 修改
  update(collecitonName, json1, json2) {
    return new Promise((resolve, reject) => {
      this.connect().then((db) => {
        const result = db.updateOne(
          collecitonName,
          json1,
          {
            $set: json2,
          },
          (err, result) => {
            if (err) {
              reject(err);
              return;
            } else {
              resolve(result);
            }
          }
        );
      });
    });
  }
  // 删除
  remove(collecitonName, json) {
    return new Promise((resolve, reject) => {
      this.collect().then((db) => {
        const result = db.removeOne(collecitonName, json, (err, result) => {
          if (err) {
            reject(err);
            return;
          } else {
            resolve(result);
          }
        });
      });
    });
  }
}

module.exports = Db.getInstance();

愿以往所学皆有所获
原文地址:https://www.cnblogs.com/Azune/p/14116345.html