1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-24 02:00:17 +01:00
haste-server/lib/document_stores/rethinkdb.js

47 lines
1 KiB
JavaScript
Raw Normal View History

const crypto = require('crypto');
const rethink = require('rethinkdbdash');
const winston = require('winston');
2018-04-16 17:52:53 +02:00
const md5 = (str) => {
2020-08-28 04:39:03 +02:00
const md5sum = crypto.createHash('md5');
md5sum.update(str);
return md5sum.digest('hex');
2018-04-16 17:52:53 +02:00
};
class RethinkDBStore {
2020-08-28 04:39:03 +02:00
constructor(options){
this.client = rethink({
silent: true,
host: options.host || '127.0.0.1',
port: options.port || 28015,
db: options.db || 'haste',
user: options.user || 'admin',
password: options.password || ''
});
}
2020-08-28 04:39:03 +02:00
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);
});
}
2020-08-28 04:39:03 +02:00
get(key, callback){
this.client.table('uploads').get(md5(key)).run((error, result) => {
if (error || !result){
callback(false);
if (error) winston.error('failed to insert to table', error);
return;
}
callback(result.data);
});
}
}
module.exports = RethinkDBStore;