Skip to content

Commit

Permalink
Support tenancy by keying documents
Browse files Browse the repository at this point in the history
  • Loading branch information
solocommand committed Oct 30, 2018
1 parent 893d54a commit d0bada6
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 d0bada6

Please sign in to comment.