hapi-mongo-models
Advanced tools
Comparing version 2.0.2 to 2.1.0
54
index.js
var Path = require('path'); | ||
var Hoek = require('hoek'); | ||
var BaseModel = require('./lib/base-model'); | ||
exports.BaseModel = BaseModel; | ||
exports.register = function (server, options, next) { | ||
Hoek.assert(options.mongodb, 'options.mongodb is required'); | ||
var models = options.models || {}; | ||
var mongodb = options.mongodb; | ||
var autoIndex = options.hasOwnProperty('autoIndex') ? options.autoIndex : true; | ||
var addModel = function (key, model) { | ||
Hoek.assert( | ||
model.prototype instanceof BaseModel.constructor, | ||
'Model [' + key + '] must be extended from BaseModel.' | ||
); | ||
models[key] = model; | ||
server.expose(key, model); | ||
}; | ||
Object.keys(models).forEach(function (key) { | ||
models[key] = require(Path.join(process.cwd(), models[key])); | ||
var modelPath = models[key]; | ||
if (modelPath !== Path.resolve(modelPath)) { | ||
modelPath = Path.join(process.cwd(), modelPath); | ||
} | ||
addModel(key, require(modelPath)); | ||
}); | ||
server.expose('addModel', addModel); | ||
server.expose('BaseModel', BaseModel); | ||
server.after(function (server, done) { | ||
if (autoIndex) { | ||
Object.keys(models).forEach(function (key) { | ||
models[key].ensureIndexes(); | ||
}); | ||
} | ||
done(); | ||
}); | ||
BaseModel.connect(mongodb, function (err, db) { | ||
@@ -23,13 +61,2 @@ | ||
Object.keys(models).forEach(function (key) { | ||
if (autoIndex) { | ||
models[key].ensureIndexes(); | ||
} | ||
server.expose(key, models[key]); | ||
}); | ||
server.expose('BaseModel', BaseModel); | ||
next(); | ||
@@ -40,7 +67,4 @@ }); | ||
exports.BaseModel = BaseModel; | ||
exports.register.attributes = { | ||
pkg: require('./package.json') | ||
}; |
{ | ||
"name": "hapi-mongo-models", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "MongoDB object models for hapi applications", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -131,9 +131,41 @@ # hapi-mongo-models | ||
- `mongodb` - is an object where: | ||
- `url` - a string representing the connection url for MongoDB. | ||
- `options` - an optional object passed to MongoDB's native connect function. | ||
- `autoIndex` - a boolean specifying if the plugin should call `ensureIndex` for each | ||
model. Defaults to `true`. Typically set to `false` in production environments. | ||
- `models` - an object where each key is the exposed model name and each value is the | ||
path (relative to the current working directory) of where to find the model on disk. | ||
- `url` - a string representing the connection url for MongoDB. | ||
- `options` - an optional object passed to MongoDB's native connect function. | ||
- `autoIndex` - a boolean specifying if the plugin should call `ensureIndexes` | ||
for each model. Defaults to `true`. Typically set to `false` in production | ||
environments. | ||
- `models` - an object where each key is the exposed model name and each value | ||
is the path (relative to the current working directory or absolute) of where | ||
to find the model on disk. | ||
### Usage in other plugins | ||
You can depend on `hapi-mongo-models` inside other plugins. This allows you to | ||
access models that were defined in the plugin config and add models | ||
dynamically. | ||
For example, in a plugin you author: | ||
```js | ||
var DynamoKitty = require('./models/dynamo-kitty'); | ||
exports.register = function (server, options, next) { | ||
var addModel = server.plugins['hapi-mongo-models'].addModel; | ||
addModel('DynamoKitty', DynamoKitty); | ||
next(); | ||
}; | ||
exports.register.attributes = { | ||
name: 'dynamo', | ||
version: '1.0.0', | ||
dependencies: ['hapi-mongo-models'] | ||
}; | ||
``` | ||
The `addModel` method is a function with the signature `function (key, model)` | ||
where: | ||
- `key` - is a string representing the name that will be exported. | ||
- `model` - is a model class created by using `BaseModel.extend(...)`. | ||
### Example | ||
@@ -187,3 +219,3 @@ | ||
## API | ||
## Model API | ||
@@ -289,3 +321,4 @@ ### Constructor | ||
Loops over the static `indexes` array property of a model class calling | ||
`ensureIndex`. | ||
`ensureIndex`. The server plugin calls this method for each model after the | ||
server has started. | ||
@@ -376,3 +409,3 @@ Indexes are defined as a static property on your models like: | ||
// { name: true, 'email': true } | ||
// { name: true, email: true } | ||
``` | ||
@@ -379,0 +412,0 @@ |
@@ -0,1 +1,2 @@ | ||
var Path = require('path'); | ||
var Lab = require('lab'); | ||
@@ -6,2 +7,3 @@ var Code = require('code'); | ||
var Config = require('./config'); | ||
var DummyPlugin = require('./fixtures/dummy-plugin'); | ||
@@ -20,3 +22,3 @@ | ||
lab.test('it returns and error when the db connection fails', function (done) { | ||
lab.test('it returns an error when the db connection fails', function (done) { | ||
@@ -30,4 +32,9 @@ var realConnect = stub.BaseModel.connect; | ||
var server = new Hapi.Server(); | ||
var Plugin = { | ||
register: ModelsPlugin, | ||
options: Config | ||
}; | ||
server.connection({ port: 0 }); | ||
server.register(ModelsPlugin, function (err) { | ||
server.register(Plugin, function (err) { | ||
@@ -98,3 +105,3 @@ Code.expect(err).to.be.an.object(); | ||
lab.test('it successfuly connects to the db and exposes defined models and skips indexing', function (done) { | ||
lab.test('it successfuly connects to the db and exposes defined models (with absolute paths)', function (done) { | ||
@@ -107,5 +114,4 @@ var server = new Hapi.Server(); | ||
models: { | ||
Dummy: './test/fixtures/dummy-model' | ||
}, | ||
autoIndex: false | ||
Dummy: Path.join(__dirname, 'fixtures/dummy-model') | ||
} | ||
} | ||
@@ -129,2 +135,71 @@ }; | ||
}); | ||
lab.test('it successfuly connects to the db and exposes defined models and skips indexing', function (done) { | ||
var server = new Hapi.Server(); | ||
var Plugin = { | ||
register: ModelsPlugin, | ||
options: { | ||
mongodb: Config.mongodb, | ||
models: { | ||
Dummy: './test/fixtures/dummy-model' | ||
}, | ||
autoIndex: false | ||
} | ||
}; | ||
server.connection({ port: 0 }); | ||
server.register(Plugin, function (err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
server.start(function (err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
Code.expect(server.plugins['hapi-mongo-models']).to.be.an.object(); | ||
Code.expect(server.plugins['hapi-mongo-models'].Dummy).to.exist(); | ||
server.plugins['hapi-mongo-models'].BaseModel.disconnect(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
lab.test('it allows models to be added dynamically specifically during another plugin\'s registration', function (done) { | ||
var server = new Hapi.Server(); | ||
var hapiMongoModelsPlugin = { | ||
register: ModelsPlugin, | ||
options: { | ||
mongodb: Config.mongodb | ||
} | ||
}; | ||
var plugins = [hapiMongoModelsPlugin, DummyPlugin]; | ||
server.connection({ port: 0 }); | ||
server.register(plugins, function (err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
server.start(function (err) { | ||
Code.expect(server.plugins['hapi-mongo-models']).to.be.an.object(); | ||
Code.expect(server.plugins['hapi-mongo-models'].Dummy).to.exist(); | ||
server.plugins['hapi-mongo-models'].BaseModel.disconnect(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
85307
14
1554
684