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

@easygrating/sequelize-query-parser

v1.0.0

Published

Http query parser middlewares to sequelize query object

Downloads

2

Readme

sequelize-query-parser

Sequelize Query Parser is a middleware library for Express.js designed to simplify the process of parsing client requests and creating Sequelize-compatible queries effortlessly.

Installation

You can install Sequelize Query Parser via npm:

npm install @easygrating/sequelize-query-parser

Features

  • Parse Express.js request data into Sequelize-compatible queries.
  • Build where clauses for Sequelize queries based on request parameters.
  • Specify attributes to include/exclude in query responses.
  • Enable searching within string or text attributes of Sequelize models.
  • Define query ordering and sorting attributes easily.
  • Pagination support with limit and skip parameters.

Middleware Functions

  • buildModel(db, modelName?): Extracts the database model to be used in the query from the request params or set by the user.
  • buildAttributes: Selects attributes to include/exclude in the query response from query params. Example query: ?attributes=name,age will generate:
{ select: ['name', 'age'] } 
  • buildWhere: Builds the where clause for Sequelize queries using the query params from the client. Example query: ?age=30 will generate:
{ where: { age: 30 } }
  • buildSearch: Enables searching within string or text parameters of the model. Example query: ?search=Zeus will generate:
{ where: { [Op.iLike]: { name: '%Zeus%'} } }
  • buildOrder: Specifies the order and attribute for sorting the query results. Example query: ?order=age:asc,name:desc will generate:
{ order: [ [col('name'), 'DESC'], [col('age'), 'ASC'] ] }
  • buildQuery(): Builds a valid Sequelize query object, using the resulting object from the previous middlewares use. Finally this will generate Sequelize-compatible query object:
{ 
  select: ['name', 'age'], 
  where: { [Op.iLike]: { name: '%Zeus%'}, age: 30 }, 
  order: [[col('name'), 'DESC'], [col('age'), 'ASC']] 
}

Implementation

After installing

  1. Import the middleware functions into your Express.js application.
// Import required functions
const { buildModel, buildWhere, buildAttributes, buildSearch, buildOrder, buildQuery } = require('@easygrating/sequelize-query-parser');
// ...
  1. Apply the middleware functions to your routes.

Example usage within an Express.js route:

// Sample route implementing Sequelize Query Parser middlewares
app.get('/api/:model', buildModel(db, 'YourModelName'), buildWhere, buildAttributes, buildSearch, buildOrder, buildQuery(), async (req, res) => {
  // Execute Sequelize query
  const query = req.sequelizeQueryParser.query;
  try{
    await YourModelName.findAll(query)
    res.json(result);
  }catch(error){ 
    res.status(500).json({ error: error.message });
  }
});

License

This project is licensed under the MIT License.

Keywords

sequelize, query, parser