Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion __mocks__/neo4j.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,22 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

module.exports = {}
var out = {
v1: {
types: {
Node: function Node (id) {
this.identity = id
},
Relationship: function Relationship (id) {
this.identity = id
},
Path: function Path () {}
}
}
}

out.v1.types.Node.prototype.toString = function () { return 'node' }
out.v1.types.Relationship.prototype.toString = function () { return 'rel' }
out.v1.types.Path.prototype.toString = function () { return 'path' }

module.exports = out
21 changes: 21 additions & 0 deletions src/shared/services/bolt/boltMappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export function extractNodesAndRelationshipsFromRecordsForOldVis (records, types
} else {
records.forEach((record) => {
let graphItems = keys.map((key) => record.get(key))
graphItems = flattenArray(recursivelyExtractGraphItems(types, graphItems)).filter((item) => item !== false)
rawNodes = [...rawNodes, ...graphItems.filter((item) => item instanceof types.Node)]
rawRels = [...rawRels, ...graphItems.filter((item) => item instanceof types.Relationship)]
let paths = graphItems.filter((item) => item instanceof types.Path)
Expand All @@ -161,6 +162,26 @@ export function extractNodesAndRelationshipsFromRecordsForOldVis (records, types
return { nodes: nodes, relationships: relationships }
}

const recursivelyExtractGraphItems = (types, item) => {
if (item instanceof types.Node) return item
if (item instanceof types.Relationship) return item
if (item instanceof types.Path) return item
if (Array.isArray(item)) return item.map((i) => recursivelyExtractGraphItems(types, i))
if (['number', 'string', 'boolean'].indexOf(typeof item) !== -1) return false
if (item === null) return false
if (typeof item === 'object') {
return Object.keys(item).map((key) => recursivelyExtractGraphItems(types, item[key]))
}
return item
}

const flattenArray = (arr) => {
return arr.reduce((all, curr) => {
if (Array.isArray(curr)) return all.concat(flattenArray(curr))
return all.concat(curr)
}, [])
}

const extractNodesAndRelationshipsFromPath = (item, rawNodes, rawRels) => {
let paths = Array.isArray(item) ? item : [item]
paths.forEach((path) => {
Expand Down
40 changes: 39 additions & 1 deletion src/shared/services/bolt/boltMappings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/* global test, expect */
/* global describe, test, expect */
import { v1 as neo4j } from 'neo4j-driver-alias'
import {
itemIntToString,
arrayIntToString,
extractNodesAndRelationshipsFromRecords,
extractNodesAndRelationshipsFromRecordsForOldVis,
extractPlan,
flattenProperties,
objIntToString
Expand Down Expand Up @@ -329,4 +330,41 @@ describe('boltMappings', () => {
expect(flattenedProperties).toEqual(result)
})
})
describe('extractNodesAndRelationshipsFromRecordsForOldVis', () => {
test('should recursively look for graph items', () => {
// Given
const firstNode = new neo4j.types.Node('1', ['Person'], {prop1: 'prop1'})
const nodeCollection = [
new neo4j.types.Node('2', ['Person'], {prop1: 'prop1'}),
new neo4j.types.Node('3', ['Person'], {prop1: 'prop1'}),
new neo4j.types.Node('4', ['Person'], {prop1: 'prop1'})
]
const boltRecord = {
keys: ['n', 'c'],
get: (key) => {
if (key === 'n') {
return firstNode
}
if (key === 'c') {
return nodeCollection
}
}
}
const records = [boltRecord]

// When
const out = extractNodesAndRelationshipsFromRecordsForOldVis(
records,
neo4j.types,
false,
{
intChecker: () => true,
intConverter: (a) => a
}
)

// Then
expect(out.nodes.length).toEqual(4)
})
})
})