-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_init.js
112 lines (103 loc) · 3.47 KB
/
db_init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Created by Antonio Altamura on 05/06/2018.
*/
"use strict";
let neo4j = require('neo4j-driver').v1,
config = require('./config'),
driver = neo4j.driver(config.bolt, neo4j.auth.basic(config.user, config.password)),
λ = require('./utils');
async function init_db() {
const session = driver.session(neo4j.session.READ);
try{
await session.run(`MATCH (n) DETACH DELETE n`);
console.warn("DB cleared!");
let res = await session.run(`
LOAD CSV WITH HEADERS FROM "file:///Paper.csv" AS r FIELDTERMINATOR ';'
CREATE (p:Paper {
nodeId: toInteger(r.\`nodeId:ID(Paper)\`),
name: r.name,
date: r.date,
file: r.file
})
RETURN count(*) AS c
`);
console.warn("Paper added: " + λ.toInt(res.records[0].get('c')));
res = await session.run(`
LOAD CSV WITH HEADERS FROM "file:///Paper_data.csv" AS r FIELDTERMINATOR ';'
MATCH (a:Paper {nodeId: toInteger(r.\`id:ID(Paper)\`)})
SET a.abstract = r.abstract
RETURN count(*) AS c
`);
console.warn("Paper extra data added: " + λ.toInt(res.records[0].get('c')));
res = await session.run(`LOAD CSV WITH HEADERS FROM "file:///Book.csv" AS r FIELDTERMINATOR ';'
CREATE (p:Book {
nodeId: toInteger(r.\`nodeId:ID(Book)\`),
name: r.name,
date: r.date,
file: r.file
})
RETURN count(*) AS c
`);
console.warn( "Book added: " + λ.toInt(res.records[0].get('c')));
res = await session.run(`LOAD CSV WITH HEADERS FROM "file:///Journal.csv" AS r FIELDTERMINATOR ';'
CREATE (p:Journal {
nodeId: toInteger(r.nodeId),
name: r.name,
date: r.date,
volume:toInteger(r.volume),
issue:toInteger(r.issue)
})
RETURN count(*) AS c
`);
console.warn( "Journal added: " + λ.toInt(res.records[0].get('c')));
res = await session.run(`LOAD CSV WITH HEADERS FROM "file:///Author_HAS_WRITTEN_Paper.csv" AS r FIELDTERMINATOR ';'
MERGE (a:Author {name: r.name})
WITH a AS a, r AS r
MATCH (g:Paper {nodeId: toInteger(r.\`:END_ID(Paper)\`)})
CREATE (a)-[:HAS_WRITTEN]->(g)
RETURN count(*) AS c
`);
console.warn( "Author_HAS_WRITTEN_Paper added: " + λ.toInt(res.records[0].get('c')));
res = await session.run(`LOAD CSV WITH HEADERS FROM "file:///Author_HAS_WRITTEN_Book.csv" AS r FIELDTERMINATOR ';'
MERGE (a:Author {name: r.name})
WITH a AS a, r AS r
MATCH (g:Book {nodeId: toInteger(r.\`:END_ID(Book)\`)})
CREATE (a)-[:HAS_WRITTEN]->(g)
RETURN count(*) AS c
`);
console.warn( "Author_HAS_WRITTEN_Book added: " + λ.toInt(res.records[0].get('c')));
res = await session.run(`
LOAD CSV WITH HEADERS FROM "file:///Paper_HAS_KEYWORD_Topic.csv" AS r FIELDTERMINATOR ';'
MATCH (a:Paper {nodeId: toInteger(r.\`:START_ID(Paper)\`)})
WITH a AS a, r AS r
MERGE (t:Topic {name:r.topic})
CREATE (a)-[:HAS_KEYWORD]->(t)
RETURN count(*) AS c
`);
console.warn( "Paper_HAS_KEYWORD_Topic added: " + λ.toInt(res.records[0].get('c')));
res = await session.run(`
LOAD CSV WITH HEADERS FROM "file:///Paper_PUBLISHED_IN_Journal.csv" AS r FIELDTERMINATOR ';'
MATCH (a:Paper {nodeId: toInteger(r.\`:START_ID(Paper)\`)})
MATCH (j:Journal {nodeId: toInteger(r.\`:END_ID(Journal)\`)})
WITH a,r,j
CREATE (a)-[:PUBLISHED_IN]->(j)
RETURN count(*) AS c
`);
console.warn( "Paper_PUBLISHED_IN_Journal added: " + λ.toInt(res.records[0].get('c')));
} catch (e) {
console.warn("Somehow somewhere something is gone wrong");
console.warn(e)
throw (e)
}
}
init_db()
.then( () => {
console.warn(λ.southPark() + `
The DB has been correctly initialized.\n
And Kenny is alive.\n\n`);
process.exit()
})
.catch( () => {
console.warn("Somehow somewhere something is gone wrong");
process.exit()
});