1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-23 17:50:19 +01:00

Merge pull request #216 from PassTheMayo/master

Fixed RethinkDB document store
This commit is contained in:
John Crepezzi 2018-04-16 11:53:28 -04:00 committed by GitHub
commit faa7e679ca
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,7 +1,15 @@
const crypto = require('crypto');
const rethink = require('rethinkdbdash');
const winston = require('winston');
var RethinkDBStore = (options) => {
const md5 = (str) => {
const md5sum = crypto.createHash('md5');
md5sum.update(str);
return md5sum.digest('hex');
};
class RethinkDBStore {
constructor(options) {
this.client = rethink({
silent: true,
host: options.host || '127.0.0.1',
@ -10,30 +18,29 @@ var RethinkDBStore = (options) => {
user: options.user || 'admin',
password: options.password || ''
});
};
}
RethinkDBStore.md5 = (str) => {
const md5sum = crypto.createHash('md5');
md5sum.update(str);
return md5sum.digest('hex');
};
RethinkDBStore.prototype.set = (key, data, callback) => {
try {
this.client.table('uploads').insert({ id: RethinkDBStore.md5(key), data: data }).run((error) => {
if (error) return callback(false);
set(key, data, callback) {
this.client.table('uploads').insert({ id: md5(key), data: data }).run((error) => {
if (error) {
callback(false);
winston.error('failed to insert to table', error);
return;
}
callback(true);
});
} catch (err) {
callback(false);
}
};
RethinkDBStore.prototype.get = (key, callback) => {
this.client.table('uploads').get(RethinkDBStore.md5(key)).run((error, result) => {
if (error || !result) return callback(false);
get(key, callback) {
this.client.table('uploads').get(md5(key)).run((error, result) => {
if (error || !result) {
callback(false);
winston.error('failed to insert to table', error);
return;
}
callback(result.data);
});
};
}
}
module.exports = RethinkDBStore;