From 6676bd64c4a32fadc1bda93a0af8d5c69f963adc Mon Sep 17 00:00:00 2001 From: luyuan <1162963624@qq.com> Date: Mon, 11 Jan 2021 11:14:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=20=E5=85=A8=E5=B1=80=E9=93=BE=E6=8E=A5=E4=B8=80=E6=AC=A1=20?= =?UTF-8?q?=E9=95=BF=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/mongodb.js | 204 +++++++++++++++++++++++-------------------------- 1 file changed, 95 insertions(+), 109 deletions(-) diff --git a/bin/mongodb.js b/bin/mongodb.js index 1737faf..dd5422b 100644 --- a/bin/mongodb.js +++ b/bin/mongodb.js @@ -1,5 +1,21 @@ var MongoClient = require('mongodb').MongoClient; var dburl; +var db; +function init() { + new Promise((res, err) => { + MongoClient.connect(dburl.url, { useUnifiedTopology: true, useNewUrlParser: true }, (e, db) => { + if (e) { + err(e) + } + res(db) + + }) + }).then((res) => { + db = res.db(dburl.dbname) + }).catch((e) => { + throw e; + }) +} let idadd = async (set) => { let id = 0 if (set == "idadd") { @@ -33,21 +49,17 @@ let add = async (set, data, url = dburl) => { return new Promise((resolve, reject) => { - MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { - if (err) throw err; - var dbo = db.db(url.dbname); - try { - dbo.collection(set).insertOne(data, function (err, res) { - if (err) throw err; - db.close(); - resolve({ code: 0 ,msg:"添加成功"}); - }); - } catch (error) { - reject({ code: 1, error }) - } + try { + db.collection(set).insertOne(data, function (err, res) { + if (err) throw err; - }); - }) + resolve({ code: 0, msg: "添加成功" }); + }); + } catch (error) { + reject({ code: 1, error }) + } + + }); } let adds = async (set, data, url = dburl) => { @@ -59,38 +71,30 @@ let adds = async (set, data, url = dburl) => { } return new Promise((resolve, reject) => { - MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { - if (err) throw err; - var dbo = db.db(url.dbname); - try { - dbo.collection(set).insertMany(data, function (err, res) { - if (err) throw err; - db.close(); - resolve({ code: 0, num: res.insertedCount }); - }); - } catch (error) { - reject({ code: 1, error }) - } + try { + db.collection(set).insertMany(data, function (err, res) { + if (err) throw err; + + resolve({ code: 0, num: res.insertedCount }); + }); + } catch (error) { + reject({ code: 1, error }) + } - }); }) } let remove = async (set, where, url = dburl) => { return new Promise((resolve, reject) => { - MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { - if (err) throw err; - var dbo = db.db(url.dbname); - try { - dbo.collection(set).deleteOne(where, function (err, obj) { - if (err) throw err; - db.close(); - resolve({ code: 0 }); - }); - } catch (error) { - reject({ code: 1, error }) - } + try { + db.collection(set).deleteOne(where, function (err, obj) { + if (err) throw err; + + resolve({ code: 0 }); + }); + } catch (error) { + reject({ code: 1, error }) + } - }); }) } let removes = async (set, where, url = dburl) => { @@ -98,101 +102,83 @@ let removes = async (set, where, url = dburl) => { if (where == "" || Object.keys(where).length == 0) { reject({ code: 1 }) } - MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { - if (err) throw err; - var dbo = db.db(url.dbname); - try { - dbo.collection(set).deleteMany(where, function (err, obj) { - if (err) throw err; - db.close(); - resolve({ code: 0, num: obj.result.n }); - }); - } catch (error) { - reject({ code: 1, error }) - } + try { + db.collection(set).deleteMany(where, function (err, obj) { + if (err) throw err; + + resolve({ code: 0, num: obj.result.n }); + }); + } catch (error) { + reject({ code: 1, error }) + } - }); }) } let find = async (set, where = {}, page = 0, num = 0, url = dburl) => { return new Promise((resolve, reject) => { if (num == 0) { - MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { - if (err) throw err; - var dbo = db.db(url.dbname); - try { - dbo.collection(set).find(where).toArray(function (err, result) { - if (err) throw err; - resolve({ code: 0, data: result }); - db.close(); - }); - } catch (error) { - reject({ code: 1, error }) - } + try { + console.log(db) + db.collection(set).find(where).toArray(function (err, result) { + if (err) throw err; + resolve({ code: 0, data: result }); + + }); + } catch (error) { + reject({ code: 1, error }) + } - }); } else { - MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { - if (err) throw err; - var dbo = db.db(url.dbname); - try { - dbo.collection(set).find(where).skip(num * page).limit(num).toArray(function (err, result) { - if (err) throw err; - resolve({ code: 0, data: result }); - db.close(); - }); - } catch (error) { - reject({ code: 1, error }) - } + try { + db.collection(set).find(where).skip(num * page).limit(num).toArray(function (err, result) { + if (err) throw err; + resolve({ code: 0, data: result }); + + }); + } catch (error) { + reject({ code: 1, error }) + } - }); } }) } let update = async (set, where = {}, data, defin = true, url = dburl) => { return new Promise((resolve, reject) => { - MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { - if (err) throw err; - var dbo = db.db(url.dbname); - if (defin) { - data = { $set: data } - } - try { - dbo.collection(set).updateOne(where, data, function (err, res) { - if (err) throw err; - resolve({ code: 0 }); - db.close(); - }); - } catch (error) { - reject({ code: 1, error }) - } + if (defin) { + data = { $set: data } + } + try { + db.collection(set).updateOne(where, data, function (err, res) { + if (err) throw err; + resolve({ code: 0 }); + + }); + } catch (error) { + reject({ code: 1, error }) + } - }); }) } let updates = async (set, where = {}, data, url = dburl) => { return new Promise((resolve, reject) => { - MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { - if (err) throw err; - var dbo = db.db(url.dbname); - data = { $set: data } - try { - dbo.collection(set).updateMany(where, data, function (err, res) { - if (err) throw err; - db.close(); - resolve({ code: 0, num: res.result.nModified }); - }); - } catch (error) { - reject({ code: 1, error }) - } + data = { $set: data } + try { + db.collection(set).updateMany(where, data, function (err, res) { + if (err) throw err; + + resolve({ code: 0, num: res.result.nModified }); + }); + } catch (error) { + reject({ code: 1, error }) + } - }); }) } module.exports = function (url, dbname) { dburl = { url, dbname }; + init(); return { add, adds,