Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
hapi-nosql-mongoose
Advanced tools
Mongoose plugin for HapiJS (v17+)
# npm
npm install hapi-nosql-mongoose mongoose
# yarn
yarn add hapi-nosql-mongoose mongoose
const Mongoose = require('hapi-nosql-mongoose');
const schemas = require('./my/mongoose/schemas');
await server.register({
plugin: Mongoose,
options: {
uri: 'mongodb://localhost:27017/database',
config: {...},
schemas: {...}
}
});
For ease of use you can have a folder with all your schema definitions along an index.js
file that exports all the schemas inside the folder. e.g:
-- /my/mongoose/schemas/
|-- index.js
|-- post.js
|-- user.js
// Post schema (post.js)
'use strict';
const Schema = require('mongoose').Schema;
const Post = new Schema({
title: {
type: String,
trim: true
},
content: String,
authorId: {
type: String // referencing the User as you see fit
},
createdAt: {
type: Date,
'default': Date.now
}
});
module.exports = Post;
// User schema (user.js)
'use strict';
const Schema = require('mongoose').Schema;
const User = new Schema({
uuid: {
type: String,
'default': uuid.v4 // using an uuid library
},
name: {
type: String,
trim: true
},
lastName: {
type: String,
trim: true
},
createdAt: {
type: Date,
'default': Date.now
}
});
module.exports = User;
// Exporter (index.js)
'use strict';
const Post = require('./post');
const User = require('./user');
const schemas = {
Post,
User
};
module.exports = schemas
This plugin decorates the server object, adding a method called mongoose:connector
that returns the full Connector object.
Use the Connector object to get your models in your controllers like this:
server.route({
method: 'GET',
path: '/posts',
handler: async (request, h) => {
const Post = request.server['mongoose:connector'].getModel('Post');
// More code below
}
});
server.route({
method: 'GET',
path: '/posts',
handler: async (request, h) => {
const MongooseConnection = request.server.plugins['hapi-nosql-mongoose'].connection;
// More code below
}
});
server.route({
method: 'GET',
path: '/posts',
handler: async (request, h) => {
const Mongoose = request.server.plugins['hapi-nosql-mongoose'].mongoose;
// More code below
}
});
FAQs
Mongoose implementation for HapiJS
We found that hapi-nosql-mongoose 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.