An hapi plugin to handle Mongoose handshake and initial setup.
npm install --save hapi-mongoose
- Mongoose
npm install --save mongoose
const options = {
promises: 'native',
uri: 'mongodb://localhost:27017'
};
const server = new Hapi.Server();
await server.register({
plugin: require('hapi-mongoose'),
options: options
});
const db = server.plugins['hapi-mongoose'].connection;
const mongoose = server.plugins['hapi-mongoose'].lib;const db = server.plugins['hapi-mongoose'].connection; // Get the current connection for this server instance
const mongoose = server.plugins['hapi-mongoose'].lib;
const Schema = mongoose.Schema;
const tankSchema = new Schema({
// Tank properties
});
const Tank = db.model('Tank', tankSchema);
const small = new Tank({ size: 'small' });
small.save(function (err) {
if (err) return handleError(err);
// Saved!
});It is important to use server.plugins['hapi-mongoose'].lib instead of require('mongoose') due to this issue.
promises- Choose your promises implementation. Valid string options arebluebird,native(ores6). Any other value will result in the use of Mongoose's built-inmpromise(read more).uri- A MongoDB connection string.mongooseOptions- A JavaScript object with Mongoose connection options.