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

mongoose-list

v0.2.2

Published

List plugin for mongoose that allows pagination, filtering, and sorting.

Downloads

9

Readme

mongoose-list Build Status

List plugin for mongoose that allows pagination, filtering, sorting.

Installation

$ npm install mongoose-list

Usage

model.js

var mongoose = require('mongoose')
  , schema

mongoose.plugin(require('../lib/mongoose-list'),{searchFields: ['name']})

schema = new mongoose.Schema({
  name: {
    type: String,
    require: true
  }
})

module.exports = mongoose.model('Test',schema)

app.js

var Model = require('./model')

//with sorting
Model.list({start: 0, limit: 10, sort: 'name'},function(err,count,results){
  if(err) throw err
  console.log('found ' + count + 'records')
  results.forEach(function(row){
    console.log('name: ' + row.name)
  })
})

//with searching
Model.list({start: 0, limit: 10, sort: 'name', find: 'foo'},function(err,count,results){
  if(err) throw err
  console.log('found ' + count + 'records')
  results.forEach(function(row){
    console.log('name: ' + row.name)
  })
})

Instantiation Options

Search Fields

  • Variable searchFields
  • Default all non hidden schema paths

This option will limit what fields are considered searchable. By default it will search any field that does not start with _

Call Time Options

Start

  • Variable start
  • Default 0

Where to start showing records from. Also known as offset.

Limit

  • Variable limit
  • Default 10

Limit of records to return in result set.

Sort

  • Variable sort
  • Default ''

Uses a Mongoose style sort string eg: `+name -author'

Find

  • Variable find
  • Default ''

Populate

  • Variable populate
  • Default null

Uses a Mongoose populate to populate objectIds

Searching

Filter results by value applying to the searchFields

Alternatively, the find variable can also be a custom mongoose query object like the following example:

var query = {
  find: {
    $or: [
      { field1: /something/ },
      { field2: new RegExp('else', 'i') }
    ]
  }
}
Model.list({start: 0, limit: 10, sort: 'name', find: query},function(err,count,results){
  if(err) throw err
  console.log('found ' + count + 'records')
  results.forEach(function(row){
    console.log('name: ' + row.name)
  })
})

This allows you to perform custom and complex queries and still make use of the remaining features of this module such as pagination.

Changelog

0.2.2

  • Adds populate feature to list plugin.

0.2.1

  • Fixed issue with plugin crashing with out of range start and limit values.

0.2.0

  • Added custom find object support that can be a direct mongoose query object rather than having one built automatically.

0.1.1

  • Fixed bug with searching on non string fields

0.1.0

  • Initial Release