Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fixtures): add support for neo4j #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions fixtures/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require( 'babel-register' );
require( './load' );

183 changes: 183 additions & 0 deletions fixtures/load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
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 )
;

File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions fixtures/outlines.json → fixtures/mongo/outlines.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
{
"_id": "D0N1iLYimG",
"world_id": "BdvmHwZsvA",
"title": "Hero's Journey",

"readers": [
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@
"dev": "nodemon index.js",
"test": "tape -r babel-polyfill -r babel-register src/**/*spec.js | faucet",
"test:ci": "tape -r babel-polyfill -r babel-register src/**/*spec.js",
"data:worlds": "mongoimport --db dev --drop --jsonArray --type json --file fixtures/worlds.json --collection worlds",
"data:outlines": "mongoimport --db dev --drop --jsonArray --type json --file fixtures/outlines.json --collection outlines",
"data:characters": "mongoimport --db dev --drop --jsonArray --type json --file fixtures/characters.json --collection characters",
"data:genes": "mongoimport --db dev --drop --jsonArray --type json --file fixtures/genes.json --collection genes",
"data:users": "mongoimport --db dev --drop --jsonArray --type json --file fixtures/users.json --collection users",
"data:elements": "mongoimport --db dev --drop --jsonArray --type json --file fixtures/elements.json --collection elements",
"data": "npm run data:worlds && npm run data:characters && npm run data:users && npm run data:outlines && npm run data:elements && npm run data:genes"
"data": "node fixtures/index.js"
},
"repository": {
"type": "git",
Expand Down