UNPKG

flexmonster-mongo-connector

Version:

Custom data source API implementation for MongoDB

88 lines 4.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SchemaBuilder = void 0; const APISchema_1 = require("./APISchema"); const SchemaValueObject_1 = require("./SchemaValueObject"); const MongoFieldType_1 = require("../utils/consts/MongoFieldType"); const ClientSideFieldType_1 = require("../utils/consts/ClientSideFieldType"); const SupportedAggregations_1 = require("../utils/consts/SupportedAggregations"); class SchemaBuilder { constructor() { this._shemaObject = null; if (SchemaBuilder._instance != null) throw new Error("Initialization failed: " + "use Singleton.getInstance() instead of new."); SchemaBuilder._instance = this; } static getInstance() { if (this._instance == null) { this._instance = new SchemaBuilder(); } return this._instance; } createShemaFromDocument(document) { this._shemaObject = new APISchema_1.APISchema(); this._parseDocumentShema(document, ""); return this._shemaObject; } _parseDocumentShema(documentObj, folder) { let element = null; let fieldsType = null; let fieldsKey = ""; for (let fieldsCaption in documentObj) { element = documentObj[fieldsCaption]; fieldsType = typeof element; fieldsKey = this._getKeyFromFolder(folder, fieldsCaption); if (MongoFieldType_1.MongoFieldType.FUNCTION == fieldsType) { continue; } else if (MongoFieldType_1.MongoFieldType.STRING == fieldsType || MongoFieldType_1.MongoFieldType.BOOLEAN == fieldsType || fieldsCaption == "_id" || element === null) { this._shemaObject.fields.set(fieldsKey, this._createSchemaObject(fieldsKey, fieldsCaption, ClientSideFieldType_1.ClientSideFieldType.STRING, folder)); } else if (MongoFieldType_1.MongoFieldType.NUMBER == fieldsType) { this._shemaObject.fields.set(fieldsKey, this._createSchemaObject(fieldsKey, fieldsCaption, ClientSideFieldType_1.ClientSideFieldType.NUMBER, folder)); } else if (MongoFieldType_1.MongoFieldType.OBJECT == fieldsType && element instanceof Date) { this._shemaObject.fields.set(fieldsKey, this._createSchemaObject(fieldsKey, fieldsCaption, ClientSideFieldType_1.ClientSideFieldType.DATE, folder)); } else if (MongoFieldType_1.MongoFieldType.OBJECT == fieldsType && (Array.isArray(element) || element instanceof ArrayBuffer)) { continue; } else if (MongoFieldType_1.MongoFieldType.OBJECT == fieldsType && fieldsCaption != "_id" && element != null && element["_bsontype"] != null && element["_bsontype"] == MongoFieldType_1.MongoFieldType.DECIMAL128) { this._shemaObject.fields.set(fieldsKey, this._createSchemaObject(fieldsKey, fieldsCaption, ClientSideFieldType_1.ClientSideFieldType.NUMBER, folder)); } else if (MongoFieldType_1.MongoFieldType.OBJECT == fieldsType && fieldsCaption != "_id" && element != null && element["_bsontype"] == null) { this._parseDocumentShema(element, (folder == "") ? fieldsCaption.toString() : folder + "/" + fieldsCaption.toString()); } } } _createSchemaObject(key, caption, type, folder) { let shemaObject = new SchemaValueObject_1.SchemaValueObject(key, type, caption); if (folder != "") { shemaObject.folder = folder; } if (ClientSideFieldType_1.ClientSideFieldType.NUMBER == type) { shemaObject.aggregations = this._getAggregationList(SupportedAggregations_1.SupportedAggregations.numericFieldAggregations); } else if (ClientSideFieldType_1.ClientSideFieldType.DATE == type) { shemaObject.aggregations = this._getAggregationList(SupportedAggregations_1.SupportedAggregations.dateFieldAggregations); } else { shemaObject.aggregations = this._getAggregationList(SupportedAggregations_1.SupportedAggregations.nonNumericFieldAggregations); } return shemaObject; } _getAggregationList(supportedAggregations) { let aggregations = []; for (let keys in supportedAggregations) { aggregations.push(keys.toString()); } return aggregations; } _getKeyFromFolder(folder, key) { return (folder != "") ? folder.replace(/\//g, '.') + '.' + key : key; } } exports.SchemaBuilder = SchemaBuilder; //# sourceMappingURL=SchemaBuilder.js.map