import * as Mongoose from "mongoose";
import debug from "debug";
import Ingredient from "./Ingredient";
import Drink from "./Drink";
import Container from "./Container";

const log = debug("itender:server");

export class Database {
    public static connect(): Promise<void> {
        return new Promise(async (resolve, reject) => {
            try {
                await Mongoose.connect("mongodb://localhost:27017/iTender?retryWrites=true");
                log("Connected to Database");
                /*if (Mongoose.connection.readyState == Mongoose.ConnectionStates.connected) {
                    resolve();
                } else {
                    reject("Can't connect to database");
                }*/

                // Preload schemes
                Ingredient.find();
                Drink.find();
                Container.find();

                resolve();
            } catch (e) {
                reject("Exception whilst connecting to Database " + e);
            }

        });
    }
}