nails-boilerplate 0.10.2 → 0.11.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.
@@ -58,7 +58,7 @@ MongoDBConnector.prototype._put_one = function(collection_name, doc) {
58
58
  // TODO: replacing document completely is sow
59
59
  // will want to only send changed attr
60
60
  // TODO: write concerns?
61
- return this._db.collection(collection_name).update({_id: doc._id}, doc);
61
+ return this._db.collection(collection_name).replaceOne({_id: doc._id}, doc);
62
62
  }
63
63
  /**
64
64
  * _put_many will call _update on each individual object in collection
@@ -1,13 +1,37 @@
1
1
  const mongoose = require('mongoose');
2
- const mongooseOptions = {useNewUrlParser: true};
3
2
 
4
- module.exports.connect = async function(options) {
5
- if (options.uri) return mongoose.connect(options.uri, mongooseOptions);
3
+ class MongooseDbConnector {
4
+ async connect(options) {
5
+ if (options.uri) {
6
+ this.connection = await mongoose.createConnection(options.uri/*, mongooseOptions*/).asPromise();
7
+ debugger;
8
+ } else {
9
+ var url = options.url || 'mongodb://127.0.0.1';
10
+ var port = options.port || '27017';
11
+ var database = options.database || options.dbName || 'nails';
12
+ this.connection = await mongoose.createConnection(`${url}:${port}/${database}`).asPromise();
13
+ }
14
+ }
15
+
16
+ generateModelSuperclass(name, options) {
17
+ let schema = options.schema instanceof mongoose.Schema
18
+ ? options.schema
19
+ : new mongoose.Schema(options.schema);
20
+ if (options.indexes) {
21
+ options.indexes.forEach(index => schema.index(index));
22
+ }
23
+ return this.connection.model(name, options.schema);
24
+ }
25
+ }
26
+ module.exports = MongooseDbConnector;
27
+ /*
28
+ module.exports.connect = function(options) {
29
+ if (options.uri) return mongoose.createConnection(options.uri, mongooseOptions);
6
30
  else {
7
31
  var url = options.url || 'mongodb://localhost';
8
32
  var port = options.port || '27017';
9
33
  var database = options.database || 'nails';
10
- return mongoose.connect(`${url}:${port}/${database}`, mongooseOptions);
34
+ return mongoose.createConnection(`${url}:${port}/${database}`, mongooseOptions);
11
35
  }
12
36
  }
13
37
 
@@ -20,3 +44,4 @@ module.exports.generateModelSuperclass = function(name, options) {
20
44
  }
21
45
  return mongoose.model(name, options.schema);
22
46
  }
47
+ */
package/lib/nails.js CHANGED
@@ -4,6 +4,7 @@ const URL = require('url');
4
4
  const path = require('path');
5
5
  const fs = require('fs');
6
6
  const domain = require('domain');
7
+ const EventEmitter = require('events').EventEmitter;
7
8
 
8
9
  const Controller = require('./controller.js');
9
10
  const Model = require('./model.js');
@@ -32,6 +33,7 @@ nails.application = express_app;
32
33
  nails.Controller = Controller;
33
34
  nails.Model = ModelV2;
34
35
  nails.ModelDeprecated = Model;
36
+ nails.events = new EventEmitter();
35
37
 
