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-footprints

v0.2.4

Published

Auto-generate API with hapi using dogwater(waterline)

Downloads

5

Readme

hapi-footprints

Docs WIP

Some codes are borrowed from bedwetter, which is an alternative for hapi-footprints.

NPM

Usage

$ npm install hapi-footprints --save
// ES6 Syntax
import Footprints from 'hapi-footprints';

API

const footprints = new Footprints(options, overrideRoutes)

Creates a new instance of footprint.

  • options (Object) Optional
    • prefix (string)
      • Defaults to: /api
      • Prefix for the routes created by footprints
    • excludeModels (Array)
      • Defaults to: []
      • Example: ['user', 'post']
      • An array of model names to exclude from creating the routes.
    • excludeActions (Object)
      • Defaults to: {}
      • Example: {modelName: ['find', 'remove']}
      • An object for excluding a particular action or actions.
    • routeDefaults (Object)
      • Defaults to: {}
      • Example: {config: {tags: ['api']}}
      • An object which will be merged to every routes generated. Useful adding configurations for hapi.
    • cache (Object or false)
      • Defaults to:
        {
          cache: 'redisCache',
          expiresIn: 1000 * 60 * 5,
          segment: '!footprints',
          generateTimeout: 300,
        }
      • Disables cache when false, else configurations are passed down to hapi's server method's options. Caching is available for find, findCount, findOne actions. See here for more information about actions.
    • handler (Object)
      • See the comment-outs below for description of each keys available
      • Defaults to:
      {
        // name of handler to register
        name: 'footprints',
      
        // override default options of the handler
        options: {
          // will be deleted
          model: null,
      
          // enable cache for this footprint
          // works only on 'GET' method actions
          cache: true,
      
          // function to inject before executing a query to the database
          // could be used for caching the response, custom errors, etc.
          // it is injected before caching, therefore caching will not work.
          // NOTE: this option will not be executed in association routes
          // example: (query, options, callback) => { query.exec(callback) };
          injectBeforeQuery: false,
      
          // primaryKey of model, defaults to 'id' when set to false
          primaryKey: false,
      
          // paginations
          pagination: true,
      
          // default limits for 'find'
          limit: 30,
      
          // max limit for 'find'
          maxLimit: 30,
      
          // default offset
          offset: 0,
      
          // soft deletes
          softDelete: {
            // enable soft deletes
            enabled: true,
            // set attribute for soft deletes
            attr: 'deletedAt',
          },
      
          // population
          populateByDefault: true,
          populateLimit: 30,
      
          // associations
          child: {
            // primaryKey for the child model
            primaryKey: false,
          },
        },
      }
  • overrideRoutes (Object) Optional
    • Route configurations for specific route-actions.
    • Example:
      {
        modelName: {
          find: {
            // any hapi route configurations here,
            // but NOT "path" or "method", footprints will throw an error.
          },
          findCount: {},
          findOne: {},
          create: {},
          update: {},
          populate: {},
          populateWithId: {},
          populateCount: {},
          add: {},
          addWithId: {},
          destroy: {},
          remove: {},
        },
        // and so on...
      }

footprints.plugin()

Returns a plugin for hapi. See here for more about hapi's plugin.

Example

import hapi from 'hapi';
import dogwater from 'dogwater';
import models from './someModelFile.js';
import overrideRoutes from './someOtherFile.js';

const server = new Hapi.Server();
server.connection({ port: 3000 });

const footprints = new Footprints({
  // configurations for footprints...
}, overrideRoutes);

server.register([
  {
    register: dogwater,
    options: {
      models: models,
      // ... configurations for dogwater
    },
  },
  footprints.plugin(),
], (err) => {});

server.start(function () {
  console.log('Server running at:', server.info.uri);
});

Exposed by plugin

  • a handler, with a name passed in with options.handler.name
    • this handler is used for the actual route.handler, and can be used inside the overrideRoutes
  • a server method called cacheQuery, when options.cache is not disabled.
    • this server method is used for caching wateline model's query, by the criteria used in the query

Example of generated routes

  • GET /api/users

    • Action name: find
    • Find all users records
  • GET /api/users/count

    • Action name: findCount
    • Count all users records
  • GET /api/users/{id}

    • Action name: findOne
    • Find one user record
  • POST /api/users

    • Action name: create
    • Create a new user
  • POST, PATCH /api/users/{id}

    • Action name: update
    • Update user with {id}
  • GET /api/users/{id}/associatedModel

    • Action name: populate
    • Get all associated records for a user (records are associatedModel, in this case)
  • GET /api/users/{id}/associatedModel/{childId}

    • Action name: populateWithId
    • Get One associated record for user (associatedModel, in this case)
  • GET /api/users/{id}/associatedModel/count

    • Action name: populateCount
    • Count all associated records (count of associatedModels, in this case)
  • POST /api/users/{id}/associatedModel

    • Action name: add
    • Create a new associatedModel record, and adds a relation to the user with {id}
  • POST /api/users/{id}/associatedModel/{childId}

    • Action name: addWithId
    • Associates associatedModel with {childId}, with the user with {id}
  • DELETE /api/users/{id}

    • Action name: destroy
    • Deletes a user with {id}
  • DELETE /api/users/{id}/associatedModel/{childId}

    • Action name: remove
    • Removes association with {childId} from user with {id}

Run tests

$ npm test

License

MIT