Skip to content

Commit

Permalink
Release 1.0.2
Browse files Browse the repository at this point in the history
This release includes custom primary key support by checking the
model configuration dynamically.

- Added Changelog
- Added Custom Primary Key Support
- Added Validation on Stats Wrapper to verify if methods exist
  • Loading branch information
Jonathan Casarrubias committed Apr 1, 2016
1 parent 3e21ef8 commit e106446
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[![NPM](https://nodei.co/npm/loopback-stats-mixin.png?compact=true)](https://nodei.co/npm/loopback-stats-mixin/)

Loopback Stats Mixin - CHANGELOG
=============

The **loopback-stats-mixin** module change .

- **Version 1.0.2**.-
- Added validation to verify micro-services to be wrapped actually exists.
- Added custom primary key support.
- **Version 1.0.1**.-
- Added Stats Mixin Wrapper
- **Version 1.0.0**.-
- Created Stats Mixin
- Initial Publish

For issues or contributions: https://github.com/jonathan-casarrubias/loopback-stats-mixin.


LICENSE
=============
[MTI](LICENSE)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "loopback-stats-mixin",
"version": "1.0.1",
"version": "1.0.2",
"author": "Jonathan Casarrubias <http://github.com/jonathan-casarrubias>",
"description": "A mixin to provide statistical functionallity for Loopback Models, Relations and Nested Datasets",
"main": "index.js",
Expand Down
18 changes: 16 additions & 2 deletions stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ module.exports = function(Model, ctx) {
now: ctx.now,
nowISOString: ctx.nowISOString,
params: {
pk: new PrimaryKeyBuilder(Model).build(),
id: ctx.params.id,
relation: ctx.params.relation
}
};

if (ctx.type === 'model') {
options.params.where = ctx.params.where;
options.params.range = ctx.params.range;
Expand Down Expand Up @@ -219,7 +219,8 @@ class QueryBuilder {
query.where = this.ctx.params.where || {};
// If stat type is relation, then we set the root id
if ((this.ctx.type === 'relation' || this.ctx.type === 'nested') && this.ctx.params.id)
query.where.id = this.ctx.params.id;
query.where[this.ctx.params.pk] = this.ctx.params.id;
// query.where[this.ctx.Model.settings.relations[this.ctx.params.relation].] = this.ctx.params.id;
// If stat type is relation, then we set the root id
if (this.ctx.type === 'relation' && this.ctx.params.relation)
query.include = this.ctx.params.relation;
Expand Down Expand Up @@ -292,3 +293,16 @@ class AcceptBuilder {
return accepts;
}
}
/**
* Builds the primary key name depending on model configurations
*/
class PrimaryKeyBuilder {
constructor(Model) { this.Model = Model; }
build() {
let pk = 'id';
if (!this.Model.settings.idInjection)
for (let key in this.Model.rawProperties)
if (this.Model.rawProperties[key].id) pk = key;
return pk;
}
}
6 changes: 5 additions & 1 deletion stats_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ module.exports = function(Model, ctx) {
ctx.result[item] = dataset;
next();
};
Model[item].apply(Model, Array.from(ctx.args));
if (Model[item]) {
Model[item].apply(Model, Array.from(ctx.args));
} else {
next(new Error(Model.definition.name + '.' + item + ' does not exist, verify your configuration.'));
}
}, err => ctx.next(err, ctx.result));
};
/**
Expand Down

0 comments on commit e106446

Please sign in to comment.