Skip to content

Commit

Permalink
Merge branch 'release/1.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanfoster committed Oct 25, 2017
2 parents 7077afc + 65857c0 commit 6b6d5d0
Show file tree
Hide file tree
Showing 22 changed files with 443 additions and 45 deletions.
4 changes: 0 additions & 4 deletions .babelrc

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/npm-debug.log
docs
.nyc_output
yarn-error.log
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<a name="1.3.0"></a>
# [1.3.0](https://github.com/dylanfoster/parch/compare/1.2.0...1.3.0) (2017-10-24)


### Features

* **serializers:** add RestSerializer ([0cfca6a](https://github.com/dylanfoster/parch/commit/0cfca6a))
* **store:** add json serializer ([77406aa](https://github.com/dylanfoster/parch/commit/77406aa))



<a name="1.2.0"></a>
# [1.2.0](https://github.com/dylanfoster/parch/compare/1.1.1...1.2.0) (2017-10-19)

Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ const Route = require("./lib/route");
const RouteSegment = require("./lib/route_segment");
const Store = require("./lib/store");
const containment = require("./lib/containment");
const serializers = require("./lib/serializers");

/**
* @module parch
*/
module.exports = {
Application,
Controller,
JSONSerializer: serializers.JSONSerializer,
Model,
RestSerializer: serializers.RestSerializer,
Route,
RouteSegment,
Store,
Expand Down
17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "parch",
"version": "1.2.0",
"version": "1.3.0",
"description": "Restify + Sequelize",
"main": "index.js",
"repository": "dylanfoster/parch",
Expand All @@ -19,7 +19,8 @@
"lint": "eslint ./src ./test",
"prepare-release": "curl -sL https://gist.githubusercontent.com/dylanfoster/35b06db9aaa9237da77eff4a8eec0a22/raw/83ec714ec64f7ec36c5004588aa259354fc87b51/prepare-release.sh | bash -s",
"prepublish": "yarn build",
"test": "NO_DEPRECATION=parch NODE_ENV=test mocha --recursive --compilers js:babel-register --timeout 6000",
"pretest": "yarn build",
"test": "NO_DEPRECATION=parch NODE_ENV=test mocha --recursive --compilers js:babel-register --timeout 12000",
"watch:test": "chokidar 'src/**' 'test/**/*.js' -c 'yarn test' --initial",
"watch:build": "chokidar 'src/**' 'test/**/*.js' -c 'yarn build' --initial",
"watch:cover": "chokidar 'src/**' 'test/**/*.js' -c 'yarn cover' --initial"
Expand Down Expand Up @@ -48,7 +49,9 @@
"yuidocjs": "^0.10.2"
},
"dependencies": {
"@parch-js/orm": "^0.0.2",
"@parch-js/json-serializer": "^0.0.1",
"@parch-js/orm": "^0.0.3",
"@parch-js/rest-serializer": "^0.1.1",
"bunyan": "^1.8.1",
"callsite": "^1.0.0",
"depd": "^1.1.0",
Expand Down Expand Up @@ -88,6 +91,14 @@
]
}
},
"babel": {
"presets": [
"es2015"
],
"plugins": [
"babel-plugin-add-module-exports"
]
},
"nyc": {
"reporter": [
"text",
Expand Down
18 changes: 15 additions & 3 deletions src/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,19 @@ class Application {
const projectDirectory = this._getProjectDirectory();
const registry = this.registry = new Registry();

this.DEFAULT_CONTROLLER_LOOKUP_PATH = resolve(projectDirectory, "controllers");
this.DEFAULT_INITIALIZERS_LOOKUP_PATH = resolve(projectDirectory, "initializers");
this.DEFAULT_CONTROLLER_LOOKUP_PATH = resolve(
projectDirectory,
"controllers"
);
this.DEFAULT_INITIALIZERS_LOOKUP_PATH = resolve(
projectDirectory,
"initializers"
);
this.DEFAULT_MODEL_LOOKUP_PATH = resolve(projectDirectory, "models");
this.DEFAULT_SERIALIZER_LOOKUP_PATH = resolve(
projectDirectory,
"serializers"
);
options = this._configure(options);

registry.register("config:main", options);
Expand All @@ -47,9 +57,9 @@ class Application {
this._initialize("loaders");
this._initialize("model-manager");
this._initialize("models");
this._initialize("store");
this._initialize("middleware");
this._initialize("router");
this._initialize("store");
this._initialize("application");
}

Expand Down Expand Up @@ -115,6 +125,8 @@ class Application {
config.initializers = config.initializers || {};
config.initializers.dir = config.initializers.dir || this.DEFAULT_INITIALIZERS_LOOKUP_PATH;
config.logging = config.logging || {};
config.serializers = config.serializers || {};
config.serializers.dir = config.serializers.dir || this.DEFAULT_SERIALIZER_LOOKUP_PATH;
config.server = config.server || {};
config.server.middlewares = config.server.middlewares || [];

Expand Down
10 changes: 10 additions & 0 deletions src/initializers/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ module.exports = {
type: "model",
path: config.database.models.dir
});
let serializerLoader;

try {
serializerLoader = new Loader({
filter: /(.*).js$/,
type: "serializer",
path: config.serializers.dir
});
} catch (err) {}

registry.register("loader:controller", controllerLoader);
registry.register("loader:model", modelLoader);
registry.register("loader:serializer", serializerLoader);
},

name: "loaders"
Expand Down
3 changes: 1 addition & 2 deletions src/initializers/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import Store from "../store";

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

registry.register("service:store", store, { singleton: true });
registry.inject(application, "service:store", "store");
Expand Down
2 changes: 1 addition & 1 deletion src/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default class Registry {
}

/**
* Find an object in the registry. If the object isn't found in the registry
* Find an object in the registry. If the object isn't found in the registry,
* lookup will attempt to find it by requiring it in. If the require fails
* the lookup fails
*
Expand Down
44 changes: 43 additions & 1 deletion src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import inflect from "inflect";

import { JSONSerializer } from "./serializers";
import Route from "./route";
import { getOwner, setOwner } from "./containment";

Expand Down Expand Up @@ -208,15 +209,56 @@ class Router {
*/
_loadControllers() {
const controllerLoader = getOwner(this).lookup("loader:controller");
const modelLoader = getOwner(this).lookup("loader:model");
const { modules: controllers } = controllerLoader;
const { modules: models } = modelLoader;

Object.keys(controllers).forEach(controller => {
const Klass = controllers[controller];
const instance = new Klass(getOwner(this));
const instanceName = inflect.singularize(instance.name);
const registry = getOwner(this);

getOwner(this).register(`controller:${instanceName}`, instance);
registry.register(`controller:${instanceName}`, instance);
});

Object.keys(models).forEach(model => {
const instanceName = inflect.singularize(model);
const registry = getOwner(this);
const serializer = this._lookupSerializer(instanceName);

registry.register(`serializer:${instanceName}`, serializer);
});
}

/**
* Attempts to lookup a serializer by 'name' in the module loader. If one exists
* it is instantiated and registered by 'name'. If one does not exist the
* default (RestSerializer at the moment) is instantiated and registered.
*
* @method _lookupSerializer
* @private
* @param {String} name lowercase singular lookup name (e.g. "user")
* @return {Object} serializer instance
*/
_lookupSerializer(name) {
const serializerLoader = getOwner(this).lookup("loader:serializer");

if (!serializerLoader) {
return new JSONSerializer();
}

const { modules: serializers } = serializerLoader;
const Serializer = serializers[name];
let serializer;

if (Serializer) {
serializer = new Serializer();
} else {
serializer = new JSONSerializer();
}

return serializer;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/serializers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";

export { default as JSONSerializer } from "./json";
export { default as RestSerializer } from "./rest";
10 changes: 10 additions & 0 deletions src/serializers/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

import Serializer from "@parch-js/json-serializer";

/**
* @class JSONSerializer
* @constructor
* @extends <a href="parch-js/json-serializer"" target="_blank"> Parch JSONSerializer</a>
*/
export default class JSONSerializer extends Serializer {}
10 changes: 10 additions & 0 deletions src/serializers/rest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

import Serializer from "@parch-js/rest-serializer";

/**
* @class RestSerializer
* @constructor
* @extends <a href="https://github.com/parch-js/rest-serializer"" target="_blank"> Parch RestSerializer</a>
*/
export default class RestSerializer extends Serializer {}
98 changes: 96 additions & 2 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,103 @@

import ORM from "@parch-js/orm";

import { getOwner, setOwner } from "./containment";

/**
* Parch overrides the base ORM class to implement the serializer defined for
* each controller. If no serializer is defined, the
* {{#crossLink "RestSerializer"}}RestSerializer{{/crossLink}} is used.
*
* @class Store
* @constructor
* @extends <a href="https://github.com/parch-js/orm" target="_blank">Parch ORM</a>
* @extends <a href="parch-js/orm" target="_blank">Parch ORM</a>
*/
export default class Store extends ORM {}
export default class Store extends ORM {
constructor(registry) {
const models = registry.lookup("service:model-manager").models;

super(models);

setOwner(this, registry);
}

/**
* createRecord
*
* @param name
* @returns {undefined}
*/
createRecord(name) {
const serializer = this._lookupSerializer(name);

return super.createRecord(...arguments).then(
record => serializer.normalizeResponse(record, "createRecord")
);
}

/**
* findAll
*
* @param name
* @returns {undefined}
*/
findAll(name) {
const serializer = this._lookupSerializer(name);

return super.findAll(...arguments).then(
records => serializer.normalizeResponse(records, "findAll", name)
);
}

/**
* findOne
*
* @param name
* @returns {undefined}
*/
findOne(name) {
const serializer = this._lookupSerializer(name);

return super.findOne(...arguments).then(
record => serializer.normalizeResponse(record, "findOne", name)
);
}

/**
* queryRecord
*
* @param name
* @returns {undefined}
*/
queryRecord(name) {
const serializer = this._lookupSerializer(name);

return super.queryRecord(...arguments).then(
record => serializer.normalizeResponse(record, "queryRecord", name)
);
}

/**
* updateRecord
*
* @param name
* @returns {undefined}
*/
updateRecord(name) {
const serializer = this._lookupSerializer(name);

return super.updateRecord(...arguments).then(
record => serializer.normalizeResponse(record, "updateRecord", name)
);
}

/**
* _lookupSerializer
*
* @param name
* @returns {undefined}
*/
_lookupSerializer(name) {
return getOwner(this).lookup(`serializer:${name}`);
}
}

0 comments on commit 6b6d5d0

Please sign in to comment.