nails-boilerplate 2.0.3 → 2.0.6

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/index.js CHANGED
@@ -1,4 +1,7 @@
1
1
  // Sets the Nails global object
2
- import nails from './lib/nails.js';
2
+ import nails, {initializeModels} from './lib/nails.js';
3
+
3
4
  export default nails;
5
+
6
+ export const initializeModels = initializeModels;
4
7
  // set up Nails here
package/lib/nails.js CHANGED
@@ -1,19 +1,11 @@
1
1
  // The file which configures the nails application
2
- import http from 'node:http';
2
+ // import http from 'node:http';
3
3
  import https from 'node:https';
4
- import URL from 'node:url';
4
+ // import URL from 'node:url';
5
5
  import path from 'node:path';
6
6
  import fs from 'node:fs';
7
7
  import { EventEmitter } from 'node:events';
8
8
 
9
- // const http = require('http');
10
- // const https = require('https');
11
- // const URL = require('url');
12
- // const path = require('path');
13
- // const fs = require('fs');
14
- // const EventEmitter = require('events').EventEmitter;
15
-
16
-
17
9
  import Controller from './controller.js';
18
10
  import ModelV2 from './model_v2.js';
19
11
  import Router from './router.js';
@@ -40,6 +32,7 @@ nails.Controller = Controller;
40
32
  nails.Model = ModelV2;
41
33
  nails.events = new EventEmitter();
42
34
  nails._dbConnector = null;
35
+ nails.MODELS = {};
43
36
 
44
37
  async function configure( app_config ) {
45
38
  express_app.set('nails_config', application);
@@ -67,6 +60,18 @@ async function configure( app_config ) {
67
60
  application.router = new Router( [] );
68
61
  console.log("Application Router initialized");
69
62
 
63
+ // init models
64
+ await initializeModels(app_config);
65
+
66
+ // init Controllers
67
+ Controller.setRouter(application.router);
68
+ application.controller = Controller.extend(ApplicationController);
69
+ console.log('initializing controllers: ', app_config.config.CONTROLLERS_ROOT);
70
+ await init_controllers(app_config.config.CONTROLLERS_ROOT);
71
+ application.router.addRoutes(app_config.routes);
72
+ };
73
+
74
+ export async function initializeModels( app_config ) {
70
75
  // init models
71
76
  console.log("Initializing DB connection...");
72
77
  var DBConnector = await get_dbconnector(app_config.db.connector);
@@ -79,17 +84,10 @@ async function configure( app_config ) {
79
84
  console.log("Generating model superclass...");
80
85
  ModelV2.setConnector(dbConnector);
81
86
  await init_models_v2(app_config.config.MODELS_ROOT);
82
- console.log("Done importing models")
87
+ console.log("Done importing models");
83
88
  await dbConnector.afterInitialization();
84
89
  console.log("DB Connection complete");
85
-
86
- // init Controllers
87
- Controller.setRouter(application.router);
88
- application.controller = Controller.extend(ApplicationController);
89
- console.log('initializing controllers: ', app_config.config.CONTROLLERS_ROOT);
90
- await init_controllers(app_config.config.CONTROLLERS_ROOT);
91
- application.router.addRoutes(app_config.routes);
92
- };
90
+ }
93
91
 
94
92
  function startServer(config) {
95
93
  // Log the config.
@@ -137,9 +135,7 @@ function startServer(config) {
137
135
  async function init_controllers(controller_lib) {
138
136
  await init_app_lib(Controller, controller_lib);
139
137
  }
140
- // async function init_models(model_lib) {
141
- // await init_app_lib(Model, model_lib);
142
- // }
138
+
143
139
  async function init_app_lib(superclass, abs_path) {
144
140
  console.log('attempting to import:', abs_path);
145
141
  if (!fs.existsSync(abs_path))
@@ -169,12 +165,11 @@ async function init_models_v2(abs_path) {
169
165
  // We just need to import each model once so the generateSuperclass
170
166
  // method is called at least once for each model.
171
167
  let modelClass = (await import(abs_path)).default;
172
- if (modelClass && modelClass.name) console.log('imported model:', modelClass.name);
168
+ if (modelClass && modelClass.name) {
169
+ console.log('imported model:', modelClass.name);
170
+ nails.MODELS[modelClass.name] = modelClass;
171
+ }
173
172
  else console.warn("No model found at:", abs_path);
174
- // // Constructor function was provided
175
- // if (!superclass.isPrototypeOf(subclass))
176
- // return superclass.extend(subclass);
177
- // ES6 Class was provided
178
173
  return;
179
174
  }
180
175
  const directory_contents = fs.readdirSync(abs_path);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nails-boilerplate",
3
- "version": "2.0.3",
3
+ "version": "2.0.6",
4
4
  "description": "A node.js webserver scaffold",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -34,5 +34,6 @@ describe('ModelV2 using SequelizeConnector', function() {
34
34
  const models = await TestSequelizeModel.findAll();
35
35
  assert(models.length == 1, "Should have one model");
36
36
  assert(models[0].name == MODEL_NAME, "Name should be consistent");
37
+ assert(models[0] instanceof TestSequelizeModel);
37
38
  });
38
39
  });
@@ -25,7 +25,7 @@ var config = {
25
25
 
26
26
  ENABLE_HTTP: true,
27
27
  //IP: "0.0.0.0",
28
- PORT: 3333,
28
+ PORT: 4444,
29
29
 
30
30
  ASYNC: false,
31
31
 
@@ -15,7 +15,7 @@ let mongod = null;
15
15
  chai.use(chaiHttp);
16
16
  chai.should();
17
17
 
18
- describe("Integration", function () {
18
+ describe("Integration: Sequelize", function () {
19
19
  before(async function () {
20
20
  // this.timeout(30000);
21
21
  try {
@@ -26,17 +26,6 @@ describe("Integration", function () {
26
26
  }
27
27
  console.log("got here");
28
28
  express_app = nails.application;
29
- return new Promise((resolve, reject) => {
30
- nails.events.on("ready", () => {
31
- console.log("ready was emitted!");
32
- resolve();
33
- });
34
- });
35
- // })
36
- // MongoMemoryServer.create({instance: service_config.db}).then((mongodb) => {
37
- // mongod = mongodb;
38
-
39
- // });
40
29
  });
41
30
  describe("GET /", function () {
42
31
  it('should return the expected JSON from index', function (done) {
@@ -7,8 +7,6 @@ import WebSocket from 'ws';
7
7
  // const WebSocket = require('ws');
8
8
 
9
9
  var express_app;
10
- // const {MongoMemoryServer} = require('mongodb-memory-server');
11
- let mongod = null;
12
10
 
13
11
  // Configure chai
14
12
  chai.use(chaiHttp);
@@ -24,18 +22,13 @@ describe("Integration", function () {
24
22
  }
25
23
  console.log("got here");
26
24
  express_app = nails.application;
27
- return new Promise((resolve, reject) => {
28
- nails.events.on("ready", () => {
29
- console.log("ready was emitted!");
30
- resolve();
31
- });
32
- });
33
25
  });
34
26
  describe("GET /", function () {
35
27
  it('should return the expected JSON from index', function (done) {
36
28
  request.execute(express_app)
37
29
  .get('/')
38
30
  .end((err, res) => {
31
+ if (err) console.error(err);
39
32
  res.should.have.status(200);
40
33
  assert(res.text == JSON.stringify({ home_index: true }));
41
34
  done();