Skip to content

Commit

Permalink
Merge branch 'release/1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Akeri committed Mar 8, 2019
2 parents 4a5188d + 91b3ecf commit 796457e
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 91 deletions.
53 changes: 2 additions & 51 deletions .eslintrc.json
@@ -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"
]
}
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -2,7 +2,8 @@

[![Build Status](https://travis-ci.org/aliatech/loopback-mongo-aggregate-mixin.svg?branch=master)](https://travis-ci.org/aliatech/loopback-mongo-aggregate-mixin)
[![Coverage Status](https://coveralls.io/repos/github/aliatech/loopback-mongo-aggregate-mixin/badge.svg?branch=master)](https://coveralls.io/github/aliatech/loopback-mongo-aggregate-mixin?branch=master)
[![npm version](https://badge.fury.io/js/%40aliatech%2Floopback-mongo-aggregate-mixin.svg)](https://badge.fury.io/js/%40aliatech%2Floopback-mongo-aggregate-mixin)
[![npm version](https://img.shields.io/npm/v/@aliatech/loopback-mongo-aggregate-mixin.svg?color=blue)](https://www.npmjs.com/package/@aliatech/loopback-mongo-aggregate-mixin)
<!--![npm total downloads](https://img.shields.io/npm/dt/@aliatech/loopback-mongo-aggregate-mixin.svg?color=9cf)-->

Give models the ability to query native **MongoDB aggregates** and **build instances** from results.

Expand Down Expand Up @@ -247,4 +248,4 @@ Developed by
for
[ALIA Technologies](https://github.com/aliatech "Github's profile")

<img src="http://alialabs.com/images/logos/logo-full-big.png" title="ALIA Technologies" alt="ALIA Technologies" height=100/>
[<img src="http://alialabs.com/images/logos/logo-full-big.png" alt="ALIA Technologies" height=100/>](http://alialabs.com "Go to ALIA Technologies' website")
17 changes: 6 additions & 11 deletions lib/aggregate.js
Expand Up @@ -4,12 +4,10 @@ const _ = require('lodash');
const Aggregation = require('./aggregation');
const rewriteId = require('./rewrite-id');
const debug = require('debug')('loopback:mixins:aggregate');
const BuildBehavior = require('./build');

const buildBehavior = require('./build');

module.exports = function (Model, options) {

BuildBehavior(Model);
buildBehavior(Model);

const settings = _.merge({
build: true, // Build model instances right after getting results from MongoDB
Expand All @@ -28,7 +26,6 @@ module.exports = function (Model, options) {
* @param {Object} filter Loopback query filter.
* @param {Object} options Loopback query options.
* @param {Function} next Callback.
* @returns {*}
*/
Model.aggregate = function (filter, options, next) {
if (options === undefined && next === undefined) {
Expand Down Expand Up @@ -93,7 +90,7 @@ module.exports = function (Model, options) {
}
debug('Exec pipeline', JSON.stringify(aggregation.pipeline));
const cursor = aggregation.exec(connector.collection(modelName));
return cursor.toArray(function (err, data) {
cursor.toArray((err, data) => {
if (err) return next(err);
const build = _.get(options, 'build', true);
const docs = data.map(rewriteId);
Expand All @@ -117,8 +114,8 @@ module.exports = function (Model, options) {

/**
* State what fields correspond to relations given a where filter.
* @param {Object} where Where filter
* @returns {String[]} Fields which correspond to relation names.
* @param {Object} where Where filter.
* @returns {string[]} Fields which correspond to relation names.
*/
Model.whichFieldsAreRelational = function (where) {
return _.keys(where).filter((key) => {
Expand All @@ -132,7 +129,6 @@ module.exports = function (Model, options) {
* @param {Aggregation} aggregate Aggregation to be mutated.
* @param {Object} where Where filter. Will search for properties with dot notation.
* @param {Relation} [parentRelation] States that is a nested relation of this one.
* @returns {void}
*/
function buildLookup (aggregate, where, parentRelation = null) {
_.each(where, (value, key) => {
Expand All @@ -149,7 +145,7 @@ module.exports = function (Model, options) {
unwindPath = `${parentRelationName}.${unwindPath}`;
}
aggregate.unwind({
path: '$' + unwindPath,
path: `$${ unwindPath}`,
preserveNullAndEmptyArrays: true,
});
}
Expand Down Expand Up @@ -181,5 +177,4 @@ module.exports = function (Model, options) {
as: relationName,
};
}

};
1 change: 0 additions & 1 deletion lib/aggregation.js
Expand Up @@ -2,7 +2,6 @@

const _ = require('lodash');


module.exports = class Aggregation {

/**
Expand Down
10 changes: 4 additions & 6 deletions lib/build.js
Expand Up @@ -13,7 +13,6 @@ const List = require(path.join(__dirname, '../node_modules/loopback-datasource-j
* @returns {void}
*/
module.exports = function (Model) {

/**
* Build model instances.
* @param {Object[]} result Documents from database.
Expand All @@ -34,13 +33,13 @@ module.exports = function (Model) {
Model.buildResultItem(resultItem, filter, nextItem);
} else {
const context = {
Model: Model,
Model,
data: resultItem,
isNewInstance: false,
hookState: hookState,
options: options,
hookState,
options,
};
Model.notifyObserversOf('loaded', context, function (err) {
Model.notifyObserversOf('loaded', context, (err) => {
/* istanbul ignore if */
if (err) return nextItem(err);
Model.buildResultItem(context.data, filter, nextItem);
Expand Down Expand Up @@ -117,5 +116,4 @@ module.exports = function (Model) {
}
});
}

};
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "@aliatech/loopback-mongo-aggregate-mixin",
"version": "1.0.0",
"version": "1.0.1",
"description": "Loopback mixin to query MongoDB aggregation pipeline and build the instances from results",
"publishConfig": {
"access": "public"
Expand All @@ -17,7 +17,8 @@
"url": "https://github.com/aliatech/loopback-mongo-aggregate-mixin.git"
},
"scripts": {
"lint": "eslint index.js",
"lint": "eslint lib",
"lint-fix": "eslint --fix lib",
"test": "mocha",
"test-with-coverage": "nyc --reporter=text --reporter=text-summary --reporter=html mocha",
"publish-coverage": "nyc report --reporter=text-lcov | coveralls"
Expand All @@ -37,9 +38,9 @@
"lodash": "^4.17.11"
},
"devDependencies": {
"@aliatech/eslint-config-aliatech": "^0.1.0",
"coveralls": "^3.0.2",
"eslint": "^5.13.0",
"eslint-config-google": "^0.12.0",
"loopback": "^3.25.0",
"loopback-boot": "^2.27.1",
"loopback-connector-mongodb": "^3.9.2",
Expand Down
5 changes: 5 additions & 0 deletions test/.eslintrc.json
@@ -0,0 +1,5 @@
{
"extends": [
"@aliatech/aliatech/env-mocha"
]
}
7 changes: 1 addition & 6 deletions test/test.js
@@ -1,15 +1,11 @@
/* eslint-disable require-jsdoc */

'use strict';

const _ = require('lodash');
const should = require('should');
const Seeder = require('./fixtures/simple-app/seeder');
const rewriteId = require('../lib/rewrite-id');


describe('Aggregate features', function () {

describe('Aggregate features', () => {
let City, Company, Person, assert, context;

before((done) => {
Expand Down Expand Up @@ -450,5 +446,4 @@ describe('Aggregate features', function () {
done();
});
});

});
10 changes: 5 additions & 5 deletions yarn.lock
Expand Up @@ -2,6 +2,11 @@
# yarn lockfile v1


"@aliatech/eslint-config-aliatech@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@aliatech/eslint-config-aliatech/-/eslint-config-aliatech-0.1.0.tgz#601096c9ac2b31212c05a89fb905d1b839fbee5a"
integrity sha512-j5udyyoKjCN6Ih2OQqvA9KamU707NJYJwySbAGgzUSndSs0v0FmtKFwQTl1ZO6z9ecg8QehcD03muoB2hx2X1w==

"@babel/code-frame@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0.tgz#06e2ab19bdb535385559aabb5ba59729482800f8"
Expand Down Expand Up @@ -879,11 +884,6 @@ escape-string-regexp@1.0.5, escape-string-regexp@^1.0.5:
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=

eslint-config-google@^0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/eslint-config-google/-/eslint-config-google-0.12.0.tgz#b9bcc52d0f24cf946e862fe8b09c773ad21e511b"
integrity sha512-SHDM3nIRCJBACjf8c/H6FvCwRmKbphESNl3gJFBNbw4KYDLCONB3ABYLXDGF+iaVP9XSTND/Q5/PuGoFkp4xbg==

eslint-scope@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-4.0.0.tgz#50bf3071e9338bcdc43331794a0cb533f0136172"
Expand Down

0 comments on commit 796457e

Please sign in to comment.