Skip to content

Commit c75afb6

Browse files
committed
feat(helpers): add document archiver
Closes #13
1 parent 4594236 commit c75afb6

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

src/falcor/transforms/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,21 @@ export function remove ( collection, user, _id ) {
196196
});
197197
}
198198

199+
200+
export function archiveDocument(collection, _id, userId) {
201+
return this.flatMap(db => {
202+
db = db.mongo.collection(collection);
203+
return db.findOneAndUpdate(
204+
{_id: _id},
205+
{
206+
$set: {
207+
archived: true,
208+
archived_at: Date.now(),
209+
archiver: userId,
210+
}
211+
},
212+
{ returnOriginal: false }
213+
)
214+
})
215+
.map(r => r.value);
216+
}

src/falcor/transforms/spec.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import test from 'tape';
2+
3+
import database from '../../db';
4+
import {archiveDocument} from './index'
5+
import {generateId} from '../../utils'
6+
7+
const dbconf = {
8+
mongodb: {
9+
uri: process.env.MONGO_URI || 'mongodb://localhost:27017/dev',
10+
},
11+
12+
neo4j: {
13+
uri: process.env.NEO_URI || 'bolt://localhost',
14+
},
15+
};
16+
17+
test( 'archiveDocument', t => {
18+
const db = database( dbconf );
19+
const worldA = {_id: generateId(),title: "Test World A"};
20+
const worldB = {_id: generateId(),title: "Test World B"};
21+
const invalidWorld = {_id: generateId(),title: "Invalid world"};
22+
const userId = "testingId";
23+
const collection = 'world';
24+
let neo, mongo, actual, expected;
25+
26+
db
27+
.map( db => {
28+
neo = db.neo;
29+
mongo = db.mongo;
30+
return mongo;
31+
})
32+
.flatMap( mongo => {
33+
return mongo.collection(collection).insertMany( [worldA,worldB] )
34+
})
35+
.flatMap( () => {
36+
return db::archiveDocument(collection,worldA._id,userId)
37+
})
38+
.flatMap( document => {
39+
40+
expected = worldA._id;
41+
actual = document._id;
42+
t.equal(actual, expected,'should have the same _id that has been passed to archiveDocument');
43+
44+
expected = userId;
45+
actual = document.archiver;
46+
t.equal(actual, expected,'should match the userId that has been passed to archiveDocument');
47+
48+
expected = true;
49+
actual = document.archived;
50+
t.equal(actual, expected,'should be archived')
51+
52+
return mongo.collection(collection).findOne({_id : worldB._id})
53+
})
54+
.flatMap(document => {
55+
56+
expected = worldB;
57+
actual = document;
58+
t.deepEqual(actual, expected,'should not change when the _id does not match the document _id');
59+
60+
return mongo.collection(collection).deleteMany(
61+
{
62+
_id : { $in: [worldA._id,worldB._id]}
63+
}
64+
)
65+
})
66+
.flatMap(r =>
67+
db::archiveDocument(collection,invalidWorld._id,userId)
68+
)
69+
.subscribe((document) => {
70+
71+
expected = null;
72+
actual = document;
73+
t.equal(actual, expected,'should not exist when the document is invalid');
74+
75+
t.end();
76+
77+
mongo.close().then( () => neo.close( () => neo.disconnect() ));
78+
})
79+
});
80+

0 commit comments

Comments
 (0)