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

sequelize-rest-handlers

v2.1.0

Published

Create REST handlers for Sequelize models

Downloads

18

Readme

Sequelize Handlers

Create REST handers for Sequelize models

npm version Build Status Test Coverage Code Climate

Installation

npm install sequelize-rest-handlers

Usage

Creating Individual Controllers

If you only need a couple of handlers you can create one for each model on your own with the syntax below. If you want to create an API for a series of models check out Creating Entire APIs.

	const { createController } = require('sequelize-rest-handlers');
	const models = require('./models');
	const app = require('express')();

	const router = createController(models.Post, {
		// Allow the primary key to be modified during a PUT
		allowChangingPrimaryKey: false,
		// Include model relations in a request to `/`
		// WARNING: this will get slower with large databases
		includeRelationsInGetAll: false
		// Customize the top-level property in the resulting JSON
		overrideOutputName: 'article',
		// By default `body-parser.json()` is enabled on the router--If you wish to
		// disable this you *must* populate `req.body` another way
		disableBodyParser: false,
		// These relations are passed directly to Model.findAll.include
		relationships: [
			db.Author,
			{
				model: db.Comment,
				attributes: [
					'content',
					'author'
				]
			}
		],
		// Optionally create child routes e.g. GET /api/v1/articles/:articleId/author
		createChildren: false,
		// Optionally add middleware to run at the start of the request
		middleware: [
			(req, res, next) => {
				// ... do something with the request before the
				// other handlers get the request
			}
		],
		hooks: {
			// Need to track changes on models?
			beforeUpdate: function(model, instance) {
				let changes = instance.changed();
				// ... do something with changelog
			},
			// Or maybe you need to modify the Sequelize query
			// based upon some property in the request
			beforeQuery: function(query, request) {
				query.where.locationId = req.currentLocation.get('id');
			}
		}
	});

	// The resulting router will be able to handle:
	// GET /api/v1/articles/
	// GET /api/v1/articles/:articleId
	// PUT /api/v1/articles/:articleId
	// POST /api/v1/articles/
	// DELETE /api/v1/articles/:articleId

	app.use('/api/v1/articles', router);

	app.listen(8080);

Creating Entire APIs

If you just want to generate endpoints for all of your models you can use createRouter.

	const { createRouter } = require('sequelize-rest-handlers');
	const models = require('./models');
	const app = require('express')();

	const router = createRouter([
		// You can include either a model by itself
		models.Article,
		// Or you can include a set of options as you would for `createController`
		{
			model: models.Author,
			options: {
				handlers: {
					put: false
				},
				// Optionally create child routes only for this model
				createChildren: true
			},
			limit: 10
		}
	], {
		// You can also include top-level middleware that will go before
		// all other endpoints
		middleware: [
			(req, res, next) => {
				logger.debug(`${req.method}: ${req.originalUrl} [${req.ip}]`);
				return next();
			}
		],
		// Optionally create child routes e.g. GET /api/v1/articles/:articleId/author
		createChildren: false,
		// Or you can specify global options for items such as `limit`
		// Note: You can override the globals by setting the same property
		// with a different value on the specific controller's options
		limit: 5
	});

	app.use('/api/v1', router);

	// The resulting app will be able to handle:
	// GET /api/v1/articles/
	// GET /api/v1/articles/:articleId
	// PUT /api/v1/articles/:articleId
	// POST /api/v1/articles/
	// DELETE /api/v1/articles/:articleId
	// GET /api/v1/posts
	// GET /api/v1/posts/:postId
	// PUT /api/v1/posts/:postId
	// ... etc ...

	app.listen(8080);

Query Parameters

Routes generated will support filtering in the Ember style i.e. `/api/v1/articles/?filter[published]=true.

In addition both limit and offset are supported.

Supported Hooks

  • beforeUpdate
  • afterUpdate
  • afterCreate