Skip to content

Commit

Permalink
Merge pull request #3 from solocommand/tenancy
Browse files Browse the repository at this point in the history
Support tenancy by keying documents
  • Loading branch information
solocommand authored Oct 30, 2018
2 parents a8706a3 + d0bada6 commit 1a09918
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions graph/src/mongodb.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
const { MongoClient, ObjectId } = require('mongodb');
const { MONGO_DSN } = require('./env');
const { MONGO_DSN, B4GRAPH_TENANT_KEY } = require('./env');

const client = new MongoClient(MONGO_DSN);
const promise = client.connect();
const tenant = B4GRAPH_TENANT_KEY;

// eslint-disable-next-line no-console
promise.then(() => process.stdout.write(`\n💾 MongoDB connected to ${MONGO_DSN}\n`));
promise.catch((e) => { throw e; });

module.exports = {
connect: () => promise,
retrieve: id => client.db().collection('submission').findOne({ _id: new ObjectId(id) }),
insert: payload => client.db().collection('submission').insertOne(payload),
complete: id => client.db().collection('submission').updateOne({ _id: new ObjectId(id) }, { $set: { reviewed: true } }),
submissions: ({ reviewed = false } = { reviewed: false }) => client.db().collection('submission').find({ reviewed }).toArray(),
submissionCount: () => client.db().collection('submission').countDocuments({ reviewed: false }),
retrieve: id => client.db().collection('submission').findOne({ tenant, _id: new ObjectId(id) }),
insert: payload => client.db().collection('submission').insertOne({ tenant, ...payload }),
complete: id => client.db().collection('submission').updateOne({ tenant, _id: new ObjectId(id) }, { $set: { reviewed: true } }),
submissions: ({ reviewed = false } = { reviewed: false }) => client.db().collection('submission').find({ tenant, reviewed }).toArray(),
submissionCount: () => client.db().collection('submission').countDocuments({ tenant, reviewed: false }),
};

0 comments on commit 1a09918

Please sign in to comment.