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

api-features

v1.5.2

Published

Make the pagination - sort and more api features with one line

Downloads

4

Readme

Api-Feaatures

To adapt the README.md for api-features so that it covers both SQL and NoSQL (e.g., MongoDB) databases, you’ll need to update the installation and usage instructions to reflect the flexibility of the package for different database types. Below is a revised version of the README.md that covers both SQL and NoSQL usage:

api-features

api-features is an npm package designed to simplify query operations for both SQL and NoSQL databases. It provides a unified API for common features such as pagination, filtering, sorting, field selection, and search, making it easier to work with different database systems.

Installation

You can install the api-features package via npm. Run the following command in your project directory:

bash

Copy code

npm install api-features

Usage

For SQL Databases (e.g., using Sequelize) javascript Copy code

const ApiFeature = require('api-features');  // Use require if you are using CommonJS
// or
import ApiFeature from 'api-features';  // Use import if you are using ES Modules

const { Op } = require('sequelize');  // Import Sequelize operators if using SQL
const sequelizeQuery = SomeModel.findAll();  // Replace with your Sequelize query
const searchQuery = {
    page: 2, // take the pageNumber from req.query 
    limit: 10, // add the limit for pagination
    sort: 'name,-createdAt', // specify sorting fields
    fields: 'name,logo', // select which fields to include
    search: 'example' // add search keyword
};

const apiFeature = new ApiFeature(sequelizeQuery, searchQuery, 'sql');

// Apply pagination
apiFeature.pagination(10);

// Apply filtering
apiFeature.filter();

// Apply sorting
apiFeature.sort();

// Apply field selection
apiFeature.fields();

// Apply search
apiFeature.search(['name', 'description']); 

const results = await apiFeature.apply();  // Execute the query
console.log(results);
For NoSQL Databases (e.g., using MongoDB)
javascript
Copy code
const ApiFeature = require('api-features');  // Use require if you are using CommonJS
// or
import ApiFeature from 'api-features';  // Use import if you are using ES Modules

const mongooseQuery = SomeModel.find();  // Replace with your Mongoose query
const searchQuery = {
    page: 2, // take the pageNumber from req.query 
    limit: 10, // add the limit for pagination
    sort: 'name,-createdAt', // specify sorting fields
    fields: 'name,logo', // select which fields to include
    search: 'example' // add search keyword
};

const apiFeature = new ApiFeature(mongooseQuery, searchQuery, 'mongodb');

// Apply pagination
apiFeature.pagination(10);

// Apply filtering
apiFeature.filter();

// Apply sorting
apiFeature.sort();

// Apply field selection
apiFeature.fields();

// Apply search
apiFeature.search(['name', 'description']); 

const results = await apiFeature.apply();  // Execute the query
console.log(results);

API

ApiFeature

constructor(query, queryString, dbType)

query: A query object (e.g., Mongoose query for NoSQL, Sequelize query for SQL). queryString: An object containing query parameters (e.g., page, sort, fields, search). dbType: Type of database ('sql' or 'mongodb'). pagination(limit)

Applies pagination to the query. Takes a limit parameter for the number of items per page. filter()

Applies filtering to the query based on the queryString object. sort()

Applies sorting to the query based on the queryString object. fields()

Selects specific fields to include in the query results. search(fields)

Applies a search to the query based on the queryString.search parameter. Takes an array of field names to search. Example Queries SQL:

sql Copy code

SELECT * FROM Products WHERE name LIKE '%example%' LIMIT 10 OFFSET 20;

NoSQL (MongoDB):

javascript Copy code

db.products.find({ name: /example/i }).skip(20).limit(10);

Made By Just Mahmoud