2.5.41 • Published 2 years ago

@sahgoku/adonisjs-crud-generator v2.5.41

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

AdonisJS CRUD Generator

Ask Telegram

Downloads GitHub package.json version Github Hello, I hope this is useful for your projects. I need your feedback to improve the tool. Thank you.

This package allows you to easily generate essential files for making CRUD with a database table using legacy version of AdonisJS app. The package generates the following:

  • Routes
  • Controllers
  • Models
  • Services

Currently Supported

comment: <> (- PostgreSQL, MySQL)

npm.io npm.io

Installation

You can install the package via npm:

npm install @sahgoku/adonisjs-crud-generator

yarn:

yarn add @sahgoku/adonisjs-crud-generator

Setup

  • In start/app.js, add the following to commands array:
  '@sahgoku/adonisjs-crud-generator/Crud/CrudGenerator',
  '@sahgoku/adonisjs-crud-generator/Crud/ModelGenerator',
  '@sahgoku/adonisjs-crud-generator/Crud/RouteGenerator',
  '@sahgoku/adonisjs-crud-generator/Crud/ControllerGenerator',
  '@sahgoku/adonisjs-crud-generator/Crud/ServiceGenerator',
  • In start/app.js, add the following to aliases object:
  ControllerHelper: '@sahgoku/adonisjs-crud-generator/Helpers/ControllerHelper',
  SearchFilterHelper: '@sahgoku/adonisjs-crud-generator/Helpers/SearchFilterHelper',
  CustomException: '@sahgoku/adonisjs-crud-generator/Helpers/CustomException',

Usage

To generate CRUD for existing table roles, run adonis crud:generate roles and follow the instructions. The following will be created :

  • Controller app/Controllers/Http/RoleController.
  • start/routes.js file will be updated with new routes.
  • Role model will be created with appropriate relationships. The corresponding linked models will be updated with the new relationships.
  • Role services will be created.

npm.io

Services

Request Body:

{
  "populates" : ['permissions'], 
  "filter" : {"type":"operator","operand":"and","value":[{"type":"condition","value":5,"field":"id","operand":"equal"}, {"type":"condition","value":6,"field":"id","operand":"equal"}]}
  "pagination": {"sortBy":"id", "descending":true, "page":1, "rowsPerPage":10}, 
  "count" : ['permissions'], 
  "select" : ['name'] 
}
  • list.js
'use strict';
const RoleModel = use("App/Models/auth/Role");
const ControllerHelper = use('ControllerHelper')

class List {

  constructor(transaction, user) {
    this.transaction = transaction;
    this.user = user;
  }

  async execute(payload) {
      let query = RoleModel.query();
      ControllerHelper.populate(query, payload.populates);
      return await ControllerHelper.search(query, payload.pagination, payload.filter, payload.count, payload.select);
    }
}

module.exports = List;
  • create.js
'use strict';
const RoleModel = use("App/Models/auth/Role");
const _ = require('lodash');

class Create {

  constructor(transaction, user) {
    this.transaction = transaction;
    this.user = user;
  }

  async execute(form) {

    return await RoleModel.create({
        ..._.pick(form, ['name','code']),
        created_by: this.user.id,
      },
      this.transaction);
  }
}

module.exports = Create;
  • update.js
'use strict';
const RoleModel = use("App/Models/auth/Role");
const CustomException = use('CustomException')
const _ = require('lodash');

class Update {
  constructor(transaction, user) {
    this.transaction = transaction;
    this.user = user;
  }

  async execute(form) {

    const response = await RoleModel.query(this.transaction)
      .where('id', form.id).first();

    if (!response) throw new CustomException("Role not found", 400, "ROLE_NOT_FOUND")

    response.merge({
      ..._.pick(form, ['name','code']),
      updated_by: this.user.id,
    })
    await response.save(this.transaction);
    return response;
  }

}

module.exports = Update;
  • show.js
'use strict';
const RoleModel = use("App/Models/auth/Role");
const CustomException = use('CustomException')
const ControllerHelper = use('ControllerHelper')

class Show {

  constructor(transaction, user) {
    this.transaction = transaction;
    this.user = user;
  }

  async execute(form) {
    const query = RoleModel
      .query(this.transaction)
      .where('id', form.id)

       let response = await ControllerHelper
            .populate(query, form.populates)
            .first();

    if (!response) {
      throw new CustomException("Role not found", 400, "ROLE_NOT_FOUND")
    }
    return response;
  }
}

module.exports = Show;
  • destroy.js
'use strict';
const RoleModel = use("App/Models/auth/Role");
const CustomException = use('CustomException')

class Destroy {

  constructor(transaction, user) {
    this.transaction = transaction;
    this.user = user;
  }

  async execute(form) {
    const response = await RoleModel.query(this.transaction).where('id', form.id).first();
    if (!response) throw new CustomException("Role not found", 400, "ROLE_NOT_FOUND")
    await response.delete();
  }
}

module.exports = Destroy;

Available Commands

  • adonis crud:generate tableName: This run all the below commands.
  • adonis crud:model tableName: This create model file with relationships.
  • adonis crud:controller tableName: This create controller file.
  • adonis crud:service tableName: This create services files.
  • adonis crud:route tableName: This create route file.

comment: <> (## Options)

comment: <> (- --connection: This option allows you specify which DB connection to use for the command e.g)

comment: <> ( adonis crud:controller tableName --connection=sqlite)

comment: <> (>NB: The connection must have been defined in config/database.js)

Production

Run npm run build or yarn build

Note: In running the commands, if you don't have adonis CLI installed globally, you can use node ace instead. For example, to generate CRUD for table posts, run node ace crud:generate posts

Donation

If this project help you reduce time to develop, you can give me a beer 🍺 :)

Donate

Credits

2.5.40

2 years ago

2.5.41

2 years ago

2.5.36

2 years ago

2.5.37

2 years ago

2.5.38

2 years ago

2.5.39

2 years ago

2.5.35

2 years ago

2.5.34

3 years ago

2.5.33

3 years ago

2.5.32

3 years ago

2.5.31

3 years ago

2.5.30

3 years ago

2.5.29

3 years ago

2.5.28

3 years ago

2.5.27

3 years ago

2.0.26

3 years ago

2.0.25

3 years ago

2.0.24

3 years ago

2.0.23

3 years ago

2.0.22

3 years ago

2.0.21

3 years ago

2.0.20

3 years ago

2.0.19

3 years ago