
Product
Introducing Webhook Events for Pull Request Scans
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
hapi-quick-api-mongoose
Advanced tools
This package will simply convert passed schema configs to APIs in HapiJS with the help of get-it-ready.
The Schema Config should look like this:
//Person.js
var Mongoose = require('mongoose'),
Schema = Mongoose.Schema;
var Joi = require('joi');
let schema = {
firstName: { type: String, required: true, joi: Joi.string() },
lastName: { type: String, required: true, joi: Joi.string() },
photo: { type: String, required: false, joi: Joi.string() },
createdOn: { type: Date, required: false, default: Date.now, joi: Joi.date() }
}
export default {
definition: schema,
name: 'Person',
version: '0.1.0'
}
And in the HapiJS server can be simply coded in following way:
//index.js
//THIS FILE WILL BE BABEL TRANSPILED
import Hapi from 'hapi';
import Inert from 'inert';
import Vision from 'vision';
import Mongoose from 'mongoose';
const HapiSwagger = require('hapi-swagger');
import person from './Person';
const hapiQuickApiMongoose = require('hapi-quick-api-mongoose');
var config = {
server: {
host: '0.0.0.0',
port: process.env.PORT || 8000
}
};
// Create a server with a host and port
const server = new Hapi.Server();
server.connection(config.server);
//Connect and create placeholders
server.app.models = {};
server.app.db = Mongoose.createConnection(uri);
server.app.db.on('error', console.error.bind(console, 'connection error'));
server.app.db.once('open', function callback() {
console.log("Connection with database succeeded.");
});
//Register the plugins/modules
server.register([
Inert,
Vision,
{
'register': HapiSwagger,
'options': {
info: {
title: 'API Documentation',
version: 0.1.0,
}
}
},
{
register: hapiQuickApiMongoose,
options: {
connection: server.app.db,
models: [person]
}
}], err => {
console.log(err)
}
);
// Start the server
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
Now head over to browser on url http://localhost:8000/documentation and see the magic.
FAQs
Quick Hapijs REST APIs with mixed Mongoose Schema and Joi
We found that hapi-quick-api-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.
Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.
Product
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.