2019-12-11 01:38:55 +00:00
|
|
|
var MongoClient = require('mongodb').MongoClient;
|
|
|
|
var dburl;
|
2021-01-11 03:14:05 +00:00
|
|
|
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;
|
|
|
|
})
|
|
|
|
}
|
2019-12-11 01:38:55 +00:00
|
|
|
let idadd = async (set) => {
|
|
|
|
let id = 0
|
|
|
|
if (set == "idadd") {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
await find('idadd', { 'set': set }).then((res) => {
|
|
|
|
if (res.data.length != 0) {
|
|
|
|
id = res.data[0].num_key
|
|
|
|
} else {
|
|
|
|
id = 1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == 1) {
|
|
|
|
await add('idadd', { set, 'num_key': 2 })
|
|
|
|
} else if (set != "idadd") {
|
|
|
|
console.log(id)
|
|
|
|
|
|
|
|
await update('idadd', { 'set': set }, { 'num_key': id + 1 })
|
|
|
|
}
|
|
|
|
console.log(id)
|
|
|
|
return id
|
|
|
|
|
|
|
|
}
|
|
|
|
let add = async (set, data, url = dburl) => {
|
|
|
|
if (set != "idadd") {
|
|
|
|
let id = await idadd(set)
|
|
|
|
data.num_key = id
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
2021-01-11 03:14:05 +00:00
|
|
|
try {
|
|
|
|
db.collection(set).insertOne(data, function (err, res) {
|
|
|
|
if (err) throw err;
|
2019-12-11 01:38:55 +00:00
|
|
|
|
2021-01-11 03:14:05 +00:00
|
|
|
resolve({ code: 0, msg: "添加成功" });
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
reject({ code: 1, error })
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2019-12-11 01:38:55 +00:00
|
|
|
}
|
|
|
|
let adds = async (set, data, url = dburl) => {
|
|
|
|
|
|
|
|
if (set != "idadd") {
|
|
|
|
for (let i in data) {
|
|
|
|
let id = await idadd(set)
|
|
|
|
data[i].num_key = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
2021-01-11 03:14:05 +00:00
|
|
|
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 })
|
|
|
|
}
|
2019-12-11 01:38:55 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
let remove = async (set, where, url = dburl) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-01-11 03:14:05 +00:00
|
|
|
try {
|
|
|
|
db.collection(set).deleteOne(where, function (err, obj) {
|
|
|
|
if (err) throw err;
|
|
|
|
|
|
|
|
resolve({ code: 0 });
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
reject({ code: 1, error })
|
|
|
|
}
|
2019-12-11 01:38:55 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
let removes = async (set, where, url = dburl) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (where == "" || Object.keys(where).length == 0) {
|
|
|
|
reject({ code: 1 })
|
|
|
|
}
|
2021-01-11 03:14:05 +00:00
|
|
|
try {
|
|
|
|
db.collection(set).deleteMany(where, function (err, obj) {
|
2019-12-11 01:38:55 +00:00
|
|
|
if (err) throw err;
|
|
|
|
|
2021-01-11 03:14:05 +00:00
|
|
|
resolve({ code: 0, num: obj.result.n });
|
2019-12-11 01:38:55 +00:00
|
|
|
});
|
2021-01-11 03:14:05 +00:00
|
|
|
} catch (error) {
|
|
|
|
reject({ code: 1, error })
|
2019-12-11 01:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
2021-01-11 03:14:05 +00:00
|
|
|
let find = async (set, where = {}, page = 0, num = 0, url = dburl) => {
|
2019-12-11 01:38:55 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-01-11 03:14:05 +00:00
|
|
|
if (num == 0) {
|
2019-12-11 01:38:55 +00:00
|
|
|
try {
|
2021-01-11 03:14:05 +00:00
|
|
|
console.log(db)
|
|
|
|
db.collection(set).find(where).toArray(function (err, result) {
|
2019-12-11 01:38:55 +00:00
|
|
|
if (err) throw err;
|
2021-01-11 03:14:05 +00:00
|
|
|
resolve({ code: 0, data: result });
|
|
|
|
|
2019-12-11 01:38:55 +00:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
reject({ code: 1, error })
|
|
|
|
}
|
|
|
|
|
2021-01-11 03:14:05 +00:00
|
|
|
} else {
|
2019-12-11 01:38:55 +00:00
|
|
|
try {
|
2021-01-11 03:14:05 +00:00
|
|
|
db.collection(set).find(where).skip(num * page).limit(num).toArray(function (err, result) {
|
2019-12-11 01:38:55 +00:00
|
|
|
if (err) throw err;
|
2021-01-11 03:14:05 +00:00
|
|
|
resolve({ code: 0, data: result });
|
|
|
|
|
2019-12-11 01:38:55 +00:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
reject({ code: 1, error })
|
|
|
|
}
|
|
|
|
|
2021-01-11 03:14:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
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 })
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
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 })
|
|
|
|
}
|
|
|
|
|
2019-12-11 01:38:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function (url, dbname) {
|
|
|
|
dburl = { url, dbname };
|
2021-01-11 03:14:05 +00:00
|
|
|
init();
|
2019-12-11 01:38:55 +00:00
|
|
|
return {
|
|
|
|
add,
|
|
|
|
adds,
|
|
|
|
remove,
|
|
|
|
find,
|
|
|
|
update,
|
|
|
|
updates,
|
|
|
|
removes
|
|
|
|
}
|
|
|
|
}
|