Skip to content

Commit

Permalink
feat: remove deprecations
Browse files Browse the repository at this point in the history
  - remove application.app injection
  - remove application.modelManager injection
  - remove controller finders

BREAKING CHANGE: controllers finders have been removed. users will
need to use the store. application.modelManager has been removed,
application.app has been moved to a getter

commit 389e4b6
Author: Dylan Foster <dylan947@gmail.com>
Date:   Sat Nov 11 20:24:34 2017 -0800

    test: fix failing tests after deprecation removals

commit d056cf2
Author: Dylan Foster <dylan947@gmail.com>
Date:   Sat Nov 11 20:15:37 2017 -0800

    style: remove unused import

commit 592f148
Author: Dylan Foster <dylan947@gmail.com>
Date:   Sat Nov 11 20:14:16 2017 -0800

    feat(deprecations): remove controller finders

commit 4b861b4
Author: Dylan Foster <dylan947@gmail.com>
Date:   Sat Nov 11 20:13:02 2017 -0800

    feat(deprecations): remove Application#getApp and put behind a normal getter

commit a3ea4bd
Author: Dylan Foster <dylan947@gmail.com>
Date:   Sat Nov 11 20:11:53 2017 -0800

    feat(deprecations): remove model manager and app
  • Loading branch information
dylanfoster committed Nov 12, 2017
1 parent c500a73 commit 87adb2c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 182 deletions.
12 changes: 1 addition & 11 deletions src/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import callsite from "callsite";
import includeAll from "include-all";

import Registry from "./registry";
import deprecate from "./utils/deprecate";

