Skip to content

Commit

Permalink
Rename redis_port and redis_host to port and host; Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Mar 10, 2012
1 parent ee2c4b0 commit 1103b7e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 47 deletions.
4 changes: 2 additions & 2 deletions conf/test.coffee.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

module.exports =
redis_port: 4501
redis_host: 'localhost'
port: 4501
host: 'localhost'
name: 'test'
18 changes: 9 additions & 9 deletions lib/Client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ module.exports = class Client
* `name` A namespace for the application, all keys with be prefixed with "#{name}:". Default to "ron"
* `redis` Provide an existing instance in case you don't want a new one to be created.
* `redis_host` Redis database hostname.
* `redis_port` Redis database port.
* `redis_password` Redis databse password.
* `redis_database` Integer defining the redis database.
* `host` Redis database hostname.
* `port` Redis database port.
* `password` Redis databse password.
* `database` Integer defining the redis database.
Basic example:
ron = require 'ron'
client = ron
redis_host: '127.0.0.1'
redis_port: 6379
host: '127.0.0.1'
port: 6379
###
constructor: (options = {}) ->
Expand All @@ -43,9 +43,9 @@ module.exports = class Client
if @options.redis
@redis = @options.redis
else
@redis = redis.createClient options.redis_port ? 6379, options.redis_host ? '127.0.0.1'
@redis.auth options.redis_password if options.redis_password?
@redis.select options.redis_database if options.redis_database?
@redis = redis.createClient options.port ? 6379, options.host ? '127.0.0.1'
@redis.auth options.password if options.password?
@redis.select options.database if options.database?
###
`get(schema)` Records definition and access
Expand Down
64 changes: 44 additions & 20 deletions lib/Records.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ module.exports = class Records extends Schema
`options` Options properties include:
* `properties` Array of properties to be returned.
* `identifiers` Return only the created identifiers instead of the records.
* `validate` Validate the records.
* `properties` Array of properties to be returned.
* `milliseconds` Convert date value to milliseconds timestamps instead of `Date` objects.
* `seconds` Convert date value to seconds timestamps instead of `Date` objects.
`callback` Called on success or failure. Received parameters are:
Expand Down Expand Up @@ -316,18 +318,19 @@ module.exports = class Records extends Schema
@unserialize records
callback null, if isArray then records else records[0]
###
id(records, [options], callback)
--------------------------------
Extract record identifiers or set the identifier to null if its associated record could not be found.
Create or extract one or several ids.
-------------------------------------
The method doesn't hit the database to check the existance of an id if it is already provided.
Set the provided object to null if an id couldn't be found.
The method doesn't hit the database to validate record values and if an id is
provided, it wont check its existence. When a record has no identifier but a unique value, then its
identifier will be fetched from Redis.
todo: With no argument, generate an new id
todo: IF first argument is a number, genererate the number of new id
If first argument is an object or an array of object, extract the id from those objects
Options:
- object: return record objects instead of ids
- accept_null: prevent error throwing if record is null
The id will be set to null if the record wasn't discovered in the database
`records` Record object or array of record objects.
Expand All @@ -337,16 +340,26 @@ module.exports = class Records extends Schema
* `accept_null` Skip objects if they are provided as null.
* `object` Return an object in the callback even if it recieve an id instead of a record.
Provisionning 2 identifiers:
Use reverse index lookup to extract user ids:
users = [
Users.get 'users', properties:
user_id: identifier: true
username: unique: true
Users.id [
{username: 'username_1'}
{username: 'username_2'}
]
], (err, ids) ->
should.not.exist err
console.log ids
Use the `object` option to return records instead of ids:
Users.get 'users', properties:
user_id: identifier: true
username: unique: true
Users.id users, (err, users) ->
Users.id [
1, {user_id: 2} ,{username: 'username_3'}
], object: true, (err, users) ->
should.not.exist err
ids = for user in users then user.user_id
console.log ids
Expand All @@ -370,10 +383,6 @@ module.exports = class Records extends Schema
return callback new Error 'Null record'
else if record[identifier]?
# It's perfect, no need to hit redis
else if record.username? #todo
cmds.push ['hget', "#{db}:#{name}_username", record.username, ((record) -> (err, recordId) ->
record[identifier] = recordId
)(record)]
else
withUnique = false
for property of unique
Expand Down Expand Up @@ -405,9 +414,24 @@ module.exports = class Records extends Schema
callback null, if isArray then records else records[0]
###
`list([options], callback)` List records
--------------------------------------
List records with filtering and sorting.
`list([options], callback)`
---------------------------
List records with support for filtering and sorting.
`options` Options properties include:
* `where`
* `operation`
* `sort`
* `direction`
* `properties` Array of properties to be returned.
* `milliseconds` Convert date value to milliseconds timestamps instead of `Date` objects.
* `seconds` Convert date value to seconds timestamps instead of `Date` objects.
`callback` Called on success or failure. Received parameters are:
* `err` Error object if any.
* `records` Records fetched from Redis.
Using the `union` operation:
Expand Down Expand Up @@ -486,7 +510,7 @@ module.exports = class Records extends Schema
record = {}
for property, j in keys
record[property] = values[i + j]
@unserialize record
@unserialize record, options
callback null, result
# Run command
multi.sort args...
Expand Down
46 changes: 31 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,33 @@ Usage

