Skip to content

Commit

Permalink
Forward- and reverse-engineering
Browse files Browse the repository at this point in the history
  • Loading branch information
pdesmarets committed Mar 29, 2018
1 parent e27a935 commit 51d6efb
Show file tree
Hide file tree
Showing 19 changed files with 1,141 additions and 424 deletions.
1 change: 1 addition & 0 deletions forward_engineering/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
127 changes: 127 additions & 0 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const helper = require('../helper/helper.js');

module.exports = {
generateScript(data, logger, cb) {
const { jsonSchema, modelData, containerData, entityData, isUpdateScript } = data;
let result = "";
let mappingScript = {
mappings: {
[entityData.collectionName.toLowerCase()]: {
properties: this.getMappingScript(JSON.parse(jsonSchema))
}
}
};

if (isUpdateScript) {
result = this.getCurlScript(mappingScript, modelData, containerData);
} else {
result += this.getKibanaScript(mappingScript, containerData);
}

cb(null, result);
},

getCurlScript(mapping, modelData, indexData) {
const host = modelData.host || 'localhost';
const port = modelData.port || 9200;
const indexName = indexData.name || "";

return `curl -XPUT '${host}:${port}/${indexName.toLowerCase()}?pretty' -H 'Content-Type: application/json' -d '\n${JSON.stringify(mapping, null, 4)}\n'`;
},

getKibanaScript(mapping, indexData) {
const indexName = indexData.name || "";

return `PUT /${indexName.toLowerCase()}\n${JSON.stringify(mapping, null, 4)}`;
},

getMappingScript(jsonSchema) {
let schema = {};

if (!(jsonSchema.properties && jsonSchema.properties._source && jsonSchema.properties._source.properties)) {
return schema;
}

schema = this.getSchemaByItem(jsonSchema.properties._source.properties)

return schema;
},

getSchemaByItem(properties) {
let schema = {};

for (let fieldName in properties) {
let field = properties[fieldName];

schema[fieldName] = this.getField(field);
}

return schema;
},

getField(field) {
let schema = {};
const fieldProperties = helper.getFieldProperties(field.type, field, {});
let type = this.getFieldType(field);

if (type !== 'object' && type !== 'array') {
schema.type = type;
}

if (type === 'object') {
schema.properties = {};
}

this.setProperties(schema, fieldProperties);

if (type === 'geo_shape' || type === 'geo_point') {
return schema;
} else if (field.properties) {
schema.properties = this.getSchemaByItem(field.properties);
} else if (field.items) {
let arrData = field.items;

if (Array.isArray(field.items)) {
arrData = field.items[0];
}

schema = Object.assign(schema, this.getField(arrData));
}

return schema;
},

getFieldType(field) {
switch(field.type) {
case 'geo-shape':
return 'geo_shape';
case 'geo-point':
return 'geo_point';
case 'number':
return field.mode || 'long';
case 'string':
return field.mode || 'text';
case 'range':
return field.mode || 'integer_range';
case 'null':
return 'long';
default:
return field.type;
}
},

setProperties(schema, properties) {
for (let propName in properties) {
if (propName === 'stringfields') {
try {
schema['fields'] = JSON.parse(properties[propName]);
} catch (e) {
}
} else {
schema[propName] = properties[propName];
}
}

return schema;
}
};
6 changes: 6 additions & 0 deletions forward_engineering/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extension": "txt",
"filterName": "Plain text",
"namePrefix": "Elasticsearch Mapping",
"hasUpdateScript": true
}
8 changes: 8 additions & 0 deletions forward_engineering/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "elasticsearch",
"version": "1.0.0",
"description": "",
"author": "Hackolade",
"dependencies": {
}
}
37 changes: 37 additions & 0 deletions helper/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require('fs');
const path = require('path');
const fieldLevelConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '../properties_pane/field_level/fieldLevelConfig.json')).toString().replace(/\/\*[.\s\S]*\*\//ig, ""));

module.exports = {
getTargetFieldLevelPropertyNames(type, data) {
if (!fieldLevelConfig.structure[type]) {
return [];
}

return fieldLevelConfig.structure[type].filter(property => {
if (typeof property === 'object' && property.isTargetProperty) {
if (property.dependency) {
return (data[property.dependency.key] == property.dependency.value);
} else {
return true;
}
}

return false;
}).map(property => property.propertyKeyword);
},

getFieldProperties(type, data, pseudonyms) {
const propertyNames = this.getTargetFieldLevelPropertyNames(type, data);

return propertyNames.reduce((result, propertyName) => {
if (Object.prototype.hasOwnProperty.call(data, propertyName)) {
result[propertyName] = data[propertyName];
} else if (Object.prototype.hasOwnProperty.call(data, pseudonyms[propertyName])) {
result[pseudonyms[propertyName]] = data[pseudonyms[propertyName]];
}

return result;
}, {});
}
};
15 changes: 10 additions & 5 deletions localization/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"MAIN_MENU___INSERT_FIELD": "Insert Field",
"MAIN_MENU___APPEND_FIELD": "Append Field",
"MAIN_MENU___REVERSE_DB_COLLECTIONS": "Elasticsearch indices...",
"MAIN_MENU___FORWARD_DB_COLLECTIONS": "Elasticsearch Mapping",
"TOOLBAR___ADD_BUCKET": "Add index",
"TOOLBAR___ADD_COLLECTION": "Add type",
"TOOLBAR___ADD_VIEW": "Add Filtered Alias",
Expand Down Expand Up @@ -84,6 +85,7 @@
"MODAL_WINDOW___CONTAIN_BUCKET": "indices",
"MODAL_WINDOW___CONTAIN_COLLECTION": "type",
"MODAL_WINDOW___DB_CONNECTION_PROCESS": "Elasticsearch Reverse-Engineering Process",
"MODAL_WINDOW___DB_CONNECTIONS_LIST_TITLE": "Elasticsearch Connections",
"PROGRESS_BAR___DATABASE": "Index",
"PROGRESS_BAR___COLLECTION": "Type",
"PROGRESS_BAR___PROCESS": "Process",
Expand Down Expand Up @@ -132,10 +134,13 @@
"COLLECTION_SCHEMA_DEFINITION_TYPE": "document",
"MONGODB_SCRIPT_WARNING_MESSAGE": "This view is not associated to a type (viewOn property).",
"TYPE": {},
"CENTRAL_PANE___TAB_MODEL_DEFINITIONS": "User-Defined Types",
"CONTEXT_MENU___ADD_MODEL_REFERENCE": "User-Defined Type",
"CONTEXT_MENU___GO_TO_DEFINITION": "Go to User-Defined Type",
"DOCUMENTATION___DB_DEFINITIONS": "User-Defined Types",
"CENTRAL_PANE___TAB_MODEL_DEFINITIONS": "Model Definitions",
"CONTEXT_MENU___ADD_MODEL_REFERENCE": "Model Definition",
"CONTEXT_MENU___GO_TO_DEFINITION": "Go to Model Definition",
"DOCUMENTATION___DB_DEFINITIONS": "Model Definitions",
"CONTEXT_MENU___CONVERT_TO_PATTERN_FIELD": "Convert to Pattern Field",
"CONTEXT_MENU___CONVERT_PATTERN_TO_REGULAR_FIELD": "Convert to Regular Field"
"CONTEXT_MENU___CONVERT_PATTERN_TO_REGULAR_FIELD": "Convert to Regular Field",
"CENTRAL_PANE___FE_SCRIPT": "Elasticsearch Mapping",
"MODAL_WINDOW___FE_SCRIPT_OPTION_UPDATE": "CURL Script",
"MODAL_WINDOW___FE_SCRIPT_OPTION_CREATE": "Kibana Script"
}
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "Elasticsearch",
"version": "0.1.4",
"versionDate": "2018-02-25",
"version": "0.1.5",
"versionDate": "2018-03-29",
"author": "hackolade",
"engines": {
"hackolade": "1.9.x",
"hackolade": "1.12.7",
"hackoladePlugin": "1.0.0"
},
"contributes": {
Expand All @@ -21,12 +21,15 @@
"5.4.x",
"5.5.x",
"5.6.x",
"6.0.x"
"6.0.x",
"6.1.x",
"6.2.x"
]
},
"features": {
"nestedCollections": true,
"disableMultipleTypes": true
"disableMultipleTypes": true,
"enableReverseEngineering": true,
"enableForwardEngineering": true
}
},
"description": "Hackolade plugin for Elasticsearch"
Expand Down
Loading

0 comments on commit 51d6efb

Please sign in to comment.