"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 _database = require("@tamanu/database/services/database");
const _generateId = require("@tamanu/utils/generateId");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const getOrCreateConnection = async (configOverrides, key = 'main')=>{
    const testMode = process.env.NODE_ENV === 'test';
    return await (0, _database.openDatabase)(key, {
        ..._config.default.db,
        ...configOverrides,
        testMode
    });
};
async function initDatabase(configOverrides = {}) {
    const testMode = process.env.NODE_ENV === 'test';
    return getOrCreateConnection({
        configOverrides,
        primaryKeyDefault: testMode ? _generateId.fakeUUID : undefined
    });
}
async function closeDatabase() {
    return (0, _database.closeAllDatabases)();
}

//# sourceMappingURL=initDatabase.js.map