commit
56b939124e
2 changed files with 33 additions and 26 deletions
@ -1,39 +1,46 @@ |
|||||||
const crypto = require('crypto'); |
const crypto = require('crypto'); |
||||||
const rethink = require('rethinkdbdash'); |
const rethink = require('rethinkdbdash'); |
||||||
|
const winston = require('winston'); |
||||||
|
|
||||||
var RethinkDBStore = (options) => { |
const md5 = (str) => { |
||||||
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 || '' |
|
||||||
}); |
|
||||||
}; |
|
||||||
|
|
||||||
RethinkDBStore.md5 = (str) => { |
|
||||||
const md5sum = crypto.createHash('md5'); |
const md5sum = crypto.createHash('md5'); |
||||||
md5sum.update(str); |
md5sum.update(str); |
||||||
return md5sum.digest('hex'); |
return md5sum.digest('hex'); |
||||||
}; |
}; |
||||||
|
|
||||||
RethinkDBStore.prototype.set = (key, data, callback) => { |
class RethinkDBStore { |
||||||
try { |
constructor(options) { |
||||||
this.client.table('uploads').insert({ id: RethinkDBStore.md5(key), data: data }).run((error) => { |
this.client = rethink({ |
||||||
if (error) return callback(false); |
silent: true, |
||||||
|
host: options.host || '127.0.0.1', |
||||||
|
port: options.port || 28015, |
||||||
|
db: options.db || 'haste', |
||||||
|
user: options.user || 'admin', |
||||||
|
password: options.password || '' |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
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); |
callback(true); |
||||||
}); |
}); |
||||||
} catch (err) { |
|
||||||
callback(false); |
|
||||||
} |
} |
||||||
}; |
|
||||||
|
|
||||||
RethinkDBStore.prototype.get = (key, callback) => { |
get(key, callback) { |
||||||
this.client.table('uploads').get(RethinkDBStore.md5(key)).run((error, result) => { |
this.client.table('uploads').get(md5(key)).run((error, result) => { |
||||||
if (error || !result) return callback(false); |
if (error || !result) { |
||||||
callback(result.data); |
callback(false); |
||||||
}); |
if (error) winston.error('failed to insert to table', error); |
||||||
}; |
return; |
||||||
|
} |
||||||
|
callback(result.data); |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
module.exports = RethinkDBStore; |
module.exports = RethinkDBStore; |
||||||
|
Loading…
Reference in new issue