Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
cmbf-hapi-restmodel
Advanced tools
Readme
This HAPI plugin exposes helpers to easily create RESTful API from a model definition based on Joi schema.
It requires the use of other CMBF plugins. See CMBF-Core for more details
Install using npm:
npm install cmbf-hapi-restmodel --save
With the CMBF launcher, you just add a server hook to load additional plugins like this :
{'register-plugins': function(params, defaultImpl) {
"use strict";
var Cmbf = this;
Cmbf.log.debug("Registering specific plugins");
return defaultImpl().then(function() {
return P.join(
Cmbf.installPlugin(require('cmbf-hapi-restmodel'))
// Load other plugins...
);
});
}}
You just register this server hook by calling the registerHooks method:
Cmbf.registerHooks(require('./lib/server-hooks'));
The primary service to register models is called modelManager. Inside your plugin implementation, you retrieve a reference to it through the configured plugins:
var modelManager = server.plugins['cmbf-restmod'].modelManager;
This service exposes a registerModel
method that you call, providing a model definition.
modelManager.registerModel('banana', require('./models/bananas.js);
The model will create all required routes and mongodb collections for the following operations:
Ideally, you define your model using the new ES6 class concept:
var Joi = require('joi');
module.exports = function(server, config, log) {
"use strict";
var BaseModel = server.plugins['cmbf-hapi-restmodel'].BaseModel;
var clock = server.plugins['covistra-system'].clock;
class Document extends BaseModel {
static get endpoint() {
return "documents";
}
static get auth() {
return "token";
}
static get name() {
return "document";
}
static get collection() {
return 'documents';
}
static get Schema() {
return Joi.object().keys({
key: Joi.string().required(),
name: Joi.string().required(),
ownerId: Joi.string(),
customerId: Joi.string(),
type: Joi.string(),
tags: Joi.array().items(Joi.string()),
content: Joi.any(),
created_at: Joi.date().default(clock.nowTs, "Default to current time")
});
}
}
return Document;
};
You then register this model descriptor:
var Document = require('./models/document')(server, config, log);
modelManager.registerModel('document', Document`);
This model will get automatically exposed when the server stats with all standard routes generated for you.
Sometimes you need to override the field through which you model is referenced. By default,
we use a id
field, but you just have to override the idField property in your
derived model:
class MyModel extends BaseModel {
static get idField() {
return 'key';
}
}
FAQs
Hapi plugin building a REST Api from Joi schema, backed by covistra-mongo plugin
The npm package cmbf-hapi-restmodel receives a total of 52 weekly downloads. As such, cmbf-hapi-restmodel popularity was classified as not popular.
We found that cmbf-hapi-restmodel 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
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
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.