Commit 3c7a56e7 authored by Tobias Steiner's avatar Tobias Steiner
Browse files

Refactor linker

parent 6e804582
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 = builder.geoname(node.id);
return driver.session().run(`MERGE (n:Place { uri: '${uri}'})
const uri = this.uriBuilder.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)}`);
});
});
},
};
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment