npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sfp-group/adonisjs-crud-generator

v2.5.50

Published

Auto generate Models/Routes/Controllers/Services for existing tables.

Downloads

13

Readme

AdonisJS CRUD Generator

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

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.

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.

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