From df1b0e0ef05a8c923834dd2e310deb2a31144343 Mon Sep 17 00:00:00 2001 From: Valentin Kaelin Date: Sun, 15 Dec 2019 20:50:27 +0100 Subject: [PATCH] feat: add ace command to delete specific matches from the db --- server/app/Commands/DeleteMatch.js | 33 ++++++++++++++++++++++++++++++ server/start/app.js | 4 +++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 server/app/Commands/DeleteMatch.js diff --git a/server/app/Commands/DeleteMatch.js b/server/app/Commands/DeleteMatch.js new file mode 100644 index 0000000..1baf53e --- /dev/null +++ b/server/app/Commands/DeleteMatch.js @@ -0,0 +1,33 @@ +'use strict' + +const { Command } = require('@adonisjs/ace') +const Database = use('Database') +const Match = use('App/Models/Match') + +class DeleteMatch extends Command { + static get signature() { + return ` + delete:match + { field : Delete row if entered field is null } + ` + } + + static get description() { + return 'Delete matches from db with a condition' + } + + async handle(args, options) { + console.time('DeleteMatches') + const nbMatchesBefore = await Match.count() + const matches = await Match.all() + for (const match of matches.toJSON()) { + await Match.where('_id', match._id).where(args.field, null).delete() + } + const nbMatchesAfter = await Match.count() + Database.close() + console.timeEnd('DeleteMatches') + this.success(`${this.icon('success')} Delete Matches completed: ${nbMatchesBefore - nbMatchesAfter} matche(s) deleted`) + } +} + +module.exports = DeleteMatch diff --git a/server/start/app.js b/server/start/app.js index b20f92f..ce57666 100644 --- a/server/start/app.js +++ b/server/start/app.js @@ -57,6 +57,8 @@ const aliases = {} | Here you store ace commands for your package | */ -const commands = [] +const commands = [ + 'App/Commands/DeleteMatch', +] module.exports = { providers, aceProviders, aliases, commands }