then-redis
Advanced tools
Comparing version 0.3.12 to 1.0.0
exports.Client = require('./client'); | ||
exports.ReplyParser = require('./reply-parser'); | ||
@@ -7,9 +6,1 @@ exports.createClient = function (options) { | ||
}; | ||
exports.connect = function (options) { | ||
var client = exports.createClient(options); | ||
return client.connect().then(function () { | ||
return client; | ||
}); | ||
}; |
{ | ||
"name": "then-redis", | ||
"version": "0.3.12", | ||
"version": "1.0.0", | ||
"description": "A small, promise-based Redis client", | ||
"main": "modules/index.js", | ||
"scripts": { | ||
"test": "mocha spec" | ||
"test": "mocha modules/**/__tests__/*-test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/mjijackson/then-redis.git" | ||
"url": "git://github.com/mjackson/then-redis.git" | ||
}, | ||
"dependencies": { | ||
"rsvp": "~3.0.6", | ||
"hiredis": "~0.1.16" | ||
"redis": "^0.12.1", | ||
"when": "^3.6.3" | ||
}, | ||
"devDependencies": { | ||
"mocha": "1.8.1", | ||
"mocha-as-promised": "1.2.1", | ||
"expect": "0.1.1" | ||
"expect": "^1.1.0", | ||
"jshint": "^2.5.10", | ||
"mocha": "^2.0.1" | ||
}, | ||
@@ -22,0 +22,0 @@ "keywords": [ |
176
README.md
@@ -1,134 +0,104 @@ | ||
then-redis | ||
========== | ||
[![npm package](https://img.shields.io/npm/v/then-redis.svg?style=flat-square)](https://www.npmjs.org/package/then-redis) | ||
[![build status](https://img.shields.io/travis/mjackson/then-redis.svg?style=flat-square)](https://travis-ci.org/mjackson/then-redis) | ||
[![dependency status](https://img.shields.io/david/mjackson/then-redis.svg?style=flat-square)](https://david-dm.org/mjackson/then-redis) | ||
[![code climate](https://img.shields.io/codeclimate/github/mjackson/then-redis.svg?style=flat-square)](https://codeclimate.com/github/mjackson/then-redis) | ||
[then-redis](https://github.com/mjijackson/then-redis) is a small, promise-based [Redis](http://redis.io) client for [node.js](http://nodejs.org). It supports all the features of Redis in a simple, user-friendly package. | ||
[then-redis](https://github.com/mjijackson/then-redis) is a fast, promise-based [Redis](http://redis.io) client for [node.js](http://nodejs.org). It's build on top of [node_redis](https://github.com/mranney/node_redis), so it's safe and stable. | ||
The two major differences between then-redis and [node_redis](https://github.com/mranney/node_redis) are: | ||
1. then-redis returns a [CommonJS Promises/A+ promise](http://promises-aplus.github.com/promises-spec/) when you issue a command | ||
2. The entire codebase is very small (~300 LOC), just like Redis | ||
then-redis gets out of your way as much as possible. Command arguments and return values are exactly what you see in [Redis' Command Reference](http://redis.io/commands)*. | ||
then-redis uses [pipelining](http://redis.io/topics/pipelining) to issue all commands. This means that commands are issued over the socket connection as quickly as possible, and that subsequent commands do not need to wait to find out the result of previous commands before they are issued. Of course, if you need to find out the result of a previous command first, just use `then` (see the examples below). | ||
\* `INFO`, `MSET`, `MSETNX`, `HMSET` and `HGETALL` optionally accept/return JavaScript objects for convenience in dealing with Redis' multi-key and hash APIs | ||
### Usage | ||
All of the usage examples assume the following: | ||
To create a client: | ||
var redis = require('then-redis'); | ||
```js | ||
var redis = require('then-redis'); | ||
To create a client: | ||
var db = redis.createClient(); | ||
var db = redis.createClient('tcp://localhost:6379'); | ||
var db = redis.createClient({ | ||
host: 'localhost', | ||
port: 6379, | ||
password: 'password' | ||
}); | ||
``` | ||
var db = redis.createClient(); | ||
var db = redis.createClient('tcp://localhost:6379'); | ||
var db = redis.createClient({ | ||
host: 'localhost', | ||
port: 6379 | ||
}); | ||
Once you have a client, you're ready to issue some commands. All [Redis commands](http://redis.io/commands) are present on the `Client` prototype and may be called with variable length argument lists*. Every command returns a promise for its result. [Pipelining](http://redis.io/topics/pipelining) happens automatically in most normal usage. | ||
If you need to use [AUTH](http://redis.io/commands/auth) or [SELECT](http://redis.io/commands/select) you can include them in the auth segment of your URL or in the `password` and `database` properties of an object literal. | ||
```js | ||
// Simple set, incrby, and get | ||
db.set('my-key', 1); | ||
db.incrby('my-key', 5); | ||
db.get('my-key').then(function (value) { | ||
assert.strictEqual(value, 6); | ||
}); | ||
var db = redis.createClient('tcp://1:password@localhost:6379'); | ||
var db = redis.createClient({ | ||
host: 'localhost', | ||
port: 6379, | ||
database: 1, | ||
password: 'password' | ||
}); | ||
// Multi-key set/get | ||
db.mset({ a: 'one', b: 'two' }); | ||
db.mget('a', 'b').then(function (values) { | ||
assert.deepEqual(values, [ 'one', 'two' ]); | ||
}); | ||
Once you have a client, you're ready to issue some commands. All Redis commands are present on the `redis.Client` prototype and may be called with variable length argument lists. | ||
// Sets | ||
db.sadd('my-set', 1, 2, 3); | ||
db.sismember('my-set', 2).then(function (value) { | ||
assert.strictEqual(value, 1); | ||
}); | ||
// Simple set, incrby, and get | ||
db.set('my-key', 1); | ||
db.incrby('my-key', 5); | ||
db.get('my-key').then(function (value) { | ||
assert.strictEqual(value, 6); | ||
}); | ||
// Hashes | ||
var originalHash = { a: 'one', b: 'two' }; | ||
db.hmset('my-hash', originalHash); | ||
db.hgetall('my-hash').then(function (hash) { | ||
assert.deepEqual(hash, originalHash); | ||
}); | ||
// Multi-key set/get | ||
db.mset({ a: 'one', b: 'two' }); | ||
db.mget('a', 'b').then(function (values) { | ||
assert.deepEqual(values, [ 'one', 'two' ]); | ||
}); | ||
// Transactions | ||
db.multi(); | ||
db.incr('first-key'); | ||
db.incr('second-key'); | ||
db.exec().then(function (reply) { | ||
assert.deepEqual(reply, [ 1, 1 ]); | ||
}); | ||
// Sets | ||
db.sadd('my-set', 1, 2, 3); | ||
db.sismember('my-set', 2).then(function (value) { | ||
assert.strictEqual(value, 1); | ||
}); | ||
// Pubsub | ||
var subscriber = redis.createClient(); | ||
subscriber.on('message', function (channel, message) { | ||
console.log('Received message: ' + message); | ||
}); | ||
subscriber.subscribe('my-channel').then(function () { | ||
db.publish('my-channel', 'a message'); | ||
}); | ||
``` | ||
// Hashes | ||
var originalHash = { a: 'one', b: 'two' }; | ||
db.hmset('my-hash', originalHash); | ||
db.hgetall('my-hash').then(function (hash) { | ||
assert.deepEqual(hash, originalHash); | ||
}); | ||
If you don't like the variable-length argument lists, or you already have an array of arguments that you need to pass to a command, you can always call `client.send()` directly. It takes two arguments: 1) the name of the Redis command and 2) an array of command arguments. | ||
// Transactions | ||
db.multi(); | ||
db.incr('first-key'); | ||
db.incr('second-key'); | ||
db.exec().then(function (reply) { | ||
assert.deepEqual(reply, [ 1, 1 ]); | ||
}); | ||
```js | ||
db.send('get', [ 'my-key' ]); | ||
db.send('incrby', [ 'my-key', 5 ]); | ||
db.send('mset', [ 'a', 'one', 'b', 'two' ]); | ||
``` | ||
// Pubsub | ||
var subscriber = redis.createClient(); | ||
subscriber.on('message', function (channel, message) { | ||
console.log('Received message: ' + message); | ||
}); | ||
subscriber.subscribe('my-channel').then(function () { | ||
db.publish('my-channel', 'a message'); | ||
}); | ||
\* `INFO`, `MSET`, `MSETNX`, `HMSET` and `HGETALL` optionally accept/return JavaScript objects for convenience in dealing with Redis' multi-key and hash APIs | ||
If you don't like the variable-length argument lists, or you already have an array of arguments that you need to pass to a command, you can always call `client.send()` directly. It takes two arguments: 1) the name of the Redis command and 2) an array of command arguments. | ||
### Compatibility | ||
db.send('get', [ 'my-key' ]); | ||
db.send('incrby', [ 'my-key', 5 ]); | ||
db.send('mset', [ 'a', 'one', 'b', 'two' ]); | ||
For best results, it is recommended that you use Redis 2.6 or above. | ||
When you create a client without explicitly calling `client.connect()` it will try to automatically establish a connection the first time you issue a command. While it's waiting for the connection to be established it will buffer all commands and then flush them in the correct order once the socket is open. This works beautifully most of the time (all the specs are written in this style), but it will throw if your connection fails for some reason. | ||
### Installation | ||
To be sure you have a good connection to the database before issuing any commands, call `client.connect()` or use the high-level `redis.connect(options)` method to create a client and connect in one call. Use `then` to wait for the response from Redis before continuing. | ||
Using [npm](https://www.npmjs.org/): | ||
// Create a separate client instance and connect() it. | ||
var db = redis.createClient(options); | ||
db.connect().then(function () { | ||
db.get('my-key'); | ||
}, function (error) { | ||
console.log('Failed to connect to Redis: ' + error); | ||
}); | ||
$ npm install then-redis | ||
// Or use redis.connect() to do both in one call. | ||
redis.connect(options).then(function (db) { | ||
db.get('my-key'); | ||
}, function (error) { | ||
console.log('Failed to connect to Redis: ' + error); | ||
}); | ||
### Issues | ||
The [specs](https://github.com/mjijackson/then-redis/tree/master/spec) also have lots of good usage examples. | ||
Please file issues on the [issue tracker on GitHub](https://github.com/mjackson/then-redis/issues). | ||
### Testing | ||
### Tests | ||
To run the tests: | ||
To run the tests in node, first start a redis server on the default port and host and then: | ||
$ redis-server --port 6379 | ||
$ npm install | ||
$ npm test | ||
### Compatibility | ||
For best results, it is recommended that you use Redis 2.6 | ||
### License | ||
Copyright 2013 Michael Jackson | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non-infringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software. | ||
[MIT](http://opensource.org/licenses/MIT) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11
1
1
8325
142
105
+ Addedredis@^0.12.1
+ Addedwhen@^3.6.3
+ Addedredis@0.12.1(transitive)
+ Addedwhen@3.7.8(transitive)
- Removedhiredis@~0.1.16
- Removedrsvp@~3.0.6
- Removedbindings@1.5.0(transitive)
- Removedfile-uri-to-path@1.0.0(transitive)
- Removedhiredis@0.1.17(transitive)
- Removednan@1.1.2(transitive)
- Removedrsvp@3.0.21(transitive)