Skip to content

Commit 1dd8ee9

Browse files
committed
Added properties to PP, FE & RE
1 parent 51d6efb commit 1dd8ee9

File tree

9 files changed

+264
-18
lines changed

9 files changed

+264
-18
lines changed

forward_engineering/api.js

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ module.exports = {
44
generateScript(data, logger, cb) {
55
const { jsonSchema, modelData, containerData, entityData, isUpdateScript } = data;
66
let result = "";
7-
let mappingScript = {
8-
mappings: {
9-
[entityData.collectionName.toLowerCase()]: {
10-
properties: this.getMappingScript(JSON.parse(jsonSchema))
11-
}
12-
}
13-
};
7+
let fieldsSchema = this.getFieldsSchema(JSON.parse(jsonSchema));
8+
let typeSchema = this.getTypeSchema(entityData, fieldsSchema);
9+
let mappingScript = this.getMappingScript(containerData, typeSchema);
1410

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

38-
getMappingScript(jsonSchema) {
34+
getFieldsSchema(jsonSchema) {
3935
let schema = {};
4036

4137
if (!(jsonSchema.properties && jsonSchema.properties._source && jsonSchema.properties._source.properties)) {
@@ -123,5 +119,89 @@ module.exports = {
123119
}
124120

125121
return schema;
122+
},
123+
124+
getTypeSchema(typeData, fieldsSchema) {
125+
let script = {};
126+
127+
if (typeData.dynamic) {
128+
script.dynamic = typeData.dynamic;
129+
}
130+
131+
script.properties = fieldsSchema;
132+
133+
return {
134+
[(typeData.collectionName || "").toLowerCase()]: script
135+
};
136+
},
137+
138+
getMappingScript(indexData, typeSchema) {
139+
let mappingScript = {};
140+
let settings = this.getSettings(indexData);
141+
let aliases = this.getAliases(indexData);
142+
143+
if (settings) {
144+
mappingScript.settings = settings;
145+
}
146+
147+
if (aliases) {
148+
mappingScript.aliases = aliases;
149+
}
150+
151+
mappingScript.mappings = typeSchema;
152+
153+
return mappingScript;
154+
},
155+
156+
getSettings(indexData) {
157+
let settings;
158+
let properties = helper.getContainerLevelProperties();
159+
160+
properties.forEach(propertyName => {
161+
if (indexData[propertyName]) {
162+
if (!settings) {
163+
settings = {};
164+
}
165+
166+
settings[propertyName] = indexData[propertyName];
167+
}
168+
});
169+
170+
return settings;
171+
},
172+
173+
getAliases(indexData) {
174+
let aliases;
175+
176+
if (!indexData.aliases) {
177+
return aliases;
178+
}
179+
180+
indexData.aliases.forEach((alias) => {
181+
if (alias.name) {
182+
if (!aliases) {
183+
aliases = {};
184+
}
185+
186+
aliases[alias.name] = {};
187+
188+
if (alias.filter) {
189+
let filterData = "";
190+
try {
191+
filterData = JSON.parse(alias.filter);
192+
} catch (e) {}
193+
194+
aliases[alias.name].filter = {
195+
term: filterData
196+
};
197+
}
198+
199+
if (alias.routing) {
200+
aliases[alias.name].routing = alias.routing;
201+
}
202+
}
203+
});
204+
205+
return aliases;
126206
}
127207
};

helper/helper.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const fs = require('fs');
22
const path = require('path');
3-
const fieldLevelConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '../properties_pane/field_level/fieldLevelConfig.json')).toString().replace(/\/\*[.\s\S]*\*\//ig, ""));
3+
const readConfig = (pathToConfig) => {
4+
return JSON.parse(fs.readFileSync(path.join(__dirname, pathToConfig)).toString().replace(/\/\*[.\s\S]*\*\//ig, ""));
5+
};
6+
const fieldLevelConfig = readConfig('../properties_pane/field_level/fieldLevelConfig.json');
7+
const containerLevelConfig = readConfig('../properties_pane/container_level/containerLevelConfig.json');
48

59
module.exports = {
610
getTargetFieldLevelPropertyNames(type, data) {
@@ -33,5 +37,19 @@ module.exports = {
3337

3438
return result;
3539
}, {});
40+
},
41+
42+
getContainerLevelProperties() {
43+
let properties = [];
44+
45+
containerLevelConfig.forEach((tab) => {
46+
tab.structure.forEach(property => {
47+
if (property.isTargetProperty) {
48+
properties.push(property.propertyKeyword);
49+
}
50+
});
51+
});
52+
53+
return properties;
3654
}
3755
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Elasticsearch",
3-
"version": "0.1.5",
4-
"versionDate": "2018-03-29",
3+
"version": "0.1.6",
4+
"versionDate": "2018-04-11",
55
"author": "hackolade",
66
"engines": {
77
"hackolade": "1.12.7",

properties_pane/container_level/containerLevelConfig.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,55 @@ making sure that you maintain a proper JSON format.
138138
"underERDDivider": true
139139
}],
140140
"structure": [
141+
{
142+
"propertyName": "Number of shards",
143+
"propertyKeyword": "number_of_shards",
144+
"propertyValidate": false,
145+
"propertyType": "numeric",
146+
"valueType": "integer",
147+
"allowNegative": false,
148+
"isTargetProperty": true
149+
},
150+
{
151+
"propertyName": "Number of replicas",
152+
"propertyKeyword": "number_of_replicas",
153+
"propertyValidate": false,
154+
"propertyType": "numeric",
155+
"valueType": "integer",
156+
"allowNegative": false,
157+
"isTargetProperty": true
158+
},
159+
{
160+
"propertyName": "Aliases",
161+
"propertyType": "group",
162+
"propertyKeyword": "aliases",
163+
"shouldValidate": false,
164+
"propertyTooltip": "The index aliases API allows aliasing an index with a name",
165+
"structure": [
166+
{
167+
"propertyName": "Name",
168+
"propertyKeyword": "name",
169+
"shouldValidate": false,
170+
"propertyTooltip": "",
171+
"propertyType": "text"
172+
},
173+
{
174+
"propertyName": "Filter",
175+
"propertyKeyword": "filter",
176+
"propertyValidate": false,
177+
"propertyType": "details",
178+
"propertyTooltip": "",
179+
"template": "textarea"
180+
},
181+
{
182+
"propertyName": "Routing",
183+
"propertyKeyword": "routing",
184+
"shouldValidate": false,
185+
"propertyTooltip": "",
186+
"propertyType": "text"
187+
}
188+
]
189+
},
141190
{
142191
"propertyName": "Description",
143192
"propertyKeyword": "description",

properties_pane/defaultData.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
},
88
"container": {
99
"name": "New index",
10-
"indexes": []
10+
"indexes": [],
11+
"number_of_shards": 5,
12+
"number_of_replicas": 1
1113
},
1214
"collection": {
1315
"collectionName": "New type",
1416
"collectionUsers": [],
15-
"collation": {}
17+
"collation": {},
18+
"dynamic": "true"
1619
},
1720
"field": {
1821
"name": "New field"

properties_pane/entity_level/entityLevelConfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ making sure that you maintain a proper JSON format.
8484
{
8585
"lowerTab": "Details",
8686
"structure": [
87+
{
88+
"propertyName": "Dynamic",
89+
"propertyKeyword": "dynamic",
90+
"shouldValidate": false,
91+
"propertyTooltip": "dynamic setting value",
92+
"propertyType": "select",
93+
"options": [
94+
"true",
95+
"false",
96+
"strict"
97+
]
98+
},
8799
{
88100
"propertyName": "Comments",
89101
"propertyKeyword": "comments",

properties_pane/field_level/fieldLevelConfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,7 @@ making sure that you maintain a proper JSON format.
537537
"long_range",
538538
"double_range",
539539
"date_range"
540-
],
541-
"isTargetProperty": true
540+
]
542541
},
543542
{
544543
"propertyName": "coerce",

reverse_engineering/SchemaCreator.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ module.exports = {
5252
});
5353
},
5454

55+
getSettings(client) {
56+
return client.indices.getSettings({
57+
index: this.indices
58+
});
59+
},
60+
61+
getAliases(client) {
62+
return client.indices.getAlias({
63+
index: this.indices
64+
});
65+
},
66+
5567
getSchemaTemplate() {
5668
return {
5769
$schema: "http://json-schema.org/draft-04/schema#",
@@ -68,6 +80,10 @@ module.exports = {
6880
schema.properties = this.getServiceFields(sample);
6981
schema.properties._source.properties = this.getFields(elasticMapping.properties, sample._source);
7082

83+
if (elasticMapping.dynamic) {
84+
schema.dynamic = elasticMapping.dynamic;
85+
}
86+
7187
return schema;
7288
},
7389

reverse_engineering/api.js

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ module.exports = {
148148
const indices = data.collectionData.dataBaseNames;
149149
const types = data.collectionData.collections;
150150

151-
const bucketInfo = {
151+
const defaultBucketInfo = {
152152
indexName: '_index',
153153
indexType: 'string',
154154
docTypeName: '_type',
@@ -200,6 +200,7 @@ module.exports = {
200200

201201
(client, modelInfo, jsonSchemas, next) => {
202202
async.map(indices, (indexName, nextIndex) => {
203+
let bucketInfo = Object.assign(getBucketData(jsonSchemas[indexName]), defaultBucketInfo);
203204
if (!types[indexName]) {
204205
if (includeEmptyCollection) {
205206
nextIndex(null, [{
@@ -381,6 +382,8 @@ function getInfoSocket() {
381382
}
382383

383384
function getSchemaMapping(indices, client) {
385+
let result = {};
386+
384387
SchemaCreator.init();
385388
for (let indexName in indices) {
386389
SchemaCreator.addIndex(indexName);
@@ -389,5 +392,71 @@ function getSchemaMapping(indices, client) {
389392
}
390393
}
391394

392-
return SchemaCreator.getMapping(client);
393-
}
395+
return SchemaCreator.getMapping(client).then(schemas => {
396+
result.jsonSchemas = schemas;
397+
398+
return SchemaCreator.getSettings(client);
399+
}).then(settings => {
400+
result.settings = settings;
401+
402+
return SchemaCreator.getAliases(client);
403+
}).then(aliases => {
404+
result.aliases = aliases;
405+
406+
return result;
407+
}).then(res => {
408+
let data = {};
409+
410+
for (let indexName in res.jsonSchemas) {
411+
data[indexName] = res.jsonSchemas[indexName];
412+
data[indexName].settings = res.settings[indexName].settings;
413+
data[indexName].aliases = res.aliases[indexName].aliases;
414+
}
415+
416+
return data;
417+
});
418+
}
419+
420+
function getBucketData(mappingData) {
421+
let data = {};
422+
423+
if (mappingData.settings) {
424+
let settingContainer = mappingData.settings;
425+
426+
if (mappingData.settings.index) {
427+
settingContainer = mappingData.settings.index;
428+
}
429+
430+
if (settingContainer.number_of_shards) {
431+
data.number_of_shards = settingContainer.number_of_shards;
432+
}
433+
434+
if (settingContainer.number_of_replicas) {
435+
data.number_of_replicas = settingContainer.number_of_replicas;
436+
}
437+
}
438+
439+
if (mappingData.aliases) {
440+
let aliases = [];
441+
442+
for (let aliasName in mappingData.aliases) {
443+
let alias = {
444+
name: aliasName
445+
};
446+
447+
if (mappingData.aliases[aliasName].filter) {
448+
alias.filter = JSON.stringify(mappingData.aliases[aliasName].filter.term, null, 4);
449+
}
450+
451+
if (mappingData.aliases[aliasName].index_routing) {
452+
alias.routing = mappingData.aliases[aliasName].index_routing;
453+
}
454+
455+
aliases.push(alias);
456+
}
457+
458+
data.aliases = aliases;
459+
}
460+
461+
return data;
462+
}

0 commit comments

Comments
 (0)