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

restify-sequelize-query

v1.0.5

Published

Create Restify middleware that queries Sequelize models.

Downloads

9

Readme

restify-sequelize-query

Create REST APIs using Restify and Sequelize.

Example:

import query from 'restify-sequelize-query';
import cache from 'restify-sequelize-query/lib/RedisCache';

cache.configure({
	'StuffModel': 'redis://localhost:7777',
	'OtherStuff': 'redis://localhosT:7778',
	'MoreStuff': 'redis://localhost:7779',
	'raw': 'redis://localhost:77780' // exec/multiexec
});

server.get(
	{ path: '/stuff/:id' },
	query({
		method: 'select',
		model: StuffModel,
		attributes: [ 'id', 'col1', 'col2' ],
		order: [ 'id' ],
		where: (params) => ({ id: params.id }),
		limit: 1,
		format: (results) => results.map(result => ({ column1: result.col1, column2: result.col2 })),
		cache: cache
	})
);

query(options):

Returns:

A Restify middleware function.

options

Specifies Sequelize query options. Options are passed directly to the Sequelize method selected by options.method.

options.method:

  • select: Execute one findAll query
  • update: Execute update calls based on req.body contents
  • upsert: Execute upsert calls based on req.body contents
  • delete: Execute delete calls based on req.body contents
  • exec: Execute one raw SQL query
  • multiexec: Execute raw SQL queries based on req.body contents

options.model:

Specifies the Sequelize model to query.

options.where:

A function with one parameter that returns either an object literal or a Promise that resolves to an object literal describing the where clause. The parameter is either req.params or req.body[idx] depending on which query method is used. This option is only supported for select, update, and delete query methods.

Use this to create custom where clauses based on either the request parameters or individual request body items.

options.parse:

A function with req.body as it's parameter and returns either a Promise or undefined. req.body is forced to be an Array if it isn't already. The parse function should iterate and alter each body item if necessary.

Use this to alter the request body if necessary.

options.format:

A function that receives an array of query results and must return either an array of formatted results or a Promise that resolves to the same.

Use this to format data results.

options.sql:

A SQL string used to specify the raw SQL to execute for exec and multiexec methods.

options.sequelize:

Set this to the sequelize instance used to execute raw SQL queries when using the exec and multiexec query methods.

options.replacements:

For exec queries:

This can be set to a function accepting req as it's parameter and returning a value or Promise that describes the parameter replacements to be made in the raw SQL.

For multiexec queries:

This can be set to a function accepting req.body[idx] as it's parameter and returning a value or Promise that describes the parameter replacements to be made in the raw SQL.

options.bind:

For exec queries:

This can be set to a function accepting req as it's parameter and returning a value or Promise that describes the parameter binds to be made in the raw SQL.

For multiexec queries:

This can be set to a function accepting req.body[idx] as it's parameter and returning a value or Promise that describes the parameter binds to be made in the raw SQL.

options.cache:

This can be set to a cache class that should have the following methods:

  • set(shard, key, value): Return a Promise that sets a cache value and resolves to that same value.
  • get(shard, key): Return a Promise that resolves either to the cache value specified by the key or undefined.
  • clear(shard): Return a Promise that removes all keys associated with the shard.

Two sample cache classes are included: MemoryCache and RedisCache.

options.invalidate:

This is an Array of either model names or model references used to invalidate cache entries.