• Stars
    star
    171
  • Rank 222,266 (Top 5 %)
  • Language
    JavaScript
  • Created about 14 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Additional handy types for mongoose

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);
});

Tests

To run tests:

make test

Contributors

License

MIT License


Author

Brian Noguchi

More Repositories

1

everyauth

node.js auth package (password, facebook, & more) for Connect and Express apps
JavaScript
3,504
star
2

mongoose-auth

User authentication plugin for mongoose nodejs orm
JavaScript
742
star
3

hooks-js

Augment your methods with pre and post hooks
JavaScript
284
star
4

node-hash-ring

A Consistent Hashing C++ add-on for node.js
C++
139
star
5

redis-node

A Redis client for node.js
JavaScript
132
star
6

Socket.IO-connect

Use Socket.IO-node as middleware in your Connect app
JavaScript
83
star
7

logoot

JavaScript implementation of the Logoot CRDT
JavaScript
23
star
8

node-notify-send

Ubuntu growl-like notifications for node.js
JavaScript
20
star
9

google-refresh-token

Refreshes Google OAuth 2 Access Tokens
JavaScript
16
star
10

class-js

Simple OO Class factory
JavaScript
14
star
11

browser-require

The easiest way to require NPM and CommonJS modules from your browser
JavaScript
13
star
12

node-binary-search

An addon to node.js that provides a binary search function that runs in native C++.
C++
11
star
13

awesome-clojure-spec

A catalog of interesting clojure.spec repos and gists
10
star
14

balanced-go

Balanced API Client for Go
Go
8
star
15

dependency-promise

Add the Deferrable Pattern to Your Dependency Graphs
JavaScript
5
star
16

data-structures-js

Data structures for Javascript
JavaScript
5
star
17

array-promise

Act on asynchronously loaded arrays via forEach, map, etc without the usual, messy callback interface.
JavaScript
2
star
18

bnoguchi.github.com

Github User Page
2
star
19

private-slack-welcome

Slack RTM client that sends a Direct Message welcome message when a new team member joins
Go
1
star