2.2.1 • Published 7 months ago

nodejs-artisan v2.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Node Artisan

npm npm npm Node Current license GitHub Tag

⭐ Support Us

If you find this project helpful, consider giving it a star to show your support!

GitHub stars

Tweet Share on Facebook

Node Artisan is a command-line tool for generating various components in Node.js projects, inspired by Laravel's Artisan CLI. This tool helps developers quickly scaffold controllers, models, services, middleware, and other project files, making the development process faster and more efficient. It is particularly suited for Express.js projects, enabling easy generation of essential backend components.

Screenshots

Available Command List

Command List Screenshot

Initialize Artisan

Initialization Screenshot

Make a Controller

Make a controller without options

Make Controller Screenshot

Make a controller with options

Make Controller Screenshot

Make a Model

Make a model without options

Make Model Screenshot

Make a model with options

Make Model Screenshot

Make a Service

Make Service Screenshot

Make a Repository

Make Repository Screenshot

Make a Utility

Make Utility Screenshot

Make an Enum

Make Enum Screenshot

Make a Type

Make Type Screenshot

Make a Validator

Make Validator Screenshot

Make a Transformer

Make Transformer Screenshot

Make a Helper

Make Helper Screenshot

Features

  • Generate controllers, models, services, repositories, middleware, transformers, etc., effortlessly.
  • Command structure inspired by Laravel Artisan.
  • Simplifies repetitive tasks during project development.

Installation

To add Node Artisan as a project dependency, run:

npm install nodejs-artisan

Usage

To initialize Node Artisan in your project, run:

npx artisan init

Purpose: This command enables you to use node artisan commands seamlessly by:

  1. Creating an artisan.js file in the root of your project.
  2. Automatically adding a bin entry for artisan in your package.json.

Once initialized, you can use Node Artisan commands as follows:

node artisan <command> <name>

For example, to generate a controller named UserController, use:

node artisan make:controller UserController

Alternatively, you can use:

npx artisan make:<command> <name>

Available Commands

CommandDescription
listList all available artisan commands
initInitialize Artisan in the project root directory
make:controllerCreate a new controller in src/controllers directory
Options:
--service (-s) Create a service for the controller
--repository (-r) Create a repository for the controller
--all (-a) Create both service and repository for the controller
make:enumCreate a new enum in src/enums directory
make:middlewareCreate a new middleware in src/middleware directory
make:modelCreate a new model in src/models directory
Options:
--controller (-c) Create a controller for the model
--all (-a) Create a controller, service, and repository for the model
make:transformerCreate a new transformer in src/transformers directory
make:serviceCreate a new service in src/services directory
make:repositoryCreate a new repository in src/repositories directory
make:typeCreate a new type in src/types directory
make:utilCreate a new util in src/utils directory
make:helperCreate a new helper in src/helpers directory
make:validatorCreate a new validator in src/validators directory
make:routeCreate a new route in src/routes directory

Command Details

Generate Controller

node artisan make:controller <ControllerName> index show update

Creates a new controller file in the controllers folder.

Options for make:controller:

  • -s, --service: Create a service for the controller.
  • -r, --repository: Create a repository for the controller.
  • -a, --all: Create both a service and a repository for the controller.

Example:

node artisan make:controller UserController -a

Generates:

  • src/controllers/UserController.controller.ts
  • src/services/UserController.service.ts
  • src/repositories/UserController.repository.ts

Generate Model

node artisan make:model <ModelName>

Creates a new model file in the models folder.

Options for make:model:

  • -c, --controller: Create a controller for the model.
  • -a, --all: Create a controller, service, and repository for the model.

Example:

node artisan make:model User -a

Generates:

  • src/models/User.model.ts
  • src/controllers/User.controller.ts
  • src/services/User.service.ts
  • src/repositories/User.repository.ts

Recommendation: Integrate your models with Sutando ORM for improved type safety and functionality.

Generate Middleware

node artisan make:middleware <MiddlewareName>

Creates a new middleware file in the middleware folder.

Generate Enum

node artisan make:enum <EnumName>

Creates a new enum file in the enums folder.

Generate Transformer

node artisan make:transformer <TransformerName>

Creates a new transformer file in the transformers folder.

Purpose and Functionality: Transformers serve as a formatting layer for API responses, similar to Laravel's resources. They allow you to structure and standardize the output of your APIs, ensuring that data returned to the client is consistently formatted and easy to consume.

You only need to call the transformer without additional conditions:

  1. List Data:

    const users = User.all();
    UserShowTransformer.transform(users);
  2. Single Data:

    const user = User.find(1);
    UserShowTransformer.transform(user);

The transformer will automatically handle formatting for both cases, ensuring consistent API responses.

Example Implementation:

import { Transformer, Str } from 'nodejs-artisan';

export default class UserShowTransformer extends Transformer {
  /**
   * Override the protected _format method
   * @param {any} data - The input data
   * @param {string} [_lang] - Optional language parameter
   * @returns {Record<string, any> | null} - The formatted result
   */
  static override _format(data: any, _lang?: string): Record<string, any> | null {
    return Str.attributes({
      id: data.id,
      name: data.name,
      email: data.email,
      created_at: data.createdAt,
      updated_at: data.updatedAt
    });
  }
}

The transformer will automatically handle formatting for both cases, ensuring consistent API responses.

Generate Service

node artisan make:service <ServiceName>

Creates a new service file in the services folder.

Generate Repository

node artisan make:repository <RepositoryName>

Creates a new repository file in the repositories folder.

Generate Validator

node artisan make:validator <ValidatorName>

Creates a new validator file in the validators folder.

Recommendation: Use VineJS for robust and extensible validation.

License

This project is licensed under the MIT License.

Author

šŸ‘¤ Created by: Miftah704
šŸ“§ Email: miftahshidiq704@gmail.com
šŸ”— LinkedIn: Miftah Shidiq

Feel free to connect for feedback, collaborations, or just to say hi!

2.2.1

7 months ago

2.2.0

7 months ago

2.1.1

7 months ago

2.1.0

7 months ago

2.0.2

7 months ago

2.0.1

7 months ago

2.0.0

7 months ago

1.2.4

7 months ago

1.2.3

7 months ago

1.2.2

7 months ago

1.2.1

7 months ago

1.2.0

7 months ago

1.1.2

8 months ago

1.1.1

8 months ago

1.1.0

8 months ago

1.0.10

8 months ago

1.0.9

8 months ago

1.0.8

8 months ago

1.0.7

8 months ago

1.0.6

8 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago