Skip to content

add mongodb as optional database #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"gulp-sourcemaps": "1.6.0",
"gulp-typescript": "2.11.0",
"gulp-watch": "4.3.5",
"mongoose": "^4.4.6",
"oauth2-server": "2.4.1",
"pg": "4.4.4",
"reflect-metadata": "0.1.3",
Expand Down
12 changes: 12 additions & 0 deletions src/nodejs/WebAPI/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Created by ppa on 3/3/16.
*/
module.exports = {
db: {
// setup database type
/*type: 'mongo',
connectionString: 'mongodb://localhost/customer-sample-node'*/
type: 'postgres',
connectionString: 'postgres://postgres:Hallo11@localhost:5432/CustomerSampleNodejs'
}
};
41 changes: 41 additions & 0 deletions src/nodejs/WebAPI/database/mongo/customerModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Created by ppa on 3/3/16.
*/
const mongoose = require('mongoose'),
Schema = mongoose.Schema;

var customerSchema = new Schema({
firstName: String,
lastName: String
}, {
toObject: {virtuals: true},
toJSON: {virtuals: true}
});

customerSchema.virtual('id').get(function () {
return this._id;
});

var Customer = mongoose.model('Customer', customerSchema);

module.exports = Customer;

/**
* @swagger
* definitions:
* CustomerModel:
* required:
* - id
* - firstName
* - lastName
* properties:
* id:
* type: string
* description: A unique identifier
* firstName:
* type: string
* description: The first name of the given customer
* lastName:
* type: string
* description: The last name of the given customer
*/
10 changes: 10 additions & 0 deletions src/nodejs/WebAPI/database/mongo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const mongoose = require('mongoose');

// Using a provider pattern for db configuration and initialization
module.exports = {
configure: (connectionString) => {
mongoose.connect(connectionString);
}
};
12 changes: 8 additions & 4 deletions src/nodejs/WebAPI/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ const restify = require('restify'),
// Require the services
const services = require('../service'),

// Require configuration
config = require('../config'),

// Require the controllers
controllers = require('../controllers'),

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

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

/**
* Restify server exposing some APIs to manipulate customer data
Expand Down Expand Up @@ -65,8 +69,8 @@ function Server() {
// Initialize all controllers
controllers.initialize(server);

// Configure the database to use PostgreSQL
database.configure('postgres://CustomerSample:CustomerSample@localhost:5432/CustomerSampleNodejs');
// Configure the database to use PostgreSQL or Mongodb
database.configure(config.db.connectionString);

// Configure services to use database as backend storage
services.configure(false);
Expand Down Expand Up @@ -94,7 +98,7 @@ function Server() {
host: `localhost:${port}`,
// Reference the files containing the swagger definitions
apis: [
path.join(__dirname, '..', 'database', 'customerModel.js'),
path.join(__dirname, '..', 'database', config.db.type, 'customerModel.js'),
path.join(__dirname, '..', 'controllers', 'customer.js')
],
produces: ['application/json'],
Expand Down
44 changes: 44 additions & 0 deletions src/nodejs/WebAPI/service/customer.mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

const database = require('../database/mongo'),
Customer = require('../database/mongo/customerModel');

/**
* @public
* @constructor
*/
function CustomerService() {
/**
* Returns a list of customers
* @returns {Promise<Array>}
*/
this.list = () => {
return Customer.find();
};

/**
* Creates a new customer
* @param {string} firstName
* @param {string} lastName
* @returns {Promise}
*/
this.create = (firstName, lastName) => {
var customer = new Customer({
firstName: firstName,
lastName: lastName
});

return customer.save();
};

/**
* Removes a customer
* @param {number} id
* @returns {Promise}
*/
this.remove = id => {
return Customer.find({_id: id}).remove().exec();
};
}

module.exports = new CustomerService();
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const database = require('../database');
const database = require('../database/postgres');

/**
* @public
Expand Down
3 changes: 2 additions & 1 deletion src/nodejs/WebAPI/service/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const customerInMemoryService = require('./customer.inmemory'),
customerDatabaseService = require('./customer.database');
globalConfig = require('../config'),
customerDatabaseService = require(`./customer.${globalConfig.db.type}`);

let config = {};

Expand Down