Return this from connect for chaining

This commit is contained in:
John Lyon-Smith
2018-04-20 17:51:02 -07:00
parent f7c73ee277
commit 60d51ea5a9
2 changed files with 33 additions and 24 deletions

View File

@@ -1,13 +1,13 @@
import path from 'path' import path from "path"
import config from 'config' import config from "config"
import mongoose from 'mongoose' import mongoose from "mongoose"
import { DB } from '../database' import { DB } from "../database"
import credential from 'credential' import credential from "credential"
import readlineSync from 'readline-sync' import readlineSync from "readline-sync"
import crypto from 'crypto' import crypto from "crypto"
import urlSafeBase64 from 'urlsafe-base64' import urlSafeBase64 from "urlsafe-base64"
import util from 'util' import util from "util"
import autobind from 'autobind-decorator' import autobind from "autobind-decorator"
@autobind @autobind
class AddUserTool { class AddUserTool {
@@ -18,7 +18,7 @@ class AddUserTool {
async run() { async run() {
try { try {
const mongoUri = config.get('uri.mongo') const mongoUri = config.get("uri.mongo")
const db = await new DB().connect(mongoUri) const db = await new DB().connect(mongoUri)
console.log(`Connected to MongoDB at '${mongoUri}'`) console.log(`Connected to MongoDB at '${mongoUri}'`)
@@ -27,10 +27,10 @@ class AddUserTool {
let user = new User({ let user = new User({
administrator: true, administrator: true,
}) })
user.firstName = readlineSync.question('First name? ') user.firstName = readlineSync.question("First name? ")
user.lastName = readlineSync.question('Last name? ') user.lastName = readlineSync.question("Last name? ")
user.email = readlineSync.question('Email? ') user.email = readlineSync.question("Email? ")
let password = readlineSync.question('Password? ', {hideEchoBack: true}) let password = readlineSync.question("Password? ", { hideEchoBack: true })
let cr = credential() let cr = credential()
const json = await util.promisify(cr.hash)(password) const json = await util.promisify(cr.hash)(password)
@@ -40,7 +40,7 @@ class AddUserTool {
const savedUser = await user.save() const savedUser = await user.save()
console.log(`User is ${user}`) console.log(`User is ${user}`)
} catch(error) { } catch (error) {
console.log(`error: ${error.message}`) console.log(`error: ${error.message}`)
} }
} }
@@ -48,14 +48,21 @@ class AddUserTool {
const log = { const log = {
info: console.info, info: console.info,
error: function() { console.error(chalk.red('error:', [...arguments].join(' ')))}, error: function() {
warning: function() { console.error(chalk.yellow('warning:', [...arguments].join(' ')))} 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) => { tool
process.exit(exitCode) .run(process.argv.slice(2))
}).catch((err) => { .then((exitCode) => {
console.error(err) process.exit(exitCode)
}) })
.catch((err) => {
console.error(err)
})

View File

@@ -29,6 +29,8 @@ export class DB {
this.Activity = connection.model("Activity", Schemas.activitySchema) this.Activity = connection.model("Activity", Schemas.activitySchema)
this.Team = connection.model("Team", Schemas.teamSchema) this.Team = connection.model("Team", Schemas.teamSchema)
this.Counter = connection.model("Counter", Schemas.counterSchema) this.Counter = connection.model("Counter", Schemas.counterSchema)
return this
} }
newObjectId(s) { newObjectId(s) {