diff --git a/package.json b/package.json index 3c0825c..615dfd2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/nodejs/WebAPI/config/index.js b/src/nodejs/WebAPI/config/index.js new file mode 100644 index 0000000..439779e --- /dev/null +++ b/src/nodejs/WebAPI/config/index.js @@ -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' + } +}; \ No newline at end of file diff --git a/src/nodejs/WebAPI/database/mongo/customerModel.js b/src/nodejs/WebAPI/database/mongo/customerModel.js new file mode 100644 index 0000000..7f99718 --- /dev/null +++ b/src/nodejs/WebAPI/database/mongo/customerModel.js @@ -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 + */ \ No newline at end of file diff --git a/src/nodejs/WebAPI/database/mongo/index.js b/src/nodejs/WebAPI/database/mongo/index.js new file mode 100644 index 0000000..3d0d811 --- /dev/null +++ b/src/nodejs/WebAPI/database/mongo/index.js @@ -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); + } +}; diff --git a/src/nodejs/WebAPI/database/customerModel.js b/src/nodejs/WebAPI/database/postgres/customerModel.js similarity index 100% rename from src/nodejs/WebAPI/database/customerModel.js rename to src/nodejs/WebAPI/database/postgres/customerModel.js diff --git a/src/nodejs/WebAPI/database/index.js b/src/nodejs/WebAPI/database/postgres/index.js similarity index 100% rename from src/nodejs/WebAPI/database/index.js rename to src/nodejs/WebAPI/database/postgres/index.js diff --git a/src/nodejs/WebAPI/server/index.js b/src/nodejs/WebAPI/server/index.js index 7e67aac..910450f 100644 --- a/src/nodejs/WebAPI/server/index.js +++ b/src/nodejs/WebAPI/server/index.js @@ -15,6 +15,9 @@ const restify = require('restify'), // Require the services const services = require('../service'), + // Require configuration + config = require('../config'), + // Require the controllers controllers = require('../controllers'), @@ -22,7 +25,8 @@ const services = require('../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 @@ -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); @@ -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'], diff --git a/src/nodejs/WebAPI/service/customer.mongo.js b/src/nodejs/WebAPI/service/customer.mongo.js new file mode 100644 index 0000000..8024617 --- /dev/null +++ b/src/nodejs/WebAPI/service/customer.mongo.js @@ -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} + */ + 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(); diff --git a/src/nodejs/WebAPI/service/customer.database.js b/src/nodejs/WebAPI/service/customer.postgres.js similarity index 94% rename from src/nodejs/WebAPI/service/customer.database.js rename to src/nodejs/WebAPI/service/customer.postgres.js index b5afa17..eca4cb9 100644 --- a/src/nodejs/WebAPI/service/customer.database.js +++ b/src/nodejs/WebAPI/service/customer.postgres.js @@ -1,6 +1,6 @@ 'use strict'; -const database = require('../database'); +const database = require('../database/postgres'); /** * @public diff --git a/src/nodejs/WebAPI/service/index.js b/src/nodejs/WebAPI/service/index.js index 585bbfc..830f70d 100644 --- a/src/nodejs/WebAPI/service/index.js +++ b/src/nodejs/WebAPI/service/index.js @@ -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 = {};