2015-08-22 13:35:06 +01:00
|
|
|
import mongodb from 'mongodb-promisified';
|
|
|
|
const MongoClient = mongodb().MongoClient;
|
|
|
|
import debugname from 'debug';
|
|
|
|
const debug = debugname('hostr:mongo');
|
|
|
|
|
|
|
|
const uristring = process.env.MONGO_URL || process.env.MONGOLAB_URI || 'mongodb://localhost:27017/hostr';
|
|
|
|
|
2015-08-23 22:12:32 +01:00
|
|
|
const configuredClient = new Promise((resolve, reject) => {
|
2015-08-22 13:35:06 +01:00
|
|
|
debug('Connecting to Mongodb');
|
|
|
|
return MongoClient.connect(uristring).then((client) => {
|
|
|
|
debug('Successfully connected to Mongodb');
|
|
|
|
client.Users = client.collection('users');
|
|
|
|
client.Files = client.collection('files');
|
|
|
|
client.Transactions = client.collection('transactions');
|
|
|
|
client.Logins = client.collection('logins');
|
|
|
|
client.Remember = client.collection('remember');
|
|
|
|
client.Reset = client.collection('reset');
|
|
|
|
client.Remember.ensureIndex({'created': 1}, {expireAfterSeconds: 2592000});
|
|
|
|
client.Files.ensureIndex({'owner': 1, 'status': 1, 'time_added': -1});
|
2015-08-22 16:16:15 +01:00
|
|
|
client.ObjectId = mongodb().ObjectId;
|
2015-08-22 13:35:06 +01:00
|
|
|
return resolve(client);
|
|
|
|
}).catch((e) => {
|
2015-08-23 22:12:32 +01:00
|
|
|
reject(e);
|
2015-08-22 13:35:06 +01:00
|
|
|
});
|
|
|
|
}).catch((e) => {
|
|
|
|
debug(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default function() {
|
2015-08-23 22:12:32 +01:00
|
|
|
return function* dbMiddleware(next) {
|
2015-08-22 13:35:06 +01:00
|
|
|
try {
|
|
|
|
this.db = yield configuredClient;
|
|
|
|
} catch (e) {
|
|
|
|
debug(e);
|
|
|
|
}
|
|
|
|
yield next;
|
2015-08-23 22:12:32 +01:00
|
|
|
};
|
2015-08-22 13:35:06 +01:00
|
|
|
}
|