2019-11-12 23:30:26 +08:00
|
|
|
var MongoClient = require('mongodb').MongoClient;
|
|
|
|
var dburl;
|
2019-11-13 20:17:51 +08:00
|
|
|
let add = (set, data, url = dburl) => {
|
2019-11-12 23:30:26 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-11-13 20:17:51 +08:00
|
|
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
|
|
|
var dbo = db.db(url.dbname);
|
|
|
|
try {
|
2019-11-13 20:17:51 +08:00
|
|
|
dbo.collection(set).insertOne(data, function (err, res) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
|
|
|
db.close();
|
2019-11-13 20:17:51 +08:00
|
|
|
resolve({ code: 0 });
|
2019-11-12 23:30:26 +08:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2019-11-13 20:17:51 +08:00
|
|
|
reject({ code: 1, error })
|
2019-11-12 23:30:26 +08:00
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
|
2019-11-12 23:30:26 +08:00
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
let adds = (set, data, url = dburl) => {
|
2019-11-12 23:30:26 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-11-13 20:17:51 +08:00
|
|
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
|
|
|
var dbo = db.db(url.dbname);
|
|
|
|
try {
|
2019-11-13 20:17:51 +08:00
|
|
|
dbo.collection(set).insertMany(data, function (err, res) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
|
|
|
db.close();
|
2019-11-13 20:17:51 +08:00
|
|
|
resolve({ code: 0, num: res.insertedCount });
|
2019-11-12 23:30:26 +08:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2019-11-13 20:17:51 +08:00
|
|
|
reject({ code: 1, error })
|
2019-11-12 23:30:26 +08:00
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
|
2019-11-12 23:30:26 +08:00
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
let remove = (set, where, url = dburl) => {
|
2019-11-12 23:30:26 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-11-13 20:17:51 +08:00
|
|
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
|
|
|
var dbo = db.db(url.dbname);
|
|
|
|
try {
|
2019-11-13 20:17:51 +08:00
|
|
|
dbo.collection(set).deleteOne(where, function (err, obj) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
|
|
|
db.close();
|
2019-11-13 20:17:51 +08:00
|
|
|
resolve({ code: 0 });
|
2019-11-12 23:30:26 +08:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2019-11-13 20:17:51 +08:00
|
|
|
reject({ code: 1, error })
|
2019-11-12 23:30:26 +08:00
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
|
2019-11-12 23:30:26 +08:00
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
let removes = (set, where, url = dburl) => {
|
2019-11-12 23:30:26 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-11-13 20:17:51 +08:00
|
|
|
if (where == "" || Object.keys(where).length == 0) {
|
|
|
|
reject({ code: 1 })
|
2019-11-12 23:54:52 +08:00
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
|
|
|
var dbo = db.db(url.dbname);
|
|
|
|
try {
|
2019-11-13 20:17:51 +08:00
|
|
|
dbo.collection(set).deleteMany(where, function (err, obj) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
|
|
|
db.close();
|
2019-11-13 20:17:51 +08:00
|
|
|
resolve({ code: 0, num: obj.result.n });
|
2019-11-12 23:30:26 +08:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2019-11-13 20:17:51 +08:00
|
|
|
reject({ code: 1, error })
|
2019-11-12 23:30:26 +08:00
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
|
2019-11-12 23:30:26 +08:00
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
let find = (set, where = {}, page = 0, num = 0, url = dburl) => {
|
2019-11-12 23:30:26 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-11-13 20:17:51 +08:00
|
|
|
if (num == 0) {
|
|
|
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
|
|
|
var dbo = db.db(url.dbname);
|
|
|
|
try {
|
2019-11-13 20:17:51 +08:00
|
|
|
dbo.collection(set).find(where).toArray(function (err, result) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
2019-11-13 20:17:51 +08:00
|
|
|
resolve({ code: 0, data: result });
|
2019-11-12 23:30:26 +08:00
|
|
|
db.close();
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2019-11-13 20:17:51 +08:00
|
|
|
reject({ code: 1, error })
|
2019-11-12 23:30:26 +08:00
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
|
2019-11-12 23:30:26 +08:00
|
|
|
});
|
2019-11-13 20:17:51 +08:00
|
|
|
} else {
|
|
|
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
|
|
|
var dbo = db.db(url.dbname);
|
|
|
|
try {
|
2019-11-13 20:17:51 +08:00
|
|
|
dbo.collection(set).find(where).skip(num * page).limit(num).toArray(function (err, result) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
2019-11-13 20:17:51 +08:00
|
|
|
resolve({ code: 0, data: result });
|
2019-11-12 23:30:26 +08:00
|
|
|
db.close();
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2019-11-13 20:17:51 +08:00
|
|
|
reject({ code: 1, error })
|
2019-11-12 23:30:26 +08:00
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
|
2019-11-12 23:30:26 +08:00
|
|
|
});
|
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
|
2019-11-12 23:30:26 +08:00
|
|
|
})
|
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
let update = (set, where = {}, data, defin = true, url = dburl) => {
|
2019-11-12 23:30:26 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-11-13 20:17:51 +08:00
|
|
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
|
|
|
var dbo = db.db(url.dbname);
|
2019-11-13 20:17:51 +08:00
|
|
|
if (defin) {
|
|
|
|
data = { $set: data }
|
|
|
|
}
|
2019-11-12 23:30:26 +08:00
|
|
|
try {
|
2019-11-13 20:17:51 +08:00
|
|
|
dbo.collection(set).updateOne(where, data, function (err, res) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
2019-11-13 20:17:51 +08:00
|
|
|
resolve({ code: 0 });
|
2019-11-12 23:30:26 +08:00
|
|
|
db.close();
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2019-11-13 20:17:51 +08:00
|
|
|
reject({ code: 1, error })
|
2019-11-12 23:30:26 +08:00
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
|
2019-11-12 23:30:26 +08:00
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
let updates = (set, where = {}, data, url = dburl) => {
|
2019-11-12 23:30:26 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-11-13 20:17:51 +08:00
|
|
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
|
|
|
var dbo = db.db(url.dbname);
|
2019-11-13 20:17:51 +08:00
|
|
|
data = { $set: data }
|
2019-11-12 23:30:26 +08:00
|
|
|
try {
|
2019-11-13 20:17:51 +08:00
|
|
|
dbo.collection(set).updateMany(where, data, function (err, res) {
|
2019-11-12 23:30:26 +08:00
|
|
|
if (err) throw err;
|
|
|
|
db.close();
|
2019-11-13 20:17:51 +08:00
|
|
|
resolve({ code: 0, num: res.result.nModified });
|
2019-11-12 23:30:26 +08:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2019-11-13 20:17:51 +08:00
|
|
|
reject({ code: 1, error })
|
2019-11-12 23:30:26 +08:00
|
|
|
}
|
2019-11-13 20:17:51 +08:00
|
|
|
|
2019-11-12 23:30:26 +08:00
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-13 20:17:51 +08:00
|
|
|
module.exports = function (url, dbname) {
|
|
|
|
dburl = { url, dbname };
|
|
|
|
return {
|
2019-11-12 23:30:26 +08:00
|
|
|
add,
|
|
|
|
adds,
|
|
|
|
remove,
|
|
|
|
find,
|
|
|
|
update,
|
|
|
|
updates,
|
|
|
|
removes
|
|
|
|
}
|
|
|
|
}
|