项目作者: opentable

项目描述 :
Mongoose common extensions
高级语言: CoffeeScript
项目地址: git://github.com/opentable/spur-mongoosemanager.git
创建时间: 2015-02-03T02:22:51Z
项目社区:https://github.com/opentable/spur-mongoosemanager

开源协议:MIT License

下载


Spur: Mongoose Manager

A mongoose manager wrapper that provides some common utilities and extends it to support promises.

NPM version
Build Status

Deprecated

This package is no longer going to be maintained. Teams have chosen to use the Mongoose directly because it allows them to customize their implementation as they choose and it’s less prescriptive.

About the Spur Framework

The Spur Framework is a collection of commonly used Node.JS libraries used to create common application types with shared libraries.

Visit NPMJS.org for a full list of Spur Framework libraries >>

Quick start

Installing

  1. $ npm install spur-mongoosemanager --save

Usage

src/injector.js

  1. spur = require "spur-ioc"
  2. spurCommon = require"spur-common"
  3. spurMongoosemanager = require "spur-mongoosemanager"
  4. module.exports = ()->
  5. ioc = spur.create("demo")
  6. # register folders in your project to be autoinjected
  7. ioc.registerFolders __dirname, [
  8. "demo"
  9. ]
  10. ioc.merge(spurCommon())
  11. ioc.merge(spurMongoosemanager())
  12. ioc

demo/DinerModel.coffee

Definition of a MongoDB schemea for a diner.

  1. module.exports = (MongooseSchema, MongooseModel, config, MongooseManager, Moment)->
  2. new class DinerModel extends MongooseModel
  3. constructor:->
  4. super
  5. @_model = @createModel()
  6. createModel:()->
  7. # Define a schema
  8. schema = new MongooseSchema {
  9. _id:
  10. type: Number
  11. required: true
  12. unique: true
  13. index: true
  14. firstName:
  15. type: String
  16. trim: true
  17. lastName:
  18. type: String
  19. trim: true
  20. }
  21. schema.virtual("globalId").get ()->
  22. this._id
  23. schema.set 'toJSON', { virtuals: true }
  24. MongooseManager.model 'Diner', schema
  25. createObject: (globalId, firstName, lastName)->
  26. {
  27. GlobalId: globalId
  28. firstName: firstName
  29. lastName: lastName
  30. }

demo/DinerService.coffee

Service that uses the mongodb diner model created above.

  1. module.exports = (Diner, Promise) ->
  2. new class DinerService
  3. getDiner: (id)->
  4. Diner
  5. .findOneAsync({ _id: id })
  6. .then (diner)->
  7. diner
  8. .error (error)->
  9. Logger.error(error)
  10. {}

start.coffee

  1. injector = require "./src/injector"
  2. injector().inject (MongooseManager)->
  3. # Initiate the connection
  4. MongooseManger.connect()

Contributing

We accept pull requests

Please send in pull requests and they will be reviewed in a timely manner. Please review this generic guide to submitting a good pull requests. The only things we ask in addition are the following:

  • Please submit small pull requests
  • Provide a good description of the changes
  • Code changes must include tests
  • Be nice to each other in comments. :innocent:

Style guide

The majority of the settings are controlled using an EditorConfig configuration file. To use it please download a plugin for your editor of choice.

All tests should pass

To run the test suite, first install the dependancies, then run npm test

  1. $ npm install
  2. $ npm test

License

MIT