Created
August 9, 2017 21:53
-
-
Save cklanac/1839ccab9adde2c9d2f238c704f171f8 to your computer and use it in GitHub Desktop.
Experiments with Mongoose Buffering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* Experiments with [Mongoose Buffering](http://mongoosejs.com/docs/connections.html#buffering) | |
* | |
* Also handle: | |
* "DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` | |
* instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. | |
* See http://mongoosejs.com/docs/connections.html#use-mongo-client" | |
* v4.10.8 and lower: | |
* - useMongoClient NOT required | |
* v4.11 | |
* - pass `useMongoClient: true` in connection options | |
* | |
*/ | |
var mongoose = require('mongoose'); | |
mongoose.Promise = global.Promise; | |
console.log('start connection'); | |
mongoose.connect('mongodb://localhost/scratchpad', { | |
useMongoClient: true | |
}).then(function () { | |
console.log('database connected'); | |
}); | |
console.log('create model'); | |
var Cat = mongoose.model('Cat', { name: String }); | |
console.log('create new document'); | |
var kitty = new Cat({ name: 'Zildjian' }); | |
console.log('document save'); | |
kitty.save() | |
.then((res) => { | |
console.log('document saved'); | |
console.log(res); | |
}) | |
.catch((err) => { | |
console.log('document error'); | |
console.error(err); | |
}); | |
function gracefulShutdown() { | |
mongoose.connection.close(function () { | |
console.log('Mongoose default connection disconnected through app termination'); | |
process.exit(0); | |
}); | |
} | |
process.on('SIGINT', gracefulShutdown); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment