SHARE
TWEET

server/server.js

a guest Dec 2nd, 2019 157 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //  common/models/utils/utils/database-seeding.js
  2. 'use strict';
  3. const mongoose = require('mongoose');
  4.  
  5. const isDev = process.env.NODE_ENV === 'prod';
  6.  
  7. const DataSourceSchema = new mongoose.Schema({
  8.   _id: String,
  9.   host: String,
  10.   port: String,
  11.   database: String,
  12.   password: String,
  13.   name: String,
  14.   user: String,
  15.   clientCode: String,
  16.   connector: String,
  17.   protocol: String
  18. })
  19.  
  20. const DataSources = mongoose.model('Datasources', DataSourceSchema);
  21.  
  22. const params = {
  23.     password: process.env.MONGO_PASSWORD,
  24.     username: process.env.MONGO_USERNAME,
  25.     port: process.env.MONGO_PORT1,
  26.     host: process.env.MONGO_HOST1,
  27.     clusterName: process.env.REPLICA_NAME,
  28.     queryStringParams: 'authSource=admin'
  29. };
  30.  
  31. const options = {
  32.   useNewUrlParser: true,
  33.   reconnectTries: Number.MAX_VALUE,
  34.   reconnectInterval: 500,
  35.   connectTimeoutMS: 10000,
  36. };
  37.  
  38. const seedDatabase = () => {
  39.     let clients = process.env.CLIENTS;
  40.     return new Promise(async (resolve, reject) => {
  41.         const mongoUrl = `mongodb://${params.username}:${params.password}@${params.host}:${params.port}/etd?replicaSet=${params.clusterName}&${params.queryStringParams}`;
  42.         console.log('****************------------------------------------MONGOURL-------------------------***********************************************');
  43.         console.log(mongoUrl);
  44.         console.log('****************------------------------------------MONGOURL------------------------***********************************************');        
  45.         await mongoose.connect(mongoUrl, options);
  46.  
  47.         const clientsList = clients.split(',');
  48.         const dataSources = createDatasourceObjects(clientsList);
  49.         console.log('****************------------------------------------DATASOURCES-------------------------***********************************************');
  50.         console.log(dataSources);
  51.         console.log('****************------------------------------------DATASOURCES------------------------***********************************************');
  52.  
  53.         for (let i = 0; i < dataSources.length; i++) {
  54.             let datasource = await DataSources.findOne({ _id: dataSources[i]._id });
  55.             if (datasource) {
  56.                 //datasource = datasource.toJSON();
  57.                 delete dataSources[i]._id;
  58.                 const ds = Object.assign(datasource, dataSources[i]);
  59.                 console.log('-----------------DS UPDATED (1) --------------------------');
  60.                 console.log(ds);
  61.                 ds.save();
  62.                 console.log('-----------------DS UPDATED (1)--------------------------');
  63.             } else {
  64.                 datasource =  new DataSources(dataSources[i]);
  65.                 console.log('-----------------DS SAVED (1) --------------------------');
  66.                 console.log(datasource);
  67.                 datasource.save();
  68.                 console.log('-----------------DS SAVED (1)--------------------------');
  69.             }
  70.         }
  71.  
  72.         resolve();
  73.  
  74.     });
  75. };
  76.  
  77. const createDatasourceObjects = clientsList => {
  78.     return clientsList.map(client => {
  79.         return {
  80.             _id: `${client}${isDev ? '_stg_etd': ''}`,
  81.             host: params.host,
  82.             port: params.port,
  83.             database: `${client}${isDev ? '_stg_etd': ''}`,
  84.             password: params.password,
  85.             name: `${client}${isDev ? '_stg_etd': ''}`,
  86.             user: params.username,
  87.             clientCode: client.substring(0, 3).toUpperCase(),
  88.             connector: 'mongodb',
  89.             protocol: 'mongodb+srv'
  90.         }
  91.     })
  92. };
  93.  
  94. module.exports = {
  95.     seedDatabase
  96. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top