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

als-mongo-list

v1.0.0

Published

A flexible, lightweight MongoDB query utility for Node.js applications. Simplifies database operations with intuitive filtering, pagination, sorting, and field selection. Ideal for REST API endpoints, providing a primary List class that abstracts complex

Downloads

74

Readme

als-mongo-list

als-mongo-list is a flexible and intuitive library for managing MongoDB queries in Node.js applications. It provides a simplified way to handle filtering, pagination, sorting, and field selection using a main List class and related modules.


Installation

npm install als-mongo-list

Main Module: List

The List class is the primary interface for creating MongoDB queries. It abstracts common database query operations and provides a seamless way to filter, paginate, sort, and select fields.

Constructor

const list = new List(Model, options);
  • Model: MongoDB model (required).
  • options (optional): Object with the following properties:
    • select (default '-__v'): Fields to select.
    • populate (default []): Array of fields to populate.
    • order (default ''): Default sorting field.
    • searchKeys (default []): Array of fields allowed for search and filtering.

Example

const List = require('als-mongo-list');
const myModel = require('./models/myModel');

const list = new List(myModel, {
   select: 'name age',
   populate: ['comments'],
   order: 'age',
   searchKeys: ['name', 'age', 'isActive']
});

Methods

query(req, search)

This method initiates a query with the given request. It returns a Query instance that manages the filtering, pagination, sorting, and field selection.

  • req: HTTP request object containing query parameters.
  • search (optional): Search conditions.

Usage:

const result = await list.query(req).read();

Example with Pagination and Filtering

const req = { query: { pageNumber: '1', pageSize: '10', age_gte: '20', age_lte: '40', isActive: 'true' } };
const result = await list.query(req).read();

console.log(result);

Options

options.select - Fields to select from the database.

options.populate - Fields to populate.

options.order - Default field for ordering.

options.searchKeys - Array of fields to filter by.

Example: Complete Usage

const req = { query: { pageNumber: '1', pageSize: '10', name_regex: 'Al', age_gte: '20', age_lte: '30' } };

const result = await list.query(req).read();
console.log(result.items); // Paginated, filtered, and sorted results.

Helper Modules

prepareSearch(query, searchKeys)

Generates a search object from a query and set of searchable keys. This function handles various conditions including exact match, range, and regex search.

  • query: Object with search criteria.
  • searchKeys: Array of [key, validator] pairs, where validator is a function from typeFns.

type-fns.js

Utility functions that convert query values to specified types.

  • String: Converts to a string.
  • Boolean: Converts to boolean.
  • Number: Converts to a number.
  • ObjectId: Validates and converts to MongoDB ObjectId.

query.js

Handles filtering, pagination, sorting, and population for MongoDB queries.

Methods:

  • populate(ref): Adds a population reference.
  • select(fields): Limits the fields returned.
  • order(field): Orders results by a specified field.

read.js

Executes the MongoDB query and returns paginated, sorted results.

  • Returns an object with items, pagination, orderBy, and error.

Example API Usage

Basic Setup

const List = require('als-mongo-list');
const model = require('./models/myModel');

const list = new List(model, {
  select: 'name age',
  populate: ['author'],
  order: 'createdAt',
  searchKeys: ['name', 'age', 'isActive']
});

// Set up your request
const req = { query: { pageNumber: '1', pageSize: '10', name_regex: 'Test' } };
const result = await list.query(req).read();
console.log(result);

This documentation provides an overview of the functionality and API for als-mongo-list. For detailed examples, refer to the test cases which illustrate typical usage scenarios.