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

// create a namespace for logging
const log = debug("itender:server");

export class Database {
    //  method to connect to the database
    public static connect(): Promise<void> {
        return new Promise(async (resolve, reject) => {
            try {
                //connect to mongodb using mongoose
                mongoose.set("strictQuery", false);
                await Mongoose.connect("mongodb://127.0.0.1:27017/iTender?retryWrites=true");
                log("Connected to Database");
                // Preload the schema for Ingredient, Drink and Container
                Ingredient.find();
                Drink.find();
                Container.find();
                resolve();
            } catch (e) {
                reject("Exception whilst connecting to Database " + e);
            }
        });
    }
}
