├── .gitignore
├── LICENSE
├── README.md
├── lib
├── model-fragments.js
└── utils.js
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 |
5 | # Runtime data
6 | pids
7 | *.pid
8 | *.seed
9 |
10 | # Directory for instrumented libs generated by jscoverage/JSCover
11 | lib-cov
12 |
13 | # Coverage directory used by tools like istanbul
14 | coverage
15 |
16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17 | .grunt
18 |
19 | # node-waf configuration
20 | .lock-wscript
21 |
22 | # Compiled binary addons (http://nodejs.org/api/addons.html)
23 | build/Release
24 |
25 | # Dependency directory
26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27 | node_modules
28 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Heath Morrison
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # loopback-component-model-fragments
2 |
3 | This component enables you to organize your Loopback models into smaller pieces, which are all collected
4 | and required during the boot phase of your app. The component looks for a subdirectory named after your
5 | model in a set of source folders you specify.
6 |
7 | ## Installation
8 |
9 | ```
10 | npm install loopback-component-model-fragments --save
11 | ```
12 |
13 | ## Configuration
14 |
15 | Place the following block at the very top of `server/component-config.json`:
16 |
17 | ```
18 | "loopback-component-model-fragments": {
19 | "sources": [
20 | "./models",
21 | "../common/models"
22 | ]
23 | }
24 | ```
25 | Two important notes:
26 | - You must put this configuration **above the explorer component**, or any routes from your models won't appear in the explorer.
27 | - It's important and necessary that you specify your model source paths in the component config. (Sorry, at the time being it does not auto-detect paths)
28 |
29 | ## How it works
30 |
31 | The component searches all `sources` paths for sub-directories named after your models and then recursively
32 | loads all modules found under the sub-directory. For example, given you have a model named `MediaReport` and the sample
33 | configuration above, the component will attempt to recursively load the following paths:
34 |
35 | ```
36 | ./models/posts/media-report/**/*.js
37 | ../common/models/media-report/**/*.js
38 | ```
39 |
40 | Each script that it loads is expected to export a single function which takes an `app` handle as a parameter. In this way these files exactly match the behavior of Loopback's normal script file (i.e. `media-report.js`).
41 |
42 | **Regarding app path**: This module uses [app-root-path](https://github.com/inxilpro/node-app-root-path) to determine project root. It then reads package.json from the project root to locate the main server script. If you need to customize the project root folder, use env-var `APP_ROOT_PATH`.
43 |
44 | ## Best practices
45 |
46 | This component will help you build cleaner, more maintainable Loopback model code by breaking down very large model files into descriptive fragments. For example we often structure our model fragments similar to the following,
47 |
48 | ```
49 | common/models/media-report/validations.js
50 | common/models/media-report/hooks.js
51 | common/models/media-report/methods.js
52 | common/models/media-report/overrides/create.js
53 | common/models/media-report/overrides/upsert.js
54 | common/models/media-report/remotes/find-linked.js
55 | common/models/media-report/remotes/find-one-linked.js
56 | common/models/media-report/remotes/find-one-linked.js
57 | ```
58 |
59 | Note: This component does not enforce any particular structure. Use what works best for you.
60 |
61 | ## Contributors
62 |
63 | - Heath Morrison (doublemarked)
64 |
65 | ## License
66 |
67 | loopback-component-model-fragments is published under the MIT license. See [LICENSE](https://github.com/doublemarked/loopback-component-model-fragments/blob/master/LICENSE) for more details.
68 |
--------------------------------------------------------------------------------
/lib/model-fragments.js:
--------------------------------------------------------------------------------
1 | var appRoot = require('app-root-path');
2 | var path = require('path');
3 | var fs = require('fs');
4 |
5 | var utils = require('./utils');
6 |
7 | var COMPONENT_NAME = 'loopback-component-model-fragments';
8 |
9 | module.exports = function loopbackComponentModelFragments(app, options) {
10 | if (!options.sources) {
11 | throw new Error(COMPONENT_NAME+': Missing required parameter \'sources\'!');
12 | }
13 |
14 | var sources = Array.isArray(options.sources) ? options.sources : [options.sources];
15 |
16 | var basePath = options.appRootDir || app.get('appRootDir') || utils.divineLoopbackServerPath();
17 | sources = sources.map(function (s) { return path.join(basePath,s); });
18 |
19 | app.models().forEach( function (Model) {
20 | utils.loadModelFragments(Model, sources)
21 | });
22 |
23 | };
24 |
--------------------------------------------------------------------------------
/lib/utils.js:
--------------------------------------------------------------------------------
1 | var _ = require('lodash');
2 | var path = require('path');
3 | var fs = require('fs');
4 | var appRoot = require('app-root-path');
5 | var requireAll = require('require-all');
6 |
7 | module.exports = {
8 | /* Unfortunately Loopback does not give us a clean way of determining the
9 | * path in which component-config.json exists, making it difficult for us
10 | * to work with relative paths from that file.
11 | * The approach below has flaws, but it will work for the typical
12 | * Loopback app, including any app created with the slc generator.
13 | */
14 | divineLoopbackServerPath: function divineLoopbackServerPath() {
15 | var package = appRoot.require('/package.json');
16 | return path.dirname(appRoot.resolve(package.main));
17 | },
18 |
19 | loadModelFragments: function loadModelFragments(ModelType, sources) {
20 | var modelPath = _.kebabCase(ModelType.modelName);
21 |
22 | // Load any fragments of this model from any of the source paths
23 | sources.forEach(function (s) {
24 | module.exports.loadModelFragmentPath(ModelType, path.join(s, modelPath));
25 | });
26 | },
27 |
28 | loadModelFragmentPath: function loadModelFragment(ModelType, fragmentPath) {
29 | try {
30 | return requireAll({
31 | dirname: fragmentPath,
32 | resolve: function (fragment) {
33 | if (_.isFunction(fragment)) {
34 | return fragment(ModelType);
35 | }
36 |
37 | return fragment;
38 | }
39 | });
40 | } catch (err) {
41 | if (err.code !== 'ENOENT') {
42 | throw err;
43 | }
44 | }
45 |
46 | return null;
47 | }
48 | };
49 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "loopback-component-model-fragments",
3 | "version": "0.1.5",
4 | "description": "Fragment your Loopback models into bite-sized pieces",
5 | "main": "lib/model-fragments.js",
6 | "scripts": {
7 | "test": ""
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "git+https://github.com/doublemarked/loopback-component-model-fragments.git"
12 | },
13 | "keywords": [
14 | "Loopback"
15 | ],
16 | "author": "Heath Morrison (http://govright.org)",
17 | "contributors": [
18 | {
19 | "name": "Heath Morrison",
20 | "email": "heath@govright.org"
21 | }
22 | ],
23 | "license": "MIT",
24 | "bugs": {
25 | "url": "https://github.com/doublemarked/loopback-component-model-fragments/issues"
26 | },
27 | "homepage": "https://github.com/doublemarked/loopback-component-model-fragments#readme",
28 | "dependencies": {
29 | "app-root-path": "^1.0.0",
30 | "lodash": "^4.11.1",
31 | "require-all": "^2.0.0"
32 | },
33 | "devDependencies": {
34 | "eslint": "^2.8.0"
35 | }
36 | }
37 |
--------------------------------------------------------------------------------