Socket
Socket
Sign inDemoInstall

cmbf-hapi-restmodel

Package Overview
Dependencies
10
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    cmbf-hapi-restmodel

Hapi plugin building a REST Api from Joi schema, backed by covistra-mongo plugin


Version published
Weekly downloads
52
increased by642.86%
Maintainers
1
Install size
7.21 MB
Created
Weekly downloads
 

Readme

Source

CMBF REST Model

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 the plugin

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'));

ModelManager

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:

  • create
  • show
  • update
  • upsert
  • remove
  • list

Model

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.

Overriding ID field

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

Last updated on 11 Apr 2016

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc