Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
218 additions
and
411 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
**.swp | ||
.DS_Store | ||
node_modules |
This file was deleted.
Oops, something went wrong.
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,6 @@ | ||
language: "node_js" | ||
node_js: | ||
- 0.6 | ||
- 0.8 | ||
services: | ||
- mongodb |
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,20 @@ | ||
copyright (c) 2012 Nicholas Penree <nick@penree.com> | ||
Original work: copyright (c) 2012 Brian Noguchi | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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 |
---|---|---|
@@ -1,103 +1,65 @@ | ||
mongoose-types - Useful types and type plugins for Mongoose | ||
============== | ||
|
||
### Types include: | ||
|
||
- Url | ||
|
||
### Plugins include: | ||
|
||
- useTimestamps | ||
Adds `createdAt` and `updatedAt` date attributes that get auto-assigned to the most recent create/update timestamp. | ||
|
||
### Installation | ||
npm install mongoose-types | ||
|
||
### Setup | ||
|
||
To include all of the defined types: | ||
|
||
var mongoose = require("mongoose"); | ||
var db = mongoose.createConnection("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"); | ||
var db = mongoose.createConnection("mongodb://localhost/sampledb"); | ||
var mongooseTypes = require("mongoose-types"); | ||
// Only load the email type | ||
mongooseTypes.loadTypes(mongoose, "email"); | ||
|
||
### Using the types | ||
|
||
Once you are setup, you can begin to use the new types. | ||
|
||
|
||
var Email = mongoose.SchemaTypes.Email; | ||
var UserSchema = new Schema({ | ||
email: { | ||
work: Email | ||
, home: Email | ||
} | ||
}); | ||
|
||
#### Url | ||
|
||
var Url = mongoose.SchemaTypes.Url; | ||
var VisitSchema = new Schema({ | ||
url: Url | ||
, referer: Url | ||
}); | ||
|
||
### Using the plugins | ||
|
||
#### The `useTimestamps` plugin | ||
|
||
var mongoose = require("mongoose"); | ||
var db = mongoose.createConnection("mongodb://localhost/sampledb"); | ||
var mongooseTypes = require("mongoose-types") | ||
, useTimestamps = mongooseTypes.useTimestamps; | ||
var UserSchema = new Schema({ | ||
username: String | ||
}); | ||
UserSchema.plugin(useTimestamps); | ||
mongoose.model('User', UserSchema); | ||
var User = db.model('User', UserSchema); | ||
|
||
var user = new User({username: 'Prince'}); | ||
user.save(function (err) { | ||
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) { | ||
console.log(user.updatedAt); // Should be approximately createdAt + 1 second | ||
console.log(user.createdAt < user.updatedAt); // true | ||
}); | ||
}, 1000); | ||
Mongoose Timestamps Plugin | ||
========================== | ||
|
||
Simple plugin for [Mongoose](https://github.com/LearnBoost/mongoose) which adds `createdAt` and `updatedAt` date attributes | ||
that get auto-assigned to the most recent create/update timestamp. | ||
|
||
## Installation | ||
|
||
`npm install mongoose-timestamps` | ||
|
||
## Usage | ||
|
||
```javascript | ||
var timestamps = require('mongoose-timestamps'); | ||
var UserSchema = new Schema({ | ||
username: String | ||
}); | ||
UserSchema.plugin(timestamps); | ||
mongoose.model('User', UserSchema); | ||
var User = mongoose.model('User', UserSchema) | ||
``` | ||
The User model will now have `createdAt` and `updatedAt` properties, which get | ||
automatically generated and updated when you save your document. | ||
|
||
```javascript | ||
var user = new User({username: 'Prince'}); | ||
user.save(function (err) { | ||
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) { | ||
console.log(user.updatedAt); // Should be approximately createdAt + 1 second | ||
console.log(user.createdAt < user.updatedAt); // true | ||
}); | ||
|
||
## Tests | ||
|
||
To run tests: | ||
|
||
make test | ||
|
||
### Contributors | ||
|
||
- [Brian Noguchi](https://github.com/bnoguchi) | ||
|
||
### License | ||
|
||
MIT License | ||
|
||
--- | ||
### Author | ||
|
||
Brian Noguchi | ||
}, 1000); | ||
}); | ||
``` | ||
## License | ||
|
||
(The MIT License) | ||
|
||
Copyright (c) 2012 Nicholas Penree <nick@penree.com> | ||
|
||
Based on [mongoose-types](https://github.com/bnoguchi/mongoose-types): Copyright (c) 2012 [Brian Noguchi](https://github.com/bnoguchi) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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 |
---|---|---|
@@ -1 +1,46 @@ | ||
module.exports = require("./lib"); | ||
/*! | ||
* Mongoose Timestamps Plugin | ||
* Copyright(c) 2012 Nicholas Penree <nick@penree.com> | ||
* Original work Copyright(c) 2012 Brian Noguchi | ||
* MIT Licensed | ||
*/ | ||
|
||
var mongoose = require('mongoose'); | ||
var BinaryParser = require('mongoose/node_modules/mongodb/node_modules/bson').BinaryParser; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
function timestampsPlugin(schema, options) { | ||
if (schema.path('_id')) { | ||
schema.add({ | ||
updatedAt: Date | ||
}); | ||
schema.virtual('createdAt') | ||
.get( function () { | ||
if (this._createdAt) return this._createdAt; | ||
var unixtime = BinaryParser.decodeInt(this._id.id.slice(0, 4), 32, true, true); | ||
return this._createdAt = new Date(unixtime * 1000); | ||
}); | ||
schema.pre('save', function (next) { | ||
if (this.isNew) { | ||
this.updatedAt = this.createdAt; | ||
} else { | ||
this.updatedAt = new Date; | ||
} | ||
next(); | ||
}); | ||
} else { | ||
schema.add({ | ||
createdAt: Date | ||
, updatedAt: Date | ||
}); | ||
schema.pre('save', function (next) { | ||
if (!this.createdAt) { | ||
this.createdAt = this.updatedAt = new Date; | ||
} else { | ||
this.updatedAt = new Date; | ||
} | ||
next(); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = timestampsPlugin; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Where is this used?