撤销更改

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 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") {
@ -49,17 +33,21 @@ let add = async (set, data, url = dburl) => {
return new Promise((resolve, reject) => {
try {
db.collection(set).insertOne(data, function (err, res) {
if (err) throw err;
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 })
}
resolve({ code: 0, msg: "添加成功" });
});
} catch (error) {
reject({ code: 1, error })
}
});
});
})
}
let adds = async (set, data, url = dburl) => {
@ -71,30 +59,38 @@ let adds = async (set, data, url = dburl) => {
}
return new Promise((resolve, reject) => {
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 })
}
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 })
}
});
})
}
let remove = async (set, where, url = dburl) => {
return new Promise((resolve, reject) => {
try {
db.collection(set).deleteOne(where, function (err, obj) {
if (err) throw err;
resolve({ code: 0 });
});
} catch (error) {
reject({ code: 1, error })
}
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 })
}
});
})
}
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) {
reject({ code: 1 })
}
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 })
}
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 })
}
});
})
}
let find = async (set, where = {}, page = 0, num = 0, url = dburl) => {
return new Promise((resolve, reject) => {
if (num == 0) {
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 })
}
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 })
}
});
} else {
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 })
}
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 })
}
});
}
})
}
let update = async (set, where = {}, data, defin = true, url = dburl) => {
return new Promise((resolve, reject) => {
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 })
}
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 })
}
});
})
}
let updates = async (set, where = {}, data, url = dburl) => {
return new Promise((resolve, reject) => {
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 })
}
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 })
}
});
})
}
module.exports = function (url, dbname) {
dburl = { url, dbname };
init();
return {
add,
adds,