nails-boilerplate 1.2.1 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/controller.js CHANGED
@@ -61,6 +61,7 @@ class Controller {
61
61
 
62
62
  /** Initializes local and global routes defined on the Controller subclass */
63
63
  _registerControllerRoutes() {
64
+ const defaultToJson = !!this.json;
64
65
  const controllerName = this._getControllerName();
65
66
  if (this.routes && this.routes.length) {
66
67
  const localizedRoutes = this.routes.map(route => {
@@ -71,6 +72,14 @@ class Controller {
71
72
  ? route[1]
72
73
  : route[1].startsWith('./') ? route[1].replace('./', routePrefix) : routePrefix + route[1];
73
74
  const modifiedOptions = {...route[2]};
75
+ // TODO: introduce a shorthand so action doesn't have to be redefined everywhere
76
+ if (!('json' in modifiedOptions)) modifiedOptions.json = defaultToJson;
77
+ if (!('action' in modifiedOptions)
78
+ && !modifiedDestination.includes(":action")
79
+ && !Object.values(modifiedOptions).includes('action')) {
80
+ const destinationParts = modifiedDestination.split("/");
81
+ modifiedOptions.action = destinationParts[destinationParts.length - 1];
82
+ }
74
83
  modifiedOptions.controller = controllerName;
75
84
  return [route[0], modifiedDestination, modifiedOptions];
76
85
  });
package/lib/model_v2.js CHANGED
@@ -2,7 +2,10 @@ let dbConnector = null;
2
2
 
3
3
  export default class Model {
4
4
  static setConnector(connector) {
5
- if (!dbConnector) dbConnector = connector;
5
+ // TODO: enforce environment using variables
6
+ if (dbConnector)
7
+ console.warn("WARNING: Model#setConnector should not be called multiple times outside of tests");
8
+ dbConnector = connector;
6
9
  }
7
10
 
8
11
  constructor(modelName, connectorOptions) {
package/lib/nails.js CHANGED
@@ -20,6 +20,7 @@ import ModelV2 from './model_v2.js';
20
20
  import Router from './router.js';
21
21
 
22
22
  import express_app from './application.js';
23
+ import DbConnector from './database_connector.js';
23
24
 
24
25
  const models = {};
25
26
 
@@ -81,7 +82,7 @@ async function configure( app_config ) {
81
82
  }
82
83
  // TODO: make this Model style mandatory
83
84
  console.log("Connecting to DB...");
84
- if (DBConnector.connect && DBConnector.generateModelSuperclass) {
85
+ if (DBConnector instanceof DbConnector) {
85
86
  console.log("Generating model superclass...");
86
87
  await DBConnector.connect(app_config.db);
87
88
  ModelV2.setConnector(DBConnector);
@@ -20,6 +20,7 @@ export default class SequelizeConnector extends DbConnector {
20
20
  }
21
21
 
22
22
  async afterInitialization() {
23
- this.sequelize.sync({alter: true});
23
+ console.log("SEQUELIZE::Writing changes to SQL Database");
24
+ await this.sequelize.sync({alter: true});
24
25
  }
25
26
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nails-boilerplate",
3
- "version": "1.2.1",
3
+ "version": "1.3.1",
4
4
  "description": "A node.js webserver scaffold",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -19,7 +19,7 @@ describe('ModelV2 using SequelizeConnector', function() {
19
19
  let util;
20
20
  beforeEach(async function() {
21
21
  util = new SequelizeConnectorUtil();
22
- let connector = await util.getTestConnector();
22
+ const connector = await util.getTestConnector();
23
23
  Model.setConnector(connector);
24
24
  TestSequelizeModel =
25
25
  class TestSequelizeModel extends new Model("TestSequelizeModel", TEST_SCHEMA) {
@@ -0,0 +1,20 @@
1
+ import nails from "../../../../../index.js";
2
+
3
+ export default class DefaultJsonController extends nails.Controller {
4
+ json = true;
5
+ routes = [
6
+ ['get', 'arbi/trary/testautoaction'],
7
+ ['get', 'arbi/trary/testautojson', {action: 'testautojson'}],
8
+ ['get', 'arbi/trary/testjsonoverridden', {action: 'testnojson', json: false}],
9
+ ];
10
+
11
+ testautoaction(params, request, response) {
12
+ return {json_testautoaction: true};
13
+ }
14
+
15
+ testautojson(params, request, response) {
16
+ return {json_testautojson: true};
17
+ }
18
+
19
+ testnojson(params, request, response) {}
20
+ }
@@ -0,0 +1 @@
1
+ I am some arbitrary text
@@ -89,6 +89,35 @@ describe("Integration", function () {
89
89
  });
90
90
  });
91
91
  });
92
+ describe("Get /defaultjson/arbi/trary", function () {
93
+ it("Should default to json if not present in route options", function(done) {
94
+ request.execute(express_app)
95
+ .get('/defaultjson/arbi/trary/testautojson')
96
+ .end((err, res) => {
97
+ res.should.have.status(200);
98
+ assert(res.text == JSON.stringify({ json_testautojson: true }));
99
+ done();
100
+ });
101
+ });
102
+ it("Should autoassign the action if not present in route options", function(done) {
103
+ request.execute(express_app)
104
+ .get('/defaultjson/arbi/trary/testautoaction')
105
+ .end((err, res) => {
106
+ res.should.have.status(200);
107
+ assert(res.text == JSON.stringify({ json_testautoaction: true }));
108
+ done();
109
+ });
110
+ });
111
+ it("Should not render json if route options explicitly say not to", function(done) {
112
+ request.execute(express_app)
113
+ .get('/defaultjson/arbi/trary/testjsonoverridden')
114
+ .end((err, res) => {
115
+ res.should.have.status(200);
116
+ assert(res.text == 'I am some arbitrary text');
117
+ done();
118
+ });
119
+ })
120
+ });
92
121
  describe("GET /^\\/(\\w+)\\/(\\w+)$/i", function () {
93
122
  it('should route to home_controller#testaction', function (done) {
94
123
  request.execute(express_app)