撤销更改

This commit is contained in:
luyuan 2021-01-11 11:17:03 +08:00
parent 6676bd64c4
commit 0e37d956a7

View File

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