Skip to content

Commit

Permalink
Added support to get every value in the Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonpereyra committed Oct 9, 2019
1 parent 146741c commit 0c5f672
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
13 changes: 11 additions & 2 deletions README.md
Expand Up @@ -49,9 +49,10 @@ Throw an `RedisError` if Redis Server fails.
**async** | Get a value.

* `entity`, type: `string`, main key.
* `id`, type: `string`, sub key.
* `id`, type: `string`, sub key. *OPTIONAL*

Returns an `object`, with the value. If it not exists returns `null`.
With `ìd` - Returns an `object`, with the value. If it not exists returns `null`.
Without `ìd` - Returns an `array`, with the values of the Entity or an empty array (if no values are found)

Throw an `RedisError` if Redis Server fails.

Expand Down Expand Up @@ -114,6 +115,14 @@ itemGetted = await Redis.get('Batman', 'artifact-02');

console.log(itemGetted); // null

itemGetted = await Redis.get('Batman');

console.log(itemGetted); // [{ name: 'BatGun', description: 'deprecated gun' }]

itemGetted = await Redis.get('Joker');

console.log(itemGetted); // []

// DELETE

let deleted = await Redis.del('Batman', 'artifact-01');
Expand Down
8 changes: 4 additions & 4 deletions lib/redis.js
Expand Up @@ -11,7 +11,7 @@ const SETTINGS_KEY = 'redis';
const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = 6379;

const promisfyMethods = ['hget', 'hset', 'hdel'];
const promisfyMethods = ['hget', 'hset', 'hdel', 'hgetall'];

class Redis {

Expand Down Expand Up @@ -69,13 +69,13 @@ class Redis {
* Get a value in Redis
* @async
* @param {string} entity
* @param {string} id
* @param {string} id Optional
* @returns {object} Value Storaged
*/
static async get(entity, id) {
try {
const value = await this.client.hget(entity, id);
return value && JSON.parse(value);
const value = id ? await this.client.hget(entity, id) : await this.client.hgetall(entity);
return id ? value && JSON.parse(value) : value.map(element => JSON.parse(element));
} catch(error) {
throw new RedisError(error, RedisError.codes.GET_PROBLEM);
}
Expand Down
38 changes: 33 additions & 5 deletions tests/redis-test.js
Expand Up @@ -81,7 +81,7 @@ describe('Redis', () => {

describe('Get', () => {

it('Should return null if a profile does not exist', async () => {
it('Should return null if an entity and id does not exist', async () => {

const redisClientStub = {
hget: sandbox.fake.resolves(null)
Expand All @@ -94,7 +94,7 @@ describe('Redis', () => {
assert.deepStrictEqual(value, null);
});

it('Should return the object if a profile is found', async () => {
it('Should return the object if an entity and id is found', async () => {

const redisClientStub = {
hget: sandbox.fake.resolves(JSON.stringify(sampleValue))
Expand All @@ -107,21 +107,49 @@ describe('Redis', () => {
assert.deepStrictEqual(value, sampleValue);
});

it('Should return an array of objects if an entity is found and no id is passed', async () => {

const redisClientStub = {
hgetall: sandbox.fake.resolves([JSON.stringify(sampleValue)])
};

sandbox.stub(Redis, 'client').get(() => redisClientStub);

const value = await Redis.get(sampleEntity);

assert.deepStrictEqual(value, [sampleValue]);
});

it('Should return an empty array of objects if an entity is not found and no id is passed', async () => {

const redisClientStub = {
hgetall: sandbox.fake.resolves([])
};

sandbox.stub(Redis, 'client').get(() => redisClientStub);

const value = await Redis.get(sampleEntity);

assert.deepStrictEqual(value, []);
});

it('Should reject if can not get', async () => {
const redisClientStub = {
hget: sandbox.fake.rejects()
hget: sandbox.fake.rejects(),
hgetall: sandbox.fake.rejects()
};

sandbox.stub(Redis, 'client').get(() => redisClientStub);

await assert.rejects(Redis.get(sampleEntity, sampleId), { code: RedisError.codes.GET_PROBLEM });
await assert.rejects(Redis.get(sampleEntity), { code: RedisError.codes.GET_PROBLEM });

});
});

describe('Delete', () => {

it('Should return 1 if a profile is deleted', async () => {
it('Should return 1 if an entity and id is deleted', async () => {

const redisClientStub = {
hdel: sandbox.fake.resolves(1)
Expand All @@ -134,7 +162,7 @@ describe('Redis', () => {
assert.deepStrictEqual(value, 1);
});

it('Should return 0 if a profile does not exist', async () => {
it('Should return 0 if an entity and id does not exist', async () => {

const redisClientStub = {
hdel: sandbox.fake.resolves(0)
Expand Down

0 comments on commit 0c5f672

Please sign in to comment.