```javascript
ron = require('ron');
// Client connection
client = ron({
redis_port: 6379
redis_host: '127.0.0.1'
port: 6379
host: '127.0.0.1'
name: 'auth'
});
users = client.define('users');
users.property('id', {identifier: true});
users.property('username', {unique: true});
users.property('email', {index: true, email: true});
users.property('name', {});
// Schema definition
Users = client.get('users');
Users.property('id', {identifier: true});
Users.property('username', {unique: true});
Users.property('email', {index: true, email: true});
Users.property('name', {});
// Record manipulation
Users.create(
{username: 'ron', email: 'ron@domain.com'},
function(err, user){
console.log(err, user.id);
}
)
```

The library provide
-------------------

* Simple & tested API
* Sortable indexes and unique values
* Records are pure object, no extended class, no magic properties
* Documented and tested API
* Records access with indexes and unique values
* Records are pure object, no extended class, no magic

Client API
----------
Expand All @@ -44,16 +53,23 @@ Client API
Schema API
----------

* Records::property
* Records::email
* Records::hash
* Records::identifier
* Records::index
* Records::property
* Records::name
* Records::serialize
* Records::temporal
* Records::unique
* Records::email
* Records::unserialize
* Records::validate

Record API
----------

* Records::all
* Records::clear
* Records::count
* Records::create
* Records::exists
Expand All @@ -66,14 +82,14 @@ Record API
Run tests
---------

Start a redis server (tested against version 2.9.0) on the default port
Start a redis server on the default port
```bash
redis-server ./conf/redis.conf
```

Run the test suite with *expresso*:
Run the tests with mocha:
```bash
expresso -s
make test
```


15 changes: 14 additions & 1 deletion test/list.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe 'list', ->
users[2].username.should.eql 'username_1'
Users.clear next

it 'Test list # where inter, same property', (next) ->
it 'should honor inter operation with a property having the same values', (next) ->
Users.create [
{ username: 'username_1', email: '1@email.com', password: 'my_password', name: 'name_1' }
{ username: 'username_2', email: '2@email.com', password: 'my_password', name: 'name_2' }
Expand All @@ -83,3 +83,16 @@ describe 'list', ->
users.length.should.eql 1
users[0].username.should.eql 'username_3'
Users.clear next

it 'should return only selected properties', (next) ->
Users.create [
{ username: 'username_1', email: '1@email.com', password: 'my_password', name: 'name_1' }
{ username: 'username_2', email: '2@email.com', password: 'my_password', name: 'name_2' }
], (err, users) ->
Users.list { properties: ['username'], sort: 'username', direction: 'desc' }, (err, users) ->
should.not.exist err
users.length.should.eql 2
for user in users then Object.keys(user).should.eql ['username']
users[0].username.should.eql 'username_2'
users[1].username.should.eql 'username_1'
Users.clear next

0 comments on commit 1103b7e

Please sign in to comment.