mongorai
Version:
Light MongoDB client for the web. Minimalistic UI used React with minimum dependencies.
141 lines (140 loc) • 4.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongoManager = void 0;
const url_1 = require("url");
const mongodb_1 = require("mongodb");
const Init_1 = require("./Init");
const Server_1 = require("./Server");
const Utils_1 = require("./Utils");
class MongoManager {
constructor() {
this._servers = {};
}
async connect(host) {
const urlStr = host.path.startsWith('mongodb')
? host.path
: `mongodb://${host.path}`;
const url = new url_1.URL(urlStr);
const hostname = url.host || host.path;
if (this._servers[hostname] instanceof Server_1.Server) {
return;
}
try {
const client = new mongodb_1.MongoClient(urlStr);
await client.connect();
const server = new Server_1.Server(hostname, client);
this._servers[hostname] = server;
console.info(`[${hostname}] Connected to ${hostname}`);
await this.checkAuth(hostname);
}
catch (err) {
console.error(`Error while connecting to ${hostname}:`, err instanceof Error ? err.message : 'Unknown error');
this._servers[hostname] = err instanceof mongodb_1.MongoError
? err
: new mongodb_1.MongoError(err instanceof Error ? err.message : 'Unknown connection error');
}
}
getServer(name) {
const server = this._servers[name] || this._servers[`${name}:27017`];
if (!server) {
throw new Error('Server does not exist');
}
return server;
}
async checkAuth(name) {
const server = this.getServer(name);
if (server instanceof mongodb_1.MongoError) {
return;
}
try {
await server.toJson();
}
catch (err) {
if (err instanceof mongodb_1.MongoError && err.code === 13) {
this._servers[name] = err;
}
}
}
async load() {
const hosts = await Init_1.Init.hostsManager.getHosts();
await Promise.all(hosts.map((h) => this.connect(h)));
}
removeServer(name) {
delete this._servers[name];
}
async getServersJson() {
const servers = [];
for (const [name, server] of Object.entries(this._servers)) {
if (server instanceof mongodb_1.MongoError) {
servers.push({
name: name,
error: {
code: server.code,
name: server.name,
message: server.message
}
});
}
else {
try {
const json = await server.toJson();
servers.push(json);
}
catch (err) {
servers.push({
name: name,
error: {
code: err instanceof mongodb_1.MongoError ? err.code : undefined,
name: 'ServerError',
message: err instanceof Error ? err.message : 'Unknown error'
}
});
}
}
}
Utils_1.Utils.fieldSort(servers, "name");
return servers;
}
async getDatabasesJson(serverName) {
const server = this.getServer(serverName);
if (server instanceof mongodb_1.MongoError) {
return [];
}
try {
const json = await server.toJson();
return json.databases;
}
catch {
return [];
}
}
async getCollectionsJson(serverName, databaseName) {
const server = this.getServer(serverName);
if (server instanceof mongodb_1.MongoError) {
return [];
}
try {
const database = await server.database(databaseName);
if (!database)
return [];
const json = await database.toJson();
return json.collections;
}
catch {
return [];
}
}
async getCollection(serverName, databaseName, collectionName) {
const server = this.getServer(serverName);
if (server instanceof mongodb_1.MongoError)
return;
try {
const database = await server.database(databaseName);
return database ? await database.collection(collectionName) : undefined;
}
catch {
return undefined;
}
}
}
exports.MongoManager = MongoManager;