Skip to content
Merged
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: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"homepage": "https://github.com/StoryShop/api#readme",
"dependencies": {
"@reactivex/rxjs": "^5.0.0-beta.2",
"@reactivex/rxjs": "^5.0.0-beta.12",
"aws-sdk": "^2.2.41",
"babel-preset-es2015": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
Expand All @@ -49,6 +49,7 @@
"jsonwebtoken": "^5.7.0",
"mongodb": "2.1.15",
"multiparty": "^4.1.2",
"neo4j-driver": "^1.0.4",
"opml-generator": "^1.1.1",
"shortid": "^2.2.4",
"uservoice-sso": "^0.1.0"
Expand Down
2 changes: 1 addition & 1 deletion src/api/auth/find-or-create-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Observable } from 'rx';
import { generateId } from '../../utils';

export default ( database, email, givenName, familyName, displayName ) => {
let db = database.map( db => db.collection( 'users' ) );
let db = database.map( db => db.mongo.collection( 'users' ) );

return db
.flatMap( db => db.findOne({
Expand Down
2 changes: 1 addition & 1 deletion src/api/auth/find-user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Observable } from 'rx';

export default ( database, _id ) => {
let db = database.map( db => db.collection( 'users' ) );
let db = database.map( db => db.mongo.collection( 'users' ) );

return db.flatMap( db => db.findOne({ _id }));
};
Expand Down
23 changes: 19 additions & 4 deletions src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Observable } from 'rx';
import express from 'express';
import bodyParser from 'body-parser';
import info from '../info';
Expand All @@ -11,7 +12,13 @@ import { withUser, isLoggedIn } from './auth/middleware';
const log = Logger( 'ApiRouter' );

const db = connectDb({
uri: process.env.MONGO_URI || 'mongodb://localhost:27017/dev',
mongodb: {
uri: process.env.MONGO_URI || 'mongodb://localhost:27017/dev',
},

neo4j: {
uri: process.env.NEO_URI || 'bolt://localhost',
},
});

const router = express.Router();
Expand All @@ -27,15 +34,23 @@ router.get( '/', function ( req, res ) {
});

router.get( '/health', function ( req, res ) {
db.subscribe( db => res.json({ status: 200, message: 'good health' }), err => {
const handleError = err => {
log.debug( 'Error connecting to DB' );
log.debug( err.stack || err );

res.status( 500 ).json({
status: 500,
message: `could not connect to db: ${err}`,
message: `could not connect to database: ${err}`,
});
});
};

db.subscribe( ({ mongo, neo }) => {
neo.run( 'MATCH (n) RETURN count(n) as cnt' )
.toArray()
.subscribe( records => {
res.json({ status: 200, message: 'good health' });
}, handleError )
}, handleError );
});

export default router;
Expand Down
2 changes: 1 addition & 1 deletion src/api/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default ( db ) => {
};

db
.map( db => db.collection( 'users' ) )
.map( db => db.mongo.collection( 'users' ) )
.flatMap( db => db.findOneAndUpdate(
{ _id: req.user._id },
{ $push: { files: upload }, $inc: { filesLength: 1 } },
Expand Down
47 changes: 36 additions & 11 deletions src/db.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
import { Observable } from 'rx';
import { MongoClient } from 'mongodb';
import { driver } from 'neo4j-driver/lib/v1';
import Logger from './logger';

const log = Logger( 'DB' );
const log = Logger( 'Database' );

export default ({ uri }) => {
log.debug(`connecting to ${uri}`);
let connection = null;
export default ({ mongodb, neo4j }) => {
log.debug( 'Connecting to databases...' );
let mongo = null;
let neo = null;

const connect = Observable.fromNodeCallback( function ( uri, cb ) {
if ( ! connection ) {
MongoClient.connect( uri, function ( err, conn ) {
connection = conn;
const getDb = () => ({
mongo,
neo: {
run ( ...args ) {
// convert the neo4j run function's Result into an Observable.
return new Observable( sub => {
neo.run( ...args ).subscribe({
onNext: ( ...args ) => sub.next( ...args ),
onError: ( ...args ) => sub.error( ...args ),
onCompleted: ( ...args ) => sub.completed( ...args ),
});
});
},
},
});

const connect = Observable.fromNodeCallback( function ( muri, nuri, cb ) {
if ( ! mongo ) {
MongoClient.connect( muri, function ( err, conn ) {
mongo = conn;

if ( err ) {
return cb( err );
}

cb( err, conn );
neo = driver( nuri ).session();

cb( err, getDb() );
});
} else {
cb( null, connection );
cb( null, getDb() );
}
});

return connect( uri );
return connect( mongodb.uri, neo4j.uri );
};


1 change: 0 additions & 1 deletion src/falcor/elements/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ export default ( db, req, res ) => {
})
;
})
.catch(e=>console.log("err",e.stack))
,
},
];
Expand Down
6 changes: 3 additions & 3 deletions src/falcor/transforms/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '../../utils';

export const getElementsForWorld = ( world, indices ) => db => {
return Observable.fromPromise( db.collection( 'elements' ).find({ world_id: world._id }).toArray() )
return Observable.fromPromise( db.mongo.collection( 'elements' ).find({ world_id: world._id }).toArray() )
.flatMap( elements => indices.map( idx => ({
_id: world._id,
idx,
Expand All @@ -17,7 +17,7 @@ export const getElementsForWorld = ( world, indices ) => db => {
};

// export const getElements = ( ids, user ) => db => {
// return Observable.fromPromise( db.collection( 'elements' ).find({ _id: { $in: ids }, $or: [
// return Observable.fromPromise( db.mongo.collection( 'elements' ).find({ _id: { $in: ids }, $or: [
// { owners: { $eq: user._id } },
// { writers: { $eq: user._id } },
// { readers: { $eq: user._id } },
Expand All @@ -27,7 +27,7 @@ export const getElementsForWorld = ( world, indices ) => db => {
// };

export const getElementCount = world => db => {
return Observable.fromPromise( db.collection( 'elements' ).count({ world_id: world._id }) )
return Observable.fromPromise( db.mongo.collection( 'elements' ).count({ world_id: world._id }) )
.map( elements => ({ _id: world._id, elements }) )
;
};
Expand Down
16 changes: 8 additions & 8 deletions src/falcor/transforms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function toPathValues ( pathGen, fields ) {
}

export const fuzzyFind = ( collection, field, patterns, user ) => db => {
return Observable.fromPromise( db.collection( collection ).find({
return Observable.fromPromise( db.mongo.collection( collection ).find({
[field]: { $in: patterns.map( p => new RegExp( `^.*${p}.*$`, 'ig' ) ) },
$or: [
{ writers: { $eq: user._id } },
Expand All @@ -61,7 +61,7 @@ export const fuzzyFind = ( collection, field, patterns, user ) => db => {

export function getProps ( collection, ids, user ) {
return this.flatMap( db => {
return Observable.fromPromise( db.collection( collection ).find({ _id: { $in: ids }, $or: [
return Observable.fromPromise( db.mongo.collection( collection ).find({ _id: { $in: ids }, $or: [
{ writers: { $eq: user._id } },
{ readers: { $eq: user._id } },
]}).toArray() )
Expand All @@ -77,7 +77,7 @@ export function setProps ( collection, propsById, user ) {
const writers = { $eq: user._id };
const $set = unwrapAtomsInObject( propsById[ _id ] );

return db.collection( collection ).findOneAndUpdate( { _id, writers }, { $set }, {
return db.mongo.collection( collection ).findOneAndUpdate( { _id, writers }, { $set }, {
returnOriginal: false,
});
})
Expand All @@ -87,7 +87,7 @@ export function setProps ( collection, propsById, user ) {
}

export const getRandom = ( collection ) => db => {
const coll = db.collection( collection );
const coll = db.mongo.collection( collection );

return Observable.fromPromise( coll.count() )
.flatMap( count => coll.find().limit( 1 ).skip( Math.floor( Math.random() * count ) ).toArray() )
Expand Down Expand Up @@ -123,7 +123,7 @@ export const getWithinArray = ( fields, indices ) => item => {

export const setWithinArray = ( collection, field, props, user ) => db => {
const ids = keys( props );
db = db.collection( collection );
db = db.mongo.collection( collection );

return Observable.from( ids )
.flatMap( id => {
Expand All @@ -145,7 +145,7 @@ export const setWithinArray = ( collection, field, props, user ) => db => {

export function pushToArray ( collection, user, ids, field, value ) {
return this.flatMap( db => {
db = db.collection( collection );
db = db.mongo.collection( collection );

return Observable.from( ids )
.flatMap( id => db.findOneAndUpdate(
Expand Down Expand Up @@ -179,7 +179,7 @@ export const addIndex = () => {

export function create ( collection, props ) {
return this.flatMap( db => {
db = db.collection( collection );
db = db.mongo.collection( collection );
props._id = generateId();

return Observable.fromPromise( db.insertOne( props ) ).flatMap( r => r.ops );
Expand All @@ -188,7 +188,7 @@ export function create ( collection, props ) {

export function remove ( collection, user, _id ) {
return this.flatMap( db => {
db = db.collection( collection );
db = db.mongo.collection( collection );

return Observable.fromPromise( db.removeMany({ _id: _id, writers: user._id }) )
.map( r => r.result.n )
Expand Down
4 changes: 2 additions & 2 deletions src/falcor/transforms/worlds.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function getWorlds ( ids, user ) {
}

return this.flatMap( db => {
return Observable.fromPromise( db.collection( 'worlds' ).find( query ).toArray() )
return Observable.fromPromise( db.mongo.collection( 'worlds' ).find( query ).toArray() )
.selectMany( w => w )
;
});
Expand All @@ -35,7 +35,7 @@ export function setWorldProps ( propsById, user ) {
{ writers: { $eq: user._id } },
];

return db.collection( 'worlds' ).findOneAndUpdate( { _id, $or }, { $set: propsById[ _id ] }, {
return db.mongo.collection( 'worlds' ).findOneAndUpdate( { _id, $or }, { $set: propsById[ _id ] }, {
returnOriginal: false,
});
})
Expand Down
8 changes: 4 additions & 4 deletions src/falcor/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import UserVoiceSSO from 'uservoice-sso';
// })));

export const setLastVisited = ( user, ids ) => db =>
keysO( ids ).filter( id => id === user._id ).flatMap( id => db.collection( 'users' ).findOneAndUpdate(
keysO( ids ).filter( id => id === user._id ).flatMap( id => db.mongo.collection( 'users' ).findOneAndUpdate(
{ _id: id },
{ $set: { 'ux.lastVisited': ids[ id ].ux.lastVisited } },
{ returnOriginal: false }
Expand All @@ -36,7 +36,7 @@ export const setLastVisited = ( user, ids ) => db =>
;

export const getLastVisited = ( user, ids ) => db =>
Observable.fromPromise( db.collection( 'users' ).find( { _id: { $in: ids, $eq: user._id } } ).toArray() )
Observable.fromPromise( db.mongo.collection( 'users' ).find( { _id: { $in: ids, $eq: user._id } } ).toArray() )
.selectMany( docs => docs )
.map( ({ _id, ux }) => ({ _id, ...ux }) )
;
Expand Down Expand Up @@ -130,15 +130,15 @@ export default ( db, req, res ) => {
{
route: 'usersById[{keys:ids}].files.length',
get: pathSet => db
.flatMap( db => db.collection( 'users' ).find( { _id: { $in: pathSet.ids, $eq: user._id } } ).toArray() )
.flatMap( db => db.mongo.collection( 'users' ).find( { _id: { $in: pathSet.ids, $eq: user._id } } ).toArray() )
.selectMany( d => d )
.map( ({ _id, ...user }) => ({ _id, length: user.files ? user.files.length : 0 }) )
::toPathValues( ( i, f ) => [ 'usersById', i._id, 'files', f ], 'length' )
},
{
route: 'usersById[{keys:ids}].files[{integers:indices}]["name", "url", "contentType", "size", "extension"]',
get: pathSet => db
.flatMap( db => db.collection( 'users' ).find( { _id: { $in: pathSet.ids, $eq: user._id } } ).toArray() )
.flatMap( db => db.mongo.collection( 'users' ).find( { _id: { $in: pathSet.ids, $eq: user._id } } ).toArray() )
.selectMany( d => d )
.flatMap( getWithinArray( 'files', pathSet.indices ) )
.map( ({ files, ...o }) => ({ ...o, ...pathSet[ 4 ].reduce( ( o, k ) => { o[k] = files[k]; return o }, {} ) }) )
Expand Down
2 changes: 1 addition & 1 deletion src/falcor/worlds/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default ( db, req, res ) => {
get: pathSet => Observable.from( pathSet.ids )
.flatMap( _id => {
return db
.map( db => db.collection( 'elements' ) )
.map( db => db.mongo.collection( 'elements' ) )
.flatMap( db => db.distinct( 'tags', {
world_id: { $in: pathSet.ids },
$or: [
Expand Down