Skip to content

Commit

Permalink
Improve schema definition
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Dec 16, 2011
1 parent 8a34212 commit 111b8f0
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 46 deletions.
3 changes: 2 additions & 1 deletion lib/Client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module.exports = class Client
@redis.select options.redis_database if options.redis_database?

define: (options) ->
new Table @, options
name = if typeof options is 'string' then options else options.name
@[name] = new Table @, options

quit: (callback) ->
@redis.quit (err, status) ->
Expand Down
64 changes: 46 additions & 18 deletions lib/Table.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module.exports = class Table
_ron = ron
redis = ron.redis
options = {name: options} if typeof options is 'string'
#@db = ron.name
@name = options.name
@schema =
db: ron.name
Expand All @@ -21,13 +20,38 @@ module.exports = class Table
index: {}
unique: {}
email: {}

###
Define a new property.
Retrieve/define a new property
------------------------------
Define a new property or overwrite the definition of an
existing property. If no schema is provide, return the
property information.
Calling this function with only the property argument will return the schema
information associated with the property.
It is possible to define a new property without any schema information by
providing an empty object.
Example:
User.property 'id', identifier: true
User.property 'username', unique: true
User.property 'email', { index: true, email: true }
User.property 'name', {}
###
property: (property, options) ->
@schema.properties[property] = options ? {}
@indentifier property if property.identifier
property: (property, schema) ->
if schema?
schema ?= {}
schema.name = property
@schema.properties[property] = schema
@identifier property if schema.identifier
@index property if schema.index
@unique property if schema.unique
@email property if schema.email
@
else
@schema.properties[property]

###
Define a property as an identifier
Expand All @@ -43,21 +67,22 @@ module.exports = class Table
@schema.properties[property] = {} unless @schema.properties[property]?
@schema.properties[property].identifier = true
@schema.identifier = property
@
# Get the property
else
@schema.identifier

###
Define a property as indexable
------------------------------
An indexed property allow records access by its property value. For exemple,
Define a property as indexable or return all index properties
-------------------------------------------------------------
An indexed property allow records access by its property value. For example,
when using the `list` function, the search can be filtered such as returned
records match one or multiple values.
Calling this function without any argument will return all the indexed
properties.
Calling this function without any argument will return an array with all the
indexed properties.
Exemple:
Example:
User.index 'email'
User.list { filter: { email: 'my@email.com' } }, (err, users) ->
console.log 'This user has the following accounts:'
Expand All @@ -70,20 +95,21 @@ module.exports = class Table
@schema.properties[property] = {} unless @schema.properties[property]?
@schema.properties[property].index = true
@schema.index[property] = true
@
# Get the property
else
@schema.index
Object.keys(@schema.index)

###
Define a property as unique
---------------------------
An unique property is similar to a unique one but,... unique. In addition for
being filterable, it could also be used as an identifer to access a record.
Calling this function without any argument will return all the unique
properties.
Calling this function without any argument will return an arrya with all the
unique properties.
Exemple:
Example:
User.unique 'username'
User.get { username: 'me' }, (err, user) ->
console.log "This is #{user.username}"
Expand All @@ -94,9 +120,10 @@ module.exports = class Table
@schema.properties[property] = {} unless @schema.properties[property]?
@schema.properties[property].unique = true
@schema.unique[property] = true
@
# Get the property
else
@schema.unique
Object.keys(@schema.unique)

###
Define property as en email
Expand All @@ -106,7 +133,7 @@ module.exports = class Table
Calling this function without any argument will return all the email
properties.
Exemple:
Example:
User.unique 'username'
User.get { username: 'me' }, (err, user) ->
console.log "This is #{user.username}"
Expand All @@ -117,6 +144,7 @@ module.exports = class Table
@schema.properties[property] = {} unless @schema.properties[property]?
@schema.properties[property].email = true
@schema.email[property] = true
@
# Get the property
else
@schema.email
Expand Down
58 changes: 31 additions & 27 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,51 @@
Redis ORM for NodeJs
====================

Ad-lib, be carefull, this project is experimental.
Installation
------------

```bash
npm install ron
```

Usage
-----

```javascript
ron = require('ron');
client = ron({
redis_port: 6379
redis_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', {});
```

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

* Simple & tested API
* Sortable indexes and unique values
* Records are pure object, no extended class, no magic properties
* Code, tests and readme written in coffescript, samples written in javascript

Quick exemple
-------------

ron = require 'ron'
app = ron
host: ''
port: ''
name: 'auth'

User = app.create 'users'
User.property 'id',
identifier: true
# Use a hash index
User.property 'username',
type: 'string'
unique: true
# Use a sorted set index
User.property 'email',
type: 'string'
index: true

Client API
----------

* Client::constructor
* Client::quit
* Client::create
* Client::define

Schema API
----------

* Records::property
* Records::identifier
* Records::unique
* Records::index
* Records::unique
* Records::email

Record API
Expand All @@ -66,9 +66,13 @@ Run tests
---------

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

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


59 changes: 59 additions & 0 deletions test/DefineTest.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

assert = require 'assert'

config = require '../conf/test'
ron = require '../index'

module.exports =
'Define # new schema # string': (next) ->
client = ron config
user = client.define 'user'
assert.ok user instanceof ron.Table
assert.eql user, client.user
client.quit next
'Define # new schema # object': (next) ->
client = ron config
user = client.define name: 'user'
assert.eql user, client.user
assert.ok user instanceof ron.Table
client.quit next
'Define # identifier': (next) ->
client = ron config
user = client.define name: 'user'
assert.eql user.identifier('id'), user
assert.eql 'id', user.identifier()
client.quit next
'Define # index': (next) ->
client = ron config
user = client.define name: 'user'
assert.eql user.identifier('my_id'), user
assert.eql 'my_id', user.identifier()
client.quit next
'Define # index': (next) ->
client = ron config
user = client.define name: 'user'
assert.eql user.index('my_index'), user
assert.eql ['my_index'], user.index()
client.quit next
'Define # unique': (next) ->
client = ron config
user = client.define name: 'user'
assert.eql user.unique('my_unique'), user
assert.eql ['my_unique'], user.unique()
client.quit next
'Define # property': (next) ->
client = ron config
user = client.define name: 'user'
# Define properties
assert.eql user.property('id', identifier: true), user
assert.eql user.property('username', unique: true), user
assert.eql user.property('email', { index: true, email: true }), user
assert.eql user.property('name', {}), user
# Retrieve properties
assert.eql user.property('id'), { name: 'id', identifier: true }
assert.eql user.property('username'), { name: 'username', unique: true }
assert.eql user.property('email'), { name: 'email', index: true, email: true }
assert.eql user.property('name'), { name: 'name' }

client.quit next

0 comments on commit 111b8f0

Please sign in to comment.