Created
September 21, 2019 14:51
-
-
Save 1fabiopereira/cadfc473e78710c4abadde04a39736e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rejson = require('./redis-json-wrapper') | |
const redis = rejson(require('redis')) | |
class RedisClient { | |
constructor ({ host, port, prefix }) { | |
/** | |
* Redis client | |
*/ | |
this.client = redis.createClient({ host, port, prefix }) | |
/** | |
* Redis connect event | |
*/ | |
this.client.on('connect', () => { | |
console.log('🥳 Redis client connected') | |
}) | |
/** | |
* Redis error event | |
*/ | |
this.client.on('error', (err) => { | |
console.log('😭 Something went wrong: ' + err) | |
}) | |
} | |
/** | |
* @method doStuff | |
* @param {Object} data - Incoming | |
* @return {Promise} | |
*/ | |
doStuff (incoming) { | |
return new Promise((resolve) => { | |
try { | |
const { key, name, id } = incoming.user | |
this.client.json_set(`${key}`, '.', '{}', 'NX') | |
this.client.json_set(`${key}`, `.ID_${id}`, JSON.stringify({ name, id }), 'NX') | |
this.client.json_set(`${key}`, `.ID_${id}.foo`, '{}', 'NX') | |
resolve({ status: true, err: null }) | |
} catch (err) { | |
resolve({ status: false, err }) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment