Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aliatech/loopback-mongo-distinct-mixin
Provide to models the ability to query distinct values from database
https://github.com/aliatech/loopback-mongo-distinct-mixin
distinct loopback mixin mongodb node orm
Last synced: about 1 month ago
JSON representation
Provide to models the ability to query distinct values from database
- Host: GitHub
- URL: https://github.com/aliatech/loopback-mongo-distinct-mixin
- Owner: aliatech
- License: mit
- Created: 2019-02-10T17:04:56.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T16:38:43.000Z (about 2 years ago)
- Last Synced: 2024-10-27T12:21:36.485Z (2 months ago)
- Topics: distinct, loopback, mixin, mongodb, node, orm
- Language: JavaScript
- Size: 659 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![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://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
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
```bash
npm i -S @aliatech/loopback-mongo-distinct-mixin
```or Yarn
```bash
yarn add --prod @aliatech/loopback-mongo-distinct-mixin
```## Server configuration
Include the mixin in `server/model-config.json`:
```json
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"../node_modules/@aliatech/loopback-mongo-distinct-mixin/lib",
"../common/mixins"
]
}
}
```## Usage
Enable the mixin in your model definition, ie `person.json`.
### Basic configuration
```json
{
"name": "Person",
"properties": {
"name": "string",
"company": "string"
},
"mixins": {
"Distinct": true
}
}
```Now you can query for distinct values this way:
```js
app.models.Person.distinctValues('name', (err, names) => {
if (err) return next(err);
// 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.
**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:
```json
{
"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.
````js
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````js
app.models.Person.distinctValues((err, names) => {
// Do something
});
````Using default property and a filter:
````js
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````js
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
````bash
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")[](http://alialabs.com "Go to ALIA Technologies' website")