npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-crud-promise

v0.4.0

Published

Easily create CRUD routes on a hapi server by providing a path and promises for each of the actions

Downloads

18

Readme

hapi-crud-promise Build Status Coverage Status

NPM NPM

Basics

Reduce repetitive route setup for basic CRUD apps.

Provide one route and a 5 handlers:

/api/things/{thingId}

And get 5 routes added to your server:

GET    /api/things
POST   /api/things
GET    /api/things/{thingId}
PUT    /api/things/{thingId}
DELETE /api/things/{thingId}

Simple Usage

const Hapi = require('hapi');
const Joi = require('joi');
const hapiCrudPromise = require('../index');

const server = new Hapi.Server();
server.connection({ host: '127.0.0.1' });

hapiCrudPromise(server, {
  path: '/api/things/{thingId}',
  config: {
    validate: {
      query: { // validation only applied to GET (all)
        limit: Joi.number().optional()
      }
      params: { // validation only applied to GET (one), DELETE, and UPDATE routes
        thingId: Joi.string().required()
      },
      payload: Joi.object({ // validation only applied to POST and PUT route
        thing: Joi.object({
          name: Joi.string().required()
        }).required()
      })
    }
  },
  crudRead(req) {
    return knex('things')
      .first()
      .where({ id: req.params.thingId });
  },
  crudReadAll(req) {
    return knex('things').limit(req.query.limit);
  },
  crudUpdate(req) {
    return knex('things')
      .update(req.payload.thing)
      .where({ id: req.params.thingId })
      .limit(1)
      .returning('*')
      .spread((thing) => ({ thing: thing }));
  },
  crudCreate(req) {
    return knex('things')
      .insert(req.payload.thing)
      .returning('*')
      .spread((thing) => ({ thing: thing }));
  },
  crudDelete(req) {
    return knex('things')
      .delete()
      .where({ id: req.params.thingId })
      .limit(1);
  }
});

Slightly-more-advanced Usage

If you have a long path in your route with multiple parameters the last one is special, it identifies the resource you are CRUD-ing and will only be included on validations for GET (one), DELETE, and UPDATE routes

const Hapi = require('hapi');
const Joi = require('joi');
const hapiCrudPromise = require('../index');

const server = new Hapi.Server();
server.connection({ host: '127.0.0.1' });

hapiCrudPromise(server, {
  path: '/api/users/{userId}/things/{thingId}',
  config: {
    validate: {
      query: { // validation only applied to GET (all)
        limit: Joi.number().optional()
      }
      params: {
        userId: Joi.string().required(), // This and other param validations applied to all routes
        thingId: Joi.string().required() // Except this one! only applied to GET (one), DELETE, and UPDATE routes
      },
      payload: Joi.object({ // validation only applied to POST and PUT route
        thing: Joi.object({
          name: Joi.string().required()
        }).required()
      })
    }
  },
  crudRead(req) {
    ...
  },
  crudReadAll(req) {
    ...
  },
  crudUpdate(req) {
    ...
  },
  crudCreate(req) {
    ...
  },
  crudDelete(req) {
    ...
  }
});

Contributing

Contributors wanted. If you are looking for a way to help out browse the Help Wanted issues and find one that looks good to you. If you have an idea to make hapi-crud-promise better submit a pull request.

Pull Request Checklist

Checklist for submitting a pull request:

  • [ ] npm run test - Unit tests must pass
  • [ ] New unit tests
  • [ ] npm run test-cov - Code coverage cannot go down
  • [ ] npm run lint - New code must have no linter errors
  • [ ] Your pull request must pass CI

FAQ

Isn't this like hapi-crud?

Yeah, but with Promises! And active. And the Github repo is still live.

Can't I just create a bunch of routes manually?

CRUD routes are repetitive. Write less code and go outside.