





var MongoClient = require('mongodb').MongoClient;
//////////////
exports.connfull=async	function (url){
    var urlfix = "mongodb://"+url;
  
     return await new  Promise(async(resolve, reject) => {
        let output={};
        try{
            output.status=200;
            output.data=await MongoClient.connect(urlfix,{ useNewUrlParser: true, useUnifiedTopology: true });
       }catch(err){
           output.status=500;
           output.data=err;
       }
        resolve(output);
    });
}
 ///////////////

exports.DBlist=async function(conn,dbase){
    const dbo = conn.db(dbase);
    return await new  Promise(async(resolve, reject) => {
        let output={};
        try{
            output.status=200;
            output.data=await dbo.listCollections().toArray();
            resolve(output);
        }catch(err){
            output.status=500;
            output.data=err;
            resolve(output);

        }
    });
}
//dbo.createCollection("customers", function(err, res) {
exports.empty=async function(conn,dbase,collection){
    const dbo = conn.db(dbase);
    return await new  Promise(async(resolve, reject) => {
        let output={};
        try{
            output.status=210;
            output.data=await dbo.collection(collection).deleteMany({});
            resolve(output);
        }catch(err){
            output.status=500;
            output.data=err;
            resolve(output);

        }
    });
}
exports.delete=async function(conn,dbase,collection){
    const dbo = conn.db(dbase);
    return await new  Promise(async(resolve, reject) => {
        let output={};
        try{
            output.status=209;
            output.data=await dbo.collection(collection).drop();
            resolve(output);
        }catch(err){
            output.status=500;
            output.data=err;
            resolve(output);

        }
    });
}
exports.import=async function(conn,dbase,collection,setobj){
    const dbo = conn.db(dbase);
    return await new  Promise(async(resolve, reject) => {
        let output={};
        try{
            output.status=200;
            output.data=await dbo.collection(collection).deleteMany({});
            output.data=await dbo.collection(collection).insertMany(setobj);
            resolve(output);
        }catch(err){
            output.status=500;
            output.data=err;
            resolve(output);

        }
    });
}
exports.creat=async function(conn,dbase,collection){
    const dbo = conn.db(dbase);
    return await new  Promise(async(resolve, reject) => {
        let output={};
        try{
            output.status=200;
            output.data=await dbo.createCollection(collection);
            resolve(output);
        }catch(err){
            output.status=500;
            output.data=err;
            resolve(output);

        }
    });
}




exports.xxx=async function(conn,dbase){
    	console.log(dbase);

    const dbo = conn.db(dbase);
    return await new  Promise(async(resolve, reject) => {
        let output={},set={},projec={};
        try{
            output.status=200;
            // output.data=await dbo.collection(collection).updateOne(filter,{$set:set});
            // output.data=await dbo.collection(collection).update(filter,set);
            output.data=await dbo.listCollections().toArray();
	console.log(output);
	//db.getCollectionNames() 
      //db.getCollectionInfos()      
            resolve(output);
        }catch(err){
            output.status=500;
            output.data=err;
            resolve(output);

        }
    });
}
