Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Akeri committed Mar 23, 2019
2 parents 8cb36ad + 9e59114 commit 34eb10c
Show file tree
Hide file tree
Showing 13 changed files with 5,254 additions and 543 deletions.
53 changes: 2 additions & 51 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,54 +1,5 @@
{
"parserOptions": {
"ecmaVersion": 2017
},
"env": {
"amd": true,
"es6": true,
"node": true,
"mocha": true
},
"extends": [
"eslint:recommended",
"google"
],
"rules": {
"max-len": [
"error",
100,
2,
{
"ignoreComments": true,
"ignoreUrls": true,
"ignorePattern": "^\\s*var\\s.+=\\s*(require\\s*\\()|(/)"
}
],
"indent": [
"error",
2
],
"space-before-function-paren": [
"error",
"always"
],
"one-var": [
"error",
{
"initialized": "never",
"uninitialized": "always"
}
],
"valid-jsdoc": [
"error",
{
"requireReturnDescription": false,
"prefer": {
"returns": "returns"
}
}
],
"capitalized-comments": "error",
"padded-blocks": "off",
"new-cap": "off"
}
"@aliatech/aliatech/env-node"
]
}
40 changes: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Build Status](https://travis-ci.org/aliatech/loopback-mongo-distinct-mixin.svg?branch=master)](https://travis-ci.org/aliatech/loopback-mongo-distinct-mixin)
[![Coverage Status](https://coveralls.io/repos/github/aliatech/loopback-mongo-distinct-mixin/badge.svg?branch=master)](https://coveralls.io/github/aliatech/loopback-mongo-distinct-mixin?branch=master)
[![npm version](https://badge.fury.io/js/%40aliatech%2Floopback-mongo-distinct-mixin.svg)](https://badge.fury.io/js/%40aliatech%2Floopback-mongo-distinct-mixin)
[![npm version](https://img.shields.io/npm/v/@aliatech/loopback-mongo-distinct-mixin.svg?color=blue)](https://www.npmjs.com/package/@aliatech/loopback-mongo-distinct-mixin)

# Loopback Distinct mixin for MongoDB

Expand All @@ -20,7 +20,7 @@ npm i -S @aliatech/loopback-mongo-distinct-mixin
or Yarn

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

## Server configuration
Expand Down Expand Up @@ -69,11 +69,22 @@ Now you can query for distinct values this way:
```js
app.models.Person.distinctValues('name', (err, names) => {
if (err) return next(err);
console.log(names);
// names = ['john', 'mary', 'anne', ...]
});
```

Or using promise:

```js
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.
Expand Down Expand Up @@ -134,6 +145,8 @@ app.models.Person.distinctValues('name', {company: 'alia'}, (err, names) => {
});
````

Note: Filter should be MongoDB native, not Loopback

#### Call distinct values using a default property.

1. Configure `defaultProperty` option
Expand Down Expand Up @@ -174,6 +187,25 @@ request('http://localhost:3000/api/Person/distinctValues',

# Testing

Install develop dependences

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

Execute tests

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

# Credits

Developed by
[Juan Costa](https://github.com/Akeri "Github's profile")
for
[ALIA Technologies](https://github.com/aliatech "Github's profile")

[<img src="http://alialabs.com/images/logos/logo-full-big.png" alt="ALIA Technologies" height=100/>](http://alialabs.com "Go to ALIA Technologies' website")
39 changes: 22 additions & 17 deletions lib/distinct.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

const _ = require('lodash');
const pg = require('polygoat');

module.exports = (Model, options) => {

const defaultProperty = _.get(options, 'defaultProperty');

const settings = _.merge({
Expand Down Expand Up @@ -42,18 +44,27 @@ module.exports = (Model, options) => {
* - distinctValues(property, where, next).
* - distinctValues(where, next) Only if defined defaultProperty.
* - distinctValues(next) Only if defined defaultProperty. No filter.
* - Also next can be omitted and a promise will be returned.
* * property {string} Specifies the name of the property to get distinct values.
* * where {object} Mongo filter.
* * next {function} Method callback. Receives error and result as arguments.
* @param {...*} args Accepts different number of arguments.
* @returns {void}
* @returns {Promise<*[]>} Only if callback is not passed.
*/
Model.distinctValues = function (...args) {
const {property, where, next} = distinctArgs(args);
if (property) {
const collection = Model.getConnector().collection(Model.modelName);
collection.distinct(property, where, (err, objects) => next(err, objects));
}
const {
property, where, next,
} = distinctArgs(args);
return pg((done) => {
if (property) {
const collection = Model.getConnector().collection(Model.modelName);
collection.distinct(property, where, (err, objects) => done(err, objects));
} else {
// Property is not specified and there is not a default one
// Abort with error
done(new Error('Need to specify a property'));
}
}, next);
};

/**
Expand All @@ -67,7 +78,7 @@ module.exports = (Model, options) => {
const args = Array.from(params);
let property = defaultProperty;
let where = {};
let next = _.noop;
let next;
while (args.length) {
const arg = args.shift();
if (typeof arg === 'string') {
Expand All @@ -78,14 +89,8 @@ module.exports = (Model, options) => {
next = arg;
}
}
if (!property) {
// Property is not specified and there is not a default one
// Abort with error
next(new Error('Must specify a property param'));
return false;
} else {
return {property, where, next};
}
return {
property, where, next,
};
}

};

0 comments on commit 34eb10c

Please sign in to comment.