Skip to content

Commit

Permalink
Merge pull request #1 from johnmanko/2.0.0
Browse files Browse the repository at this point in the history
Updated library to work with Hapi v17
  • Loading branch information
bulaluis authored Jan 16, 2018
2 parents 74877a3 + 4b78509 commit ba0aabc
Show file tree
Hide file tree
Showing 5 changed files with 2,075 additions and 105 deletions.
36 changes: 12 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,30 @@ $ npm install hapi-mongoose-models
## Usage

```javascript
var Hapi = require('hapi');
var Mongoose = require('mongoose');
var server = new Hapi.Server();
server.connection({ port: 8000 });
const Hapi = require('hapi');
const Mongoose = require('mongoose');
const server = Hapi.server({ port: 80 });

server.register({
register: require('hapi-mongoose-models'),
const provision = async () => {

await server.register({
plugin: require('hapi-mongoose-models'),
options: {
globPattern: './server/models/**/*.js', // Required
globOptions: { // https://github.com/isaacs/node-glob
cwd: __dirname, // Required
nosort: true // Optional, utils for mongoose descriptors
}
}
}, function (err) {

if (err) {
throw err;
}
};

server.start(function (err) {

if (err) {
throw err;
}
await server.start();

// Now you can load model
var model = server.plugins['hapi-mongoose-models'].User;
console.log('Server running at:', server.info.uri);
};

// or
var model = Mongoose.model('User');
provision();

console.log('Server started at: ' + server.info.uri);
});
}
});
```
## Tests
Expand Down
78 changes: 38 additions & 40 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,59 @@
// Load modules

var Hoek = require('hoek');
var Joi = require('joi');
var Mongoose = require('mongoose');
var Async = require('async');
var Path = require('path');
var Glob = require('glob');
const Hoek = require('hoek');
const Joi = require('joi');
const Mongoose = require('mongoose');
const Path = require('path');
const globby = require('globby');


// Declare internals

var internals = {
schema: Joi.object({
globPattern: Joi.string().required(),
globOptions: Joi.object({
cwd: Joi.string().required()
}).unknown(true).required()
})
};


exports.register = function (server, options, next) {

options = Hoek.applyToDefaults({}, options);
var results = Joi.validate(options, internals.schema);
Hoek.assert(!results.error, results.error);
var settings = results.value;

Glob(settings.globPattern, settings.globOptions, function (err, files) {
const internals = {};

Hoek.assert(!err, err);
internals.schema = Joi.object({
globPattern: Joi.string().required(),
globOptions: Joi.object({
cwd: Joi.string().required()
}).unknown(true).required()
});

Async.each(files, function (file, callback) {

var path = Path.resolve(settings.globOptions.cwd, file);
var module = require(path);
var model = module;
internals.pkg = require('../package.json');

internals.mapFileToPromise = (cwd, file, server) => {
return new Promise((resolve) => {
let path = Path.resolve(cwd, file);
let module = require(path);
let model = module;
if (module.hasOwnProperty('load') && typeof(module.load) === 'function') {
model = module.load(server);
}

if (model.schema instanceof Mongoose.Schema) {
server.expose(model.modelName, model);
server.log(['hapi-mongoose-models', 'plugin'], 'Model `' + model.modelName + '` loaded');
resolve(`Model '${model.modelName}' loaded`);
}
else {
server.log(['hapi-mongoose-models', 'plugin'], 'File `' + file + '` not ' +
'contains a mongoose model, will be ignored')
resolve(`File '${file}' does not contains a mongoose model, will be ignored`);
}

return Hoek.nextTick(callback)();
}, next);

});
};


exports.register.attributes = {
pkg: require('../package.json')
module.exports = {
pkg: internals.pkg,
register: async (server, options) => {
options = Hoek.applyToDefaults({}, options);
let results = Joi.validate(options, internals.schema);
Hoek.assert(!results.error, results.error);
let settings = results.value;
return globby(settings.globPattern, settings.globOptions)
.then((files) => {
return Promise.all(files.map(file => internals.mapFileToPromise(settings.globOptions.cwd, file, server)));
}).then((messages) => {
messages.forEach(message => server.log([internals.pkg.name, 'plugin'], message));
}).catch((err) => {
server.log([internals.pkg.name, 'plugin'], err);
});
}
};
Loading

0 comments on commit ba0aabc

Please sign in to comment.