Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
histhub
linker
Commits
3c7a56e7
Commit
3c7a56e7
authored
May 31, 2018
by
Tobias Steiner
Browse files
Refactor linker
parent
6e804582
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/neo4j-linker.ts
View file @
3c7a56e7
import
{
v1
as
neo4j
}
from
'
neo4j-driver
'
;
import
builder
from
'
geolinker-common/dist/uri-builder
'
;
import
nconf
=
require
(
'
nconf
'
);
import
logger
from
'
geolinker-common/dist/logger
'
;
/**
* Load config for the streamer
*/
nconf
.
argv
()
.
env
()
.
file
({
file
:
'
./config.json
'
});
export
class
Linker
{
private
nconf
;
private
logger
;
private
driver
;
private
uriBuilder
;
const
driver
=
neo4j
.
driver
(
nconf
.
get
(
'
neo4j:config:uri
'
),
neo4j
.
auth
.
basic
(
nconf
.
get
(
'
neo4j:config:user
'
),
nconf
.
get
(
'
neo4j:config:password
'
),
),
);
constructor
(
nconf
,
logger
,
uriBuilder
)
{
this
.
nconf
=
nconf
;
this
.
logger
=
logger
;
this
.
uriBuilder
=
uriBuilder
;
this
.
driver
=
neo4j
.
driver
(
nconf
.
get
(
'
neo4j:config:uri
'
),
neo4j
.
auth
.
basic
(
nconf
.
get
(
'
neo4j:config:user
'
),
nconf
.
get
(
'
neo4j:config:password
'
),
),
);
}
export
default
{
/**
* Create a new node in the db if they do not exists
* @param node
* @returns {Result}
*/
newNode
:
(
node
)
=>
{
newNode
(
node
)
{
// todo: from topic!
const
provider
=
'
geonames
'
;
const
uri
=
b
uilder
.
geoname
(
node
.
id
);
return
driver
.
session
().
run
(
`MERGE (n:Place { uri: '
${
uri
}
'})
const
uri
=
this
.
uriB
uilder
.
geoname
(
node
.
id
);
return
this
.
driver
.
session
().
run
(
`MERGE (n:Place { uri: '
${
uri
}
'})
ON CREATE SET n.id: '
${
node
.
id
}
', n.provider:
${
provider
}
ON MATCH SET n.id: '
${
node
.
id
}
', n.provider:
${
provider
}
`
);
}
,
}
/**
* Write connection int the db. Create nodes if they do not exists
* @param connection
*/
connect
:
(
connection
)
=>
{
let
query
=
`MERGE (o:Place {uri:'
${
connection
.
from
}
'}) `
;
connection
.
to
.
forEach
((
current
,
i
)
=>
{
query
+=
` MERGE (t
${
i
}
:Place {uri:'
${
current
}
'}) `
;
query
+=
` MERGE (o)-[:
${
connection
.
relation
.
type
}
{author:'
${
connection
.
relation
.
author
}
'}]->(t
${
i
}
) `
;
});
driver
.
session
().
run
(
query
).
then
((
err
)
=>
{
logger
.
error
(
'
Error while saving a connection to neo4j
'
);
logger
.
error
(
JSON
.
stringify
(
err
));
connect
(
connection
)
{
return
new
Promise
(
(
resolve
,
reject
)
=>
{
let
query
=
`MERGE (o:Place {uri:'
${
connection
.
from
}
'}) `
;
connection
.
to
.
forEach
((
current
,
i
)
=>
{
query
+=
` MERGE (t
${
i
}
:Place {uri:'
${
current
}
'}) `
;
query
+=
` MERGE (o)-[:
${
connection
.
relation
.
type
}
{author:'
${
connection
.
relation
.
author
}
'}]->(t
${
i
}
) `
;
});
// get a new session
const
session
=
this
.
driver
.
session
();
this
.
logger
.
info
(
'
Try to build network
'
);
session
.
run
(
query
).
then
(()
=>
{
session
.
close
();
resolve
();
}).
catch
((
err
)
=>
{
session
.
close
();
reject
(
`Error while saving a connection to neo4j
${
JSON
.
stringify
(
err
)}
`
);
});
});
},
};
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment