Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] - Add rejson bindings #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ node_modules

*.env
dump.rdb
.idea/
46 changes: 25 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
# Redis Connection
# Redis Connection Manager

A ***Global Redis Connection*** that can be used anywhere in your app
A ***Global Redis Connection Manager*** that can be used anywhere in your app
and closed ***once*** at the end of tests.

[![Build Status](https://travis-ci.org/dwyl/redis-connection.svg)](https://travis-ci.org/dwyl/redis-connection)
[![Code Climate](https://codeclimate.com/github/dwyl/redis-connection/badges/gpa.svg)](https://codeclimate.com/github/dwyl/redis-connection)
[![codecov.io](http://codecov.io/github/dwyl/redis-connection/coverage.svg?branch=master)](http://codecov.io/github/dwyl/redis-connection?branch=master)
[![Dependency Status](https://david-dm.org/dwyl/redis-connection.svg)](https://david-dm.org/dwyl/redis-connection)
[![devDependency Status](https://david-dm.org/dwyl/redis-connection/dev-status.svg)](https://david-dm.org/dwyl/redis-connection#info=devDependencies)
<!-- [![HitCount](https://hitt.herokuapp.com/nelsonic/redis-connection.svg)](https://github.com/dwyl/redis-connection) -->
[![npm](https://img.shields.io/npm/v/redis-connection.svg)](https://www.npmjs.com/package/redis-connection)
[![npm](https://img.shields.io/npm/v/redis-connection-manager.svg)](https://www.npmjs.com/package/redis-connection-manager)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codeslayer1 you've pushed changes on this PR which pertain to your forked package. Please revert these before fixing failing tests. 👍


## Why?

Expand Down Expand Up @@ -54,13 +48,13 @@ and *close once* at the end of your tests.
### Install from NPM

```sh
npm install redis-connection --save
npm install redis-connection-manager --save
```

### Use in your code

```js
var redisClient = require('redis-connection')(); // require & connect
var redisClient = require('redis-connection-manager')(); // require & connect
redisClient.set('hello', 'world');
redisClient.get('hello', function (err, reply) {
console.log('hello', reply.toString()); // hello world
Expand All @@ -73,10 +67,10 @@ You can use the *standard* `redisClient` for *publishing* but
will need to have a *separate* connection to subscribe on.

Create a *Subscriber* connection by supplying the word subscriber
when starting the `redis-connection`:
when starting the `redis-connection-manager`:

```js
var redisSub = require('redis-connection')('subscriber');
var redisSub = require('redis-connection-manager')('subscriber');
redisSub.subscribe("chat:messages:latest", "chat:people:new");
// see: https://github.com/dwyl/hapi-socketio-redis-chat-example ;-)
```
Expand All @@ -86,7 +80,7 @@ redisSub.subscribe("chat:messages:latest", "chat:people:new");
Closing your connections is easy.

```js
var redisClient = require('redis-connection')(); // require & connect
var redisClient = require('redis-connection-manager')(); // require & connect
redisClient.set('hello', 'world');
redisClient.get('hello', function (err, reply) {
console.log('hello', reply.toString()); // hello world
Expand All @@ -102,24 +96,34 @@ you have opened in a single command: `killall()`
e.g:

```js
var redisClient = require('redis-connection')(); // require & connect
var redisSub = require('redis-connection')('subscriber');
var redisClient = require('redis-connection-manager')(); // require & connect
var redisSub = require('redis-connection-manager')('subscriber');

// do things with redisClient and redisSub in your app...
// when you want to close both connections simply call:
require('redis-connection').killall();
require('redis-connection-manager').killall();
```

### Using `redis-connection` with `env2`
### Using `redis-connection-manager` with `env2`

If you are using [**env2**](https://github.com/dwyl/env2) to load your configuration file, simply require `env2` before requiring `redis-connection`:
If you are using [**env2**](https://github.com/dwyl/env2) to load your configuration file, simply require `env2` before requiring `redis-connection-manager`:

```js
require('env2')('.env'); // load the redis URL
var redisClient = require('redis-connection')();
var redisClient = require('redis-connection-manager')();
// now use your redis connection
```

### Using `rejson` with `redis-connection-manager`

If you are using `rejson` module with your Redis build, you can enable rejson commands by setting the ENABLE_REJSON flag before requiring `redis-connection-manager`.

```js
process.env.ENABLE_REJSON = true;
var redisClient = require('redis-connection-manager')();
// now use your redis connection
````

## Need More?

If you need us to support a different Redis-as-a-service provider
Expand All @@ -129,7 +133,7 @@ or want to have more configuration options, please let us know!
## *Contributors*

As with all @dwyl projects
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/dwyl/redis-connection/issues)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/dwyl/redis-connection-manager/issues)

### Environment Variables

Expand Down
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var redis = require('redis');
var url = require('url');
var rejson = require('redis-rejson');

var rc; // redis config
if (process.env.REDISCLOUD_URL) {
Expand All @@ -21,11 +22,16 @@ else {
var CON = {}; // store redis connections as Object

function new_connection () {
var redis_con = redis.createClient(rc.port, rc.host);
if (process.env.REDISCLOUD_URL && rc.auth) { // only auth on CI/Stage/Prod
redis_con.auth(rc.auth); // see: https://git.io/vH3TN
}
return redis_con;
//enable rejson if ENABLE_REJSON flag is set (the flag can have any value, we only check if it is set or not)
if(process.env.ENABLE_REJSON){
rejson(redis);
}

var redis_con = redis.createClient(rc.port, rc.host);
if (process.env.REDISCLOUD_URL && rc.auth) { // only auth on CI/Stage/Prod
redis_con.auth(rc.auth); // see: https://git.io/vH3TN
}
return redis_con;
}

function redis_connection (type) {
Expand Down
Loading