Skip to content

Commit 6b6d5d0

Browse files
committed
Merge branch 'release/1.3.0'
2 parents 7077afc + 65857c0 commit 6b6d5d0

File tree

22 files changed

+443
-45
lines changed

22 files changed

+443
-45
lines changed

.babelrc

Lines changed: 0 additions & 4 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/npm-debug.log
66
docs
77
.nyc_output
8+
yarn-error.log

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
<a name="1.3.0"></a>
2+
# [1.3.0](https://github.com/dylanfoster/parch/compare/1.2.0...1.3.0) (2017-10-24)
3+
4+
5+
### Features
6+
7+
* **serializers:** add RestSerializer ([0cfca6a](https://github.com/dylanfoster/parch/commit/0cfca6a))
8+
* **store:** add json serializer ([77406aa](https://github.com/dylanfoster/parch/commit/77406aa))
9+
10+
11+
112
<a name="1.2.0"></a>
213
# [1.2.0](https://github.com/dylanfoster/parch/compare/1.1.1...1.2.0) (2017-10-19)
314

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ const Route = require("./lib/route");
77
const RouteSegment = require("./lib/route_segment");
88
const Store = require("./lib/store");
99
const containment = require("./lib/containment");
10+
const serializers = require("./lib/serializers");
1011

1112
/**
1213
* @module parch
1314
*/
1415
module.exports = {
1516
Application,
1617
Controller,
18+
JSONSerializer: serializers.JSONSerializer,
1719
Model,
20+
RestSerializer: serializers.RestSerializer,
1821
Route,
1922
RouteSegment,
2023
Store,

package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "parch",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "Restify + Sequelize",
55
"main": "index.js",
66
"repository": "dylanfoster/parch",
@@ -19,7 +19,8 @@
1919
"lint": "eslint ./src ./test",
2020
"prepare-release": "curl -sL https://gist.githubusercontent.com/dylanfoster/35b06db9aaa9237da77eff4a8eec0a22/raw/83ec714ec64f7ec36c5004588aa259354fc87b51/prepare-release.sh | bash -s",
2121
"prepublish": "yarn build",
22-
"test": "NO_DEPRECATION=parch NODE_ENV=test mocha --recursive --compilers js:babel-register --timeout 6000",
22+
"pretest": "yarn build",
23+
"test": "NO_DEPRECATION=parch NODE_ENV=test mocha --recursive --compilers js:babel-register --timeout 12000",
2324
"watch:test": "chokidar 'src/**' 'test/**/*.js' -c 'yarn test' --initial",
2425
"watch:build": "chokidar 'src/**' 'test/**/*.js' -c 'yarn build' --initial",
2526
"watch:cover": "chokidar 'src/**' 'test/**/*.js' -c 'yarn cover' --initial"
@@ -48,7 +49,9 @@
4849
"yuidocjs": "^0.10.2"
4950
},
5051
"dependencies": {
51-
"@parch-js/orm": "^0.0.2",
52+
"@parch-js/json-serializer": "^0.0.1",
53+
"@parch-js/orm": "^0.0.3",
54+
"@parch-js/rest-serializer": "^0.1.1",
5255
"bunyan": "^1.8.1",
5356
"callsite": "^1.0.0",
5457
"depd": "^1.1.0",
@@ -88,6 +91,14 @@
8891
]
8992
}
9093
},
94+
"babel": {
95+
"presets": [
96+
"es2015"
97+
],
98+
"plugins": [
99+
"babel-plugin-add-module-exports"
100+
]
101+
},
91102
"nyc": {
92103
"reporter": [
93104
"text",

src/application.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,19 @@ class Application {
3636
const projectDirectory = this._getProjectDirectory();
3737
const registry = this.registry = new Registry();
3838

39-
this.DEFAULT_CONTROLLER_LOOKUP_PATH = resolve(projectDirectory, "controllers");
40-
this.DEFAULT_INITIALIZERS_LOOKUP_PATH = resolve(projectDirectory, "initializers");
39+
this.DEFAULT_CONTROLLER_LOOKUP_PATH = resolve(
40+
projectDirectory,
41+
"controllers"
42+
);
43+
this.DEFAULT_INITIALIZERS_LOOKUP_PATH = resolve(
44+
projectDirectory,
45+
"initializers"
46+
);
4147
this.DEFAULT_MODEL_LOOKUP_PATH = resolve(projectDirectory, "models");
48+
this.DEFAULT_SERIALIZER_LOOKUP_PATH = resolve(
49+
projectDirectory,
50+
"serializers"
51+
);
4252
options = this._configure(options);
4353

4454
registry.register("config:main", options);
@@ -47,9 +57,9 @@ class Application {
4757
this._initialize("loaders");
4858
this._initialize("model-manager");
4959
this._initialize("models");
50-
this._initialize("store");
5160
this._initialize("middleware");
5261
this._initialize("router");
62+
this._initialize("store");
5363
this._initialize("application");
5464
}
5565

@@ -115,6 +125,8 @@ class Application {
115125
config.initializers = config.initializers || {};
116126
config.initializers.dir = config.initializers.dir || this.DEFAULT_INITIALIZERS_LOOKUP_PATH;
117127
config.logging = config.logging || {};
128+
config.serializers = config.serializers || {};
129+
config.serializers.dir = config.serializers.dir || this.DEFAULT_SERIALIZER_LOOKUP_PATH;
118130
config.server = config.server || {};
119131
config.server.middlewares = config.server.middlewares || [];
120132

src/initializers/loaders.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@ module.exports = {
1313
type: "model",
1414
path: config.database.models.dir
1515
});
16+
let serializerLoader;
17+
18+
try {
19+
serializerLoader = new Loader({
20+
filter: /(.*).js$/,
21+
type: "serializer",
22+
path: config.serializers.dir
23+
});
24+
} catch (err) {}
1625

1726
registry.register("loader:controller", controllerLoader);
1827
registry.register("loader:model", modelLoader);
28+
registry.register("loader:serializer", serializerLoader);
1929
},
2030

2131
name: "loaders"

src/initializers/store.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import Store from "../store";
44

55
module.exports = {
66
initialize(application, registry) {
7-
const models = registry.lookup("service:model-manager").models;
8-
const store = new Store(models);
7+
const store = new Store(registry);
98

109
registry.register("service:store", store, { singleton: true });
1110
registry.inject(application, "service:store", "store");

src/registry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default class Registry {
6363
}
6464

6565
/**
66-
* Find an object in the registry. If the object isn't found in the registry
66+
* Find an object in the registry. If the object isn't found in the registry,
6767
* lookup will attempt to find it by requiring it in. If the require fails
6868
* the lookup fails
6969
*

src/router.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import inflect from "inflect";
44

5+
import { JSONSerializer } from "./serializers";
56
import Route from "./route";
67
import { getOwner, setOwner } from "./containment";
78

@@ -208,15 +209,56 @@ class Router {
208209
*/
209210
_loadControllers() {
210211
const controllerLoader = getOwner(this).lookup("loader:controller");
212+
const modelLoader = getOwner(this).lookup("loader:model");
211213
const { modules: controllers } = controllerLoader;
214+
const { modules: models } = modelLoader;
212215

213216
Object.keys(controllers).forEach(controller => {
214217
const Klass = controllers[controller];
215218
const instance = new Klass(getOwner(this));
216219
const instanceName = inflect.singularize(instance.name);
220+
const registry = getOwner(this);
217221

218-
getOwner(this).register(`controller:${instanceName}`, instance);
222+
registry.register(`controller:${instanceName}`, instance);
219223
});
224+
225+
Object.keys(models).forEach(model => {
226+
const instanceName = inflect.singularize(model);
227+
const registry = getOwner(this);
228+
const serializer = this._lookupSerializer(instanceName);
229+
230+
registry.register(`serializer:${instanceName}`, serializer);
231+
});
232+
}
233+
234+
/**
235+
* Attempts to lookup a serializer by 'name' in the module loader. If one exists
236+
* it is instantiated and registered by 'name'. If one does not exist the
237+
* default (RestSerializer at the moment) is instantiated and registered.
238+
*
239+
* @method _lookupSerializer
240+
* @private
241+
* @param {String} name lowercase singular lookup name (e.g. "user")
242+
* @return {Object} serializer instance
243+
*/
244+
_lookupSerializer(name) {
245+
const serializerLoader = getOwner(this).lookup("loader:serializer");
246+
247+
if (!serializerLoader) {
248+
return new JSONSerializer();
249+
}
250+
251+
const { modules: serializers } = serializerLoader;
252+
const Serializer = serializers[name];
253+
let serializer;
254+
255+
if (Serializer) {
256+
serializer = new Serializer();
257+
} else {
258+
serializer = new JSONSerializer();
259+
}
260+
261+
return serializer;
220262
}
221263

222264
/**

0 commit comments

Comments
 (0)