mycro-mongoose
a mongoose.js adapter for mycro
Install
npm install --save mycro-mongoose
Getting Started
First, make sure both the connections
and models
hooks are enabled. (These are enabled by default, however, if you've defined your own /config/hooks.js
file, make sure it includes both of these)
// in /config/hooks.jsmoduleexports = // .. 'connections' 'models' // ..
Then, define one or more mongoose
connections
// in /config/connections.js var mongooseAdapter = ; moduleexports = // .. mongo: adapter: mongooseAdapter config: host: processenvMONGO_HOST || 'localhost' port: processenvMONGO_PORT || 27017 user: processenvMONGO_USER password: processenvMONGO_PASSWORD database: processenvMONGO_DATABASE // ..;
Next, define a mongoose model
// in /app/models/post.js module { let options = collection: 'posts' ; let schema = title: String author: type: SchemaTypesObjectId ref: 'users' body: String comments: body: String date: Date date: type: Date default: Datenow hidden: Boolean meta: votes: Number favs: Number ; return connection;}
Lastly, use it in your app!
// in /app/controllers/post.js moduleexports = { var Posts = reqmycromodels'post'; Posts; }
Config
The connection configuration object is described in more detail below
// in config/connections.jsconst mongooseAdapter = ; moduleexports = // .. mongo: // in order to specify a mongodb connection, use this adapter as the adapter object adapter: mongooseAdapter // all config is specified in the 'config' top level key. the key can be an object // or a synchronous function (executed at runtime) that is passed the mycro instance and expects // a config object in return config: // either a valid mongodb url connection string can be provided url: 'mongodb://sampleuser:correct-horse-batter-staple@localhost:27017/test' // or the following are used to build a valid connection string host: 'localhost' port: 27017 user: 'sampleuser' password: 'correct-horse-batter-staple' database: 'test' // any additional connection options can be specified in an optional 'options' keys options: replSet: sslValidate: false // ..
Testing
running the tests:
- Update the connection info in
test/test-app/config/connections.js
- Run the
mongod
server npm test
to view coverage:
- Update the connection info in
test/test-app/config/connections.js
- Run the
mongod
server grunt coverage
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
License
Copyright (c) 2015 Chris Ludden. Licensed under the MIT license.