Skip to content

Commit

Permalink
Wrote up a README. Added package.json for npm.
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoguchi committed Dec 3, 2010
1 parent 7b91547 commit 141aaa1
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
## mongoose-types - Useful types and type plugins for Mongoose
---

### Types include:
- Counter
- Email
- Url

### Plugins include:
- useTimestamps
Adds `createdAt` and `updatedAt` date attributes that get auto-assigned to the create and most-recently-updated datetime respectively.

### Installation
npm install mongoose-types

### Setup
To include all of the defined types:
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/sampledb");
var mongooseTypes = require("mongoose-types");
mongooseTypes.loadTypes(mongoose);

You can also specify that you only want to load and use a limited subset of the types provided:
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/sampledb");
var mongooseTypes = require("mongoose-types");
// Only load the email and counter types
mongooseTypes.loadTypes(mongoose, "email", "counter");

### Using the types
Once you are setup, you can begin to use the new types.

#### Counter
mongoose.define('NewsItem')
.oid('_id')
.counter('votes');

mongoose.NewsItem.create({votes: 0}, function (err, item) {
console.log(item.votes); // 0
// We have an incr(...) method
item.incr('votes', 1, function (err, item) {
console.log(item.votes); // 1
// We also have an auto-generated incrVotes(...) method - syntactic sugar ftw!
item.incrVotes(5, function (err, item) {
console.log(item.votes); // 6
// What goes up can also come down :)
item.decr('votes', 2, function (err, item) {
console.log(item.votes); // 4
item.decrVotes(3, function (err, item) {
console.log(item.votes); // 1
});
});
});
});
});

#### Email
mongoose.define('User')
.oid('_id')
.email('homeEmail');
.email('workEmail');

#### Url
mongoose.define('Visit')
.oid('_id')
.url('url')
.url('referer');

### Using the plugins

#### The `useTimestamps` plugin

var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/sampledb");
var mongooseTypes = require("mongoose-types")
, useTimestamps = mongooseTypes.useTimestamps;
mongoose.define('User')
.oid('_id')
.string('username')
.plugin(useTimestamps);
mongoose.User.create({username: 'Prince'}, function (err, user) {
console.log(user.createdAt); // Should be approximately now
console.log(user.createdAt === user.updatedAt); // true

// Wait 1 second and then update the user
setTimeout( function () {
user.username = 'Symbol';
user.save( function (err, user) {
console.log(user.updatedAt); // Should be approximately createdAt + 1 second
console.log(user.createdAt < user.updatedAt); // true
});
}, 1000);
});
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{ "name": "mongoose-types"
, "description": "More types for mongoose"
, "version": "0.0.1"
, "author": "Brian Noguchi"
, "dependencies": { "mongoose": "1.0.0"}
, "keywords": [ "mongoose", "mongo", "mongodb", "types" ]
, "scripts": { "test": "make test" }
, "engines": { "node": ">= 0.1.101" }
, "main": "./index"
, "licenses": [ { "type": "The MIT License", "url": "http://www.opensource.org/licenses/mit-license.php" } ]
}

0 comments on commit 141aaa1

Please sign in to comment.