Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
hicsail-hapi-mongo-models
Advanced tools
Readme
A hapi plugin for mongo-models
.
$ npm install hapi-mongo-models
During plugin registration we connect to MongoDB using the supplied options.
const HapiMongoModels = require('hapi-mongo-models');
const plugin = {
register: HapiMongoModels,
options: {
mongodb: {
uri: 'mongodb://localhost:27017/hapi-mongo-models-test',
options: {}
},
autoIndex: false,
models: {
Customer: './path/to/customer',
Order: './path/to/order'
}
}
};
server.register(plugin, (err) => {
if (err) {
console.log('Failed loading plugin');
}
});
{
"connections": [{
"port": 8080
}],
"registrations": [{
"plugin": {
"register": "hapi-mongo-models",
"options": {
"mongodb": {
"uri": "mongodb://localhost:27017/hapi-mongo-models-test",
"options": {},
},
"autoIndex": false,
"models": {
"Customer": "./path/to/customer",
"Order": "./path/to/order"
}
}
}
}]
}
The options passed to the plugin is an object where:
mongodb
- is an object where:
uri
- a string representing the connection uri for MongoDB.options
- an optional object passed to MongoDB's native connect function.autoIndex
- a boolean specifying if the plugin should call createIndexes
for each model that has a static indexes
property. 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.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:
const DynamoKitty = require('./models/dynamo-kitty');
exports.register = function (server, options, next) {
const 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 usage in a route handler:
// customer plugin
exports.register = function (server, options, next) {
server.route({
method: 'GET',
path: '/customers',
config: {
validate: {
query: {
name: Joi.string().allow('')
}
}
},
handler: function (request, reply) {
const Customer = request.server.plugins['hapi-mongo-models'].Customer;
const filter = {};
if (request.query.name) {
filter.name = request.query.name;
}
Customer.find(filter, (err, results) => {
if (err) {
return reply(err);
}
reply(results);
});
}
});
next();
};
exports.register.attributes = {
name: 'customers'
};
To see hapi-mongo-models
in action, checkout the
Frame project's
models.
Any issues or questions (no matter how basic), open an issue. Please take the initiative to read relevant documentation and be pro-active with debugging.
Contributions are welcome. If you're changing something non-trivial, you may want to submit an issue before creating a large pull request.
MIT
What you create with hapi-mongo-models
is more important than hapi-mongo-models
.
FAQs
A hapi plugin for mongo-models
The npm package hicsail-hapi-mongo-models receives a total of 0 weekly downloads. As such, hicsail-hapi-mongo-models popularity was classified as not popular.
We found that hicsail-hapi-mongo-models demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.