dt-common-device
Version:
A secure and robust device management library for IoT applications
54 lines (53 loc) • 1.62 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.connectDatabase = void 0;
exports.getPostgresClient = getPostgresClient;
exports.getAccessPostgresClient = getAccessPostgresClient;
exports.getPmsPostgresClient = getPmsPostgresClient;
const pg_1 = require("pg");
const config_1 = require("../config/config");
const mongoose_1 = __importDefault(require("mongoose"));
function getPostgresClient() {
const URI = (0, config_1.getAdminPostgresDbUri)();
let pool = null;
if (!pool) {
pool = new pg_1.Pool({
connectionString: URI,
});
}
return pool;
}
function getAccessPostgresClient() {
const URI = (0, config_1.getAccessPostgresDbUri)();
let pool = null;
if (!pool) {
pool = new pg_1.Pool({
connectionString: URI,
});
}
return pool;
}
function getPmsPostgresClient() {
const URI = (0, config_1.getPmsPostgresDbUri)();
let pool = null;
if (!pool) {
pool = new pg_1.Pool({
connectionString: URI,
});
}
return pool;
}
const connectDatabase = async () => {
try {
const URI = (0, config_1.getMongoUri)();
await mongoose_1.default.connect(URI);
(0, config_1.getLogger)().info(`Connected to MongoDB at ${URI.split("/").at(-1)}`);
}
catch (error) {
(0, config_1.getLogger)().error("Error connecting to MongoDB:", error);
}
};
exports.connectDatabase = connectDatabase;