hapi-mongoose2
Mongoose plugin for hapi-based servers. Supports connecting to one or multiple databases and look up and register models by connection. The plugin options are:
options
- aconnection
or an array ofconnection
s where:connection
- an object containing:uri
- a mongo uri stringalias
- (optional) a database name alias used to namespace connections when multiple are created. otherwise ignored.loadSchemasFrom
- (optional) one of:- An array of globs from where schemas will be loaded. matching files must export a
mongoose.Schema
object or a function with the signatureasync function(server)
returning a schema. - An object containing
Mongoose.Schema
elements.
- An array of globs from where schemas will be loaded. matching files must export a
options
- (optional) options passed tomongoose
createConnection method. unknown properties are allowed:auth
- an object with auth credentialsuser
password
autoIndex
bufferCommands
connections
- an array ofconnection
objects as described above.decorations
- (optional) an array of interfaces to be decorated usingserver.decorate
method. allowed values areserver
,request
.
Connection and models are accessible under the server.app.mongo
property. When multiple connections are created the database name or alias
is used as namespace for accessing each database properties. Same applies for decorated interfaces.
Models are named as the filename matching the schema pattern. Model name first letter is capitalized by default. e.g. Animal
.
Example
const plugin = plugin: options: connections: uri: 'mongodb://localhost:27017/myapp' alias: 'safebox' uri: 'mongodb://localhost:27017/secrets' loadSchemasFrom: 'src/schemas' '!.{md,json}' options: auth: user: 'admin' password: 'pa55w0rd' autoIndex: false bufferCommands: true decorations: 'request' 'server' ; const server = ;await server; // Using database `secrets` from:// 1 - `server.app` object// 2 - `request` decorated object// 3 - `server` decorated object const Admin = serverappmongosafeboxmodels;await Admin; server; await servermongosafeboxconnection;
Dependencies
mongoose
needs to be installed alongside this plugin in order to work.