-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: replaced aws-sdk with a dedicated `@aws-sdk/client-neptune` and our `hckFetch` request handler * chore: prettier ignore package-lock.json * chore: removed unused method
- Loading branch information
1 parent
176fd12
commit 13613da
Showing
13 changed files
with
1,650 additions
and
462 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
release | ||
node_modules | ||
package.json | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,149 +1,148 @@ | ||
const { isEmpty, isString, get } = require('lodash'); | ||
|
||
const DEFAULT_INDENT = ' '; | ||
let graphName = 'g'; | ||
const graphName = 'g'; | ||
|
||
module.exports = _ => { | ||
const generateVertex = (collection, vertexData) => { | ||
const vertexName = transformToValidGremlinName(collection.collectionName); | ||
const propertiesScript = addPropertiesScript(collection, vertexData); | ||
const transformToValidGremlinName = name => { | ||
const DEFAULT_NAME = 'New_vertex'; | ||
const DEFAULT_PREFIX = 'v_'; | ||
|
||
return `${graphName}.addV(${JSON.stringify(vertexName)})${propertiesScript}`; | ||
}; | ||
if (!name || !isString(name)) { | ||
return DEFAULT_NAME; | ||
} | ||
|
||
const generateVertices = (collections, jsonData) => { | ||
const vertices = collections.map(collection => { | ||
const vertexData = JSON.parse(jsonData[collection.GUID]); | ||
const nameWithoutSpecialCharacters = name.replace(/[\s`~!@#%^&*()_|+\-=?;:'",.<>{}[\]\\/]/gi, '_'); | ||
const startsFromDigit = nameWithoutSpecialCharacters.match(/^\d.*$/); | ||
|
||
return generateVertex(collection, vertexData); | ||
}); | ||
if (startsFromDigit) { | ||
return `${DEFAULT_PREFIX}_${nameWithoutSpecialCharacters}`; | ||
} | ||
|
||
const script = vertices.join(';\n\n'); | ||
if (!script) { | ||
return ''; | ||
} | ||
return nameWithoutSpecialCharacters; | ||
}; | ||
|
||
return script + ';'; | ||
}; | ||
const generateVertex = (collection, vertexData) => { | ||
const vertexName = transformToValidGremlinName(collection.collectionName); | ||
const propertiesScript = addPropertiesScript(collection, vertexData); | ||
|
||
const generateEdge = (from, to, relationship, edgeData) => { | ||
const edgeName = transformToValidGremlinName(relationship.name); | ||
const propertiesScript = addPropertiesScript(relationship, edgeData, { cardinality: false }); | ||
return `${graphName}.addV(${JSON.stringify(vertexName)})${propertiesScript}`; | ||
}; | ||
|
||
const generateVertices = (collections, jsonData) => { | ||
const vertices = collections.map(collection => { | ||
const vertexData = JSON.parse(jsonData[collection.GUID]); | ||
|
||
return `${graphName}.addE(${JSON.stringify(edgeName)}).\n${DEFAULT_INDENT}from(${from}).\n${DEFAULT_INDENT}to(${to})${propertiesScript}`; | ||
}; | ||
return generateVertex(collection, vertexData); | ||
}); | ||
|
||
const getVertexVariableScript = vertexName => `${graphName}.V().hasLabel(${JSON.stringify(vertexName)})`; | ||
const script = vertices.join(';\n\n'); | ||
if (!script) { | ||
return ''; | ||
} | ||
|
||
const generateEdges = (collections, relationships, jsonData) => { | ||
const edges = relationships.reduce((edges, relationship) => { | ||
const parentCollection = collections.find(collection => collection.GUID === relationship.parentCollection); | ||
const childCollection = collections.find(collection => collection.GUID === relationship.childCollection); | ||
if (!parentCollection || !childCollection) { | ||
return edges; | ||
} | ||
const from = transformToValidGremlinName(parentCollection.collectionName); | ||
const to = transformToValidGremlinName(childCollection.collectionName); | ||
const edgeData = JSON.parse(jsonData[relationship.GUID]); | ||
return script + ';'; | ||
}; | ||
|
||
return edges.concat( | ||
generateEdge(getVertexVariableScript(from), getVertexVariableScript(to), relationship, edgeData), | ||
); | ||
}, []); | ||
const generateEdge = (from, to, relationship, edgeData) => { | ||
const edgeName = transformToValidGremlinName(relationship.name); | ||
const propertiesScript = addPropertiesScript(relationship, edgeData, { cardinality: false }); | ||
|
||
if (_.isEmpty(edges)) { | ||
return ''; | ||
return `${graphName}.addE(${JSON.stringify(edgeName)}).\n${DEFAULT_INDENT}from(${from}).\n${DEFAULT_INDENT}to(${to})${propertiesScript}`; | ||
}; | ||
|
||
const getVertexVariableScript = vertexName => `${graphName}.V().hasLabel(${JSON.stringify(vertexName)})`; | ||
|
||
const generateEdges = (collections, relationships, jsonData) => { | ||
const edges = relationships.reduce((edges, relationship) => { | ||
const parentCollection = collections.find(collection => collection.GUID === relationship.parentCollection); | ||
const childCollection = collections.find(collection => collection.GUID === relationship.childCollection); | ||
if (!parentCollection || !childCollection) { | ||
return edges; | ||
} | ||
const from = transformToValidGremlinName(parentCollection.collectionName); | ||
const to = transformToValidGremlinName(childCollection.collectionName); | ||
const edgeData = JSON.parse(jsonData[relationship.GUID]); | ||
|
||
return edges.join(';\n\n') + ';'; | ||
}; | ||
return edges.concat( | ||
generateEdge(getVertexVariableScript(from), getVertexVariableScript(to), relationship, edgeData), | ||
); | ||
}, []); | ||
|
||
const addPropertiesScript = (collection, vertexData, features = { cardinality: true }) => { | ||
const properties = _.get(collection, 'properties', {}); | ||
if (isEmpty(edges)) { | ||
return ''; | ||
} | ||
|
||
return Object.keys(properties).reduce((script, name) => { | ||
const property = properties[name]; | ||
return edges.join(';\n\n') + ';'; | ||
}; | ||
|
||
return script + createProperty(property, name, vertexData[name], features); | ||
}, ''); | ||
}; | ||
const addPropertiesScript = (collection, vertexData, features = { cardinality: true }) => { | ||
const properties = get(collection, 'properties', {}); | ||
|
||
const createProperty = (property, name, vertexData, features) => { | ||
const type = property.type; | ||
return Object.keys(properties).reduce((script, name) => { | ||
const property = properties[name]; | ||
|
||
if (type === 'multi-property') { | ||
return convertSet(property, name, vertexData, features); | ||
} | ||
return script + createProperty(property, name, vertexData[name], features); | ||
}, ''); | ||
}; | ||
|
||
const valueScript = convertPropertyValue(property, 2, type, vertexData); | ||
const cardinality = features.cardinality ? `${property.propCardinality || 'single'}, ` : ''; | ||
|
||
return `.\n${DEFAULT_INDENT}property(${cardinality}${JSON.stringify(name)}, ${valueScript})`; | ||
}; | ||
|
||
const convertSet = (property, name, vertexData, features) => { | ||
const items = Array.isArray(property.items) ? property.items : [property.items]; | ||
|
||
return items | ||
.map((item, i) => | ||
createProperty( | ||
{ ...item, propCardinality: 'set' }, | ||
name, | ||
Array.isArray(vertexData) ? vertexData[i] || '' : '', | ||
features, | ||
), | ||
) | ||
.join(''); | ||
}; | ||
|
||
const convertDate = value => `datetime("${value}")`; | ||
|
||
const convertNumber = (property, value) => { | ||
const mode = property.mode; | ||
const numberValue = JSON.stringify(value); | ||
|
||
switch (mode) { | ||
case 'double': | ||
return `${numberValue}d`; | ||
case 'float': | ||
return `${numberValue}f`; | ||
case 'long': | ||
return `${numberValue}l`; | ||
default: | ||
return numberValue; | ||
} | ||
}; | ||
|
||
const convertPropertyValue = (property, level, type, value) => { | ||
switch (type) { | ||
case 'date': | ||
return convertDate(value); | ||
case 'number': | ||
return convertNumber(property, value); | ||
} | ||
const createProperty = (property, name, vertexData, features) => { | ||
const type = property.type; | ||
|
||
return `${JSON.stringify(value === undefined ? '' : value)}`; | ||
}; | ||
if (type === 'multi-property') { | ||
return convertSet(property, name, vertexData, features); | ||
} | ||
|
||
const transformToValidGremlinName = name => { | ||
const DEFAULT_NAME = 'New_vertex'; | ||
const DEFAULT_PREFIX = 'v_'; | ||
const valueScript = convertPropertyValue(property, 2, type, vertexData); | ||
const cardinality = features.cardinality ? `${property.propCardinality || 'single'}, ` : ''; | ||
|
||
if (!name || !_.isString(name)) { | ||
return DEFAULT_NAME; | ||
} | ||
return `.\n${DEFAULT_INDENT}property(${cardinality}${JSON.stringify(name)}, ${valueScript})`; | ||
}; | ||
|
||
const nameWithoutSpecialCharacters = name.replace(/[\s`~!@#%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '_'); | ||
const startsFromDigit = nameWithoutSpecialCharacters.match(/^[0-9].*$/); | ||
const convertSet = (property, name, vertexData, features) => { | ||
const items = Array.isArray(property.items) ? property.items : [property.items]; | ||
|
||
return items | ||
.map((item, i) => | ||
createProperty( | ||
{ ...item, propCardinality: 'set' }, | ||
name, | ||
Array.isArray(vertexData) ? vertexData[i] || '' : '', | ||
features, | ||
), | ||
) | ||
.join(''); | ||
}; | ||
|
||
if (startsFromDigit) { | ||
return `${DEFAULT_PREFIX}_${nameWithoutSpecialCharacters}`; | ||
} | ||
const convertDate = value => `datetime("${value}")`; | ||
|
||
const convertNumber = (property, value) => { | ||
const mode = property.mode; | ||
const numberValue = JSON.stringify(value); | ||
|
||
switch (mode) { | ||
case 'double': | ||
return `${numberValue}d`; | ||
case 'float': | ||
return `${numberValue}f`; | ||
case 'long': | ||
return `${numberValue}l`; | ||
default: | ||
return numberValue; | ||
} | ||
}; | ||
|
||
return nameWithoutSpecialCharacters; | ||
}; | ||
const convertPropertyValue = (property, level, type, value) => { | ||
switch (type) { | ||
case 'date': | ||
return convertDate(value); | ||
case 'number': | ||
return convertNumber(property, value); | ||
} | ||
|
||
return `${JSON.stringify(value === undefined ? '' : value)}`; | ||
}; | ||
|
||
return { | ||
transformToValidGremlinName, | ||
generateVertices, | ||
generateEdges, | ||
}; | ||
module.exports = { | ||
generateVertices, | ||
generateEdges, | ||
}; |
Oops, something went wrong.