OCASA News | Empowering Students with Every Story

Creating Custom Commands in AdonisJs

Written by Abhay Dave | Oct 15, 2019 7:23:26 AM

Creating Custom Ace Commands in Adonis

In this blog, you should be able to find out how one can create custom ACE commands in Adonis.

Why Custom Ace Commands Matter in Backend Development Services

Custom Ace commands are essential for automating repetitive tasks in backend development services. By defining new commands, developers can streamline processes like database migrations, seeding data, and managing configurations efficiently.

So far, there are a few already defined commands provided in Adonis, which can be found using:

<code class="language-javascript">adonis -- help</code>

Creating a New Command

To create new commands, you have to use:

adonis make:command test

Once the command is created, you will be given instructions to add this command in start/app.js as follows

<code class="language-javascript">→ Open start/app.js→ Add App/Commands/Test to commands array</code>

So the following needs to be updated your app.js file

<code class="language-javascript">const commands = ['App/Commands/Test']</code>

Here, we will be updating a model called LeaveType using a JSON file 'leave-types.yml'

<code class="language-javascript">#leave-types.yml{    "types": [      "Sick Leave (SL)",      "Earned Leave (EL)",      "Loss Of Pay (LOP)",      "Restricted Holiday (RH)",      "Other"    ]}</code>

Now to update the required action to be performed from your command, you need to override the handle method of Commands/Test.js as follows

<code class="language-javascript"># add this to the beginning of your command fileconst { Command } = require('@adonisjs/ace')const LeaveType = use('LeaveType') // used an alias for Model LeaveTypeconst Database = use('Database')const yaml = require('js-yaml');const fs   = require('fs');</code>

Update the handle action as follows -

<code class="language-javascript">async handle () {  const fs = require('fs')    const data = yaml.safeLoad(fs.readFileSync('./config/leave-types.yml', 'utf8'))    data.types.forEach(function(type){    try {      const leaveType = new LeaveType()      leaveType.name = type      await leaveType.save();     }    catch (e){     console.log(e);    }    Database.close();  }}</code>

To execute the command just run the signature present in the command file with Adonis as -

<code class="language-javascript">adonis test</code>

Additional Considerations

By leveraging custom Ace commands, developers can optimize their workflow and automate various backend processes. This not only improves efficiency but also ensures consistency in handling structured data operations.

Happy Coding! ????

In case of queries, please feel free to drop a comment.

Related Blogs -

Using Kafka With Adonisjs Application
Scrum Master Vs Project Manager Everything You Need To Know
Wearable Technology And Its Past Present And Future
How Manufacturing Companies Gain Value From Business Intelligence Platform