Skip to content

Commit

Permalink
Initial refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
drudge committed Nov 14, 2012
1 parent 544613e commit 99666d1
Show file tree
Hide file tree
Showing 17 changed files with 218 additions and 411 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1 +1,2 @@
**.swp
.DS_Store
node_modules
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

6 changes: 6 additions & 0 deletions .travis.yml
@@ -0,0 +1,6 @@
language: "node_js"
node_js:
- 0.6
- 0.8
services:
- mongodb
20 changes: 20 additions & 0 deletions LICENSE
@@ -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.
11 changes: 0 additions & 11 deletions Makefile

This file was deleted.

166 changes: 64 additions & 102 deletions README.md
@@ -1,103 +1,65 @@
mongoose-types - Useful types and type plugins for Mongoose
==============

### Types include:

- Email
- 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.

#### Email

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 &lt;nick@penree.com&gt;

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.
47 changes: 46 additions & 1 deletion index.js
@@ -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.

Copy link
@Iheartweb

Iheartweb May 4, 2013

Where is this used?

This comment has been minimized.

Copy link
@drudge

drudge May 4, 2013

Author Owner

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;
17 changes: 0 additions & 17 deletions lib/index.js

This file was deleted.

38 changes: 0 additions & 38 deletions lib/plugins/useTimestamps.js

This file was deleted.

19 changes: 0 additions & 19 deletions lib/types/email.js

This file was deleted.

0 comments on commit 99666d1

Please sign in to comment.