-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1bf69b8
Showing
9 changed files
with
526 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "loopback", | ||
"rules": { | ||
"max-len": ["error", 100, 4, { | ||
"ignoreComments": true, | ||
"ignoreUrls": true, | ||
"ignorePattern": "^\\s*var\\s.+=\\s*(require\\s*\\()|(/)" | ||
}] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
################# | ||
## Misc | ||
################# | ||
**/.DS_Store | ||
node_modules/* | ||
npm-debug.log | ||
coverage | ||
|
||
################# | ||
## JetBrains | ||
################# | ||
.idea | ||
.project | ||
.settings | ||
.idea/* | ||
*.iml | ||
|
||
############ | ||
## Windows | ||
############ | ||
|
||
# Windows image file caches | ||
Thumbs.db | ||
|
||
# Folder config file | ||
Desktop.ini | ||
|
||
############ | ||
## Mac | ||
############ | ||
|
||
# Mac crap | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
################# | ||
## Misc | ||
################# | ||
**/.DS_Store | ||
node_modules/* | ||
npm-debug.log | ||
coverage | ||
test/ | ||
|
||
################# | ||
## JetBrains | ||
################# | ||
.idea | ||
.project | ||
.settings | ||
.idea/* | ||
*.iml | ||
|
||
############ | ||
## Windows | ||
############ | ||
|
||
# Windows image file caches | ||
Thumbs.db | ||
|
||
# Folder config file | ||
Desktop.ini | ||
|
||
############ | ||
## Mac | ||
############ | ||
|
||
# Mac crap | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
language: node_js | ||
cache: | ||
directories: | ||
- node_modules | ||
notifications: | ||
email: false | ||
node_js: | ||
- '7' | ||
- '6' | ||
- '4' | ||
before_script: | ||
- npm prune | ||
after_success: | ||
- npm run semantic-release | ||
branches: | ||
except: | ||
- /^v\d+\.\d+\.\d+$/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015-2016 angulartics | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
# loopback-include-through-mixin | ||
|
||
[![NPM version][npm-image]][npm-url] [![NPM downloads][npm-downloads-image]][npm-downloads-url] | ||
[](https://david-dm.org/JonnyBGod/loopback-include-through-mixin#info=devDependencies) | ||
[](https://travis-ci.org/JonnyBGod/loopback-include-through-mixin) | ||
|
||
[![MIT license][license-image]][license-url] | ||
[](https://gitter.im/JonnyBGod/loopback-include-through-mixin) | ||
|
||
##Features | ||
|
||
- include though model properties with queries | ||
- setup default bahavior | ||
- use as mixin | ||
|
||
##Installation | ||
|
||
```bash | ||
npm install loopback-include-through-mixin --save | ||
``` | ||
|
||
##How to use | ||
|
||
|
||
Add the mixins property to your server/model-config.json like the following: | ||
|
||
```json | ||
{ | ||
"_meta": { | ||
"sources": [ | ||
"loopback/common/models", | ||
"loopback/server/models", | ||
"../common/models", | ||
"./models" | ||
], | ||
"mixins": [ | ||
"loopback/common/mixins", | ||
"../node_modules/loopback-include-through-mixin", | ||
"../common/mixins" | ||
] | ||
} | ||
} | ||
|
||
``` | ||
|
||
To use with your Models add the mixins attribute to the definition object of your model config. | ||
|
||
```json | ||
{ | ||
"name": "app", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
} | ||
}, | ||
"relations": { | ||
"users": { | ||
"type": "hasMany", | ||
"model": "user", | ||
"foreignKey": "appId", | ||
"through": "userRole" | ||
} | ||
}, | ||
"mixins": { | ||
"IncludeThrough": true, | ||
} | ||
} | ||
``` | ||
|
||
Then use in you queries like: | ||
|
||
```json | ||
{ | ||
where: ... | ||
include: ... | ||
includeThrough: true | ||
} | ||
``` | ||
|
||
```json | ||
{ | ||
where: ... | ||
include: ... | ||
includeThrough: { | ||
fields: 'type' | ||
} | ||
} | ||
``` | ||
|
||
You can also set default behavior in your model definition with options. | ||
|
||
```json | ||
{ | ||
"name": "app", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
} | ||
}, | ||
"relations": { | ||
"users": { | ||
"type": "hasMany", | ||
"model": "user", | ||
"foreignKey": "appId", | ||
"through": "userRole" | ||
} | ||
}, | ||
"mixins": { | ||
"IncludeThrough": { | ||
"relations": [ | ||
"users" | ||
], | ||
"fields": { | ||
"users": "type" | ||
} | ||
}, | ||
} | ||
} | ||
``` | ||
|
||
Example of Through Model: | ||
|
||
```json | ||
{ | ||
"name": "userRole", | ||
"properties": { | ||
"type": { | ||
"type": "string", | ||
"required": true, | ||
"default": "owner", | ||
"description": "owner | administrator | collaborator" | ||
} | ||
}, | ||
"validations": [], | ||
"relations": { | ||
"app": { | ||
"type": "belongsTo", | ||
"model": "app", | ||
"foreignKey": "appId" | ||
}, | ||
"user": { | ||
"type": "belongsTo", | ||
"model": "user", | ||
"foreignKey": "userId" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
##Options | ||
|
||
| option | type | description | required | | ||
| ------ | ---- | ----------- | -------- | | ||
|relations| [String] | select relations | false | | ||
|fields| Key/Value Object | similar to filter fields. Key: relation; Value: fields filter. | false | | ||
|
||
- By setting relations in model definition it will return the though model for the specified relations by default | ||
- By passing **includeThrough** in you query filter it will override default **fields** | ||
|
||
## License | ||
|
||
[MIT](LICENSE) | ||
|
||
[npm-image]: https://img.shields.io/npm/v/angulartics2.svg | ||
[npm-url]: https://npmjs.org/package/angulartics2 | ||
[npm-downloads-image]: https://img.shields.io/npm/dm/angulartics2.svg | ||
[npm-downloads-url]: https://npmjs.org/package/angulartics2 | ||
[bower-image]: https://img.shields.io/bower/v/angulartics2.svg | ||
[bower-url]: http://bower.io/search/?q=angulartics2 | ||
[dep-status-image]: https://img.shields.io/david/angulartics/angulartics2.svg | ||
[dep-status-url]: https://david-dm.org/angulartics/angulartics2 | ||
[license-image]: http://img.shields.io/badge/license-MIT-blue.svg | ||
[license-url]: LICENSE | ||
[slack-image]: https://angulartics2.herokuapp.com/badge.svg | ||
[slack-url]: https://angulartics2.herokuapp.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict'; | ||
|
||
module.exports = function(Model, options) { | ||
Model.on('attached', function() { | ||
var relations = Model.relations; | ||
|
||
if (relations) { | ||
Object.keys(relations).forEach(function(targetModel) { | ||
var type = | ||
relations[targetModel].modelThrough ? 'hasManyThrough' : relations[targetModel].type; | ||
|
||
if (type === 'hasManyThrough') { | ||
Model.afterRemote('prototype.__get__' + targetModel, injectIncludes); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
function injectIncludes(ctx, unused, next) { | ||
if (!ctx.result || !ctx.result.length) return next(); | ||
|
||
var relationName = ctx.methodString.match(/__([a-z\d]+)$/)[1]; | ||
|
||
if ( | ||
!(options.relations && options.relations.indexOf(relationName) !== -1) && | ||
!(ctx.args.filter && JSON.parse(ctx.args.filter).includeThrough) | ||
) return next(); | ||
|
||
var relationKey = Model.relations[relationName].keyTo; | ||
var throughKey = Model.relations[relationName].keyThrough; | ||
var relationModel = Model.relations[relationName].modelTo; | ||
var throughModel = Model.relations[relationName].modelThrough; | ||
var idName = relationModel.definition.idName() || 'id'; | ||
|
||
var newResult = JSON.parse(JSON.stringify(ctx.result)); | ||
|
||
var query = {where: {}}; | ||
query.where[relationKey] = ctx.instance.id; | ||
query.where[throughKey] = {inq: newResult.map(function(item) { return item.id; })}; | ||
|
||
if ( | ||
ctx.args.filter && | ||
JSON.parse(ctx.args.filter).includeThrough && | ||
JSON.parse(ctx.args.filter).includeThrough.fields | ||
) { | ||
query.fields = [throughKey, JSON.parse(ctx.args.filter).includeThrough.fields]; | ||
} else if (options.fields && options.fields[relationName]) { | ||
query.fields = [throughKey, options.fields[relationName]]; | ||
} | ||
|
||
throughModel.find(query, function(err, results) { | ||
if (err) return next(); | ||
|
||
var resultsHash = {}; | ||
results.forEach(function(result) { | ||
resultsHash[result[throughKey].toString()] = result; | ||
}); | ||
|
||
for (var i = 0; i < newResult.length; i++) { | ||
if (resultsHash[newResult[i][idName].toString()]) { | ||
newResult[i][throughModel.definition.name] = resultsHash[newResult[i][idName].toString()]; | ||
} | ||
} | ||
|
||
ctx.result = newResult; | ||
|
||
next(); | ||
}); | ||
}; | ||
}; |
Oops, something went wrong.