UNPKG

@wbg-mde/repository

Version:

Managing all common method for file system CRUD operations.

83 lines (82 loc) 3.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const app_repo_utility_1 = require("../shared/app.repo.utility"); const transformer_helpers_1 = require("./transformer.helpers"); const fs = require("fs"); class ModelTransformer { constructor() { this.helper = new transformer_helpers_1.TransformHelpers(); } transformMetadata(mappingFile, metadata) { if (fs.existsSync(mappingFile)) { let mapping = JSON.parse(app_repo_utility_1.App_Repository_Utility.readFile(mappingFile).toString()); return Object.keys(mapping).reduce((datum, name) => { let val = this.getModelValue(mapping[name], metadata, ''); if (!app_repo_utility_1.App_Repository_Utility.isEmpty(val)) { datum[name] = val; } return datum; }, {}); } else { return metadata; } } getModelValue(mapping, metadata, parentKey) { let modelpath = mapping["_model_path"]; let value = metadata; if (modelpath) { let key = this.getKeyFromPath(modelpath, parentKey); value = app_repo_utility_1.App_Repository_Utility.getNestedProperty(metadata, key); } else { modelpath = parentKey; } if (!app_repo_utility_1.App_Repository_Utility.isEmpty(value) && mapping["_map_fn"]) { value = this.helper[mapping["_map_fn"]](value); } if (value && mapping.properties) { if (value instanceof Array) { return this.parseArrayVals(mapping, value, modelpath); } else { return this.parseObjectVals(mapping, value, modelpath); } } else { return value; } } parseArrayVals(mapping, metadata, parentKey) { parentKey = parentKey + '[]'; let values = []; metadata.forEach((meta) => { let val = this.parseObjectVals(mapping, meta, parentKey); if (!app_repo_utility_1.App_Repository_Utility.isEmpty(val)) { values.push(val); } }); return values; } parseObjectVals(mapping, metadata, parentKey) { return Object.keys(mapping.properties).reduce((datum, name) => { let propMapping = mapping.properties[name]; let val = this.getModelValue(propMapping, metadata, parentKey); if (!app_repo_utility_1.App_Repository_Utility.isEmpty(val)) { datum[name] = val; } return datum; }, {}); } getKeyFromPath(modelpath, parentKey) { if (parentKey && modelpath.lastIndexOf(parentKey) !== -1 && modelpath.length > parentKey.length) { return modelpath.substring(parentKey.length + 1, modelpath.length); } else { return modelpath; } } } exports.ModelTransformer = ModelTransformer;