diff --git a/dtp-base-cli.js b/dtp-base-cli.js index 8dedaca..3a884fd 100644 --- a/dtp-base-cli.js +++ b/dtp-base-cli.js @@ -32,20 +32,24 @@ class SiteTerminalApp extends SiteRuntime { this.processors = { 'help': { - handler: this.help.bind(this), + handler: this.cmdHelp.bind(this), help: 'help [command name]', }, 'create-account': { - handler: this.createAccount.bind(this), + handler: this.cmdCreateAccount.bind(this), help: 'create-account email username [password]', }, 'grant': { - handler: this.grant.bind(this), + handler: this.cmdGrant.bind(this), help: 'grant [admin|moderator] username', }, + 'reset-indexes': { + handler: this.cmdResetIndexes.bind(this), + help: 'reset-indexes [modelname, modelname, ...]', + }, 'revoke': { - handler: this.revoke.bind(this), + handler: this.cmdRevoke.bind(this), help: 'revoke [admin|moderator] username', }, }; @@ -67,7 +71,7 @@ class SiteTerminalApp extends SiteRuntime { return processor.handler(args); } - async help (args) { + async cmdHelp (args) { const commandName = args.shift(); if (commandName) { const command = this.processors[commandName]; @@ -87,7 +91,7 @@ class SiteTerminalApp extends SiteRuntime { } } - async createAccount (args) { + async cmdCreateAccount (args) { const { user: userService } = this.services; const email = args.shift(); const username = args.shift(); @@ -96,7 +100,7 @@ class SiteTerminalApp extends SiteRuntime { await userService.create({ email, username, password }); } - async grant (args) { + async cmdGrant (args) { const User = mongoose.model('User'); const privilege = args.shift(); @@ -119,7 +123,16 @@ class SiteTerminalApp extends SiteRuntime { } } - async revoke (args) { + async cmdResetIndexes (args) { + try { + this.log.info('resetting MongoDB indexes...', { args }); + await this.resetIndexes(args); + } catch (error) { + this.log.error('failed to reset database indexes', { error }); + } + } + + async cmdRevoke (args) { const User = mongoose.model('User'); const username_lc = args.shift(); diff --git a/lib/site-runtime.js b/lib/site-runtime.js index 7d6d88b..96ceb52 100644 --- a/lib/site-runtime.js +++ b/lib/site-runtime.js @@ -142,36 +142,44 @@ export class SiteRuntime { } } - async resetIndexes (target) { - if (target === 'all') { - for (const model of this.models) { - await this.resetIndex(model); + async resetIndexes (args) { + let target = args.shift(); + if (!target || (target === 'all')) { + for (const modelName in this.models) { + await this.resetIndex(this.models[modelName]); } return; } - - const model = this.models.find((model) => model.modelName === target); - if (!model) { - throw new Error(`requested Mongoose model does not exist: ${target}`); - } - return this.resetIndex(model); + const targets = args.map((modelName) => { + return { + modelName, + model: this.models.find(modelName), + }; + }); + for (target of targets) { + if (!target.model) { + this.log.error('unknown model', { modelName: target.modelName }); + continue; + } + await this.resetIndex(target.model); + } } async resetIndex (model) { return new Promise(async (resolve, reject) => { this.log.info('dropping model indexes', { model: model.modelName }); - model.collection.dropIndexes((err) => { + model.collection.dropIndexes(async (err) => { if (err) { return reject(err); } this.log.info('creating model indexes', { model: model.modelName }); - model.ensureIndexes((err) => { - if (err) { - return reject(err); - } + try { + await model.ensureIndexes(); return resolve(model); - }); + } catch (error) { + return reject(err); + } }); }); }