Browse files

Added example to README.

  • Loading branch information...
1 parent 92137c3 commit 871ca39b77131f7a67f069bde80840007a1ced99 @dmcquay committed Oct 13, 2010
Showing with 44 additions and 0 deletions.
  1. +44 −0 README.markdown
View
44 README.markdown
@@ -22,6 +22,50 @@ npm install nosql-thin@latest
- Provide a means to configure connections in one place.
- Provide simple connection pooling.
+#Example
+
+ var Persistable = require('nosql-thin').Persistable,
+ DataProvider = require('nosql-thin').DataProvider,
+ sys = require('sys');
+
+ //you'd likely store this in an external config or at least a separate module
+ var dataCfg = {
+ default: {
+ 'host': 'localhost',
+ 'dbname': 'test'
+ }
+ };
+
+ //you must do this once somewhere in your code before you use DataProvider
+ DataProvider.initCfg(dataCfg);
+
+ var User = function(raw) {
+ raw = raw || {};
+ Persistable.call(this);
+ this.raw = raw;
+ };
+ sys.inherits(User, Persistable);
+
+ //Implement Persistable interface
+ User.prototype.getCollectionName = function() { return 'users' };
+ User.prototype.toObject = function() { return this.raw };
+
+ //Crate a new User
+ var user = new User({name: 'Madonna'});
+
+ //Save the user
+ DataProvider.getInstance().save(user, function(err, user) {
+ if (err) throw new Error(err);
+ sys.puts('user saved!');
+
+ //Find the user
+ DataProvider.getInstance().find(User, {name: 'Madonna'}, function(err, users) {
+ if (err) throw new Error(err);
+ sys.puts('found ' + users.length + ' users');
+ sys.puts('first user\'s name is ' + users[0].raw.name);
+ });
+ });
+
#Status
This is a very new project. I've only used it so far for save and find methods (it doesn't currently

0 comments on commit 871ca39

Please sign in to comment.