flexmonster-mongo-connector
Version:
MongoDB connector for Flexmonster Pivot Table and Charts
104 lines • 5.36 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongoAPIManager = void 0;
const MongoQueryExecutor_1 = require("../query/MongoQueryExecutor");
const MongoResponseParser_1 = require("../parsers/MongoResponseParser");
const QueryBuilder_1 = require("../query/builder/QueryBuilder");
const DataManager_1 = require("../cache/DataManager");
const ConfigManager_1 = require("../config/ConfigManager");
const LoggingManager_1 = require("../logging/LoggingManager");
const RequestType_1 = require("../requests/apiRequests/RequestType");
class MongoAPIManager {
constructor(config, apiVersion) {
this._apiVersion = apiVersion;
this.initializeComponents(config);
}
getSchema(dbo, index) {
return __awaiter(this, void 0, void 0, function* () {
const apiSchema = yield this._getSchema(dbo, index);
return apiSchema.toJSON();
});
}
getMembers(dbo, index, fieldObject, pagingObject) {
return __awaiter(this, void 0, void 0, function* () {
const apiSchema = yield this._getSchema(dbo, index);
return this._dataManager.getData({
index: index,
fieldObject: fieldObject["field"],
clientQuery: { "members": fieldObject },
db: dbo,
schema: apiSchema
}, RequestType_1.RequestType.MEMBERS_REQUEST, pagingObject);
});
}
getSelectResult(dbo, index, query, pagingObject) {
return __awaiter(this, void 0, void 0, function* () {
let response = null;
const apiSchema = yield this._getSchema(dbo, index);
if (query["aggs"] != null && query["fields"] == null) {
response = this._dataManager.getData({
index: index,
clientQuery: query,
db: dbo,
schema: apiSchema
}, RequestType_1.RequestType.AGGREGATION_REQUEST, pagingObject);
}
else if (query["aggs"] == null && query["fields"] != null) {
response = this._dataManager.getData({
index: index,
clientQuery: query,
db: dbo,
schema: apiSchema
}, RequestType_1.RequestType.DRILLTHROUGH_REQUEST, pagingObject);
}
else if (query["aggs"] != null && query["fields"] != null) {
response = this._dataManager.getData({
index: index,
clientQuery: query,
db: dbo,
schema: apiSchema
}, RequestType_1.RequestType.FLAT_REQUEST, pagingObject);
}
return response;
});
}
_getSchema(dbo, index) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof index != 'string')
throw new Error("Incorrect index format");
this._mongoQueryManager.injectDBConnection(dbo);
const dbName = dbo.databaseName;
if (this._schemaCache[`${dbName}_${index}`] == null) {
let document = yield this._mongoQueryManager.runShemaQuery(index);
this._schemaCache[`${dbName}_${index}`] = this._mongoResponseParser.parseShemaFromDocument(document);
LoggingManager_1.LoggingManager.log(`Quering database ${dbName} to get schema of index ${index}`);
}
else {
LoggingManager_1.LoggingManager.log(`Getting schema of index ${index} of database ${dbName} from cache`);
}
return this._schemaCache[`${dbName}_${index}`];
});
}
initializeComponents(config) {
this._configManager = ConfigManager_1.ConfigManager.getInstance(config);
this._mongoQueryManager = new MongoQueryExecutor_1.MongoQueryExecutor();
this._mongoResponseParser = MongoResponseParser_1.MongoResponseParser.getInstance();
new LoggingManager_1.LoggingManager(this._configManager.currentConfig.logsEnabled);
this._queryBuilder = QueryBuilder_1.QueryBuilder.getInstance();
this._dataManager = new DataManager_1.DataManager(this._queryBuilder, this._mongoQueryManager);
this._schemaCache = {};
LoggingManager_1.LoggingManager.log("Version:", this._apiVersion);
LoggingManager_1.LoggingManager.log("Started with the following config", this._configManager);
}
}
exports.MongoAPIManager = MongoAPIManager;
//# sourceMappingURL=MongoAPIManager.js.map