36
38
  async function configure( app_config ) {
37
39
  express_app.set('nails_config', application);
@@ -67,28 +69,36 @@ async function configure( app_config ) {
67
69
  // TODO: make this Model style mandatory
68
70
  if (DBConnector.connect && DBConnector.generateModelSuperclass) {
69
71
  await DBConnector.connect(app_config.db);
70
- await ModelV2.setConnector(DBConnector);
72
+ ModelV2.setConnector(DBConnector);
73
+ } else {
74
+ // Try to instantiate DBConnector
75
+ let dbConnector = new DBConnector();
76
+ await dbConnector.connect(app_config.db);
77
+ ModelV2.setConnector(dbConnector);
71
78
  }
72
79
 
73
80
  // init Controllers
74
81
  Controller.setRouter(application.router);
75
82
  application.controller = Controller.extend(ApplicationController);
76
- console.warn('initializing controllers: ', app_config.config.CONTROLLERS_ROOT);
83
+ console.log('initializing controllers: ', app_config.config.CONTROLLERS_ROOT);
77
84
  init_controllers(app_config.config.CONTROLLERS_ROOT);
78
85
  };
79
86
 
80
87
  function startServer(config) {
81
88
  // Log the config.
82
89
  console.log(config);
90
+ //await application._onceConfigured;
91
+ console.log("CONFIGURATION COMPLETE");
92
+ // TODO: Use logging middleware.
83
93
  application._onceConfigured.then(() => {
84
- // TODO: Use logging middleware.
85
-
86
94
  // Use the router middleware.
87
95
  express_app.use(application.router.express_router);
88
96
  var ip = application.config.IP || 'localhost';
89
97
  var port = application.config.PORT || 3000;
90
98
  console.log("starting nails. listening to ", ip + ':' + port);
91
- express_app.listen(port, ip);
99
+ express_app.listen(port, ip, () => {
100
+ nails.events.emit("ready", null);
101
+ });
92
102
  });
93
103
  }
94
104
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nails-boilerplate",
3
- "version": "0.10.2",
3
+ "version": "0.11.1",
4
4
  "description": "A node.js webserver scaffold",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -31,7 +31,7 @@
31
31
  "express-ws": "*",
32
32
  "mime": "*",
33
33
  "mongodb": "^4.3.1",
34
- "mongoose": "*",
34
+ "mongoose": "^6.1.8",
35
35
  "react": "*",
36
36
  "react-dom": "^16.13.1",
37
37
  "wrench": "*"
@@ -40,7 +40,7 @@
40
40
  "chai": "*",
41
41
  "chai-http": "*",
42
42
  "mocha": "^7.1.2",
43
- "mongodb-memory-server": "*",
43
+ "mongodb-memory-server": "^8.2.0",
44
44
  "sinon": "*",
45
45
  "ws": "*"
46
46
  },
@@ -1,6 +1,7 @@
1
1
  const assert = require('assert');
2
2
  const Model = require('../lib/model_v2.js');
3
3
  const MongooseConnectorUtil = require("./mongoose_connector.util.js");
4
+ const mongoose = require('mongoose');
4
5
 
