"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseModel = void 0;
const events_1 = require("events");
const storage_1 = require("../services/storage");
class BaseModel {
    constructor(collectionName, storageService = storage_1.Storage) {
        this.collectionName = collectionName;
        this.storageService = storageService;
        this.connected = false;
        this.events = new events_1.EventEmitter();
        this.handleConnection();
    }
    async handleConnection() {
        const doConnect = async () => {
            if (this.storageService.db != undefined) {
                this.connected = true;
                this.db = this.storageService.db;
                const connected = this.onConnect();
                this.storageService.modelsConnected.push(connected);
                await connected;
                this.events.emit('CONNECTED');
            }
        };
        if (this.storageService.connected) {
            await doConnect();
        }
        else {
            this.storageService.connection.once('CONNECTED', async () => {
                await doConnect();
            });
        }
    }
    get collection() {
        if (this.storageService.db) {
            return this.storageService.db.collection(this.collectionName);
        }
        else {
            throw new Error('Not connected to the database yet');
        }
    }
}
exports.BaseModel = BaseModel;
//# sourceMappingURL=base.js.map