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

cf-list-aggregator

v2.3.0

Published

Compiles lists of content base on defined filtering and ordering

Downloads

221

Readme

cf-list-aggregator

Aggregates content from lists.

Installation

  npm install cf-list-aggregator

Usage

var aggregate = createAggregator(listService, sectionService, articleService, { logger: logger })
aggregate(listId, dedupe, limit, section, function (err, results) {})

Types of list

There are two types of list:

### 1. Automatic

Automatic lists are used to auto-generate list content based on a number of properties:

  • tags
  • sections

For example (some properties omitted for brevity):

// This list will get all content with the Location tag "London"
{ type: 'auto'
, tags: [ { type: 'Location', tag: 'London' } ]
}
// Assuming the section "Reviews" has ID=123 and "Guides" has ID=456, this list
// will get all content from "Reviews", including any sub-sections and all content
// from "Guides", but nothing from "Guides" > "Premium".
{ type: 'auto'
, sections:
  [ { id: '123', includeSubSections: true }
  , { id: '456', includeSubSections: false }
  ]
}

### 2. Manual

Manual lists are lists where the contents are hand picked. The list object differs from the automatic list in that the contents are described in the items array property.

{ type: 'manual'
, items: []
}

Specifying manual list content

The most common and basic way to define a piece of manual list content is to provide the id of an item that exists in the crudService provided:

{ type: 'manual'
, items: [ { itemId: '123' }, isCustom: false, overrides: {} ]
}

Overriding item properties

Sometimes the item appearing in a list will need a slight modification for the context in which it will be used, perhaps a snappier title or a more appropriate image. In order to achieve this, the overrides property is used.

// The item ID=123 look like this:
{ _id: '123', title: 'Top 10 List Aggregation Modules' /* etc… */ }
// In order to select this item but give it a different image…
{ itemId: '123', isCustom: false, overrides: { image: 'http://diff.img/123' } }

All of the original item's content will be retrieved from the crudService and then the overrides will be applied.

Inserting custom items

Sometimes it is useful to inject an item that doesn't exist in the crudService. To do this, the item format is like so:

{ isCustom: true, properties: { title: 'A Custom Item', image: 'http://notinthe.db' /* etc… */ } }

Date based previewing

To create a list aggregator which allows searching from any date perspective, pass a date parameter into the options object like so:

var aggregate = createAggregator(listService, sectionService, articleService, { logger: logger, date: new Date() })

This aggregator instance now performs all operations based on this date.

Specifying fields to return

To specify fields to return from the query, use the fields option. This field can either be an object or an array - mongo is tolerant of either.

The default list is pretty minimal: [ "_id", "headline" ] so you will want to pass in the desired list every time. It is important to only specify the properties needed in order to minimise unnecessary database traffic.

  fields: { longTitle: 1, tags: 1 }

Or an array:

  fields: ['longTitle', 'tags']

Example:

var aggregate = createAggregator(listService, sectionService, articleService, { logger: logger, fields: { longTitle: 1 } })

Extending Query or Sort

If you need to add additional query or sorting to the default query you can pass additionalAutoQuery in options.

additionalAutoQuery is a function that is given the list and returns a function that takes { query: {}, options: {} } e.g

function additionalAutoQuery(list) {
  return function (q) {
    q.query.headline = 'News Flash'
    q.options.sort = { headline: -1 }
    return q
  }
}

Overriding prepareAutoQuery and prepareManualQuery

If your application needs to modify the queries that the list aggregator makes (need a custom sort function for automatic lists? override prepareAutoQuery) then override these functions.

Just pass in either prepareAutoQuery or prepareManualQuery as options to the list aggregator. The expected input and output for these functions is:

e.g

function prepareAutoQuery(list) {
  return { options: {}, query: {} }
}
function prepareManualQuery(list, IdType) {
  return { options: {}, query: {} }
}

Changlog

v.2.0.0

  • The schema for lists that this module consumes is now totally different and not backwards compatible.
  • crudService.idType is no longer required to be passed in. Make sure the version of save-mongodb in your application is >=0.0.12 so that it will properly reach into the _id: { $in: [] } query and conver to the appropriate id type.

v1.0.0

  • The format for list.sections changed to account for functionality to include sub-sections. Previously the format was:
list.sections = [ 'id1', 'id2', 'id3' ]

This has changed to:

list.sections =
  [ { id: 'id1', includeSubSections: true }
  , { id: 'id2', includeSubSections: false }
  , { id: 'id3', includeSubSections: true }
  ]

Credits

Paul Serby follow me on twitter @serby

Licence

Licensed under the New BSD License