Skip to content

Commit

Permalink
Add specialized datatypes
Browse files Browse the repository at this point in the history
  • Loading branch information
pdesmarets committed May 7, 2019
1 parent c9b209b commit fe09546
Show file tree
Hide file tree
Showing 26 changed files with 680 additions and 38 deletions.
64 changes: 62 additions & 2 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ module.exports = {
const host = modelData.host || 'localhost';
const port = modelData.port || 9200;
const indexName = indexData.name || "";
const majorVersion = +(modelData.dbVersion || '').split('.').shift();
const includeTypeName = majorVersion >= 7 ? '&include_type_name=true' : '';

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

getKibanaScript(mapping, indexData) {
Expand Down Expand Up @@ -80,7 +82,15 @@ module.exports = {

this.setProperties(schema, fieldProperties, data);

if (type === 'geo_shape' || type === 'geo_point') {
if (type === 'alias') {
return Object.assign({}, schema, this.getAliasSchema(field, data));
} else if (type === 'join') {
return Object.assign({}, schema, this.getJoinSchema(field));
} else if (
[
'completion', 'sparse_vector', 'dense_vector', 'geo_shape', 'geo_point', 'rank_feature', 'rank_features'
].includes(type)
) {
return schema;
} else if (field.properties) {
schema.properties = this.getSchemaByItem(field.properties, data);
Expand Down Expand Up @@ -242,5 +252,55 @@ module.exports = {
}

return false;
},

getJoinSchema(field) {
if (!Array.isArray(field.relations)) {
return {};
}

const relations = field.relations.reduce((result, item) => {
if (!item.parent) {
return result;
}

if (!Array.isArray(item.children)) {
return result;
}

if (item.children.length === 1) {
return Object.assign({}, result, {
[item.parent]: (item.children[0] || {}).name
});
}

return Object.assign({}, result, {
[item.parent]: item.children.map(item => item.name || "")
});
}, {});

return { relations };
},

getAliasSchema(field, data) {
if (!Array.isArray(field.path)) {
return {};
}

if (field.path.length === 0) {
return {};
}

const pathName = schemaHelper.getPathName(
field.path[0].keyId,
[
data.jsonSchema,
data.internalDefinitions,
data.modelDefinitions,
data.externalDefinitions
]
);

return { path: pathName };
}
};
2 changes: 1 addition & 1 deletion helper/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const containerLevelConfig = readConfig('../properties_pane/container_level/cont

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

Expand Down
69 changes: 55 additions & 14 deletions helper/schemaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ const getPathById = (schema, id, path) => {
}
};

const getNameByPath = (schema, path, parentName) => {
const getNameByPath = (schema, path) => {
if (schema.properties) {
return Object.keys(schema.properties).reduce((foundedName, propertyName) => {
if (foundedName !== "") {
if (foundedName.length) {
return foundedName;
}

Expand All @@ -42,14 +42,16 @@ const getNameByPath = (schema, path, parentName) => {
}

if (path.length === 1) {
return propertyName;
return [ propertyName ];
}

return getNameByPath(property, path.slice(1), propertyName);
}, "");
return [
propertyName, ...getNameByPath(property, path.slice(1))
];
}, []);
} else if (Array.isArray(schema.items)) {
return schema.items.reduce((foundedName, property, i) => {
if (foundedName !== "") {
if (foundedName.length) {
return foundedName;
}

Expand All @@ -58,36 +60,74 @@ const getNameByPath = (schema, path, parentName) => {
}

if (path.length === 1) {
return parentName + '[' + i + ']';
return [ '[' + i + ']' ];
}

return getNameByPath(property, path.slice(1), parentName + '[' + i + ']');
}, "");
return [
'[' + i + ']',
...getNameByPath(property, path.slice(1), '[' + i + ']')
];
}, []);
} else if (Object(schema.items) === schema.items) {
const property = schema.items;

if (property.GUID !== path[0]) {
return "";
return [ "" ];
}

if (path.length === 1) {
return parentName + '[0]';
return ['[0]'];
}

return getNameByPath(property, path.slice(1), parentName + '[0]');
return [
'[0]',
...getNameByPath(property, path.slice(1), '[0]')
];
}
};

const joinIndex = (items) => {
return items.reduce((result, item) => {
if (/\[\d+\]/.test(item)) {
return [
...result.slice(0, -1),
result[result.length - 1] + item
];
} else {
return [
...result,
item
];
}
}, []);
};

const findFieldNameById = (id, source) => {
let path = getPathById(source, id, []);

if (path) {
return getNameByPath(source, path, "");
const name = joinIndex(getNameByPath(source, path, ""));

return name[name.length - 1] || "";
} else {
return "";
}
};

const getPathName = (id, sources) => {
for (let i = 0; i < sources.length; i++) {
let path = getPathById(sources[i], id, []);

if (path) {
const name = getNameByPath(sources[i], path, "");

return name.slice(1).filter(item => !/\[\d+\]/.test(item)).join('.');
}
}

return "";
};

const getNamesByIds = (ids, sources) => {
return ids.reduce((names, id) => {
for (let i = 0; i < sources.length; i++) {
Expand All @@ -103,5 +143,6 @@ const getNamesByIds = (ids, sources) => {
};

module.exports = {
getNamesByIds
getNamesByIds,
getPathName
};
2 changes: 1 addition & 1 deletion jsonSchemaProperties.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"unneededFieldProps": ["collectionName", "name", "users", "indexes", "collectionUsers", "additionalPropertieserties", "subTypes"],
"unneededFieldProps": ["collectionName", "name", "users", "indexes", "collectionUsers", "additionalProperties", "subTypes"],
"removeIfPropsNegative": ["partitionKey", "sortKey"]
}
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.19",
"versionDate": "2019-05-03",
"version": "0.1.20",
"versionDate": "2019-05-07",
"author": "hackolade",
"engines": {
"hackolade": "1.12.7",
Expand Down
Loading

0 comments on commit fe09546

Please sign in to comment.