撤销更改

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);
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) => {
@ -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 {
resolve({ code: 0, num: res.insertedCount }); dbo.collection(set).insertMany(data, function (err, res) {
}); if (err) throw err;
} catch (error) { db.close();
reject({ code: 1, error }) resolve({ code: 0, num: res.insertedCount });
} });
} 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) => {
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 {
resolve({ code: 0 }); dbo.collection(set).deleteOne(where, function (err, obj) {
}); if (err) throw err;
} catch (error) { db.close();
reject({ code: 1, error }) resolve({ code: 0 });
} });
} catch (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 {
resolve({ code: 0, num: obj.result.n }); dbo.collection(set).deleteMany(where, function (err, obj) {
}); if (err) throw err;
} catch (error) { db.close();
reject({ code: 1, error }) resolve({ code: 0, num: obj.result.n });
} });
} 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) {
try { MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
console.log(db) if (err) throw err;
db.collection(set).find(where).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).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 })
}
});
} else { } else {
try { MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
db.collection(set).find(where).skip(num * page).limit(num).toArray(function (err, result) { if (err) throw err;
if (err) throw err; var dbo = db.db(url.dbname);
resolve({ code: 0, data: result }); try {
dbo.collection(set).find(where).skip(num * page).limit(num).toArray(function (err, result) {
}); if (err) throw err;
} catch (error) { resolve({ code: 0, data: result });
reject({ code: 1, error }) db.close();
} });
} 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) => {
if (defin) { MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
data = { $set: data } if (err) throw err;
} var dbo = db.db(url.dbname);
try { if (defin) {
db.collection(set).updateOne(where, data, function (err, res) { data = { $set: data }
if (err) throw err; }
resolve({ code: 0 }); try {
dbo.collection(set).updateOne(where, data, function (err, res) {
}); if (err) throw err;
} catch (error) { resolve({ code: 0 });
reject({ code: 1, error }) db.close();
} });
} 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) => {
data = { $set: data } MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
try { if (err) throw err;
db.collection(set).updateMany(where, data, function (err, res) { var dbo = db.db(url.dbname);
if (err) throw err; data = { $set: data }
try {
resolve({ code: 0, num: res.result.nModified }); dbo.collection(set).updateMany(where, data, function (err, res) {
}); if (err) throw err;
} catch (error) { db.close();
reject({ code: 1, error }) resolve({ code: 0, num: res.result.nModified });
} });
} 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,