5
6
  const testSchema = {
6
7
  name: String,
@@ -14,17 +15,16 @@ describe('ModelV2', function() {
14
15
  var util;
15
16
  beforeEach(async function() {
16
17
  util = new MongooseConnectorUtil();
17
- await util.getTestConnector().then(connector => {
18
- Model.setConnector(connector);
19
- TestModel =
20
- class Test extends new Model(
21
- "" + Math.random(), {schema: testSchema}) {
22
- whatsMyName() {return this.name};
23
- };
24
- });
18
+ let connector = await util.getTestConnector();
19
+ Model.setConnector(connector);
20
+ TestModel =
21
+ class Test extends new Model(
22
+ "" + Math.random(), {schema: testSchema}) {
23
+ whatsMyName() {return this.name};
24
+ };
25
25
  });
26
- afterEach(function(done) {
27
- util.cleanup().then(() => done());
26
+ afterEach(async function() {
27
+ await util.cleanup();
28
28
  });
29
29
  describe('Mongoose Model Inheritance', function() {
30
30
  const testAttr = {
@@ -32,15 +32,13 @@ describe('ModelV2', function() {
32
32
  isTrue: false,
33
33
  index: 7
34
34
  };
35
- it("should be able to create a model", function(done) {
35
+ it("should be able to create a model", async function() {
36
36
  const testModel = new TestModel(testAttr);
37
37
  assert(testModel.name == testAttr.name);
38
38
  assert(testModel.isTrue == testAttr.isTrue);
39
39
  assert(testModel.index == testAttr.index);
40
- testModel.save().then(() => {
41
- console.log("The ID is:", testModel._id);
42
- done();
43
- });
40
+ await testModel.save();
41
+ console.log("The ID is:", testModel._id);
44
42
  })
45
43
  it("should be able to update a model", function() {
46
44
  const testModel = new TestModel(testAttr);
@@ -44,10 +44,13 @@ describe('MongoDBConnector', function() {
44
44
  });
45
45
  describe('Document Creation', function(){
46
46
  describe('#_post_one', function() {
47
- it('should set an _id attribute on the given attributes hash', function() {
47
+ it('should set an _id attribute on the given attributes hash', function(done) {
48
48
  var test_model = get_test_model();
49
- mdbc._post_one(test_model.collection_name(), test_model.attributes);
50
- assert.ok(test_model.attributes._id);
49
+ mdbc._post_one(test_model.collection_name(), test_model.attributes)
50
+ .then(() => {
51
+ assert.ok(test_model.attributes._id);
52
+ done();
53
+ });
51
54
  });
52
55
  });
53
56
  });
@@ -1,22 +1,29 @@
1
1
  const MongoDBConnector = require('../lib/mongodb_connector.js');
2
2
  const {MongoMemoryServer} = require('mongodb-memory-server');
3
3
 
4
+ let singularInstanceCreated = false;
5
+ let promisedMongod = null;
6
+
4
7
  class MongoDBConnectorUtil {
5
8
  constructor() {
6
- this.mongod = new MongoMemoryServer();
9
+ promisedMongod = MongoMemoryServer.create();
10
+ singularInstanceCreated = true;
7
11
  }
12
+
8
13
  async getTestConnector() {
9
- const uri = await this.mongod.getConnectionString();
10
- const port = await this.mongod.getPort();
11
- const dbPath = await this.mongod.getDbPath();
12
- const dbName = await this.mongod.getDbName();
14
+ this.mongod = await promisedMongod;
15
+ const uri = this.mongod.getUri();
16
+ const port = this.mongod.instanceInfo.port;
17
+ const dbPath = this.mongod.instanceInfo.dbPath;
18
+ const dbName = this.mongod.instanceInfo.dbName;
13
19
  const dbConfig =
14
20
  {uri: uri, port: port, database: dbName, dbPath: dbPath};
15
21
  //console.log(JSON.stringify(dbConfig));
16
22
  return new MongoDBConnector(dbConfig);
17
23
  }
18
- cleanup() {
19
- return this.mongod.stop();
24
+
25
+ async cleanup() {
26
+ await this.mongod.stop();
20
27
  }
21
28
  }
22
29
 
@@ -1,23 +1,32 @@
1
1
  const MongooseConnector = require('../lib/mongoose_connector.js');
2
2
  const {MongoMemoryServer} = require('mongodb-memory-server');
3
+ const mongoose = require('mongoose');
4
+
5
+ let promisedMongod = null;
3
6
 
4
7
  class MongooseConnectorUtil {
5
8
  constructor() {
6
- this.mongod = new MongoMemoryServer();
9
+ this.mongoose = mongoose;
10
+ promisedMongod = MongoMemoryServer.create();
7
11
  }
8
12
 
9
13
  async getTestConnector() {
10
- const uri = await this.mongod.getConnectionString();
11
- const port = await this.mongod.getPort();
12
- const dbPath = await this.mongod.getDbPath();
13
- const dbName = await this.mongod.getDbName();
14
- const dbConfig =
15
- {uri: uri, port: port, database: dbName, dbPath: dbPath};
16
- MongooseConnector.connect(dbConfig);
17
- return MongooseConnector;
14
+ this.mongod = await promisedMongod;
15
+ const uri = this.mongod.getUri();
16
+ const port = this.mongod.instanceInfo.port;
17
+ const dbPath = this.mongod.instanceInfo.dbPath;
18
+ const dbName = this.mongod.instanceInfo.dbName;
19
+ const dbConfig = {uri: uri};
20
+ //{uri: uri, port: port, database: dbName, dbPath: dbPath};
21
+ const dbConnector = new MongooseConnector();
22
+ this.connection = await dbConnector.connect(dbConfig);
23
+ debugger;
24
+ return dbConnector;
18
25
  }
19
- cleanup() {
20
- return this.mongod.stop();
26
+ async cleanup() {
27
+ //await this.mongod.stop();
28
+ //await this.connection.close();
29
+ debugger;
21
30
  }
22
31
  }
23
32
 
@@ -1,5 +1,7 @@
1
1
  var Router = require('../lib/router.js');
2
2
  var assert = require('assert');
3
+ var express_app = require('../lib/application.js');
4
+ express_app.set("public_root", __dirname + "/services/integration/public");
3
5
  var testRoutes = [
4
6
  ['GET', '^/$', {controller: 'testController', action: 'testAction'}],
5
7
  ['GET', /^\/public\/*/, {public: true}]
@@ -1,5 +1,6 @@
1
1
  module.exports = function HomeController() {
2
2
  this.index = function(params, request, response) {
3
+ console.log("HOME::INDEX");
3
4
  response.json({
4
5
  home_index: true
5
6
  });
@@ -2,9 +2,9 @@ module.exports = {
2
2
  //connector: 'sqlite3_connector.js',
3
3
  connector: 'mongoose_connector.js',
4
4
  //url: 'mongodb://localhost',
5
- port: 55555,
5
+ port: 5555,
6
6
 
7
- database: 'development',
7
+ dbName: 'development',
8
8
 
9
9
  // For the sqlite3 connector. Unless a filename is defined, an in-memory
10
10
  // database is used. In-memory databases are not persisted, and will be lost
@@ -2,5 +2,6 @@ var nails = require('../../../index.js');
2
2
 
3
3
  // See self-documented config files
4
4
  var service_config = require('./config/service.js');
5
+ console.log("starting server")
5
6
  nails( service_config ).startServer();
6
7
  module.exports = nails; // export nails for testing
@@ -3,16 +3,28 @@ var chai = require('chai');
3
3
  var chaiHttp = require('chai-http');
4
4
  const WebSocket = require('ws');
5
5
 
6
- var nails = require('./services/integration/server.js');
7
- var express_app = nails.application;
6
+ var express_app;
8
7
  const assert = require('assert');
9
8
  const {MongoMemoryServer} = require('mongodb-memory-server');
9
+ let mongod = null;
10
+ var service_config = require('./services/integration/config/service.js');
10
11
 
11
12
  // Configure chai
12
13
  chai.use(chaiHttp);
13
14
  chai.should();
14
15
 
15
16
  describe("Integration", function() {
17
+ before(function(done) {
18
+ MongoMemoryServer.create({instance: service_config.db}).then((mongodb) => {
19
+ mongod = mongodb;
20
+ var nails = require('./services/integration/server.js');
21
+ express_app = nails.application;
22
+ nails.events.on("ready", () => {
23
+ console.log("ready was emitted!");
24
+ done();
25
+ });
26
+ });
27
+ });
16
28
  describe("GET /", function() {
17
29
  it('should return the expected JSON from index', function(done) {
18
30
  chai.request(express_app)
@@ -168,13 +180,11 @@ describe("Integration", function() {
168
180
 
169
181
  });
170
182
  describe("Mongoose Model", function() {
171
- let mongod = null;
172
183
  beforeEach(async function() {
173
- mongod = new MongoMemoryServer({port: 55555, dbName: "development"});
174
- await mongod.getConnectionString();
184
+ // Used to initialize mongod here
175
185
  });
176
186
  afterEach(async function() {
177
- await mongod.stop();
187
+ //await mongod.stop();
178
188
  });
179
189
  it('should save to the correct database', async function() {
180
190
  let dogName = "Penny";
@@ -187,7 +197,7 @@ describe("Integration", function() {
187
197
  chai.request(express_app)
188
198
  .get(`modeltest/getdogbyid?id=${dogId}`)
189
199
  .end((err, res) => {
190
- asset.equals(
200
+ assert.equals(
191
201
  res.text, JSON.stringify({name: dogName, good: true}));
192
202
  done();
193
203
  });