2018-03-21 15:51:46 +00:00
|
|
|
//const debug = require('debug')('recv:users');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const path = require('path');
|
|
|
|
const mkdirp = require('mkdirp');
|
|
|
|
const Datastore = require('nedb');
|
|
|
|
const shortid = require('shortid');
|
|
|
|
const retry = require('async-retry');
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
const UserRepository = require('./user').UserRepository;
|
|
|
|
const CodeRepository = require('./code').CodeRepository;
|
|
|
|
const UploadRepository = require('./upload').UploadRepository;
|
2018-03-21 15:51:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Repository
|
|
|
|
{
|
|
|
|
constructor(config)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
self.config = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
load()
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
const initStore = (filename) =>
|
|
|
|
{
|
|
|
|
var store = new Datastore({
|
|
|
|
filename: path.join(self.config.path, filename),
|
|
|
|
autoload: true
|
|
|
|
});
|
|
|
|
|
|
|
|
store.persistence.setAutocompactionInterval(self.config.autocompactionInterval);
|
|
|
|
return store;
|
|
|
|
};
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
mkdirp(self.config.path, (err) =>
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
self.users = new UserRepository(initStore('user.db'));
|
|
|
|
self.codes = new CodeRepository(initStore('code.db'));
|
|
|
|
self.uploads = new UploadRepository(initStore('upload.db'));
|
2018-03-21 15:51:46 +00:00
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
self.users.init()
|
|
|
|
.then(() => { resolve() })
|
|
|
|
.catch((e) => { reject(e); });
|
2018-03-21 15:51:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = Repository;
|