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

@celleb/mongoorrhea

v1.1.2

Published

Restful query builder for mongodb with mongoose

Downloads

9

Readme

auto-query

Build Test

Restful query builder for mongodb with mongoose using your url query

npm i @celleb/auto-query

Usage

Define your mongoose model as usual

import { Schema } from 'mongoose';
const userSchema = new Schema({
 name: String,
 surname: String,
 likes: [String],
 car: [
  {
   model: String,
   year: Number,
  },
 ],
 activities: [
  {
   name: String,
  },
 ],
});

Define your dictionary

The dictionary is used to transform the api fields to the database fields. If the fields are the same then you can exclude it from the dictionary. The dictionary is POJO with string for both keys and values.

For example {apiField: 'databaseField' }

// shallow/flat dictionary/map
const dictionary = {
 firstName: 'name',
 lastName: 'surname',
 'vehicle.model': 'car.model',
 'vehicle.year': 'car.year',
};

Create your model and instantiate the query builder

The AutoQuery takes a model and a dictionary.

import { AutoQuery } from '@celleb/auto-query';
import mongoose from 'mongoose';

const User = mongoose.model('User', userSchema);

const qb = new AutoQuery(User, dictionary);

Use the query builder instance in your route handler

Call the query builder's .build method with the request query. The build method returns a Mongoose query and you can chain other methods before calling .exec().

async function routHandler(req: Request, res: Response) {
 return res.json(await qb.build(req.query).exec());
}

Query Parameter Interface

The following are fields support on the query

interface QueryParams = {
    match?: Record<string, string|number|Array<string|number>>;
    sort?: string;
    skip?: number;
    limit?: number;
    select?: string[];
}

For example: url?match[firstName]=Jonas&sort=firstName&skip=0&limit=10&select=firstName&select=lastName.

URL Query Fields

You decide how you encode and decode your url query but the decoded query must match the Query Parameter Interface above.

match

Allows you to query the database using specific fields and operators.

Supported operations

| Symbol | Description | Usage | | ------ | ----------------------------------------------------- | -------------------------------------------------------------------------------- | | = | Equal to or [in]. Do not add an additional equal sign | url?match[firstName]=Jonas or with array url?match[firstName]=Jonas,Jon' | | ! | Not equal to to or not in [nin]. | url?match[firstName]=!Jonas or with array url?match[likes]=Football,!Tennis' | | >: | Greater than or equal | url?match[vehicle.year]=>:2017 | | > | Greater than | url?match[vehicle.year]=>2017 | | <: | Less than or equal | url?match[vehicle.year]=<:2017 | | < | Less than or equal | url?match[vehicle.year]=<2017 |

More operations will be added in the future

sort

Specifies the field and the order by which to sort the results.

Use sort?=-fieldName for descending order and sort=fieldName for ascending order.

skip

Specifies the number of records to skip in the database.

For example skip=10 skips the first 10 records.

limit

Limits the number of matching records returned.

Example limit=10 returns the first 10 results.