forked from bnoguchi/mongoose-types
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrote up a README. Added package.json for npm.
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
- 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 | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } ] | ||
} |