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 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)
})

View File

@@ -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) {