Skip to content

Commit

Permalink
Merge branch 'master' into pubsub-support
Browse files Browse the repository at this point in the history
Conflicts:
	lib/nohm.js
	test/tests.js
  • Loading branch information
maritz committed Jan 23, 2012
2 parents fa1469d + 7b35536 commit 90955b4
Show file tree
Hide file tree
Showing 13 changed files with 2,148 additions and 1,590 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
@@ -1,3 +1,9 @@
### v0.7.2 - 2012-01-19
- add .sort()

### v0.7.1
- Fix unique handling bugs

### v0.7.0
- BREAKS BACKWARDS COMPATIBILTY! change validate() to be async only (validations all need to be async now), also changed validation usage syntax (see docs)
- add nohm.connect() connect middleware that delivers browser validation js
Expand Down
90 changes: 86 additions & 4 deletions README.md
Expand Up @@ -4,10 +4,11 @@

Nohm is an object relational mapper (ORM) written for node.js and redis.

## Install
### If you haven't done this yet: install npm
## Requirements

* redis >= 2.4

curl http://npmjs.org/install.sh | sh
## Install

### Installing nohm

Expand All @@ -18,7 +19,88 @@ http://maritz.github.com/nohm/

## Examples

* [nohm/examples/rest-user-server](https://github.com/maritz/nohm/tree/master/examples/rest-user-server) very basic user example (needs express)
~~~~ javascript
var nohm = require('nohm').Nohm;
var redis = require('redis').createClient();

nohm.setClient(redis);

nohm.model('User', {
properties: {
name: {
type: 'string',
unique: true,
validations: [
'notEmpty'
]
},
email: {
type: 'string',
unique: true,
validations: [
'email'
]
},
country: {
type: 'string',
defaultValue: 'Tibet',
validations: [
'notEmpty'
]
},
visits: {
type: function incrVisitsBy(value, key, old) {
return old + value;
},
defaultValue: 0,
index: true
}
},
methods: {
getContryFlag: function () {
return 'http://example.com/flag_'+this.p('country')+'.png';
},
}
});

var user = nohm.factory('User');
user.p({
name: 'Mark',
email: 'mark@example.com',
country: 'Mexico',
visits: 1
});
user.save(function (err) {
if (err === 'invalid') {
console.log('properties were invalid: ', user.errors);
} else if (err) {
console.log(err); // database or unknown error
} else {
console.log('saved user! :-)');
user.remove(function (err) {
if (err) {
console.log(err); // database or unknown error
} else {
console.log('successfully removed user');
}
});
}
});

// try to load a user from the db
var otherUser = nohm.factory('User', 522, function (err) {
if (err === 'not found') {
console.log('no user with id 522 found :-(');
} else if (err) {
console.log(err); // database or unknown error
} else {
console.log(otherUser.allProperties());
}
});
~~~~


* [nohm/examples/rest-user-server](https://github.com/maritz/nohm/tree/master/examples/rest-user-server) (needs express)
* [Beauvoir](https://github.com/yuchi/Beauvoir) Simple project management app - by yuchi

Do you have code that should/could be listed here? Message me!
Expand Down
54 changes: 45 additions & 9 deletions lib/nohm.js
@@ -1,4 +1,5 @@
var h = require(__dirname + '/helpers');
var async = require('async');

/**
* The Nohm object used for some general configuration and model creation.
Expand Down Expand Up @@ -59,10 +60,11 @@ Nohm.model = function (name, options) {
obj.prototype = new Nohm();

// this creates a few functions for short-form like: SomeModel.load(1, function (err, props) { /* `this` is someModelInstance here */ });
Nohm.shortFormFuncs.forEach(function (val) {
var shortFormFuncs = ['load', 'find', 'save', 'sort', 'subscribe', 'subscribeOnce', 'unsubscribe'];
shortFormFuncs.forEach(function (val) {
obj[val] = function () {
var instance = new obj();
instance[val].apply(instance, arguments);
instance[val].apply(instance, Array.prototype.slice.call(arguments, 0));
};
});

Expand All @@ -78,8 +80,6 @@ Nohm.model = function (name, options) {
return obj;
};

Nohm.shortFormFuncs = ['load', 'find', 'save'];

/**
* Factory to produce instances of models
*
Expand Down Expand Up @@ -157,11 +157,13 @@ Nohm.setExtraValidations = function (files) {
files = [files];
}
files.forEach(function (path) {
__extraValidators.push(path);
var validators = require(path);
Object.keys(validators).forEach(function (name) {
Nohm.__validators[name] = validators[name];
});
if (__extraValidators.indexOf(path) === -1) {
__extraValidators.push(path);
var validators = require(path);
Object.keys(validators).forEach(function (name) {
Nohm.__validators[name] = validators[name];
});
}
});
};

Expand Down Expand Up @@ -255,6 +257,40 @@ Nohm.prototype.init = function (name, options) {
this.__loaded = false;
};

/**
* DO NOT USE THIS UNLESS YOU ARE ABSOLUTELY SURE ABOUT IT!
*
* Deletes any keys from the db that start with nohm prefixes.
*
* DO NOT USE THIS UNLESS YOU ARE ABSOLUTELY SURE ABOUT IT!
*
* @param {Object} [redis] You can specify the redis client to use. Default: Nohm.client
* @param {Function} [callback] Called after all keys are deleted.
*/
Nohm.purgeDb = function (redis, callback) {
callback = h.getCallback(arguments);
redis = typeof(redis) !== 'function' || Nohm.client;
var delKeys = function (prefix, next) {
redis.keys(prefix+'*', function (err, keys) {
if (err || keys.length === 0) {
next(err);
} else {
keys.push(next);
redis.del.apply(redis, keys);
}
});
};
var deletes = [];

Object.keys(Nohm.prefix).forEach(function (key) {
deletes.push(async.apply(delKeys, Nohm.prefix[key]));
});

async.series(deletes, function (err) {
callback(err);
});
};

var moduleNames = ['properties', 'retrieve', 'validation', 'store', 'relations', 'connectMiddleware', 'pubsub'],
modules = {};

Expand Down
4 changes: 0 additions & 4 deletions lib/pubsub.js
Expand Up @@ -231,9 +231,5 @@ var initialize = function () {
});
};

Nohm.shortFormFuncs.push(
'subscribe', 'subscribeOnce', 'unsubscribe', 'unsubscribeAll'
);

};

0 comments on commit 90955b4

Please sign in to comment.