撤销更改
This commit is contained in:
parent
6676bd64c4
commit
0e37d956a7
@ -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,10 +33,13 @@ let add = async (set, data, url = dburl) => {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
try {
|
||||
db.collection(set).insertOne(data, function (err, res) {
|
||||
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) {
|
||||
@ -60,6 +47,7 @@ let add = 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) => {
|
||||
|
||||
try {
|
||||
db.collection(set).insertMany(data, function (err, res) {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
||||
if (err) throw err;
|
||||
var dbo = db.db(url.dbname);
|
||||
try {
|
||||
console.log(db)
|
||||
db.collection(set).find(where).toArray(function (err, result) {
|
||||
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 {
|
||||
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
||||
if (err) throw err;
|
||||
var dbo = db.db(url.dbname);
|
||||
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;
|
||||
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) => {
|
||||
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 {
|
||||
db.collection(set).updateOne(where, data, function (err, res) {
|
||||
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) => {
|
||||
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 {
|
||||
db.collection(set).updateMany(where, data, function (err, res) {
|
||||
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,
|
||||
|
Loading…
Reference in New Issue
Block a user