Skip to content

Instantly share code, notes, and snippets.

@1fabiopereira
Created September 21, 2019 14:51
Show Gist options
  • Save 1fabiopereira/cadfc473e78710c4abadde04a39736e6 to your computer and use it in GitHub Desktop.
Save 1fabiopereira/cadfc473e78710c4abadde04a39736e6 to your computer and use it in GitHub Desktop.
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