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

@aliatech/loopback-mongo-distinct-mixin

v1.1.0

Published

Loopback mixin to provide mongo distinct queries

Downloads

8

Readme

Build Status Coverage Status npm version

Loopback Distinct mixin for MongoDB

Provide to models the ability to query distinct values from database

This is a Loopback mixin to be used together with MongoDB connector. Works for Loopback 2 and 3.

How to install

Install the package through NPM

npm i -S @aliatech/loopback-mongo-distinct-mixin

or Yarn

yarn add --prod @aliatech/loopback-mongo-distinct-mixin

Server configuration

Include the mixin in server/model-config.json:

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "../node_modules/loopback-mongo-distinct-mixin/lib",
      "../common/mixins"
    ]
  }
}

Usage

Enable the mixin in your model definition, ie person.json.

Basic configuration

{
  "name": "Person",
  "properties": {
    "name": "string",
    "company": "string"
  },
  "mixins": {
    "Distinct": true
  }
}

Now you can query for distinct values this way:

app.models.Person.distinctValues('name', (err, names) => {
  if (err) return next(err);
  // names = ['john', 'mary', 'anne', ...]
});

Or using promise:

app.models.Person.distinctValues('name')
  .then((names) => {
    // names = ['john', 'mary', 'anne', ...]
  })
  .catch((err) => {
    // handle an error
  });

Advanced configuration

Enable the mixin passing an options object instead of just true.

These are the available options:

| Option | Type | Required | Description | | ------------------| ----------| --------- | ----------------- | | defaultProperty | string | optional | Default property name to get values. With this option, it won't be required to specify property argument. | | remote | object | optional | Customize a remote method to call distinct feature. | | remote.enabled | boolean | optional | Enable a remote method for distinctValues. (default false) | | remote.name | string | optional | Name of the remote method. (default 'distinctValues') | | remote.definition | object | optional | The Loopback definition of the remote method. |

Default options

These are the default options that will be merged with the model specifics:

{
  "remote": {
    "enabled": false,
    "name": "distinctValues",
    "definition": {
      "description": "Find objects with distinct property",
      "http": {
        "path": "/distinctValues",
        "verb": "get"
      },
      "accepts": [{
        "arg": "property",
        "type": "string",
        "required": true
      }, {
        "arg": "where",
        "type": "object",
        "required": false
      }],
      "returns": {
        "arg": "objects",
        "type": "array",
        "root": true
      }
    }
  }
}

Note that property argument will required only if defaultProperty is not set.

Advanced usage

Call distinct values with filter.

app.models.Person.distinctValues('name', {company: 'alia'}, (err, names) => {
  // Got distinct names for objects having company = 'alia'
});

Note: Filter should be MongoDB native, not Loopback

Call distinct values using a default property.

  1. Configure defaultProperty option
  2. Call distinctValues omitting property argument
app.models.Person.distinctValues((err, names) => {
  // Do something
});

Using default property and a filter:

app.models.Person.distinctValues({company: 'alia'}, (err, names) => {
  // Do something
});

Remote distinctValues

  1. Configure remote.enabled option to be true
  2. Call through the API
const request = require('request');

request('http://localhost:3000/api/Person/distinctValues',
  {qs: {property: 'name', where: {company: 'alia'}}},
  (err, res, body) => {
    if (err) return next(err);
    if (res.statusCode === 200){
      const names = JSON.parse(body);
      // Do something
    }
});

Testing

Install develop dependences

npm i -D # If you use NPM
yarn install # If you use Yarn

Execute tests

npm test # Without coverage check
npm run test-with-coverage # With coverage check

Credits

Developed by Juan Costa for ALIA Technologies