const MongoClient = require('mongodb').MongoClient
const url = 'mongodb://localhost:27017/TestCafeDBConnection';
const dbName = 'test';

function getClient() {
    return new Promise((resolve, reject) => {

        MongoClient.connect(url, function(err, client) {
            if (err)
                return reject(err);

            console.log("Connected successfully to server");

            resolve(client);
        });
    });
}

function findDocs (db) {
    return new Promise((resolve, reject) => {
        const collection = db.collection('UserData');
        collection.find({}).toArray(function(err, docs) {
            if (err)
                return reject(err);
            console.log("Found the following records");
            console.log(docs);
            resolve(docs);
        });
    });
}

export async function getKeywords () {
    const client = await getClient();
    const db     = client.db(dbName);
    try {
        return await findDocs(db);
    }
    finally {
        client.close();
    }
}

