From a363a2485bd75abbd38628631c8b6fd2475ba038 Mon Sep 17 00:00:00 2001 From: Michele Mancioppi Date: Sun, 7 Jun 2020 12:54:56 +0200 Subject: [PATCH] [WIP] Mongo init --- CF/mongo-init/init-db.js | 160 +++++++++++++++---------------------- CF/mongo-init/package.json | 2 +- 2 files changed, 65 insertions(+), 97 deletions(-) diff --git a/CF/mongo-init/init-db.js b/CF/mongo-init/init-db.js index 5b13146..c43f204 100644 --- a/CF/mongo-init/init-db.js +++ b/CF/mongo-init/init-db.js @@ -1,44 +1,47 @@ /*jshint esversion: 8 */ const mongoClient = require('mongodb').MongoClient; -const bindingName = 'catalogue_database'; -var mongoURL = 'mongodb://mongodb:27017/catalogue'; +new Promise((resolve, reject) => { + if (process.env.VCAP_SERVICES) { + const bindingName = 'catalogue_database'; -if (process.env.VCAP_SERVICES) { - connectionDetails = null; + connectionDetails = null; + + for (let [key, value] of Object.entries(JSON.parse(process.env.VCAP_SERVICES))) { + connectionDetails = value.find(function(binding) { + return bindingName == binding.binding_name && binding.credentials; + }).credentials; + + if (!connectionDetails) { + reject(`Cannot find a service binding with name '${bindingName}'`); + } - for (let [key, value] of Object.entries(JSON.parse(process.env.VCAP_SERVICES))) { - connectionDetails = value.find(function(binding) { - return bindingName == binding.binding_name && binding.credentials; - }).credentials; + const username = connectionDetails.username; + const password = connectionDetails.password; - if (connectionDetails) { - mongoURL = connectionDetails.uri; - break; + const normalizedAuthDetails = `${encodeURIComponent(username)}:${encodeURIComponent(password)}`; + + const mongoUrl = connectionDetails.uri.replace(`${username}:${password}`, normalizedAuthDetails); + + resolve(mongoUrl); } + } else if (process.env.MONGO_URL) { + resolve(process.env.MONGO_URL); + } else { + reject('MongoDB connection data missing'); } -} else if (process.env.MONGO_URL) { - mongoURL = process.env.MONGO_URL; -} +}).then(function (mongoUrl) { + console.log(`Connecting to ${mongoUrl}`); -if (!mongoURL) { - throw new Error('MongoDB connection data missing'); -} + return mongoClient.connect(mongoUrl); +}).then(function(db) { + const products = db.collection('products'); + const users = db.collection('users'); -mongoClient.connect(mongoURL) - .catch(function(err) { - console.log(`Cannot connect to the MongoDB database: ${err}`); - process.exit(4242); - }) - .then(function(db) { - console.log('Creating products collection'); - - products = db.collection('products'); - - // Delete all old product entries before adding new ones - products.deleteMany({}); - products.insertMany([ + return products + .deleteMany({}) + .then(products.insertMany([ {sku: 'HAL-1', name: 'HAL', description: 'Sorry Dave, I cant do that', price: 2001, instock: 2, categories: ['AI']}, {sku: 'PB-1', name: 'Positronic Brain', description: 'Highly advanced sentient processing unit with the laws of robotics burned in', price: 200, instock: 0, categories: ['AI']}, {sku: 'ROB-1', name: 'Robbie', description: 'Large mechanical workhorse, crude but effective. Comes in handy when you are lost in space', price: 1200, instock: 12, categories: ['Robot']}, @@ -50,73 +53,38 @@ mongoClient.connect(mongoURL) {sku: 'HHGTTG', name: 'Marvin', description: 'Marvin, your paranoid android. Brain the size of a planet', price: 42, instock: 48, categories: ['Robot']}, {sku: 'STAN-1', name: 'Stan', description: 'APM guru', price: 50, instock: 1000, categories: ['Robot', 'AI']}, {sku: 'STNG', name: 'Mr Data', description: 'Could be R. Daneel Olivaw? Protype positronic brain android', price: 1000, instock: 0, categories: ['Robot']} - ]); - - // - // Users - // - console.log('Creating users collection'); - - const users = db.collection('users'); - - console.log('Starting users import'); - - // Delete all old user entries before adding new ones - users.deleteMany({}); - users.insertMany([ - {name: 'user', password: 'password', email: 'user@me.com'}, - {name: 'stan', password: 'bigbrain', email: 'stan@instana.com'} - ]); - - // unique index on the name - users.createIndex( - {name: 1}, - {unique: true} - ); - - console.log('Users imported'); - - console.log('Creating catalogue collection'); - - const catalogue = db.collection('catalogue'); - - console.log('Starting catalogue import'); - - // Delete all old catalogue entries before adding new ones - catalogue.deleteMany({}); - catalogue.insertMany([ - {sku: 'HAL-1', name: 'HAL', description: 'Sorry Dave, I cant do that', price: 2001, instock: 2, categories: ['AI']}, - {sku: 'PB-1', name: 'Positronic Brain', description: 'Highly advanced sentient processing unit with the laws of robotics burned in', price: 200, instock: 0, categories: ['AI']}, - {sku: 'ROB-1', name: 'Robbie', description: 'Large mechanical workhorse, crude but effective. Comes in handy when you are lost in space', price: 1200, instock: 12, categories: ['Robot']}, - {sku: 'EVE-1', name: 'Eve', description: 'Extraterrestrial Vegetation Evaluator', price: 5000, instock: 10, categories: ['Robot']}, - {sku: 'C3P0', name: 'C3P0', description: 'Protocol android', price: 700, instock: 1, categories: ['Robot']}, - {sku: 'R2D2', name: 'R2D2', description: 'R2 maintenance robot and secret messenger. Help me Obi Wan', price: 1400, instock: 1, categories: ['Robot']}, - {sku: 'K9', name: 'K9', description: 'Time travelling companion at heel', price: 300, instock: 12, categories: ['Robot']}, - {sku: 'RD-10', name: 'Kryten', description: 'Red Drawf crew member', price: 700, instock: 5, categories: ['Robot']}, - {sku: 'HHGTTG', name: 'Marvin', description: 'Marvin, your paranoid android. Brain the size of a planet', price: 42, instock: 48, categories: ['Robot']}, - {sku: 'STAN-1', name: 'Stan', description: 'APM guru', price: 50, instock: 1000, categories: ['Robot', 'AI']}, - {sku: 'STNG', name: 'Mr Data', description: 'Could be R. Daneel Olivaw? Protype positronic brain android', price: 1000, instock: 0, categories: ['Robot']} - ]); - + ])) // full text index for searching - catalogue.createIndex({ - name: "text", - description: "text" - }); - + .then(products.createIndex({ + name: 'text', + description: 'text' + })) // unique index for product sku - catalogue.createIndex( + .then(products.createIndex( { sku: 1 }, { unique: true } - ); - - console.log('Products imported'); - }) - .then(function() { - console.log('All done'); - process.exit(0); - }) - .catch(function(err) { - console.log(`Error while importing data into the MongoDB '${bindingName}' database: ${err}`); - process.exit(42); - }); \ No newline at end of file + )) + .then(function() { + console.log('Populated \'products\' collection'); + }) + .then(users.deleteMany({})) + .then(users.insertMany([ + {name: 'user', password: 'password', email: 'user@me.com'}, + {name: 'stan', password: 'bigbrain', email: 'stan@instana.com'} + ])) + .then(users.createIndex( + {name: 1}, + {unique: true} + )) + .then(function () { + console.log('Populated \'users\' collection'); + }) +}) +.then(function() { + console.log('All done'); + process.exit(0); +}) +.catch(function(err) { + console.log(`Error while importing data into the MongoDB database: ${err}`); + process.exit(42); +}); \ No newline at end of file diff --git a/CF/mongo-init/package.json b/CF/mongo-init/package.json index f5bb722..50f36e6 100644 --- a/CF/mongo-init/package.json +++ b/CF/mongo-init/package.json @@ -9,6 +9,6 @@ "author": "SteveW", "license": "Apache-2.0", "dependencies": { - "mongodb": "^2.2.33" + "mongodb": "2.2.33" } }