Skip to content

Commit 144df92

Browse files
committed
added config option to use mongo instead of postgres
1 parent e5bb243 commit 144df92

File tree

10 files changed

+98
-6
lines changed

10 files changed

+98
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"gulp-sourcemaps": "1.6.0",
3636
"gulp-typescript": "2.11.0",
3737
"gulp-watch": "4.3.5",
38+
"mongoose": "^4.4.6",
3839
"oauth2-server": "2.4.1",
3940
"pg": "4.4.4",
4041
"reflect-metadata": "0.1.3",

src/nodejs/WebAPI/config/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Created by ppa on 3/3/16.
3+
*/
4+
module.exports = {
5+
db: {
6+
/*type: 'mongo',
7+
connectionString: 'mongodb://localhost/customer-sample-node'*/
8+
type: 'postgres',
9+
connectionString: 'postgres://postgres:Hallo11@localhost:5432/CustomerSampleNodejs'
10+
}
11+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Created by ppa on 3/3/16.
3+
*/
4+
const mongoose = require('mongoose'),
5+
Schema = mongoose.Schema;
6+
7+
var customerSchema = new Schema({
8+
firstName: String,
9+
lastName: String
10+
}, {
11+
toObject: {virtuals: true},
12+
toJSON: {virtuals: true}
13+
});
14+
15+
customerSchema.virtual('id').get(function () {
16+
return this._id;
17+
});
18+
19+
var Customer = mongoose.model('Customer', customerSchema);
20+
21+
module.exports = Customer;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
const mongoose = require('mongoose');
4+
5+
// Using a provider pattern for db configuration and initialization
6+
module.exports = {
7+
configure: (connectionString) => {
8+
mongoose.connect(connectionString);
9+
}
10+
};

src/nodejs/WebAPI/server/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ const restify = require('restify'),
1515
// Require the services
1616
const services = require('../service'),
1717

18+
// Require configuration
19+
config = require('../config'),
20+
1821
// Require the controllers
1922
controllers = require('../controllers'),
2023

2124
// Require the reference token validation service
2225
referenceTokenValidation = require('./referenceTokenValidation'),
2326

2427
// Require the database to configure it
25-
database = require('../database');
28+
// use /database/{databaseType}, where databaseType [postgres, mongo]
29+
database = require(`../database/${config.db.type}`);
2630

2731
/**
2832
* Restify server exposing some APIs to manipulate customer data
@@ -65,8 +69,8 @@ function Server() {
6569
// Initialize all controllers
6670
controllers.initialize(server);
6771

68-
// Configure the database to use PostgreSQL
69-
database.configure('postgres://CustomerSample:CustomerSample@localhost:5432/CustomerSampleNodejs');
72+
// Configure the database to use PostgreSQL or Mongodb
73+
database.configure(config.db.connectionString);
7074

7175
// Configure services to use database as backend storage
7276
services.configure(false);
@@ -94,7 +98,7 @@ function Server() {
9498
host: `localhost:${port}`,
9599
// Reference the files containing the swagger definitions
96100
apis: [
97-
path.join(__dirname, '..', 'database', 'customerModel.js'),
101+
path.join(__dirname, '..', 'database', 'mongo', 'customerModel.js'),
98102
path.join(__dirname, '..', 'controllers', 'customer.js')
99103
],
100104
produces: ['application/json'],
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const database = require('../database/mongo'),
4+
Customer = require('../database/mongo/customerModel');
5+
6+
/**
7+
* @public
8+
* @constructor
9+
*/
10+
function CustomerService() {
11+
/**
12+
* Returns a list of customers
13+
* @returns {Promise<Array>}
14+
*/
15+
this.list = () => {
16+
return Customer.find();
17+
};
18+
19+
/**
20+
* Creates a new customer
21+
* @param {string} firstName
22+
* @param {string} lastName
23+
* @returns {Promise}
24+
*/
25+
this.create = (firstName, lastName) => {
26+
var customer = new Customer({
27+
firstName: firstName,
28+
lastName: lastName
29+
});
30+
31+
return customer.save();
32+
};
33+
34+
/**
35+
* Removes a customer
36+
* @param {number} id
37+
* @returns {Promise}
38+
*/
39+
this.remove = id => {
40+
return Customer.find({_id: id}).remove().exec();
41+
};
42+
}
43+
44+
module.exports = new CustomerService();

src/nodejs/WebAPI/service/customer.database.js renamed to src/nodejs/WebAPI/service/customer.postgres.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const database = require('../database');
3+
const database = require('../database/postgres');
44

55
/**
66
* @public

src/nodejs/WebAPI/service/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
const customerInMemoryService = require('./customer.inmemory'),
4-
customerDatabaseService = require('./customer.database');
4+
globalConfig = require('../config'),
5+
customerDatabaseService = require(`./customer.${globalConfig.db.type}`);
56

67
let config = {};
78

0 commit comments

Comments
 (0)