-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathload.js
More file actions
183 lines (155 loc) · 5 KB
/
load.js
File metadata and controls
183 lines (155 loc) · 5 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import fs from 'fs';
import path from 'path';
import { Observable } from 'rx';
import connect from '../src/db';
const readFile = Observable.fromNodeCallback( fs.readFile );
const readJson = ( ...args ) => readFile( path.join( __dirname, ...args ) )
.map( contents => JSON.parse( contents ) )
;
const MONGO_DB_NAME = 'dev';
let neo, mongo;
const log = function ( ...args ) {
return this.tap( () => console.log( ...args ) );
};
const conf = {
mongodb: {
uri: process.env.MONGO_URI || `mongodb://localhost:27017/${MONGO_DB_NAME}`,
},
neo4j: {
uri: process.env.NEO_URI || 'bolt://localhost',
},
};
const disconnect = () => {
mongo.close();
neo.close();
neo.disconnect();
};
console.log( 'Connecting to databases...' );
connect( conf )
.tap( db => {
neo = db.neo;
mongo = db.mongo;
})
::log( 'Emptying Mongo...' )
.flatMap( () => mongo.collection( 'users' ).removeMany({}) )
.flatMap( () => mongo.collection( 'worlds' ).removeMany({}) )
.flatMap( () => mongo.collection( 'characters' ).removeMany({}) )
.flatMap( () => mongo.collection( 'elements' ).removeMany({}) )
.flatMap( () => mongo.collection( 'genes' ).removeMany({}) )
.flatMap( () => mongo.collection( 'outlines' ).removeMany({}) )
::log( 'Emptying Neo...' )
.flatMap( () => neo.run([
'MATCH (n)',
'OPTIONAL MATCH (n)-[rel]-()',
'DELETE n, rel',
'RETURN true', // hack to make sure the observable has something to which to subscribe
].join( "\n" )))
.toArray()
/**
* Users
*/
::log( 'Loading Users to Mongo...' )
.flatMap( () => readJson( 'mongo', 'users.json' ) )
.flatMap( users => mongo.collection( 'users' ).insertMany( users ) )
.map( result => result.ops )
::log( 'Loading Users to Neo...' )
.flatMap( users => neo.run([
'UNWIND {users} as user',
'CREATE (u:User { _id: user._id })',
'RETURN count(u) as users',
].join( "\n" ), { users } ) )
/**
* Worlds
*/
::log( 'Loading Worlds to Mongo...' )
.flatMap( () => readJson( 'mongo', 'worlds.json' ) )
.flatMap( worlds => mongo.collection( 'worlds' ).insertMany( worlds ) )
.map( result => result.ops )
::log( 'Loading Worlds to Neo...' )
.flatMap( worlds => neo.run([
'UNWIND {worlds} as world',
'CREATE (w:World { _id: world._id })',
'WITH w, world',
'UNWIND world.owners as uid',
'MATCH (user:User {_id: uid})',
'CREATE (user)-[:ROLE {type: "own"}]->(w)',
'WITH w, world',
'UNWIND world.writers as uid',
'MATCH (user:User {_id: uid})',
'CREATE (user)-[:ROLE {type: "rw"}]->(w)',
'WITH w, world',
'UNWIND world.readers as uid',
'MATCH (user:User {_id: uid})',
'CREATE (user)-[:ROLE {type: "ro"}]->(w)',
'WITH w, world',
'RETURN count(w) as worlds',
].join( "\n" ), { worlds } ) )
/**
* Characters
*/
::log( 'Loading Characters to Mongo...' )
.flatMap( () => readJson( 'mongo', 'characters.json' ) )
.flatMap( characters => mongo.collection( 'characters' ).insertMany( characters ) )
.map( result => result.ops )
::log( 'Loading Characters to Neo...' )
.flatMap( characters => neo.run([
'UNWIND {characters} as character',
'CREATE (c:Character { _id: character._id })',
'WITH character, c',
'MATCH (w:World {_id: character.world})',
'CREATE (w)<-[:OF]-(c)',
'RETURN count(c) as characters',
].join( "\n" ), { characters } ) )
/**
* Elements
*/
::log( 'Loading Elements to Mongo...' )
.flatMap( () => readJson( 'mongo', 'elements.json' ) )
.flatMap( elements => mongo.collection( 'elements' ).insertMany( elements ) )
.map( result => result.ops )
::log( 'Loading Elements to Neo...' )
.flatMap( elements => neo.run([
'UNWIND {elements} as element',
'CREATE (e:Element { _id: element._id })',
'WITH element, e',
'MATCH (w:World {_id: element.world_id})',
'CREATE (w)<-[:OF]-(e)',
'RETURN count(e) as elements',
].join( "\n" ), { elements } ) )
/**
* Outlines
*/
::log( 'Loading Outlines to Mongo...' )
.flatMap( () => readJson( 'mongo', 'outlines.json' ) )
.flatMap( outlines => mongo.collection( 'outlines' ).insertMany( outlines ) )
.map( result => result.ops )
::log( 'Loading Outlines to Neo...' )
.flatMap( outlines => neo.run([
'UNWIND {outlines} as outline',
'CREATE (o:Outline { _id: outline._id })',
'WITH outline, o',
'MATCH (w:World {_id: outline.world_id})',
'CREATE (w)<-[:OF]-(o)',
'RETURN count(o) as outlines',
].join( "\n" ), { outlines } ) )
/**
* Genes
*/
::log( 'Loading Genes to Mongo...' )
.flatMap( () => readJson( 'mongo', 'genes.json' ) )
.flatMap( genes => mongo.collection( 'genes' ).insertMany( genes ) )
.map( result => result.ops )
::log( 'Loading Genes to Neo...' )
.flatMap( genes => neo.run([
'UNWIND {genes} as gene',
'CREATE (g:Gene { _id: gene._id })',
'RETURN count(g) as genes',
].join( "\n" ), { genes } ) )
.toArray()
.subscribe( () => {
console.log( 'Success!' );
}, err => {
console.log( 'Something went wrong:', err );
disconnect();
}, disconnect )
;