2019-11-12 23:30:26 +08:00
|
|
|
var MongoClient = require('mongodb').MongoClient;
|
|
|
|
var dburl;
|
2019-11-19 13:37:11 +08: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
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:30:26 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-11-19 13:37:11 +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).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-19 13:37:11 +08: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
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 23:30:26 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-11-19 13:37:11 +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).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-19 13:37:11 +08:00
|
|
|
let remove = async (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-19 13:37:11 +08:00
|
|
|
let removes = async (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-19 13:37:11 +08:00
|
|
|
let find = async (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-19 13:37:11 +08:00
|
|
|
let update = async (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-19 13:37:11 +08:00
|
|
|
let updates = async (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
|
|
|
|
}
|
|
|
|
}
|