const DEFAULT_CONNECTION_SETTINGS = {
dialect: "sqlite",
Expand Down Expand Up @@ -63,16 +62,7 @@ class Application {
this._initialize("application");
}

/**
* Get the restify application instance
*
* @method getApp
* @deprecated use registry.lookup("service:server") instead
* @return {Object} restify application instance
*/
getApp() {
deprecate(this, "getApp", "2.0.0");

get app() {
return this.registry.lookup("service:server");
}

Expand Down
140 changes: 0 additions & 140 deletions src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,146 +82,6 @@ class Controller {

return modelName;
}

/**
* Builds, validates and saves a model instance.
*
* @method createRecord
* @deprecated use controller.store instead
* @param {Object} data the model data to create the instance with
* @return {Promise}<ModelInstance, Error> the model instance
*
* @example
* ```javascript
* return this.createRecord({ firstname: 'bar' }).then(record => {
*
* });
* ```
*/
createRecord(data) {
deprecate(this, "createRecord", "2.0.0");

return this.store.createRecord(this.modelNameLookup, data);
}

/**
* Destroy a model instance and remove it from the db
*
* @method destroyRecord
* @deprecated use controller.store instead
* @param {Number} id the id of the resource to destroy
* @return {Promise}<void, Error>
*
* @example
* ```javascript
* return this.destroyRecord(1).then(() => {
*
* });
* ```
*/
destroyRecord(id) {
deprecate(this, "destroyRecord", "2.0.0");

return this.store.destroyRecord(this.modelNameLookup, id);
}

/**
* Find all records.
*
* @method findAll
* @deprecated use controller.store instead
* @param {Object} where
* <a href="http://docs.sequelizejs.com/en/v3/docs/querying/#where" target="_blank">
* See Sequelize Where
* </a>
* @param {Object} options
* <a href="http://docs.sequelizejs.com/en/v3/api/model/#findoneoptions-promiseinstance" target="_blank">
* Sequelize finder options
* </a>
* @return {Promise}<ModelInstance[], Error> an array of model instance
*
* @example
* ```javascript
* return this.findAll().then(records => {
*
* })
*
* // You can optionally pass in a where clause
*
* return this.findAll({ username: 'john' }).then(user => {
*
* });
*
* // As well as any finder options
*
* return this.findAll(null, {
* attributes: ["title"],
* include: [this.models.User]
* }).then(user => {
*
* });
* ```
*/
findAll(where, options = {}) {
deprecate(this, "findAll", "2.0.0");

return this.store.findAll(this.modelNameLookup, where, options);
}

/**
* Find a single instance by id
*
* @method findOne
* @deprecated use controller.store instead
* @param {Number} id the id of the instance to search for
* @param {Object} options
* <a href="http://docs.sequelizejs.com/en/v3/api/model/#findoneoptions-promiseinstance" target="_blank">
* Sequelize finder options
* </a>
* @return {Promise}<ModelInstance, Error>
*
* @example
* ```javascript
* return this.findOne(1).then(record => {
*
* });
*
* // The same options apply to findOne
*
* return this.findOne(1, {
* attributes: ["firstName"]
* }).then(user => {
*
* });
* ```
*/
findOne(id, options = {}) {
deprecate(this, "findOne", "2.0.0");

return this.store.findOne(this.modelNameLookup, id, options);
}

/**
* Update a single record
*
* @method updateRecord
* @deprecated use controller.store instead
* @param {Number} id the id of the record to update
* @param {Object} data the data to update on the record
* @return {Promise}<Model, Error>
*
* @example
* ```javascript
* return this.updateRecord(1, { firstName: 'foo' }).then(record => {
*
* });
* ```
*/
updateRecord(id, data) {
deprecate(this, "updateRecord", "2.0.0");

return this.store.updateRecord(this.modelNameLookup, id, data);
}
}

export default Controller;
7 changes: 0 additions & 7 deletions src/initializers/application.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
"use strict";

import deprecate from "../utils/deprecate";

module.exports = {
initialize(application, registry) {
deprecate(application, "modelManager", "2.0.0");
deprecate(application, "app", "2.0.0");

registry.inject(application, "service:model-manager", "modelManager");
registry.inject(application, "service:server", "app");
registry.register("application:main", application);
},

Expand Down
10 changes: 5 additions & 5 deletions test/application_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe("Application", function () {
this.resource("foo");
});

supertest(application.getApp())
supertest(application.app)
.get("/foos")
.expect(200)
.end(done);
Expand Down Expand Up @@ -195,15 +195,15 @@ describe("Application", function () {
it("authenticates users via jwt", function (done) {
const token = jwt.sign({ foo: "bar" }, "secret");

supertest(application.getApp())
supertest(application.app)
.get("/users")
.set("Authorization", `Bearer ${token}`)
.expect(200)
.end(done);
});

it("skips 'unauthenticated' routes", function (done) {
supertest(application.getApp())
supertest(application.app)
.post("/users/resetPassword")
.expect(200)
.end(done);
Expand All @@ -226,7 +226,7 @@ describe("Application", function () {
this.resource("user");
});

supertest(application.getApp())
supertest(application.app)
.get("/users")
.expect(200)
.end(done);
Expand All @@ -249,7 +249,7 @@ describe("Application", function () {
application.map(function () {
this.resource("user");
});
supertest(application.getApp())
supertest(application.app)
.get("/users")
.expect(401)
.end(done);
Expand Down
32 changes: 16 additions & 16 deletions test/controller_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ describe("Controller", function () {

describe("#findAll", function () {
it("returns all records of a model", function () {
return controller.findAll().then(res => {
return controller.store.findAll("user").then(res => {
expect(res).to.eql([]);

return modelManager.models.User.create({ firstName: "john" });
}).then(() => controller.findAll())
}).then(() => controller.store.findAll("user"))
.then(res => {
expect(res[0].firstName).to.eql("john");
});
Expand All @@ -69,14 +69,14 @@ describe("Controller", function () {
return modelManager.models.User.create({ firstName: "joe" }).then(joe => {
user2 = joe;
});
}).then(() => controller.findAll({ firstName: "john" })).then(res => {
}).then(() => controller.store.findAll("user", { firstName: "john" })).then(res => {
expect(res.length).to.eql(1);
});
});

it("allows for finder options", function () {
return modelManager.models.User.create({ firstName: "john" }).then(
() => controller.findAll({ firstName: "john" }, { attributes: ["firstName"] })
() => controller.store.findAll("user", { firstName: "john" }, { attributes: ["firstName"] })
).then(res => {
expect(res[0].firstName).to.eql("john");
});
Expand All @@ -88,15 +88,15 @@ describe("Controller", function () {
describe("#findOne", function () {
it("finds a single record by id", function () {
return modelManager.models.User.create({ firstName: "john" })
.then(john => controller.findOne(john.id))
.then(john => controller.store.findOne("user", john.id))
.then(res => {
expect(res.firstName).to.eql("john");
});
});

it("allows for finder options", function () {
return modelManager.models.User.create({ firstName: "john" }).then(
john => controller.findOne(john.id, { attributes: ["firstName"] })
john => controller.store.findOne("user", john.id, { attributes: ["firstName"] })
).then(res => {
expect(res.toJSON()).to.eql({
firstName: "john"
Expand All @@ -105,7 +105,7 @@ describe("Controller", function () {
});

it("throws NotFoundError if no record is found", function (done) {
controller.findOne(1).catch(err => {
controller.store.findOne("user", 1).catch(err => {
expect(err.code).to.eql("NotFound");
expect(err.message).to.eql("user does not exist");
done();
Expand All @@ -115,21 +115,21 @@ describe("Controller", function () {

describe("#createRecord", function () {
it("creates a new record", function () {
return controller.createRecord({ firstName: "john" }).then(res => {
return controller.store.createRecord("user", { firstName: "john" }).then(res => {
expect(res.firstName).to.eql("john");
});
});

it("throws BadRequestError for invalid body", function (done) {
controller.createRecord().catch(err => {
controller.store.createRecord("user").catch(err => {
expect(err.code).to.eql("BadRequest");
expect(err.message).to.eql("Missing or invalid body");
done();
});
});

it("throws UnprocessableEntityError for validation failures", function (done) {
controller.createRecord({ firstName: 1 }).catch(err => {
controller.store.createRecord("user", { firstName: 1 }).catch(err => {
expect(err.code).to.eql("UnprocessableEntity");
expect(err.message).to.eql("firstName must be a valid string");
done();
Expand All @@ -152,30 +152,30 @@ describe("Controller", function () {
});

it("updates an existing record by id", function () {
return controller.updateRecord(user.id, { firstName: "bob" }).then(res => {
return controller.store.updateRecord("user", user.id, { firstName: "bob" }).then(res => {
expect(res.firstName).to.eql("bob");
});
});

it("throws NotFoundError if record is not found", function (done) {
user.destroy().then(() => {
controller.updateRecord(1, { firstName: "bob" }).catch(err => {
controller.store.updateRecord("user", 1, { firstName: "bob" }).catch(err => {
expect(err.code).to.eql("NotFound");
done();
});
}).catch(done);
});

it("throw BadRequestError for invalid or missing data", function (done) {
controller.updateRecord(user.id).catch(err => {
controller.store.updateRecord("user", user.id).catch(err => {
expect(err.code).to.eql("BadRequest");
expect(err.message).to.eql("Missing or invalid body");
done();
});
});

it("throws UnprocessableEntityError for validation failures", function (done) {
controller.updateRecord(user.id, { firstName: 1 }).catch(err => {
controller.store.updateRecord("user", user.id, { firstName: 1 }).catch(err => {
expect(err.code).to.eql("UnprocessableEntity");
expect(err.message).to.eql("firstName must be a valid string");
done();
Expand All @@ -196,7 +196,7 @@ describe("Controller", function () {
});

it("destroys a record by id", function () {
return controller.destroyRecord(user.id).then(() =>
return controller.store.destroyRecord("user", user.id).then(() =>
modelManager.models.User.findById(user.id)
).then(found => {
expect(found).to.be.null;
Expand All @@ -205,7 +205,7 @@ describe("Controller", function () {

it("throws NotFoundError if record doesn't exist", function (done) {
user.destroy().then(() => {
controller.destroyRecord(user.id).catch(err => {
controller.store.destroyRecord("user", user.id).catch(err => {
expect(err.code).to.eql("NotFound");
expect(err.message).to.eql("user does not exist");
done();
Expand Down
6 changes: 3 additions & 3 deletions test/initializers/application_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ describe("initializer | application", function () {
it("registers the application injections", function () {
initializer.initialize(application, registry);

expect(application.app).to.be.ok;
expect(application.modelManager).to.be.ok;
expect(registry.lookup("application:main")).to.eql(application);
const app = registry.lookup("application:main");

expect(app).to.eql(application);
});
});
});

0 comments on commit 87adb2c

Please sign in to comment.