Code Monkey home page Code Monkey logo

mongoose-types's Introduction

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

mongoose-types's People

Contributors

bnoguchi avatar turbo87 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.