From a8ddafd452c0b6da1c68c4f7ac233fdf8839f03a Mon Sep 17 00:00:00 2001 From: Ulises Gascon Date: Sun, 26 Jan 2020 19:49:08 +0100 Subject: [PATCH] Migration ES6: Artifacts Related #152 --- artifacts/db-reset.js | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/artifacts/db-reset.js b/artifacts/db-reset.js index f24523d09..2580251aa 100644 --- a/artifacts/db-reset.js +++ b/artifacts/db-reset.js @@ -6,10 +6,11 @@ // before running it (default: development). ie: // NODE_ENV=production node artifacts/db-reset.js -var _ = require("underscore"); -var MongoClient = require("mongodb").MongoClient; +const _ = require("underscore"); +const { MongoClient } = require("mongodb"); +const { db } = require("../config/config"); -var USERS_TO_INSERT = [ +const USERS_TO_INSERT = [ { "_id": 1, "userName": "admin", @@ -25,7 +26,7 @@ var USERS_TO_INSERT = [ "lastName": "Doe", "benefitStartDate": "2030-01-10", "password": "User1_123" - // "password" : "$2a$10$RNFhiNmt2TTpVO9cqZElb.LQM9e1mzDoggEHufLjAnAKImc6FNE86",// User1_123 + // "password" : "$2a$10$RNFhiNmt2TTpVO9cqZElb.LQM9e1mzDoggEHufLjAnAKImc6FNE86",// User1_123 }, { "_id": 3, "userName": "user2", @@ -33,13 +34,12 @@ var USERS_TO_INSERT = [ "lastName": "Smith", "benefitStartDate": "2025-11-30", "password": "User2_123" - //"password" : "$2a$10$Tlx2cNv15M0Aia7wyItjsepeA8Y6PyBYaNdQqvpxkIUlcONf1ZHyq", // User2_123 + //"password" : "$2a$10$Tlx2cNv15M0Aia7wyItjsepeA8Y6PyBYaNdQqvpxkIUlcONf1ZHyq", // User2_123 }]; // Getting the global config taking in account he environment (proc) -var config = require("../config/config"); -function parseResponse(err, res, comm) { +const parseResponse = (err, res, comm) => { if (err) { console.log("ERROR:"); console.log(comm); @@ -52,15 +52,13 @@ function parseResponse(err, res, comm) { // Starting here -MongoClient.connect(config.db, function(err, db) { - var usersCol, allocationsCol, countersCol; - +MongoClient.connect(db, (err, db) => { if (err) { console.log("ERROR: connect"); console.log(JSON.stringify(err)); process.exit(1); } - console.log("Connected to the database: " + config.db); + console.log("Connected to the database: " + db); // remove existing data (if any), we don't want to look for errors here db.dropCollection("users"); @@ -69,9 +67,9 @@ MongoClient.connect(config.db, function(err, db) { db.dropCollection("memos"); db.dropCollection("counters"); - usersCol = db.collection("users"); - allocationsCol = db.collection("allocations"); - countersCol = db.collection("counters"); + const usersCol = db.collection("users"); + const allocationsCol = db.collection("allocations"); + const countersCol = db.collection("counters"); // reset unique id counter countersCol.insert({ @@ -81,13 +79,10 @@ MongoClient.connect(config.db, function(err, db) { // insert admin and test users console.log("Users to insert:"); - USERS_TO_INSERT.forEach(function(user) { - console.log(JSON.stringify(user)); - }); + USERS_TO_INSERT.forEach((user) => console.log(JSON.stringify(user))); - usersCol.insertMany(USERS_TO_INSERT, function(err, data) { - var finalAllocations = []; - var ids; + usersCol.insertMany(USERS_TO_INSERT, (err, data) => { + const finalAllocations = []; // We can't continue if error here if (err) { @@ -97,7 +92,7 @@ MongoClient.connect(config.db, function(err, db) { } parseResponse(err, data, "users.insertMany"); - data.ops.forEach(function(user) { + data.ops.forEach((user) => { var stocks = Math.floor((Math.random() * 40) + 1); var funds = Math.floor((Math.random() * 40) + 1); @@ -110,11 +105,9 @@ MongoClient.connect(config.db, function(err, db) { }); console.log("Allocations to insert:"); - finalAllocations.forEach(function(allocation) { - console.log(JSON.stringify(allocation)); - }); + finalAllocations.forEach(allocation => console.log(JSON.stringify(allocation))); - allocationsCol.insertMany(finalAllocations, function(err, data) { + allocationsCol.insertMany(finalAllocations, (err, data) => { parseResponse(err, data, "allocations.insertMany"); console.log("Database reset performed successfully") process.exit(0);