Skip to content

Commit

Permalink
feat(helpers): add document archiver
Browse files Browse the repository at this point in the history
Closes #13
  • Loading branch information
itsjgf committed Oct 1, 2016
1 parent f787a6f commit 7536d24
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/falcor/transforms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,21 @@ export function remove ( collection, user, _id ) {
});
}


export function archiveDocument(collection, _id, userId) {
return this.flatMap(db => {
db = db.mongo.collection(collection);
return db.findOneAndUpdate(
{_id: _id},
{
$set: {
archived: true,
archived_at: Date.now(),
archiver: userId,
}
},
{ returnOriginal: false }
)
})
.map(r => r.value);
}
80 changes: 80 additions & 0 deletions src/falcor/transforms/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import test from 'tape';

import database from '../../db';
import {archiveDocument} from './index'
import {generateId} from '../../utils'

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

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

test( 'archiveDocument', t => {
const db = database( dbconf );
const worldA = {_id: generateId(),title: "Test World A"};
const worldB = {_id: generateId(),title: "Test World B"};
const invalidWorld = {_id: generateId(),title: "Invalid world"};
const userId = "testingId";
const collection = 'world';
let neo, mongo, actual, expected;

db
.map( db => {
neo = db.neo;
mongo = db.mongo;
return mongo;
})
.flatMap( mongo => {
return mongo.collection(collection).insertMany( [worldA,worldB] )
})
.flatMap( () => {
return db::archiveDocument(collection,worldA._id,userId)
})
.flatMap( document => {

expected = worldA._id;
actual = document._id;
t.equal(actual, expected,'should have the same _id that has been passed to archiveDocument');

expected = userId;
actual = document.archiver;
t.equal(actual, expected,'should match the userId that has been passed to archiveDocument');

expected = true;
actual = document.archived;
t.equal(actual, expected,'should be archived')

return mongo.collection(collection).findOne({_id : worldB._id})
})
.flatMap(document => {

expected = worldB;
actual = document;
t.deepEqual(actual, expected,'should not change when the _id does not match the document _id');

return mongo.collection(collection).deleteMany(
{
_id : { $in: [worldA._id,worldB._id]}
}
)
})
.flatMap(r =>
db::archiveDocument(collection,invalidWorld._id,userId)
)
.subscribe((document) => {

expected = null;
actual = document;
t.equal(actual, expected,'should not exist when the document is invalid');

t.end();

mongo.close().then( () => neo.close( () => neo.disconnect() ));
})
});

0 comments on commit 7536d24

Please sign in to comment.