初始化链接 全局链接一次 长链接

This commit is contained in:
luyuan 2021-01-11 11:14:05 +08:00
parent c7771813dc
commit 6676bd64c4

View File

@ -1,5 +1,21 @@
var MongoClient = require('mongodb').MongoClient; var MongoClient = require('mongodb').MongoClient;
var dburl; 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 idadd = async (set) => {
let id = 0 let id = 0
if (set == "idadd") { if (set == "idadd") {
@ -33,21 +49,17 @@ let add = async (set, data, url = dburl) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { try {
if (err) throw err; db.collection(set).insertOne(data, function (err, res) {
var dbo = db.db(url.dbname); if (err) throw err;
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 })
}
}); resolve({ code: 0, msg: "添加成功" });
}) });
} catch (error) {
reject({ code: 1, error })
}
});
} }
let adds = async (set, data, url = dburl) => { let adds = async (set, data, url = dburl) => {
@ -59,38 +71,30 @@ let adds = async (set, data, url = dburl) => {
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { try {
if (err) throw err; db.collection(set).insertMany(data, function (err, res) {
var dbo = db.db(url.dbname); if (err) throw err;
try {
dbo.collection(set).insertMany(data, function (err, res) { resolve({ code: 0, num: res.insertedCount });
if (err) throw err; });
db.close(); } catch (error) {
resolve({ code: 0, num: res.insertedCount }); reject({ code: 1, error })
}); }
} catch (error) {
reject({ code: 1, error })
}
});
}) })
} }
let remove = async (set, where, url = dburl) => { let remove = async (set, where, url = dburl) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { try {
if (err) throw err; db.collection(set).deleteOne(where, function (err, obj) {
var dbo = db.db(url.dbname); if (err) throw err;
try {
dbo.collection(set).deleteOne(where, function (err, obj) { resolve({ code: 0 });
if (err) throw err; });
db.close(); } catch (error) {
resolve({ code: 0 }); reject({ code: 1, error })
}); }
} catch (error) {
reject({ code: 1, error })
}
});
}) })
} }
let removes = async (set, where, url = dburl) => { 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) { if (where == "" || Object.keys(where).length == 0) {
reject({ code: 1 }) reject({ code: 1 })
} }
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { try {
if (err) throw err; db.collection(set).deleteMany(where, function (err, obj) {
var dbo = db.db(url.dbname); if (err) throw err;
try {
dbo.collection(set).deleteMany(where, function (err, obj) { resolve({ code: 0, num: obj.result.n });
if (err) throw err; });
db.close(); } catch (error) {
resolve({ code: 0, num: obj.result.n }); reject({ code: 1, error })
}); }
} catch (error) {
reject({ code: 1, error })
}
});
}) })
} }
let find = async (set, where = {}, page = 0, num = 0, url = dburl) => { let find = async (set, where = {}, page = 0, num = 0, url = dburl) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (num == 0) { if (num == 0) {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { try {
if (err) throw err; console.log(db)
var dbo = db.db(url.dbname); db.collection(set).find(where).toArray(function (err, result) {
try { if (err) throw err;
dbo.collection(set).find(where).toArray(function (err, result) { resolve({ code: 0, data: result });
if (err) throw err;
resolve({ code: 0, data: result }); });
db.close(); } catch (error) {
}); reject({ code: 1, error })
} catch (error) { }
reject({ code: 1, error })
}
});
} else { } else {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { try {
if (err) throw err; db.collection(set).find(where).skip(num * page).limit(num).toArray(function (err, result) {
var dbo = db.db(url.dbname); if (err) throw err;
try { resolve({ code: 0, data: result });
dbo.collection(set).find(where).skip(num * page).limit(num).toArray(function (err, result) {
if (err) throw err; });
resolve({ code: 0, data: result }); } catch (error) {
db.close(); reject({ code: 1, error })
}); }
} catch (error) {
reject({ code: 1, error })
}
});
} }
}) })
} }
let update = async (set, where = {}, data, defin = true, url = dburl) => { let update = async (set, where = {}, data, defin = true, url = dburl) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { if (defin) {
if (err) throw err; data = { $set: data }
var dbo = db.db(url.dbname); }
if (defin) { try {
data = { $set: data } db.collection(set).updateOne(where, data, function (err, res) {
} if (err) throw err;
try { resolve({ code: 0 });
dbo.collection(set).updateOne(where, data, function (err, res) {
if (err) throw err; });
resolve({ code: 0 }); } catch (error) {
db.close(); reject({ code: 1, error })
}); }
} catch (error) {
reject({ code: 1, error })
}
});
}) })
} }
let updates = async (set, where = {}, data, url = dburl) => { let updates = async (set, where = {}, data, url = dburl) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) { data = { $set: data }
if (err) throw err; try {
var dbo = db.db(url.dbname); db.collection(set).updateMany(where, data, function (err, res) {
data = { $set: data } if (err) throw err;
try {
dbo.collection(set).updateMany(where, data, function (err, res) { resolve({ code: 0, num: res.result.nModified });
if (err) throw err; });
db.close(); } catch (error) {
resolve({ code: 0, num: res.result.nModified }); reject({ code: 1, error })
}); }
} catch (error) {
reject({ code: 1, error })
}
});
}) })
} }
module.exports = function (url, dbname) { module.exports = function (url, dbname) {
dburl = { url, dbname }; dburl = { url, dbname };
init();
return { return {
add, add,
adds, adds,