Skip to content

Access database using Lucid Models inside a Ace command. #2523

Answered by thetutlage
kush-prof asked this question in Help
Discussion options

You must be logged in to vote

Couple of things here.

  • You must load the application using the settings.loadApp option. Commands do not boot the application by default.
  • 2nd, commands cannot have top level imports, as they are scanned when generating the ace-manifest.json file.

The following code should work

import { BaseCommand } from '@adonisjs/ace';

export default class TestCommand extends BaseCommand {
 /**
  * Command Name is used to run the command
  */
 public static commandName = 'test:command';

 /**
  * Command Name is displayed in the "help" output
  */
 public static description = '';

 public static settings = {
   loadApp: true
 }

 public async run() {
   const User = (await import('App/Models/User')).d…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@kush-prof
Comment options

@matthiasrohmer
Comment options

Answer selected by kush-prof
Comment options

You must be logged in to vote
1 reply
@RomainLanz
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
5 participants