GithubHelp home page GithubHelp logo

hapi-bookshelf-serializer's Introduction

Hapi Bookshelf Serializer

npm version Build Status Coverage Status

This plugin takes Bookshelf.js models that are returned via Hapi's reply method and serializes them using a user-defined serialize method.

Registering the Plugin

var Hapi = require('hapi');

var server = new Hapi.Server();

server.register([
  require('hapi-bookshelf-serializer'),
], function (err) {
  // An error will be available here if anything goes wrong
});

Defining Models

Models are defined just like all Bookshelf.js models, except for one small addition. A serialize function is added with the following signature function (request) { }. All model properties can be accessed in the serialize function via this.get() and the function will be passed the current Hapi request as request. The serialize function can either return a static value or a Promise.

Serializing Related Models

Currently there is no support in this module for automatically serializing all related models so you will need to call the function manually.

Example

// models/role.js
var bookshelf = require('bookshelf')(require('knex')(config));

module.exports = bookshelf.Model.extend({
  tableName: 'roles',
  serialize: function(request) {
    return {
      this.get('id'),
      this.get('name')
    };
  }
});

// models/user.js
var bookshelf = require('bookshelf')(require('knex')(config));
var Role      = require('./role.js');

module.exports = bookshelf.Model.extend({
  tableName: 'users',
  roles: this.belongsToMany(Role),
  serialize: function (request) {
    return {
      this.get('id'),
      this.get('email'),
      roles: this.related('roles').map(function (role) {
        return role.serialize(request);
      });
    };
  }
});

This plugin pairs well with the hapi-bookshelf-models plugin which makes registering models from a directory super easy.

hapi-bookshelf-serializer's People

Contributors

arkadiyt avatar dependabot[bot] avatar lvamaral avatar pon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.