var MongoClient = require('mongodb').MongoClient; var dburl; 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) => { 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 }); }); } catch (error) { reject({ code: 1, error }) } }); }) } 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) => { 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) => { 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) => { return new Promise((resolve, reject) => { if (where == "" || Object.keys(where).length == 0) { reject({ code: 1 }) } 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 { 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 { 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 { 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 { 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 }; return { add, adds, remove, find, update, updates, removes } }