From 60d51ea5a9461ce4d5f22a0d2af65cb2b5c90f2a Mon Sep 17 00:00:00 2001 From: John Lyon-Smith Date: Fri, 20 Apr 2018 17:51:02 -0700 Subject: [PATCH] Return this from connect for chaining --- server/src/bin/addUser.js | 55 ++++++++++++++++++++++----------------- server/src/database/DB.js | 2 ++ 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/server/src/bin/addUser.js b/server/src/bin/addUser.js index 4b08f96..d46a64e 100644 --- a/server/src/bin/addUser.js +++ b/server/src/bin/addUser.js @@ -1,13 +1,13 @@ -import path from 'path' -import config from 'config' -import mongoose from 'mongoose' -import { DB } from '../database' -import credential from 'credential' -import readlineSync from 'readline-sync' -import crypto from 'crypto' -import urlSafeBase64 from 'urlsafe-base64' -import util from 'util' -import autobind from 'autobind-decorator' +import path from "path" +import config from "config" +import mongoose from "mongoose" +import { DB } from "../database" +import credential from "credential" +import readlineSync from "readline-sync" +import crypto from "crypto" +import urlSafeBase64 from "urlsafe-base64" +import util from "util" +import autobind from "autobind-decorator" @autobind class AddUserTool { @@ -18,7 +18,7 @@ class AddUserTool { async run() { try { - const mongoUri = config.get('uri.mongo') + const mongoUri = config.get("uri.mongo") const db = await new DB().connect(mongoUri) console.log(`Connected to MongoDB at '${mongoUri}'`) @@ -27,10 +27,10 @@ class AddUserTool { let user = new User({ administrator: true, }) - user.firstName = readlineSync.question('First name? ') - user.lastName = readlineSync.question('Last name? ') - user.email = readlineSync.question('Email? ') - let password = readlineSync.question('Password? ', {hideEchoBack: true}) + user.firstName = readlineSync.question("First name? ") + user.lastName = readlineSync.question("Last name? ") + user.email = readlineSync.question("Email? ") + let password = readlineSync.question("Password? ", { hideEchoBack: true }) let cr = credential() const json = await util.promisify(cr.hash)(password) @@ -40,7 +40,7 @@ class AddUserTool { const savedUser = await user.save() console.log(`User is ${user}`) - } catch(error) { + } catch (error) { console.log(`error: ${error.message}`) } } @@ -48,14 +48,21 @@ class AddUserTool { const log = { info: console.info, - error: function() { console.error(chalk.red('error:', [...arguments].join(' ')))}, - warning: function() { console.error(chalk.yellow('warning:', [...arguments].join(' ')))} + error: function() { + console.error(chalk.red("error:", [...arguments].join(" "))) + }, + warning: function() { + console.error(chalk.yellow("warning:", [...arguments].join(" "))) + }, } -const tool = new AddUserTool('addUser', log) +const tool = new AddUserTool("addUser", log) -tool.run(process.argv.slice(2)).then((exitCode) => { - process.exit(exitCode) -}).catch((err) => { - console.error(err) -}) +tool + .run(process.argv.slice(2)) + .then((exitCode) => { + process.exit(exitCode) + }) + .catch((err) => { + console.error(err) + }) diff --git a/server/src/database/DB.js b/server/src/database/DB.js index 0b1ed5d..ac68a27 100644 --- a/server/src/database/DB.js +++ b/server/src/database/DB.js @@ -29,6 +29,8 @@ export class DB { this.Activity = connection.model("Activity", Schemas.activitySchema) this.Team = connection.model("Team", Schemas.teamSchema) this.Counter = connection.model("Counter", Schemas.counterSchema) + + return this } newObjectId(s) {