"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get closeDatabase () {
        return closeDatabase;
    },
    get initDatabase () {
        return initDatabase;
    }
});
const _config = /*#__PURE__*/ _interop_require_default(require("config"));
const _hooks = require("./hooks");
const _database = require("@tamanu/database/services/database");
const _setFhirRefreshTriggers = require("./setFhirRefreshTriggers");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const getOrCreateConnection = async ({ testMode, ...configOverrides }, key = 'main')=>{
    const store = await (0, _database.openDatabase)(key, {
        ..._config.default.db,
        ...configOverrides,
        testMode
    });
    // drop and recreate db
    if (testMode) {
        await store.sequelize.drop({
            cascade: true
        });
        await store.sequelize.migrate('up');
    }
    if (key === 'main') {
        await (0, _hooks.addHooks)(store);
    }
    if (!testMode) {
        await (0, _setFhirRefreshTriggers.setFhirRefreshTriggers)(store.sequelize);
    }
    return store;
};
async function initDatabase({ testMode = false, dbKey = 'main' }) {
    // connect to database
    return await getOrCreateConnection({
        testMode,
        saltRounds: _config.default.auth.saltRounds
    }, dbKey);
}
async function closeDatabase() {
    return (0, _database.closeAllDatabases)();
}

//# sourceMappingURL=index.js.map