Skip to content

Commit

Permalink
Added properties to PP, FE & RE
Browse files Browse the repository at this point in the history
  • Loading branch information
pdesmarets committed Apr 13, 2018
1 parent 51d6efb commit 1dd8ee9
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 18 deletions.
96 changes: 88 additions & 8 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ 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))
}
}
};
let fieldsSchema = this.getFieldsSchema(JSON.parse(jsonSchema));
let typeSchema = this.getTypeSchema(entityData, fieldsSchema);
let mappingScript = this.getMappingScript(containerData, typeSchema);

if (isUpdateScript) {
result = this.getCurlScript(mappingScript, modelData, containerData);
Expand All @@ -35,7 +31,7 @@ module.exports = {
return `PUT /${indexName.toLowerCase()}\n${JSON.stringify(mapping, null, 4)}`;
},

getMappingScript(jsonSchema) {
getFieldsSchema(jsonSchema) {
let schema = {};

if (!(jsonSchema.properties && jsonSchema.properties._source && jsonSchema.properties._source.properties)) {
Expand Down Expand Up @@ -123,5 +119,89 @@ module.exports = {
}

return schema;
},

getTypeSchema(typeData, fieldsSchema) {
let script = {};

if (typeData.dynamic) {
script.dynamic = typeData.dynamic;
}

script.properties = fieldsSchema;

return {
[(typeData.collectionName || "").toLowerCase()]: script
};
},

getMappingScript(indexData, typeSchema) {
let mappingScript = {};
let settings = this.getSettings(indexData);
let aliases = this.getAliases(indexData);

if (settings) {
mappingScript.settings = settings;
}

if (aliases) {
mappingScript.aliases = aliases;
}

mappingScript.mappings = typeSchema;

return mappingScript;
},

getSettings(indexData) {
let settings;
let properties = helper.getContainerLevelProperties();

properties.forEach(propertyName => {
if (indexData[propertyName]) {
if (!settings) {
settings = {};
}

settings[propertyName] = indexData[propertyName];
}
});

return settings;
},

getAliases(indexData) {
let aliases;

if (!indexData.aliases) {
return aliases;
}

indexData.aliases.forEach((alias) => {
if (alias.name) {
if (!aliases) {
aliases = {};
}

aliases[alias.name] = {};

if (alias.filter) {
let filterData = "";
try {
filterData = JSON.parse(alias.filter);
} catch (e) {}

aliases[alias.name].filter = {
term: filterData
};
}

if (alias.routing) {
aliases[alias.name].routing = alias.routing;
}
}
});

return aliases;
}
};
20 changes: 19 additions & 1 deletion helper/helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
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, ""));
const readConfig = (pathToConfig) => {
return JSON.parse(fs.readFileSync(path.join(__dirname, pathToConfig)).toString().replace(/\/\*[.\s\S]*\*\//ig, ""));
};
const fieldLevelConfig = readConfig('../properties_pane/field_level/fieldLevelConfig.json');
const containerLevelConfig = readConfig('../properties_pane/container_level/containerLevelConfig.json');

module.exports = {
getTargetFieldLevelPropertyNames(type, data) {
Expand Down Expand Up @@ -33,5 +37,19 @@ module.exports = {

return result;
}, {});
},

getContainerLevelProperties() {
let properties = [];

containerLevelConfig.forEach((tab) => {
tab.structure.forEach(property => {
if (property.isTargetProperty) {
properties.push(property.propertyKeyword);
}
});
});

return properties;
}
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Elasticsearch",
"version": "0.1.5",
"versionDate": "2018-03-29",
"version": "0.1.6",
"versionDate": "2018-04-11",
"author": "hackolade",
"engines": {
"hackolade": "1.12.7",
Expand Down
49 changes: 49 additions & 0 deletions properties_pane/container_level/containerLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,55 @@ making sure that you maintain a proper JSON format.
"underERDDivider": true
}],
"structure": [
{
"propertyName": "Number of shards",
"propertyKeyword": "number_of_shards",
"propertyValidate": false,
"propertyType": "numeric",
"valueType": "integer",
"allowNegative": false,
"isTargetProperty": true
},
{
"propertyName": "Number of replicas",
"propertyKeyword": "number_of_replicas",
"propertyValidate": false,
"propertyType": "numeric",
"valueType": "integer",
"allowNegative": false,
"isTargetProperty": true
},
{
"propertyName": "Aliases",
"propertyType": "group",
"propertyKeyword": "aliases",
"shouldValidate": false,
"propertyTooltip": "The index aliases API allows aliasing an index with a name",
"structure": [
{
"propertyName": "Name",
"propertyKeyword": "name",
"shouldValidate": false,
"propertyTooltip": "",
"propertyType": "text"
},
{
"propertyName": "Filter",
"propertyKeyword": "filter",
"propertyValidate": false,
"propertyType": "details",
"propertyTooltip": "",
"template": "textarea"
},
{
"propertyName": "Routing",
"propertyKeyword": "routing",
"shouldValidate": false,
"propertyTooltip": "",
"propertyType": "text"
}
]
},
{
"propertyName": "Description",
"propertyKeyword": "description",
Expand Down
7 changes: 5 additions & 2 deletions properties_pane/defaultData.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
},
"container": {
"name": "New index",
"indexes": []
"indexes": [],
"number_of_shards": 5,
"number_of_replicas": 1
},
"collection": {
"collectionName": "New type",
"collectionUsers": [],
"collation": {}
"collation": {},
"dynamic": "true"
},
"field": {
"name": "New field"
Expand Down
12 changes: 12 additions & 0 deletions properties_pane/entity_level/entityLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ making sure that you maintain a proper JSON format.
{
"lowerTab": "Details",
"structure": [
{
"propertyName": "Dynamic",
"propertyKeyword": "dynamic",
"shouldValidate": false,
"propertyTooltip": "dynamic setting value",
"propertyType": "select",
"options": [
"true",
"false",
"strict"
]
},
{
"propertyName": "Comments",
"propertyKeyword": "comments",
Expand Down
3 changes: 1 addition & 2 deletions properties_pane/field_level/fieldLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,7 @@ making sure that you maintain a proper JSON format.
"long_range",
"double_range",
"date_range"
],
"isTargetProperty": true
]
},
{
"propertyName": "coerce",
Expand Down
16 changes: 16 additions & 0 deletions reverse_engineering/SchemaCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ module.exports = {
});
},

getSettings(client) {
return client.indices.getSettings({
index: this.indices
});
},

getAliases(client) {
return client.indices.getAlias({
index: this.indices
});
},

getSchemaTemplate() {
return {
$schema: "http://json-schema.org/draft-04/schema#",
Expand All @@ -68,6 +80,10 @@ module.exports = {
schema.properties = this.getServiceFields(sample);
schema.properties._source.properties = this.getFields(elasticMapping.properties, sample._source);

if (elasticMapping.dynamic) {
schema.dynamic = elasticMapping.dynamic;
}

return schema;
},

Expand Down
75 changes: 72 additions & 3 deletions reverse_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ module.exports = {
const indices = data.collectionData.dataBaseNames;
const types = data.collectionData.collections;

const bucketInfo = {
const defaultBucketInfo = {
indexName: '_index',
indexType: 'string',
docTypeName: '_type',
Expand Down Expand Up @@ -200,6 +200,7 @@ module.exports = {

(client, modelInfo, jsonSchemas, next) => {
async.map(indices, (indexName, nextIndex) => {
let bucketInfo = Object.assign(getBucketData(jsonSchemas[indexName]), defaultBucketInfo);
if (!types[indexName]) {
if (includeEmptyCollection) {
nextIndex(null, [{
Expand Down Expand Up @@ -381,6 +382,8 @@ function getInfoSocket() {
}

function getSchemaMapping(indices, client) {
let result = {};

SchemaCreator.init();
for (let indexName in indices) {
SchemaCreator.addIndex(indexName);
Expand All @@ -389,5 +392,71 @@ function getSchemaMapping(indices, client) {
}
}

return SchemaCreator.getMapping(client);
}
return SchemaCreator.getMapping(client).then(schemas => {
result.jsonSchemas = schemas;

return SchemaCreator.getSettings(client);
}).then(settings => {
result.settings = settings;

return SchemaCreator.getAliases(client);
}).then(aliases => {
result.aliases = aliases;

return result;
}).then(res => {
let data = {};

for (let indexName in res.jsonSchemas) {
data[indexName] = res.jsonSchemas[indexName];
data[indexName].settings = res.settings[indexName].settings;
data[indexName].aliases = res.aliases[indexName].aliases;
}

return data;
});
}

function getBucketData(mappingData) {
let data = {};

if (mappingData.settings) {
let settingContainer = mappingData.settings;

if (mappingData.settings.index) {
settingContainer = mappingData.settings.index;
}

if (settingContainer.number_of_shards) {
data.number_of_shards = settingContainer.number_of_shards;
}

if (settingContainer.number_of_replicas) {
data.number_of_replicas = settingContainer.number_of_replicas;
}
}

if (mappingData.aliases) {
let aliases = [];

for (let aliasName in mappingData.aliases) {
let alias = {
name: aliasName
};

if (mappingData.aliases[aliasName].filter) {
alias.filter = JSON.stringify(mappingData.aliases[aliasName].filter.term, null, 4);
}

if (mappingData.aliases[aliasName].index_routing) {
alias.routing = mappingData.aliases[aliasName].index_routing;
}

aliases.push(alias);
}

data.aliases = aliases;
}

return data;
}

0 comments on commit 1dd8ee9

Please sign in to comment.