Koa

Koa(三、mongodb工具类)

2018-06-13  本文已影响40人  强某某
工具类db.js:
const MongoClient = require('mongodb').MongoClient,
ObjectID = require('mongodb').ObjectID,
    Config = require('./config');
class DB {
    static getInstance() {//解决多次实例化的问题
        if (!DB.instance) {
            DB.instance = new DB();
        }
        return DB.instance;
    }
    constructor() {
        this.dbClient = '';//存放db对象
        this.connect();
    }
    connect() {
        return new Promise((resolve, reject) => {
            if (!this.dbClient) {//解决数据库多次连接的问题
                MongoClient.connect(Config.url, (err, client) => {
                    if (err) {
                        reject(err);
                        return;
                    }
                    const db = client.db(Config.dbName);
                    this.dbClient = db;
                    resolve(db);
                });
            } else {
                resolve(this.dbClient);
            }
        })
    }

    find(collectionName, json) {//collection是表名,json是查询条件
        return new Promise((resolve, reject) => {
            this.connect().then(db => {
                let result = db.collection(collectionName).find(json);
                result.toArray((err, docs) => {
                    if (err) {
                        reject(err);
                        return
                    }
                    resolve(docs);
                })
            })
        })
    }
    update(collectionName, json1, json2) {//json1 :查找的条件,json2:要更新的数据
        return new Promise((resolve, reject) => {
            this.connect().then(db => {
                db.collection(collectionName).updateOne(json1, {
                    $set: json2
                }, (err, result) => {
                    if (err) {
                        reject(err);
                        return
                    }
                    resolve(result);
                })
            })
        })
    }
    insert(collectionName, json) {//集合名称,增加的数据
        return new Promise((resolve, reject) => {
            this.connect().then(db => {
                db.collection(collectionName).insertOne(json, (err, result) => {
                    if (err) {
                        reject(err);
                        return
                    }
                    resolve(result);
                });
            })
        })
    }
    remove(collectionName, json) {//集合名称,增加的数据
        return new Promise((resolve, reject) => {
            this.connect().then(db => {
                db.collection(collectionName).removeOne(json, (err, result) => {
                    if (err) {
                        reject(err);
                        return
                    }
                    resolve(result);
                });
            })
        })
    }
    //根据id查询数据,因为mongodb会把id封装成objectid,所以查询
    //需要特殊处理
    getObjectID(id){
        //mongodb里面查询_id把字符串转换成对象
        //所以使用的时候,如果需要查id,可以先调用该方法获取对象
        /**
         * find('user',{ "_id":myDB.getObjectID("5b20e0a94ee9071db8c83092") }).then(data => {
            console.log(data);
            })
         */
        return new ObjectID(id);
    }
}

配置文件config.js
//配置文件
let app={
    url:'mongodb://localhost:27017',
    dbName:'Demo'
}
module.exports=app;
上一篇 下一篇

猜你喜欢

热点阅读